update styling and templating, add filters for groups

This commit is contained in:
Sophie Schiller 2021-01-02 13:47:09 +01:00
parent 8ae8a218e9
commit e72c7ded65
3 changed files with 100 additions and 47 deletions

View file

@ -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
```

View file

@ -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 = """
<li class="list-group-item d-flex justify-content-between align-items-center">
{}
<span class="badge badge-success">OK</span>
</li>
"""
hosts_warning_template = """
<li class="list-group-item d-flex justify-content-between align-items-center">
{}
<span class="badge badge-warning">WARNING</span>
</li>
"""
hosts_critical_template = """
<li class="list-group-item d-flex justify-content-between align-items-center">
{}
<span class="badge badge-danger">CRITICAL</span>
</li>
"""
for host in data['hosts']['results']:
if host['attrs']['state'] == 0:
hosts_operational = hosts_operational + """<p><button type="button" class="btn btn-success">{}</button></p>\n""".format(host['name'])
hosts_operational = hosts_operational + hosts_operational_template.format(host['name'])
elif host['attrs']['state'] == 1:
hosts_warning = hosts_warning + """<p><button type="button" class="btn btn-warning">{}</button></p>\n""".format(host['name'])
hosts_warning = hosts_warning + hosts_critical_template.format(host['name'])
else:
hosts_critical = hosts_critical + """<p><button type="button" class="btn btn-danger">{}</button></p>\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 = """
<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']):
if service['attrs']['host_name'] == host:
if service['attrs']['state'] == 0:
services_operational = services_operational + """<p><button type="button" class="btn btn-success">{}</button></p>\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 + """<p><button type="button" class="btn btn-warning">{}</button></p>\n""".format(service['attrs']['display_name'])
services_warning = services_warning + services_template.format(service['attrs']['display_name'], 'warning', 'WARNING')
else:
services_critical = services_critical + """<p><button type="button" class="btn btn-danger">{}</button></p>\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

View file

@ -1,32 +1,11 @@
<div class="row">
<div class="col-lg-12">
<div class="page-header">
<h3 id="typography">{host}</h3>
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="card text-white border-success mb-3" style="max-width: 20rem;">
<h5 class="card-header">Operational</h5>
<div class="card text-white border-primary mb-3">
{card_header}
<div class="card-body">
{services_operational}
</div>
</div>
</div>
<div class="col">
<div class="card text-white border-warning mb-3" style="max-width: 20rem;">
<h5 class="card-header">Warning</h5>
<div class="card-body">
{services_warning}
</div>
</div>
</div>
<div class="col">
<div class="card text-white border-danger mb-3" style="max-width: 20rem;">
<h5 class="card-header">Critical</h5>
<div class="card-body">
{services_critical}
<ul class="list-group">{services_critical}</ul>
<ul class="list-group">{services_warning}</ul>
<ul class="list-group">{services_operational}</ul>
</div>
</div>
</div>