-
Notifications
You must be signed in to change notification settings - Fork 1
/
icinga.py
264 lines (219 loc) · 8.98 KB
/
icinga.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
from datetime import datetime
from getpass import getuser
from requests import get, post
from urllib.parse import quote_plus
import urllib3
urllib3.disable_warnings()
class IcingaItem(object):
def __init__(self, json, only_host_name=None):
if json is None and only_host_name:
self.host_name = only_host_name
self.service_name = '-- HOST --'
self.type = 'Host'
self.acknowledgement = 0
self.downtime_depth = 0
self.output_lines = []
self.state = 0
self.state_type = 0
return
self.acknowledgement = int(json['attrs']['acknowledgement'])
self.downtime_depth = int(json['attrs']['downtime_depth'])
self.state = int(json['attrs']['state'])
self.state_type = int(json['attrs']['state_type'])
self.type = json['type']
if self.type == 'Service':
self.host_name = json['attrs']['host_name']
self.service_name = json['attrs']['display_name']
else:
if self.state != 0:
self.state = 2
self.host_name = json['attrs']['display_name']
self.service_name = '-- HOST --'
lcr = json['attrs'].get('last_check_result', {})
if lcr is not None:
self.output_lines = lcr.get('output', '').splitlines()
else:
self.output_lines = []
def __eq__(self, other):
return (
isinstance(other, IcingaItem) and
self.acknowledgement == other.acknowledgement and
self.downtime_depth == other.downtime_depth and
self.host_name == other.host_name and
self.output_lines == other.output_lines and
self.service_name == other.service_name and
self.state == other.state and
self.state_type == other.state_type and
self.type == other.type
)
def __hash__(self):
return hash(
(
self.acknowledgement,
self.downtime_depth,
self.host_name,
tuple(self.output_lines),
self.service_name,
self.state,
self.state_type,
self.type,
)
)
def __lt__(self, other):
if self.state == other.state:
if self.host_name == other.host_name:
return self.service_name < other.service_name
else:
return self.host_name < other.host_name
else:
return -self.state < -other.state
def __str__(self):
# Only useful for debugging. Use get_line_to_show() for
# formatted output.
return f'{self.host_name} {self.service_name}'
def get_filter(self):
f = f'match("{self.host_name}", host.name)'
if self.type == 'Service':
f += f' && match("{self.service_name}", service.name)'
return f
def get_line_to_show(self, len_col1):
prefixes = ''
prefixes += 'S' if self.state_type == 0 else ' '
prefixes += 'A' if self.acknowledgement != 0 else ' '
prefixes += 'D' if self.downtime_depth > 0 else ' '
host_name = self.host_name + ' ' * (len_col1 - len(self.host_name))
return f'{prefixes} {host_name} {self.service_name}'
class Icinga(object):
def _api(self, endpoint):
return self.settings['base_url'] + '/api/v1/' + endpoint
def get_current_state(self):
# Filtering for groups has to be done differently depending on
# whether you use terminga-proxy or not. In plain Icinga, you
# need to use a full-blown filter query ("filter=..."). Filter
# queries are not implemented in terminga-proxy at all. Instead,
# we pass "hostgroup=..." directly (which is ignored by plain
# Icinga).
filters = {
'host': [None],
'service': [None],
}
if self.settings['use_group_filters']:
filters['host'] = self.settings['group_filters'].get('host_groups', [None])
filters['service'] = self.settings['group_filters'].get('service_groups', [None])
results = {
'hosts': [],
'services': [],
}
for thing in ('host', 'service'):
for group in filters[thing]:
params = {}
if group is not None:
group = quote_plus(group)
params = f'filter="{group}"%20in%20{thing}.groups'
params += f'&{thing}group={group}'
r = get(
self._api(f'objects/{thing}s'),
auth=self.settings['auth'],
params=params,
verify=self.settings['ssl_verify'],
timeout=5,
)
r.raise_for_status()
decoded_json = r.json()
results[f'{thing}s'].extend([IcingaItem(i) for i in decoded_json['results']])
return results
def _queue_check_typed(self, items, item_type):
items = [i for i in items if i.type == item_type]
chunk_size = 20
for c in range(0, len(items), chunk_size):
filters = []
for i in items[c:c + chunk_size]:
filters.append('(' + i.get_filter() + ')')
data = {
'type': item_type,
'filter': ' || '.join(filters),
'force': True,
}
r = post(
self._api('actions/reschedule-check'),
auth=self.settings['auth'],
headers={'Accept': 'application/json'},
json=data,
verify=self.settings['ssl_verify'],
timeout=5,
)
def queue_check(self, items):
self._queue_check_typed(items, 'Host')
self._queue_check_typed(items, 'Service')
def _set_ack_typed(self, items, comment, end_time, item_type):
items = [i for i in items if i.type == item_type]
chunk_size = 20
for c in range(0, len(items), chunk_size):
filters = []
for i in items[c:c + chunk_size]:
filters.append('(' + i.get_filter() + ')')
data = {
'author': getuser(),
'comment': comment,
'type': item_type,
'filter': ' || '.join(filters),
# "Whether the acknowledgement will be set until the
# service or host fully recovers. Defaults to false."
'sticky': True,
}
if end_time:
data['expiry'] = end_time
r = post(
self._api('actions/acknowledge-problem'),
auth=self.settings['auth'],
headers={'Accept': 'application/json'},
json=data,
verify=self.settings['ssl_verify'],
timeout=5,
)
def set_ack(self, items, comment, end_time):
self._set_ack_typed(items, comment, end_time, 'Host')
self._set_ack_typed(items, comment, end_time, 'Service')
def set_ack_for_host(self, items, all_items, comment, end_time):
items_for_action = set()
for i in all_items['hosts'] + all_items['services']:
for sel in items:
if i.host_name == sel.host_name:
items_for_action.add(i)
self._set_ack_typed(items_for_action, comment, end_time, 'Host')
self._set_ack_typed(items_for_action, comment, end_time, 'Service')
def _set_downtime_typed(self, items, comment, start_time, end_time, item_type):
items = [i for i in items if i.type == item_type]
chunk_size = 20
for c in range(0, len(items), chunk_size):
filters = []
for i in items[c:c + chunk_size]:
filters.append('(' + i.get_filter() + ')')
data = {
'author': getuser(),
'comment': comment,
'start_time': start_time,
'end_time': end_time,
'type': item_type,
'filter': ' || '.join(filters),
'child_options': 'DowntimeTriggeredChildren',
}
r = post(
self._api('actions/schedule-downtime'),
auth=self.settings['auth'],
headers={'Accept': 'application/json'},
json=data,
verify=self.settings['ssl_verify'],
timeout=5,
)
def set_downtime(self, items, comment, start_time, end_time):
self._set_downtime_typed(items, comment, start_time, end_time, 'Host')
self._set_downtime_typed(items, comment, start_time, end_time, 'Service')
def set_downtime_for_host(self, items, comment, start_time, end_time):
host_names = set()
for i in items:
host_names.add(i.host_name)
dummys = set()
for i in host_names:
dummys.add(IcingaItem(None, only_host_name=i))
self.set_downtime(dummys, comment, start_time, end_time)