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 pathCMSPhedexAgents.py
185 lines (163 loc) · 7.56 KB
/
CMSPhedexAgents.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# -*- coding: utf-8 -*-
#
# Copyright 2012 Institut für Experimentelle Kernphysik - Karlsruher Institut für Technologie
#
# 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, FLOAT, Column
from lxml import etree
from string import strip
class CMSPhedexAgents(hf.module.ModuleBase):
config_keys = {
'blacklist': ('list all agents to be neglected', ''),
'eval_blacklist': ('list all agents, which should be shown but neglected in status evaluation', ''),
'time_warning': ('time in seconds, if reports are older than this, agent status is switched to warning', '3600'),
'time_critical': ('time in seconds, if reports are older than this, agent status is switched to warning', '7200'),
'source_url': ('set url of source', 'both||url')
}
config_hint = ''
table_columns = [
Column('requestTime', FLOAT)
],[]
subtable_columns = {
'details': ([
Column('name', TEXT),
Column('host', TEXT),
Column('label', TEXT),
Column('version', TEXT),
Column('state_dir', TEXT),
Column('time_diff', FLOAT),
Column('pid', INT),
Column('status', TEXT)
],[])
}
def prepareAcquisition(self):
self.time_warning = float(self.config['time_warning'])
self.time_critical = float(self.config['time_critical'])
self.source = hf.downloadService.addDownload(self.config['source_url'])
self.source_url = self.source.getSourceUrl()
self.details_db_value_list = []
try:
self.blacklist = map(strip, self.config['blacklist'].split(','))
except AttributeError:
self.blacklst = None
try:
self.eval_blacklist = map(strip, self.config['eval_blacklist'].split(','))
except AttributeError:
self.eval_blacklist = None
def extractData(self):
data = {}
help_list = []
root = etree.parse(open(self.source.getTmpPath())).getroot()
status_list = []
try:
reqTime = data['requestTime'] = float(root.get('request_timestamp'))
except KeyError:
# raise apropriate Exception, currently not content in core :-/
data['status'] = -1
data['error_string'] = 'Request_Timestamp could not be found in downloaded file'
return data
except ValueError:
# raise apropriate Exception, currently not content in core :-/
data['status'] = -1
data['error_string'] = 'Request_Timestamp could not be converted to float, something is wrong with the file'
return data
for node in root:
agent_dict = {}
node_name = node.get('name')
node_host = node.get('host')
for agent in node:
if self.blacklist is not None:
if agent.get('label') not in self.blacklist:
agent_dict['label'] = agent.get('label')
agent_dict['version'] = agent.get('version')
agent_dict['state_dir'] = agent.get('state_dir')
agent_dict['time_diff'] = reqTime - float(agent.get('time_update'))
agent_dict['pid'] = int(agent.get('pid'))
agent_dict['name'] = node_name
agent_dict['host'] = node_host
if agent_dict['time_diff'] >= self.time_critical:
agent_dict['status'] = 'critical'
elif agent_dict['time_diff'] >= self.time_warning:
agent_dict['status'] = 'warning'
else:
agent_dict['status'] = 'ok'
help_list.append(agent_dict)
else:
agent_dict['label'] = agent.get('label')
agent_dict['version'] = agent.get('version')
agent_dict['state_dir'] = agent.get('state_dir')
agent_dict['time_diff'] = reqTime - float(agent.get('time_update'))
agent_dict['pid'] = int(agent.get('pid'))
agent_dict['name'] = node_name
agent_dict['host'] = node_host
if agent_dict['time_diff'] >= self.time_critical:
agent_dict['status'] = 'critical'
elif agent_dict['time_diff'] >= self.time_warning:
agent_dict['status'] = 'warning'
else:
agent_dict['status'] = 'ok'
help_list.append(agent_dict)
#there might be several entries for the same process, so you need to find the newest
for i,agent in enumerate(help_list):
pid = agent['pid']
min_list = [agent]
checked = True
for j in range(i,len(help_list)):
if pid == help_list[j]['pid']:
min_list.append(help_list[j])
while True:
if len(min_list)> 1:
if min_list[0]['time_diff'] < min_list[1]['time_diff']:
min_list.pop(1)
else:
min_list.pop(0)
else:
break
min_list = min_list[0]
for q in self.details_db_value_list:
if min_list['pid'] == q['pid']:
checked = False
if checked:
self.details_db_value_list.append(min_list)
if self.eval_blacklist is not None:
for i,agent in enumerate(self.details_db_value_list):
if agent['label'] not in self.eval_blacklist:
status_list.append(agent['status'])
else:
for i,agent in enumerate(self.details_db_value_list):
status_list.append(agent['status'])
if 'critical' in status_list:
data['status'] = 0.0
elif 'warning' in status_list:
data['status'] = 0.5
else:
data['status'] = 1.0
return data
def fillSubtables(self, parent_id):
self.subtables['details'].insert().execute([dict(parent_id=parent_id, **row) for row in self.details_db_value_list])
def getTemplateData(self):
data = hf.module.ModuleBase.getTemplateData(self)
details_list = self.subtables['details'].select().where(self.subtables['details'].c.\
parent_id==self.dataset['id']).order_by(self.subtables['details'].c.name.asc()).execute().fetchall()
details_list = [dict(time_str=self.formatTime(row['time_diff']), **row) for row in details_list]
data['details'] = details_list
return data
def formatTime(self,time_diff):
#TODO rewrite this function and use time package or something like that
time_string = ""
d = int(time_diff/24/3600)
h = int((time_diff-d*24*3600)/3600)
m = int((time_diff-d*24*3600-h*3600)/60)
time_string = "%02id:%02ih:%02im" % (d, h, m)
return time_string