-
Notifications
You must be signed in to change notification settings - Fork 0
/
service
executable file
·171 lines (138 loc) · 5.06 KB
/
service
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
#!/usr/bin/env python
import requests
import traceback
import time
import json
import urllib2
import pytz
import hashlib
from datetime import datetime, timedelta
from itertools import islice
from hosted import CONFIG, NODE
CONFIG.restart_on_update()
SORT_ORDER = [3, 2, 0, 1]
def current_time():
timezone = pytz.timezone("Europe/Berlin")
now = datetime.utcnow()
now = now.replace(tzinfo=pytz.utc)
now = now.astimezone(timezone)
now = now.replace(tzinfo=None)
return now
def to_unixtimestamp(dt):
return int(time.mktime(dt.timetuple()))
def limit_output_lines(output, num_lines=10):
actual = len(output)
if actual <= num_lines:
return output
more = actual-(num_lines-1)
if more > 1:
s = 's'
else:
s = ''
result = output[:(num_lines-1)]
result.append('... and {} more line{}'.format(
more,
s,
))
return result
def regenerate():
now = current_time()
services = {
'generated': to_unixtimestamp(now),
'prettytime': now.strftime('%d.%m.%Y %H:%M:%S'),
'services': [],
}
try:
broken_hosts = set()
hosts = requests.get(CONFIG["url_hosts"], auth=(CONFIG["api_user"], CONFIG["api_password"]), verify=CONFIG["ssl_verify"]).json()
serv = requests.get(CONFIG["url_services"], auth=(CONFIG["api_user"], CONFIG["api_password"]), verify=CONFIG["ssl_verify"]).json()
if 'results' not in hosts:
raise KeyError('API call for hosts did not return any results')
if 'results' not in serv:
raise KeyError('API call for services did not return any results')
for host in hosts['results']:
if host['attrs']['problem']:
broken_hosts.add(host['attrs']['display_name'])
if (
host['attrs']['downtime_depth'] > 0
or (
host['attrs']['acknowledgement'] > 0
and not CONFIG['show_ack']
)
):
continue
if not CONFIG['show_soft'] and int(host['attrs']['state_type']) == 0:
continue
services['services'].append({
'host': host['attrs']['display_name'],
'service': '',
'state': 2,
'type': int(host['attrs']['state_type']),
'output': limit_output_lines(host['attrs']['last_check_result']['output'].splitlines(), 3),
'ack': bool(host['attrs']['acknowledgement'] > 0),
'sort': '{}{}{}{}'.format(
int(host['attrs']['state_type'])*-1,
SORT_ORDER[2],
host['attrs']['display_name'],
'--',
),
})
for svc in serv['results']:
if svc['attrs']['problem']:
if (
svc['attrs']['host_name'] in broken_hosts
or svc['attrs']['downtime_depth'] > 0
or (
svc['attrs']['acknowledgement'] > 0
and not CONFIG['show_ack']
)
):
continue
if not CONFIG['show_soft'] and int(svc['attrs']['state_type']) == 0:
continue
services['services'].append({
'host': svc['attrs']['host_name'],
'service': svc['attrs']['display_name'],
'state': int(svc['attrs']['state']),
'type': int(svc['attrs']['state_type']),
'output': limit_output_lines(svc['attrs']['last_check_result']['output'].splitlines()),
'ack': bool(svc['attrs']['acknowledgement'] > 0),
'sort': '{}{}{}{}'.format(
int(svc['attrs']['state_type'])*-1,
SORT_ORDER[int(svc['attrs']['state'])],
svc['attrs']['host_name'],
svc['attrs']['display_name'],
),
})
except Exception as e:
services['services'].append({
'host': 'icinga2beamer',
'service': 'INTERNAL',
'state': 2,
'type': 1,
'ack': '',
'output': [repr(e)],
'sort': 999,
})
if len(services['services']) == 0:
services['services'].append({
'host': '',
'service': 'icinga2',
'state': 0,
'type': 1,
'ack': '',
'output': ['Everything is fine. Go get some coffee.'],
'sort': 1000,
})
services['services'].sort(key=lambda x: x['sort'])
with file("services.json", "wb") as f:
f.write(json.dumps(services, ensure_ascii=False).encode("utf8"))
def main():
while 1:
try:
regenerate()
except Exception:
traceback.print_exc()
time.sleep(CONFIG["refresh_interval"])
if __name__ == "__main__":
main()