This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBlack_Holes.py
143 lines (120 loc) · 5.72 KB
/
Black_Holes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Copyright 2013 II. Institute of Physics - Georg-August University Goettingen
# Author: Christian Wehrberger ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import hf
from sqlalchemy import TEXT, INT, Column
import re
from BeautifulSoup import BeautifulSoup
class Black_Holes(hf.module.ModuleBase):
bhwt_help = ("If the number of failures of "
"a specific worker node compared to "
"the total number of failures exceeds this threshold, "
"the status is set to warning."
)
bhct_help = ("If the number of failures of "
"a specific worker node compared to "
"the total number of failures exceeds this threshold, "
"the status is set to critical."
)
config_keys = {
'source_url': ('Source URL', 'both||panda.cern.ch/server/pandamon/query?jobsummary=site&site=%s'),
'queues': ('Name of the queues to check for black hole worker nodes', 'GoeGrid,ANALY_GOEGRID'),
'black_hole_warning_threshold': (bhwt_help, '25'),
'black_hole_critical_threshold': (bhct_help, '50'),
}
table_columns = [
Column('source_url', TEXT),
Column('queues', TEXT),
], []
subtable_columns = {
'queue_details': ([
Column('queue_name', TEXT),
Column('worker_node', TEXT),
Column('failed', INT),
], [])}
def prepareAcquisition(self):
queues_string = self.config['queues']
self.queues = queues_string.split(',')
for index, queue in enumerate(self.queues):
self.queues[index] = queue.strip()
self.queues[:] = [entry for entry in self.queues if not entry == '']
# prepare download (if key is of type local/global/both|options|url)
self.sources = {}
for queue in self.queues:
self.sources[queue] = hf.downloadService.addDownload(self.config['source_url']%(queue))
self.subtable_queue_details = []
def extractData(self):
data = {
'source_url': self.config['source_url'],
'queues': ','.join(self.queues),
'status': 1.,
}
self.failed = {}
# read content from downloaded file
contents = {}
for queue in self.queues:
contents[queue] = open(self.sources[queue].getTmpPath()).read()
for queue in self.queues:
table_start_identifier = re.compile('<table id=\'sitetable\' class=\'display\' >(?!</table>)')
table_end_identifier = re.compile('</table>')
table_start = table_start_identifier.search(contents[queue]).span()[1]
table_end = table_end_identifier.search((contents[queue])[table_start:]).span()[0]
table_string = (contents[queue])[table_start:table_end+table_start]
soup = BeautifulSoup(table_string)
for body in soup.findAll('tbody'):
for index, row in enumerate(body.findAll('tr')):
col = row.findAll('td')
if index == 0:
self.failed[queue] = int(col[6].string)
else:
worker_node = col[0].string
failed = int(col[6].string)
self.subtable_queue_details.append(
{
'queue_name': queue,
'worker_node': worker_node,
'failed': failed,
}
)
for entry in self.subtable_queue_details:
if self.failed[entry['queue_name']] == 0:
data['status'] = min(data['status'],1)
if float(entry['failed'])/float(self.failed[entry['queue_name']]) >= int(self.config['black_hole_critical_threshold']):
data['status'] = min(data['status'],0)
elif float(entry['failed'])/float(self.failed[entry['queue_name']]) >= int(self.config['black_hole_warning_threshold']):
data['status'] = min(data['status'],0.5)
else:
data['status'] = min(data['status'],1)
return data
def fillSubtables(self, parent_id):
self.subtables['queue_details'].insert().execute([dict(parent_id=parent_id, **row) for row in self.subtable_queue_details])
def getTemplateData(self):
data = hf.module.ModuleBase.getTemplateData(self)
details = self.subtables['queue_details'].select().where(self.subtables['queue_details'].c.parent_id==self.dataset['id']).execute().fetchall()
details_mapped = map(dict, details)
data['details'] = sorted(details_mapped, key=lambda k: k['failed'], reverse=True)
queues = self.dataset['queues'].split(',')
failed = {}
for queue in queues:
failed[queue] = 0
for detail in details:
if detail['queue_name'] == queue:
failed[queue] += int(detail['failed'])
data['failed'] = failed
config_settings = {
'black_hole_warning_threshold': int(self.config['black_hole_warning_threshold']),
'black_hole_critical_threshold': int(self.config['black_hole_critical_threshold']),
}
data['config_settings'] = config_settings
return data