From e72c7ded65f038dae06b0a7fd8b8b4c3c168f24f Mon Sep 17 00:00:00 2001 From: Sophie Schiller Date: Sat, 2 Jan 2021 13:47:09 +0100 Subject: [PATCH] update styling and templating, add filters for groups --- README.md | 9 ++++ services.py | 107 +++++++++++++++++++++++++++++++++-------- services_template.html | 31 ++---------- 3 files changed, 100 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index e9907ab..0cf0f9b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,11 @@ # simple-icinga-dashboard + +## Config file +This script requires an ini-style config file for icinga base url and credentials. +``` +[icinga2_api] +baseurl = https://example.org:5665 +username = root +password = foobar +``` diff --git a/services.py b/services.py index 87cd4ed..a878d76 100644 --- a/services.py +++ b/services.py @@ -10,20 +10,50 @@ 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() + #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" ], + "filter": "\"checks_with_sms\" in service.groups", + } + 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() + print(json.dumps(data['services'])) + # hosts + request_url = "{}/v1/objects/hosts".format(config['icinga2_api']['baseurl']) + headers = { + 'Accept': 'application/json', + 'X-HTTP-Method-Override': 'GET' + } + requestbody = { + "attrs": [ "name", "state" ], + "filter": "\"checks_with_sms\" in host.groups", + } + 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() + return data def render_text_output(data): @@ -39,13 +69,32 @@ def render_hosts(data): hosts_warning = '' hosts_critical = '' + hosts_operational_template = """ +
  • + {} + OK +
  • + """ + hosts_warning_template = """ +
  • + {} + WARNING +
  • + """ + hosts_critical_template = """ +
  • + {} + CRITICAL +
  • + """ + for host in data['hosts']['results']: if host['attrs']['state'] == 0: - hosts_operational = hosts_operational + """

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

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

    \n""".format(host['name']) + hosts_critical = hosts_critical + hosts_critical_template.format(host['name']) with open("hosts_template.html", "r") as f: htmlTemplate = f.read() @@ -60,21 +109,37 @@ def render_services_per_host(host, data): services_operational = '' services_warning = '' services_critical = '' + card_header = '' - for service in data['services']['results']: + services_template = """ +
  • + {} + {} +
  • + """ + services_hostname_template = """

    {}

    OK
    """ + + for service in sorted(data['services']['results'], key=lambda x: x['attrs']['display_name']): if service['attrs']['host_name'] == host: if service['attrs']['state'] == 0: - services_operational = services_operational + """

    \n""".format(service['attrs']['display_name']) + services_operational = services_operational + services_template.format(service['attrs']['display_name'], 'success', 'OK') elif service['attrs']['state'] == 1: - services_warning = services_warning + """

    \n""".format(service['attrs']['display_name']) + services_warning = services_warning + services_template.format(service['attrs']['display_name'], 'warning', 'WARNING') else: - services_critical = services_critical + """

    \n""".format(service['attrs']['display_name']) + services_critical = services_critical + services_template.format(service['attrs']['display_name'], 'danger', 'CRITICAL') + + 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') + else: + card_header = services_hostname_template.format(host, 'danger', 'DOWN') with open("services_template.html", "r") as f: htmlTemplate = f.read() htmlOutput = htmlTemplate.format( - host = host, + card_header = card_header, services_operational = services_operational, services_warning = services_warning, services_critical = services_critical diff --git a/services_template.html b/services_template.html index 59713f0..1e1ab50 100644 --- a/services_template.html +++ b/services_template.html @@ -1,32 +1,11 @@ -
    -
    - -
    -
    -
    -
    Operational
    +
    + {card_header}
    - {services_operational} -
    -
    -
    -
    -
    -
    Warning
    -
    - {services_warning} -
    -
    -
    -
    -
    -
    Critical
    -
    - {services_critical} +
      {services_critical}
    +
      {services_warning}
    +
      {services_operational}