@@ -37,24 +37,54 @@ def extractJson(col, startDate, endDate):
3737
3838#_______________________________________________________________________
3939
40- # function needed to fetch a list of all sites from metric 153
41- def fetch_all_sites (jsn ):
42- site_T2 = []
43- for row in jsn ['csvdata' ]:
44- if row ['VOName' ][0 :2 ] == 'T2' :
45- site_T2 .append (row ['VOName' ])
40+ # function needed to fetch a list of all sites from siteDB
41+ def fetch_all_sites (url ,api ):
42+ # examples in:
43+ # https://github.com/gutsche/old-scripts/blob/master/SiteDB/extract_site_executive_mail_addresses.py
44+ # https://github.com/dballesteros7/dev-scripts/blob/master/src/reqmgr/input-blocks.py
45+ # https://twiki.cern.ch/twiki/bin/view/CMSPublic/CompOpsWorkflowOperationsWMAgentScripts#Resubmit
46+ headers = {"Accept" : "application/json" }
47+ if 'X509_USER_PROXY' in os .environ :
48+ print 'X509_USER_PROXY found'
49+ conn = httplib .HTTPSConnection (url , cert_file = os .getenv ('X509_USER_PROXY' ), key_file = os .getenv ('X509_USER_PROXY' ))
50+ elif 'X509_USER_CERT' in os .environ and 'X509_USER_KEY' in os .environ :
51+ print 'X509_USER_CERT and X509_USER_KEY found'
52+ conn = httplib .HTTPSConnection (url , cert_file = os .getenv ('X509_USER_CERT' ), key_file = os .getenv ('X509_USER_KEY' ))
53+ else :
54+ print 'You need a valid proxy or cert/key files'
55+ sys .exit ()
56+ print 'conn found in if else structure'
57+ r1 = conn .request ("GET" ,api , None , headers )
58+ print 'r1 passed'
59+ r2 = conn .getresponse ()
60+ print 'r2 passed'
61+ inputjson = r2 .read ()
62+ print '-------------------------------------------------------------'
63+ jn = simplejson .loads (inputjson )
64+ conn .close ()
65+
66+ site_T2 = []
67+ for i in jn ['result' ]:
68+ if i [jn ['desc' ]['columns' ].index ('type' )]== 'cms' :
69+ sitedbname = i [jn ['desc' ]['columns' ].index ('alias' )]
70+ if 'T2' in sitedbname :
71+ site_T2 .append (sitedbname )
4672 return site_T2
4773#_______________________________________________________________________
4874
49- def main_function (outputfile_txt , submonths ):
75+ def main_function (outputfile_txt , submonths , allSites ):
5076 print outputfile_txt
5177 columnnumber = '153'
5278 enddate = date .today () # query date
5379 startdate = addmonths (enddate ,submonths ) # query date
5480 start = startdate .strftime ("%Y-%m-%d" ) # as string field to query
5581 end = enddate .strftime ("%Y-%m-%d" ) # as string field to query
5682 jsn = extractJson (columnnumber , start , end ) # results from dashboard extracts into JSON file
57- allSites = fetch_all_sites (jsn )
83+ filename = 'tmpjson' + start + '.json'
84+ f = open (filename ,'w' )
85+ f .write (unicode (simplejson .dumps (jsn , ensure_ascii = False )))
86+ f .close ()
87+
5888 # sum @ days
5989 days_per_site = {}
6090 for site in allSites : # Read all sites from allsites variable
@@ -93,12 +123,12 @@ def main_function(outputfile_txt, submonths):
93123 print '------------------------------'
94124 print days_per_site
95125
126+
96127 # write to file
97- #f1=open('./'+outputfile_txt, 'w+')
98- f1 = open ('/afs/cern.ch/user/c/cmst1/scratch0/Waitingroom_Dashboard/Waitingroom_SummedMetric/' + outputfile_txt , 'w' )
128+ f1 = open ('./' + outputfile_txt , 'w+' )
99129 f1 .write ('# This txt goes into SSB and shows the number of days a site has been in the Waiting Room for X months --> See filename)\n ' )
100130 f1 .write ('# Readme:\n # https://raw.githubusercontent.com/CMSCompOps/MonitoringScripts/master/SR_View_SSB/WRDays/Readme.txt\n ' )
101- now_write = time . strftime (' %Y-%m-%d %H:%M:%S' )
131+ now_write = ( datetime . utcnow ()). strftime (" %Y-%m-%d %H:%M:%S" )
102132 print "Local current time :" , now_write
103133 link = "https://dashb-ssb.cern.ch/dashboard/request.py/siteviewhistorywithstatistics?columnid=153#time=2184&start_date=&end_date=&use_downtimes=false&merge_colors=false&sites=all"
104134 for key , number in days_per_site .iteritems ():
@@ -112,12 +142,12 @@ def main_function(outputfile_txt, submonths):
112142if __name__ == '__main__' :
113143 outputfile_txt = sys .argv [1 ]
114144 print 'starting to fetch all sites from siteDB'
115- main_function (outputfile_txt + '1MonthSum.txt' ,- 1 )
145+ allSitesList = fetch_all_sites ('cmsweb.cern.ch' ,'/sitedb/data/prod/site-names' )
146+ main_function (outputfile_txt + '1MonthSum.txt' ,- 1 ,allSitesList )
116147 print '__________________________________________________'
117148 print '__________________________________________________'
118- main_function (outputfile_txt + '2MonthSum.txt' ,- 2 )
149+ main_function (outputfile_txt + '2MonthSum.txt' ,- 2 , allSitesList )
119150 print '__________________________________________________'
120151 print '__________________________________________________'
121- main_function (outputfile_txt + '3MonthSum.txt' ,- 3 )
122-
152+ main_function (outputfile_txt + '3MonthSum.txt' ,- 3 ,allSitesList )
123153
0 commit comments