2020-12-18 20:55:00 +00:00
|
|
|
#!/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()
|
|
|
|
|
2021-01-02 10:44:29 +00:00
|
|
|
SORT_ORDER = [3, 2, 0, 1]
|
2020-12-19 07:45:40 +00:00
|
|
|
|
2020-12-18 20:55:00 +00:00
|
|
|
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
|
|
|
|
|
2021-05-26 06:53:22 +00:00
|
|
|
|
2020-12-18 20:55:00 +00:00
|
|
|
def to_unixtimestamp(dt):
|
|
|
|
return int(time.mktime(dt.timetuple()))
|
|
|
|
|
2021-05-26 06:53:22 +00:00
|
|
|
|
|
|
|
def limit_output_lines(output, num_lines=10):
|
|
|
|
actual = len(output)
|
|
|
|
|
|
|
|
if actual <= num_lines:
|
|
|
|
return output
|
|
|
|
|
|
|
|
more = actual-(num_lines-1)
|
2021-05-26 06:55:57 +00:00
|
|
|
if more > 1:
|
|
|
|
s = 's'
|
|
|
|
else:
|
|
|
|
s = ''
|
2021-05-26 06:53:22 +00:00
|
|
|
|
|
|
|
result = output[:(num_lines-1)]
|
2021-05-26 06:55:57 +00:00
|
|
|
result.append('... and {} more line{}'.format(
|
2021-05-26 06:53:22 +00:00
|
|
|
more,
|
2021-05-26 06:55:57 +00:00
|
|
|
s,
|
2021-05-26 06:53:22 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2020-12-18 20:55:00 +00:00
|
|
|
def regenerate():
|
|
|
|
now = current_time()
|
|
|
|
|
|
|
|
services = {
|
|
|
|
'generated': to_unixtimestamp(now),
|
|
|
|
'prettytime': now.strftime('%d.%m.%Y %H:%M:%S'),
|
|
|
|
'services': [],
|
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
2020-12-19 07:46:13 +00:00
|
|
|
broken_hosts = set()
|
|
|
|
|
2020-12-19 08:08:53 +00:00
|
|
|
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()
|
2020-12-18 20:55:00 +00:00
|
|
|
|
|
|
|
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']:
|
2020-12-19 07:46:13 +00:00
|
|
|
broken_hosts.add(host['attrs']['display_name'])
|
|
|
|
|
|
|
|
if host['attrs']['downtime_depth'] > 0:
|
|
|
|
continue
|
|
|
|
|
2021-06-22 04:55:22 +00:00
|
|
|
if host['attrs']['acknowledgement'] > 0:
|
2021-01-02 10:49:59 +00:00
|
|
|
if not CONFIG['show_ack']:
|
|
|
|
continue
|
|
|
|
|
2023-03-06 05:40:50 +00:00
|
|
|
ack = '[ACK] '
|
2021-01-02 10:49:59 +00:00
|
|
|
else:
|
|
|
|
ack = ''
|
|
|
|
|
2021-02-14 10:49:07 +00:00
|
|
|
if not CONFIG['show_soft'] and int(host['attrs']['state_type']) == 0:
|
|
|
|
continue
|
|
|
|
|
2020-12-18 20:55:00 +00:00
|
|
|
services['services'].append({
|
2023-03-06 05:40:50 +00:00
|
|
|
'host': ack + host['attrs']['display_name'],
|
2020-12-18 20:55:00 +00:00
|
|
|
'service': '-- HOST --',
|
|
|
|
'state': 2,
|
2020-12-19 07:45:40 +00:00
|
|
|
'type': int(host['attrs']['state_type']),
|
2021-05-26 06:53:22 +00:00
|
|
|
'output': limit_output_lines(host['attrs']['last_check_result']['output'].splitlines(), 3),
|
2021-01-02 10:49:59 +00:00
|
|
|
'ack': ack,
|
2020-12-19 07:45:40 +00:00
|
|
|
'sort': '{}{}{}{}'.format(
|
2021-01-02 10:44:29 +00:00
|
|
|
int(host['attrs']['state_type'])*-1,
|
2020-12-19 07:45:40 +00:00
|
|
|
SORT_ORDER[2],
|
|
|
|
host['attrs']['display_name'],
|
|
|
|
'--',
|
|
|
|
),
|
2020-12-18 20:55:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
for svc in serv['results']:
|
|
|
|
if svc['attrs']['problem']:
|
2020-12-19 07:46:13 +00:00
|
|
|
if svc['attrs']['downtime_depth'] > 0 or svc['attrs']['host_name'] in broken_hosts:
|
|
|
|
continue
|
|
|
|
|
2021-06-22 04:55:22 +00:00
|
|
|
if svc['attrs']['acknowledgement'] > 0:
|
2021-01-02 10:49:59 +00:00
|
|
|
if not CONFIG['show_ack']:
|
|
|
|
continue
|
|
|
|
|
|
|
|
ack = 'ACKNOWLEDGED'
|
|
|
|
else:
|
|
|
|
ack = ''
|
|
|
|
|
2021-02-14 10:49:07 +00:00
|
|
|
if not CONFIG['show_soft'] and int(svc['attrs']['state_type']) == 0:
|
|
|
|
continue
|
|
|
|
|
2020-12-18 20:55:00 +00:00
|
|
|
services['services'].append({
|
|
|
|
'host': svc['attrs']['host_name'],
|
|
|
|
'service': svc['attrs']['display_name'],
|
2020-12-19 07:45:40 +00:00
|
|
|
'state': int(svc['attrs']['state']),
|
|
|
|
'type': int(svc['attrs']['state_type']),
|
2021-05-26 06:53:22 +00:00
|
|
|
'output': limit_output_lines(svc['attrs']['last_check_result']['output'].splitlines()),
|
2021-01-02 10:49:59 +00:00
|
|
|
'ack': ack,
|
2020-12-19 07:45:40 +00:00
|
|
|
'sort': '{}{}{}{}'.format(
|
2021-01-02 10:44:29 +00:00
|
|
|
int(svc['attrs']['state_type'])*-1,
|
2020-12-19 07:45:40 +00:00
|
|
|
SORT_ORDER[int(svc['attrs']['state'])],
|
|
|
|
svc['attrs']['host_name'],
|
|
|
|
svc['attrs']['display_name'],
|
|
|
|
),
|
2020-12-18 20:55:00 +00:00
|
|
|
})
|
|
|
|
except Exception as e:
|
|
|
|
services['services'].append({
|
|
|
|
'host': 'icinga2beamer',
|
|
|
|
'service': 'INTERNAL',
|
|
|
|
'state': 2,
|
2020-12-19 07:45:40 +00:00
|
|
|
'type': 1,
|
2021-01-02 10:49:59 +00:00
|
|
|
'ack': '',
|
2020-12-18 20:55:00 +00:00
|
|
|
'output': [repr(e)],
|
2021-01-02 10:44:29 +00:00
|
|
|
'sort': 999,
|
2020-12-18 20:55:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if len(services['services']) == 0:
|
|
|
|
services['services'].append({
|
|
|
|
'host': '',
|
|
|
|
'service': 'icinga2',
|
|
|
|
'state': 0,
|
2020-12-19 07:45:40 +00:00
|
|
|
'type': 1,
|
2021-01-02 10:49:59 +00:00
|
|
|
'ack': '',
|
2020-12-18 20:55:00 +00:00
|
|
|
'output': ['Everything is fine. Go get some coffee.'],
|
2020-12-19 07:45:40 +00:00
|
|
|
'sort': 1000,
|
2020-12-18 20:55:00 +00:00
|
|
|
})
|
|
|
|
|
2021-01-02 10:44:29 +00:00
|
|
|
services['services'].sort(key=lambda x: x['sort'])
|
2020-12-19 07:45:40 +00:00
|
|
|
|
2020-12-18 20:55:00 +00:00
|
|
|
with file("services.json", "wb") as f:
|
|
|
|
f.write(json.dumps(services, ensure_ascii=False).encode("utf8"))
|
|
|
|
|
2021-05-26 06:53:22 +00:00
|
|
|
|
2020-12-18 20:55:00 +00:00
|
|
|
def main():
|
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
regenerate()
|
|
|
|
except Exception:
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
time.sleep(20)
|
|
|
|
|
2021-05-26 06:53:22 +00:00
|
|
|
|
2020-12-18 20:55:00 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|