#!/usr/bin/env python3 import json import requests import configparser import urllib3 from mako.template import Template urllib3.disable_warnings() def do_api_calls(config): data = {} for i in ['hosts', 'services']: request_url = "{}/v1/objects/{}".format(config['icinga2_api']['baseurl'], i) headers = { 'Accept': 'application/json', } r = requests.get(request_url, headers=headers, auth=(config['icinga2_api']['username'], config['icinga2_api']['password']), verify=False) if (r.status_code == 200): data[i] = r.json() else: r.raise_for_status() return data 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_hosts(data): hosts_operational = '' hosts_warning = '' hosts_critical = '' for host in data['hosts']['results']: if host['attrs']['state'] == 0: hosts_operational = hosts_operational + """

\n""".format(host['name']) elif host['attrs']['state'] == 1: hosts_warning = hosts_warning + """

\n""".format(host['name']) else: hosts_critical = hosts_critical + """

\n""".format(host['name']) with open("hosts_template.html", "r") as f: htmlTemplate = f.read() htmlOutput = htmlTemplate.format( hosts_operational = hosts_operational, hosts_warning = hosts_warning, hosts_critical = hosts_critical, ) return htmlOutput def render_services_per_host(host, data): services_operational = '' services_warning = '' services_critical = '' for service in data['services']['results']: if service['attrs']['host_name'] == host: if service['attrs']['state'] == 0: services_operational = services_operational + """

\n""".format(service['attrs']['display_name']) elif service['attrs']['state'] == 1: services_warning = services_warning + """

\n""".format(service['attrs']['display_name']) else: services_critical = services_critical + """

\n""".format(service['attrs']['display_name']) with open("services_template.html", "r") as f: htmlTemplate = f.read() htmlOutput = htmlTemplate.format( host = host, services_operational = services_operational, services_warning = services_warning, services_critical = services_critical ) return htmlOutput 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 = "" for host in sorted(host_names): html_output = html_output + render_services_per_host(host, data) return html_output def render_index_html(host_summary, service_details): with open("template.html", "r") as f: htmlTemplate = f.read() htmlOutput = htmlTemplate.format( hosts = host_summary, services = service_details ) with open("index.html", "w") as f: 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) host_summary = render_hosts(data) service_details = render_service_details(data) render_index_html(host_summary, service_details) if __name__ == "__main__": main()