-
Notifications
You must be signed in to change notification settings - Fork 336
/
nagios_retention_file_scheduler.py
345 lines (309 loc) · 13.6 KB
/
nagios_retention_file_scheduler.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2009-2012:
# Gabes Jean, naparuba@gmail.com
# Gerhard Lausser, Gerhard.Lausser@consol.de
# Gregory Starck, g.starck@gmail.com
# Hartmut Goebel, h.goebel@goebel-consult.de
#
# This file is part of Shinken.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken. If not, see <http://www.gnu.org/licenses/>.
# This Class is an example of an Scheduler module
# Here for the configuration phase AND running one
import re
from shinken.objects import Timeperiod, Timeperiods
from shinken.objects import Service, Services
from shinken.objects import Host, Hosts
from shinken.objects import Contact, Contacts
from shinken.comment import Comment
from shinken.downtime import Downtime
from shinken.basemodule import BaseModule
from shinken.util import to_bool
from shinken.log import logger
properties = {
'daemons': ['scheduler'],
'type': 'nagios_retention_file',
'external': False,
}
def get_instance(plugin):
"""
Called by the plugin manager to get a broker
"""
logger.debug("Get a Nagios3 retention scheduler module for plugin %s" % plugin.get_name())
path = plugin.path
instance = Nagios_retention_scheduler(plugin, path)
return instance
class Nagios_retention_scheduler(BaseModule):
def __init__(self, mod_conf, path):
BaseModule.__init__(self, mod_conf)
self.path = path
def hook_save_retention(self, daemon):
"""
main function that is called in the retention creation pass
"""
logger.info("[NagiosRetention] asking me to update the retention objects, but I won't do it.")
def _cut_line(self, line):
#punct = '"#$%&\'()*+/<=>?@[\\]^`{|}~'
tmp = re.split("=", line)
r = [elt for elt in tmp if elt != '']
return r
def read_retention_buf(self, buf):
params = []
objectscfg = {'void': [],
'timeperiod': [],
'command': [],
'contactgroup': [],
'hostgroup': [],
'contact': [],
'notificationway': [],
'host': [],
'service': [],
'servicegroup': [],
'servicedependency': [],
'hostdependency': [],
'hostcomment': [],
'hostdowntime': [],
'servicecomment': [],
'servicedowntime': [],
}
tmp = []
tmp_type = 'void'
in_define = False
continuation_line = False
tmp_line = ''
lines = buf.split('\n')
for line in lines:
line = line.decode('utf8', 'ignore')
line = line.split(';')[0]
# A backslash means, there is more to come
if re.search("\\\s*$", line):
continuation_line = True
line = re.sub("\\\s*$", "", line)
line = re.sub("^\s+", " ", line)
tmp_line += line
continue
elif continuation_line:
# Now the continuation line is complete
line = re.sub("^\s+", "", line)
line = tmp_line + line
tmp_line = ''
continuation_line = False
if re.search("}", line):
in_define = False
if re.search("^\s*\t*#|^\s*$|^\s*}", line):
pass
# A define must be catch and the type save
# The old entry must be save before
elif re.search("{$", line):
in_define = True
if tmp_type not in objectscfg:
objectscfg[tmp_type] = []
objectscfg[tmp_type].append(tmp)
tmp = []
# Get new type
elts = re.split('\s', line)
tmp_type = elts[0]
#tmp_type = tmp_type.split('{')[0]
#print "TMP type", tmp_type
else:
if in_define:
tmp.append(line)
else:
params.append(line)
objectscfg[tmp_type].append(tmp)
objects = {}
#print "Loaded", objectscfg
for type in objectscfg:
objects[type] = []
for items in objectscfg[type]:
tmp = {}
for line in items:
elts = self._cut_line(line)
if elts != []:
prop = elts[0]
value = ' '.join(elts[1:])
tmp[prop] = value
if tmp != {}:
objects[type].append(tmp)
return objects
def create_objects(self, raw_objects, types_creations):
"""
Create real Instances from raw objects in string.
"""
all_obj = {}
for t in types_creations:
all_obj[t] = self.create_objects_for_type(raw_objects, t, types_creations)
return all_obj
def pythonize_running(self, obj, obj_cfg):
cls = obj.__class__
running_properties = cls.running_properties
for prop, entry in running_properties.items():
if hasattr(obj, prop) and prop in obj_cfg:
#if 'pythonize' in entry:
f = entry.pythonize
if f is not None: # mean it's a string
#print "Apply", f, "to the property", prop, "for ", cls.my_type
val = getattr(obj, prop)
val = f(val)
setattr(obj, prop, val)
else: # no pythonize, int by default
# if cls.my_type != 'service':
# print "Intify", prop, getattr(obj, prop)
if prop != 'state_type':
val = int(getattr(obj, prop))
setattr(obj, prop, val)
else: # state type is a int, but should be set HARd or SOFT
val = int(getattr(obj, prop))
if val == 1:
setattr(obj, prop, 'HARD')
else:
setattr(obj, prop, 'SOFT')
def create_objects_for_type(self, raw_objects, type, types_creations):
t = type
# Ex: the above code do for timeperiods:
# timeperiods = []
# for timeperiodcfg in objects['timeperiod']:
# t = Timeperiod(timeperiodcfg)
# t.clean()
# timeperiods.append(t)
#self.timeperiods = Timeperiods(timeperiods)
(cls, clss, prop) = types_creations[t]
# List where we put objects
lst = []
for obj_cfg in raw_objects[t]:
# We create the object
# print "Create obj", obj_cfg
o = cls(obj_cfg)
o.clean()
if t in self.property_mapping:
entry = self.property_mapping[t]
for (old, new) in entry:
value = getattr(o, old)
setattr(o, new, value)
delattr(o, old)
obj_cfg[new] = obj_cfg[old]
del obj_cfg[old]
o.pythonize()
self.pythonize_running(o, obj_cfg)
lst.append(o)
#print "Got", lst
# we create the objects Class and we set it in prop
#setattr(self, prop, clss(lst))
#print "Object?", clss(lst)
return clss(lst)
def create_and_link_comments(self, raw_objects, all_obj):
# first service
for obj_cfg in raw_objects['servicecomment']:
#print "Managing", obj_cfg
host_name = obj_cfg['host_name']
service_description = obj_cfg['service_description']
srv = all_obj['service'].find_srv_by_name_and_hostname(host_name, service_description)
#print "Find my service", srv
if srv is not None:
cmd = Comment(srv, to_bool(obj_cfg['persistent']), obj_cfg['author'], obj_cfg['comment_data'], 1, int(obj_cfg['entry_type']), int(obj_cfg['source']), to_bool(obj_cfg['expires']), int(obj_cfg['expire_time']))
#print "Created cmd", cmd
srv.add_comment(cmd)
# then hosts
for obj_cfg in raw_objects['hostcomment']:
#print "Managing", obj_cfg
host_name = obj_cfg['host_name']
hst = all_obj['host'].find_by_name(host_name)
#print "Find my host", hst
if hst is not None:
cmd = Comment(hst, to_bool(obj_cfg['persistent']), obj_cfg['author'], obj_cfg['comment_data'], 1, int(obj_cfg['entry_type']), int(obj_cfg['source']), to_bool(obj_cfg['expires']), int(obj_cfg['expire_time']))
#print "Created cmd", cmd
hst.add_comment(cmd)
def create_and_link_downtimes(self, raw_objects, all_obj):
# first service
for obj_cfg in raw_objects['servicedowntime']:
#print "Managing", obj_cfg
host_name = obj_cfg['host_name']
service_description = obj_cfg['service_description']
srv = all_obj['service'].find_srv_by_name_and_hostname(host_name, service_description)
#print "Find my service", srv
if srv is not None:
dwn = Downtime(srv, int(obj_cfg['start_time']), int(obj_cfg['end_time']), to_bool(obj_cfg['fixed']), int(obj_cfg['triggered_by']), int(obj_cfg['duration']), obj_cfg['author'], obj_cfg['comment'])
#print "Created dwn", dwn
srv.add_downtime(dwn)
# then hosts
for obj_cfg in raw_objects['hostdowntime']:
#print "Managing", obj_cfg
host_name = obj_cfg['host_name']
hst = all_obj['host'].find_by_name(host_name)
#print "Find my host", hst
if hst is not None:
dwn = Downtime(hst, int(obj_cfg['start_time']), int(obj_cfg['end_time']), to_bool(obj_cfg['fixed']), int(obj_cfg['triggered_by']), int(obj_cfg['duration']), obj_cfg['author'], obj_cfg['comment'])
#print "Created dwn", dwn
hst.add_downtime(dwn)
# Should return if it succeed in the retention load or not
def hook_load_retention(self, sched):
logger.debug("[NagiosRetention] asking me to load the retention file")
# Now the old flat file way :(
logger.info("[NagiosRetention]Reading from retention_file %s" % self.path)
try:
f = open(self.path)
buf = f.read()
f.close()
except (EOFError, ValueError, IOError), exp:
logger.warning(repr(exp))
return False
except (IndexError, TypeError), exp:
logger.warning("Sorry, the ressource file is not compatible")
return False
logger.debug("Finished reading config")
raw_objects = self.read_retention_buf(buf)
types_creations = {'timeperiod': (Timeperiod, Timeperiods, 'timeperiods'),
'service': (Service, Services, 'services'),
'host': (Host, Hosts, 'hosts'),
'contact': (Contact, Contacts, 'contacts'),
}
self.property_mapping = {
'service': [('current_attempt', 'attempt'), ('current_state', 'state_type_id'),
('plugin_output', 'output'), ('last_check', 'last_chk'),
('performance_data', 'perf_data'), ('next_check', 'next_chk'),
('long_plugin_output', 'long_output'), ('check_execution_time', 'execution_time'),
('check_latency', 'latency')],
'host': [('current_attempt', 'attempt'), ('current_state', 'state_type_id'),
('plugin_output', 'output'), ('last_check', 'last_chk'),
('performance_data', 'perf_data'), ('next_check', 'next_chk'),
('long_plugin_output', 'long_output'), ('check_execution_time', 'execution_time'),
('check_latency', 'latency')],
}
all_obj = self.create_objects(raw_objects, types_creations)
logger.info('Received all obj %s' % (all_obj))
self.create_and_link_comments(raw_objects, all_obj)
self.create_and_link_downtimes(raw_objects, all_obj)
# Taken from the scheduler code... sorry
all_data = {'hosts': {}, 'services': {}}
for h in all_obj['host']:
d = {}
running_properties = h.__class__.running_properties
for prop, entry in running_properties.items():
if entry.retention:
d[prop] = getattr(h, prop)
all_data['hosts'][h.host_name] = d
# Now same for services
for s in all_obj['service']:
d = {}
running_properties = s.__class__.running_properties
for prop, entry in running_properties.items():
if entry.retention:
d[prop] = getattr(s, prop)
all_data['services'][(s.host_name, s.service_description)] = d
#all_data = {'hosts': {}, 'services': {}}
sched.restore_retention_data(all_data)
logger.info("[NagiosRetention] Retention objects loaded successfully.")
return True