1010
1111from .webtools import get_json
1212
13+
14+ def _is_problem (success , data ):
15+ """
16+ :param float success: the rate of success desired
17+ :param list data: Data from the WLCG SAM server
18+ :returns: True if the success rate of SAM test is below success
19+ :rtype: bool
20+ """
21+ # if success is less then 85% call it a problem
22+ items = 0
23+ bad = 0
24+ for item in data :
25+ stat = item [1 ]
26+ if stat == 'WHITE' :
27+ continue
28+
29+ items += 1
30+ if stat in ['CRITICAL' , 'WARNING' ]:
31+ bad += 1
32+
33+ return float (bad )/ float (items ) > (1.0 - success )
34+
35+
1336def is_sam_good (site , time_span = 24 , success = 0.85 ):
1437 """
1538 Checks the SAM tests for success rate, and returns True if SAM tests were passing
@@ -33,12 +56,18 @@ def is_sam_good(site, time_span=24, success=0.85):
3356 srm_host_name = ''
3457 ce_host_name = ''
3558 ce_flavour = ''
36- for item in gen_json ['data' ]['results' ][0 ]['flavours' ]:
37- if item ['servicename' ] == 'SRM' :
38- srm_host_name = item ['hosts' ][0 ]['hostname' ]
39- else :
40- ce_host_name = item ['hosts' ][0 ]['hostname' ]
41- ce_flavour = item ['servicename' ]
59+
60+ try :
61+ for item in gen_json ['data' ]['results' ][0 ]['flavours' ]:
62+ if item ['servicename' ] == 'SRM' :
63+ srm_host_name = item ['hosts' ][0 ]['hostname' ]
64+ else :
65+ ce_host_name = item ['hosts' ][0 ]['hostname' ]
66+ ce_flavour = item ['servicename' ]
67+
68+ except (KeyError , IndexError ):
69+ # If there's something wrong with the JSON file, go through
70+ return True
4271
4372 get_data = lambda flav , host : get_json ('wlcg-sam-cms.cern.ch' ,
4473 '/dashboard/request.py/getTestResults' ,
@@ -47,33 +76,16 @@ def is_sam_good(site, time_span=24, success=0.85):
4776 'hostname' : host ,
4877 'time_range' : time_span_str })
4978
50- def is_problem (success , data ):
51- """
52- :param float success: the rate of success desired
53- :param list data: Data from the WLCG SAM server
54- :returns: True if the success rate of SAM test is below success
55- :rtype: bool
56- """
57- # if success is less then 85% call it a problem
58- items = 0
59- bad = 0
60- for item in data :
61- stat = item [1 ]
62- if stat == 'WHITE' :
63- continue
64-
65- items += 1
66- if stat in ['CRITICAL' , 'WARNING' ]:
67- bad += 1
68-
69- return float (bad )/ float (items ) > (1.0 - success )
70-
71- for probe in get_data ('SRM' , srm_host_name )['data' ]:
72- if is_problem (success , probe [1 ]):
73- return False
74-
75- for probe in get_data (ce_flavour , ce_host_name )['data' ]:
76- if 'WN-xrootd-access' in probe [0 ] and is_problem (success , probe [1 ]):
77- return False
79+ try :
80+ for probe in get_data ('SRM' , srm_host_name )['data' ]:
81+ if _is_problem (success , probe [1 ]):
82+ return False
83+
84+ for probe in get_data (ce_flavour , ce_host_name )['data' ]:
85+ if 'WN-xrootd-access' in probe [0 ] and _is_problem (success , probe [1 ]):
86+ return False
87+
88+ except KeyError :
89+ pass
7890
7991 return True
0 commit comments