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,8 @@ def execute(self):
131134
132135 for backend in self .datastores :
133136 if backend .lower () == "monitoring" :
137+ rD ["Tier" ] = site_metadata ["Tier" ]
138+ rD ["Type" ] = site_metadata ["Type" ]
134139 rD ["timestamp" ] = int (TimeUtilities .toEpochMilliSeconds (now ))
135140 self .datastores ["Monitoring" ].addRecord (rD )
136141
@@ -154,3 +159,27 @@ def execute(self):
154159 self .log .verbose (f"Done committing WMSHistory to { backend } backend" )
155160
156161 return S_OK ()
162+
163+ def _getSitesMetadata (self ):
164+ """Get the metadata for the sites"""
165+ res = getSites ()
166+ if not res ["OK" ]:
167+ return res
168+ sites = res ["Value" ]
169+ site_metadata = {}
170+
171+ for site in sites :
172+ grid = site .split ("." )[0 ]
173+ res = gConfig .getOptionsDict (f"Resources/Sites/{ grid } /{ site } " )
174+ if not res ["OK" ]:
175+ self .log .error ("Failure getting options dict for site" , f"{ site } : { res ['Message' ]} " )
176+ continue
177+ siteInfoCS = res ["Value" ]
178+
179+ # The site tier is normally 1 or 2. Few VOs may define tier 3.
180+ # If the tier is not defined, we assume it is 4, with 4 meaning "not pledged" (opportunistic).
181+ site_metadata [site ]["Tier" ] = siteInfoCS .get ("MoUTierLevel" , "4" )
182+ # The site type is defined by the first part of the site name.
183+ # It needs to be interpreted at the Monitoring side (e.g. in Grafana).
184+ site_metadata [site ]["Type" ] = site .split ("." )[0 ]
185+ return site_metadata
0 commit comments