99"""
1010import datetime
1111
12- from DIRAC import S_ERROR , S_OK
12+ from DIRAC import S_ERROR , S_OK , gConfig
1313from DIRAC .AccountingSystem .Client .DataStoreClient import DataStoreClient
1414from DIRAC .AccountingSystem .Client .Types .WMSHistory import WMSHistory
1515from DIRAC .ConfigurationSystem .Client .Helpers .Operations import Operations
16+ from DIRAC .ConfigurationSystem .Client .Helpers .Resources import getSites
1617from DIRAC .Core .Base .AgentModule import AgentModule
1718from DIRAC .Core .Utilities import TimeUtilities
1819from DIRAC .MonitoringSystem .Client .MonitoringReporter import MonitoringReporter
@@ -77,6 +78,8 @@ def initialize(self):
7778 def execute (self ):
7879 """Main execution method"""
7980
81+ site_metadata = self ._getSitesMetadata ()
82+
8083 # on the first iteration of the agent, do nothing in order to avoid double committing after a restart
8184 if self .am_getModuleParam ("cyclesDone" ) == 0 :
8285 self .log .notice ("Skipping the first iteration of the agent" )
@@ -131,6 +134,9 @@ def execute(self):
131134
132135 for backend in self .datastores :
133136 if backend .lower () == "monitoring" :
137+ site_name = rD ["Site" ]
138+ rD ["Tier" ] = site_metadata [site_name ]["Tier" ]
139+ rD ["Type" ] = site_metadata [site_name ]["Type" ]
134140 rD ["timestamp" ] = int (TimeUtilities .toEpochMilliSeconds (now ))
135141 self .datastores ["Monitoring" ].addRecord (rD )
136142
@@ -154,3 +160,30 @@ def execute(self):
154160 self .log .verbose (f"Done committing WMSHistory to { backend } backend" )
155161
156162 return S_OK ()
163+
164+ def _getSitesMetadata (self ):
165+ """Get the metadata for the sites"""
166+ res = getSites ()
167+ if not res ["OK" ]:
168+ return res
169+ sites = res ["Value" ]
170+ site_metadata = {}
171+
172+ for site in sites :
173+ site_metadata [site ] = {}
174+
175+ # Get the site metadata from the Configuration System
176+ grid = site .split ("." )[0 ]
177+ res = gConfig .getOptionsDict (f"Resources/Sites/{ grid } /{ site } " )
178+ if not res ["OK" ]:
179+ self .log .error ("Failure getting options dict for site" , f"{ site } : { res ['Message' ]} " )
180+ continue
181+ siteInfoCS = res ["Value" ]
182+
183+ # The site tier is normally 1 or 2. Few VOs may define tier 3.
184+ # If the tier is not defined, we assume it is 4, with 4 meaning "not pledged" (opportunistic).
185+ site_metadata [site ]["Tier" ] = siteInfoCS .get ("MoUTierLevel" , "4" )
186+ # The site type is defined by the first part of the site name.
187+ # It needs to be interpreted at the Monitoring side (e.g. in Grafana).
188+ site_metadata [site ]["Type" ] = site .split ("." )[0 ]
189+ return site_metadata
0 commit comments