2020-11-21 21:35:43 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import json
|
|
|
|
import requests
|
|
|
|
import configparser
|
|
|
|
import urllib3
|
|
|
|
urllib3.disable_warnings()
|
|
|
|
|
|
|
|
|
|
|
|
def do_api_calls(config):
|
|
|
|
data = {}
|
2021-01-02 12:47:09 +00:00
|
|
|
|
|
|
|
#services
|
|
|
|
request_url = "{}/v1/objects/services".format(config['icinga2_api']['baseurl'])
|
|
|
|
headers = {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'X-HTTP-Method-Override': 'GET'
|
|
|
|
}
|
|
|
|
requestbody = {
|
|
|
|
"attrs": [ "name", "state", "last_check_result", "host_name", "display_name" ],
|
|
|
|
"joins": [ "host.name", "host.state", "host.last_check_result" ],
|
2021-01-02 13:02:14 +00:00
|
|
|
"filter": config['filters']['services'],
|
2021-01-02 12:47:09 +00:00
|
|
|
}
|
|
|
|
r = requests.get(request_url,
|
|
|
|
headers=headers,
|
|
|
|
data=json.dumps(requestbody),
|
|
|
|
auth=(config['icinga2_api']['username'], config['icinga2_api']['password']),
|
|
|
|
verify=False)
|
|
|
|
|
|
|
|
if (r.status_code == 200):
|
|
|
|
data['services'] = r.json()
|
|
|
|
else:
|
|
|
|
r.raise_for_status()
|
2021-01-02 13:33:40 +00:00
|
|
|
|
2021-01-02 12:47:09 +00:00
|
|
|
# hosts
|
|
|
|
request_url = "{}/v1/objects/hosts".format(config['icinga2_api']['baseurl'])
|
|
|
|
headers = {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'X-HTTP-Method-Override': 'GET'
|
|
|
|
}
|
|
|
|
requestbody = {
|
|
|
|
"attrs": [ "name", "state" ],
|
2021-01-02 13:02:14 +00:00
|
|
|
"filter": config['filters']['hosts'],
|
2021-01-02 12:47:09 +00:00
|
|
|
}
|
|
|
|
r = requests.get(request_url,
|
|
|
|
headers=headers,
|
|
|
|
data=json.dumps(requestbody),
|
|
|
|
auth=(config['icinga2_api']['username'], config['icinga2_api']['password']),
|
|
|
|
verify=False)
|
|
|
|
|
|
|
|
if (r.status_code == 200):
|
|
|
|
data['hosts'] = r.json()
|
|
|
|
else:
|
|
|
|
r.raise_for_status()
|
2021-01-02 13:33:40 +00:00
|
|
|
|
2020-11-21 21:35:43 +00:00
|
|
|
return data
|
|
|
|
|
2021-01-02 13:33:40 +00:00
|
|
|
|
2020-11-21 21:35:43 +00:00
|
|
|
def render_text_output(data):
|
|
|
|
print("{:50s} {:10s}".format("host", "status"))
|
|
|
|
for host in data['hosts']['results']:
|
|
|
|
print("{:50s} {}".format(host['name'], host['attrs']['state']))
|
|
|
|
for service in data['services']['results']:
|
|
|
|
print("{:50s} {}".format(service['name'], service['attrs']['state']))
|
|
|
|
|
|
|
|
|
|
|
|
def render_services_per_host(host, data):
|
|
|
|
services_operational = ''
|
|
|
|
services_warning = ''
|
|
|
|
services_critical = ''
|
2021-01-02 12:47:09 +00:00
|
|
|
card_header = ''
|
2020-11-21 21:35:43 +00:00
|
|
|
|
2021-01-02 12:47:09 +00:00
|
|
|
services_template = """
|
|
|
|
<li class="list-group-item d-flex justify-content-between align-items-center">
|
|
|
|
{}
|
|
|
|
<span class="badge badge-{}">{}</span>
|
|
|
|
</li>
|
|
|
|
"""
|
|
|
|
services_hostname_template = """<div class="card-header d-flex justify-content-between align-items-center"><h4>{}</h4> <span class="badge badge-success">OK</span></div>"""
|
|
|
|
|
|
|
|
for service in sorted(data['services']['results'], key=lambda x: x['attrs']['display_name']):
|
2020-11-21 21:35:43 +00:00
|
|
|
if service['attrs']['host_name'] == host:
|
|
|
|
if service['attrs']['state'] == 0:
|
2021-01-02 12:47:09 +00:00
|
|
|
services_operational = services_operational + services_template.format(service['attrs']['display_name'], 'success', 'OK')
|
2020-11-21 21:35:43 +00:00
|
|
|
elif service['attrs']['state'] == 1:
|
2021-01-02 12:47:09 +00:00
|
|
|
services_warning = services_warning + services_template.format(service['attrs']['display_name'], 'warning', 'WARNING')
|
|
|
|
else:
|
|
|
|
services_critical = services_critical + services_template.format(service['attrs']['display_name'], 'danger', 'CRITICAL')
|
2021-01-02 13:33:40 +00:00
|
|
|
|
2021-01-02 12:47:09 +00:00
|
|
|
if service['joins']['host']['state'] == 0:
|
|
|
|
card_header = services_hostname_template.format(host, 'success', 'UP')
|
|
|
|
elif service['joins']['host']['state'] == 0:
|
|
|
|
card_header = services_hostname_template.format(host, 'warning', 'WARNING')
|
2020-11-21 21:35:43 +00:00
|
|
|
else:
|
2021-01-02 12:47:09 +00:00
|
|
|
card_header = services_hostname_template.format(host, 'danger', 'DOWN')
|
2020-11-21 21:35:43 +00:00
|
|
|
|
|
|
|
with open("services_template.html", "r") as f:
|
|
|
|
htmlTemplate = f.read()
|
|
|
|
|
|
|
|
htmlOutput = htmlTemplate.format(
|
2021-01-02 12:47:09 +00:00
|
|
|
card_header = card_header,
|
2020-11-21 21:35:43 +00:00
|
|
|
services_operational = services_operational,
|
|
|
|
services_warning = services_warning,
|
|
|
|
services_critical = services_critical
|
|
|
|
)
|
|
|
|
return htmlOutput
|
|
|
|
|
2021-01-02 13:33:40 +00:00
|
|
|
|
2020-11-21 21:35:43 +00:00
|
|
|
def render_service_details(data):
|
|
|
|
# generate list of hosts by scanning services for unique host_name
|
|
|
|
host_names = []
|
|
|
|
for service in data['services']['results']:
|
|
|
|
if service['attrs']['host_name'] not in host_names:
|
|
|
|
host_names.append(service['attrs']['host_name'])
|
|
|
|
# render html for each host_name
|
|
|
|
html_output = ""
|
2020-11-21 21:41:51 +00:00
|
|
|
for host in sorted(host_names):
|
2020-11-21 21:35:43 +00:00
|
|
|
html_output = html_output + render_services_per_host(host, data)
|
|
|
|
return html_output
|
|
|
|
|
2021-01-02 13:33:40 +00:00
|
|
|
|
|
|
|
def render_index_html(filename, service_details):
|
2020-11-21 21:35:43 +00:00
|
|
|
with open("template.html", "r") as f:
|
|
|
|
htmlTemplate = f.read()
|
|
|
|
|
|
|
|
htmlOutput = htmlTemplate.format(
|
|
|
|
services = service_details
|
|
|
|
)
|
|
|
|
|
2021-01-02 13:02:14 +00:00
|
|
|
with open(filename, "w") as f:
|
2020-11-21 21:35:43 +00:00
|
|
|
f.write(htmlOutput)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config['icinga2_api'] = {
|
|
|
|
'baseurl': 'https://localhost:5665',
|
|
|
|
'username': 'root',
|
|
|
|
'password': 'foobar'
|
|
|
|
}
|
|
|
|
with open('config.conf', 'r') as configfile:
|
|
|
|
config.read('config.conf')
|
|
|
|
data = do_api_calls(config)
|
|
|
|
service_details = render_service_details(data)
|
2021-01-02 13:33:40 +00:00
|
|
|
render_index_html(config['output']['filename'], service_details)
|
2020-11-21 21:35:43 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|