forked from jbreams/nagmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
halib.py
200 lines (162 loc) · 5.77 KB
/
halib.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
import zmq, time, json, hashlib
class haloop:
_meventa = ""
_mstatea = ""
_ctx = None
_lostate = None
_locmd = None
_loevents = None
_meventsubs = [ 'program_status', 'host_check_processed',
'service_check_processed', 'downtime_add', 'comment_add',
'acknowledgement', 'adaptiveservice_update', 'adaptivehost_update' ]
def __init__(self, meventa, mstatea, leventa, lcmda, lstatea, ctx = None):
if not ctx:
ctx = zmq.Context()
self._meventa = meventa
self._mstatea = mstatea
lostate = ctx.socket(zmq.REQ)
locmd = ctx.socket(zmq.PUSH)
loevents = ctx.socket(zmq.SUB)
lostate.connect(lstatea)
locmd.connect(lcmda)
loevents.connect(leventa)
loevents.setsockopt(zmq.SUBSCRIBE, 'program_status')
self._lostate = lostate
self._locmd = locmd
self._loevents = loevents
self._ctx = ctx
def send_nagios_commands(*cmds):
for c in cmds:
cmd = { 'type': 'command', 'command_name': c }
self._locmd.send(json.dumps(cmd))
return True
def _hash_downtime(obj):
rethash = hashlib.sha1()
for k in [ 'host_name', 'service_description', 'start_time',
'end_time', 'fixed', 'duration', 'author_name', 'comment_data' ]:
rethash.update(str(obj[k]))
return rethash.digest()
def _hash_comment(obj):
rethash = hashlib.sha1()
for k in [ 'host_name', 'service_description', 'author_data', 'comment_data',
'expires', 'expire_time', 'persistent', 'source', 'entry_type' ]:
rethash.update(str(obj[k]))
return rethash.digest()
def resolve_diffs(self, timeout):
castate = self._ctx.socket(zmq.REQ)
castate.connect(self._mstatea)
self._lostate.send(json.dumps({'list_downtimes': True }))
lodowntimes = set()
for d in json.loads(self._lostate.recv()):
lodowntimes.add(hash_downtime(d))
if castate.poll(timeout=timeout, flags=zmq.POLLOUT) == 0:
castate.close()
return False
castate.send(json.dumps({'list_downtimes': True }))
for d in json.loads(castate.recv()):
dh = self._hash_downtime(d)
if dh in lodowntimes:
continue
self._locmd.send(json.dumps(d))
self._lostate.send(json.dumps({'list_comments': True }))
locomments = set()
for c in json.loads(self._lostate.recv()):
locomments.add(hash_downtime(c))
castate.send(json.dumps({'list_comments': True }))
for c in json.loads(castate.recv()):
ch = self._hash_downtime(c)
if ch in locomments:
continue
self._locmd.send(json.dumps(c))
castate.send(json.dumps({ 'list_hosts': True, 'expand_lists': True,
'include_services': True, 'keys': [ 'host_name', 'service_description',
'plugin_output', 'long_output', 'perf_data', 'current_state',
'current_attempt', 'state_type', 'is_flapping', 'notifications_enabled',
'checks_enabled', 'event_handler_enabled', 'flap_detection_enabled',
'problem_has_been_acknowledged', 'accept_passive_service_checks',
'accept_passive_host_checks', 'type' ] }))
state_data = json.loads(castate.recv())
self._locmd.send(json.dumps({'type': 'state_data', 'data': state_data }))
castate.close()
return True
# This should be overwritten to become the passive node in your environment.
def become_passive(self):
pass
def passive_loop(self, timeout):
if not self.resolve_diffs():
return False
if not self.become_passive():
return False
caevents = self._ctx.socket(zmq.SUB)
caevents.connect(self._meventsa)
for s in self._meventsubs:
caevents.setsockopt(zmq.SUBSCRIBE, s)
while caevents.poll(timeout=timeout) != 0:
typemsg, pmsg = caevents.recv_multipart()
payload = json.loads(pmsg)
if typemsg in set('service_check_processed', 'host_check_processed'):
out = { }
tocopy = [ 'host_name', 'service_description', 'return_code',
'start_time', 'finish_time', 'latency', 'early_timeout' ]
for k in tocopy:
if k in payload:
out[k] = payload[k]
out['check_options'] = 0
out['check_type'] = 1
out['exited_ok'] = 1
out['scheduled_check'] = 1
out['reschedule_check'] = 1
final_output = str()
if payload['perf_data']:
final_output = "{0}|{1}\n".format(
payload['output'], payload['perf_data'])
else:
final_output = "{0}\n".format(payload['output'])
if payload['long_output']:
final_output += payload['long_output']
out['output'] = final_output
self._locmd.send(json.dumps(out))
elif typemsg in set('adaptivehost_update', 'adaptiveservice_update'):
out = { 'type': 'command' }
for k in [ 'host_name', 'service_description' ]:
if k in payload:
out[k] = payload[k]
cmd_map = { 'notifications_enabled':
[ 'enable_%s_notifications', 'disable_%s_notifications' ],
'checks_enabled':
[ 'enable_%s_checks', 'disable_%s_checks' ],
'accept_passive_service_checks':
[ 'enable_passive_%s_checks', 'disable_passive_%s_checks' ],
'accept_passive_host_checks':
[ 'enable_passive_%s_checks', 'disable_passive_%s_checks' ],
'event_handler_enabled':
[ 'enable_%s_event_handler', 'enable_%s_event_handler' ],
'obsess_over_service':
[ 'start_obsessing_over_%s', 'stop_obsessing_over_%s' ],
'obsess_over_host':
[ 'start_obsessing_over_%s', 'stop_obsessing_over_%s' ]
}
for k in cmd_map:
if k not in payload:
continue
nku = cmd_map[k][0] if payload[k] else cmd_map[k][1]
nk = nku % 'service' if 'service_description' in out else 'host'
out['command_name'] = nk
break
self._locmd.send(json.dumps(out))
elif typemsg == 'program_status':
continue
else:
self._locmd.send(pmsg)
caevents.close()
return True
# This should be overwritten to become the active node in your environment.
def become_active(self):
pass
def active_loop(self, timeout):
if not self.become_active():
return False
while self._loevents.poll(timeout=timeout) != 0:
tmsg, pmsg = self._loevents.recv_multipart()
pass
return True