first draft

This commit is contained in:
Sophie Schiller 2020-11-21 22:35:43 +01:00
parent 3305099229
commit 7e1c6ecdca
6 changed files with 238 additions and 0 deletions

5
.gitignore vendored
View file

@ -129,3 +129,8 @@ dmypy.json
# Pyre type checker # Pyre type checker
.pyre/ .pyre/
# vim stuff
*.swp
# generated output
index.html

12
bootstrap.min.css vendored Normal file

File diff suppressed because one or more lines are too long

26
hosts_template.html Normal file
View file

@ -0,0 +1,26 @@
<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-body">
{hosts_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">
{hosts_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">
{hosts_critical}
</div>
</div>
</div>
</div>

126
services.py Normal file
View file

@ -0,0 +1,126 @@
#!/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 + """<p><button type="button" class="btn btn-success">{}</button></p>\n""".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'])
else:
hosts_critical = hosts_critical + """<p><button type="button" class="btn btn-danger">{}</button></p>\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 + """<p><button type="button" class="btn btn-success">{}</button></p>\n""".format(service['name'])
elif service['attrs']['state'] == 1:
services_warning = services_warning + """<p><button type="button" class="btn btn-warning">{}</button></p>\n""".format(service['name'])
else:
services_critical = services_critical + """<p><button type="button" class="btn btn-danger">{}</button></p>\n""".format(service['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'])
print(host_names)
# render html for each host_name
html_output = ""
for host in 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)
print(service_details)
render_index_html(host_summary, service_details)
if __name__ == "__main__":
main()

33
services_template.html Normal file
View file

@ -0,0 +1,33 @@
<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-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}
</div>
</div>
</div>
</div>

36
template.html Normal file
View file

@ -0,0 +1,36 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Status Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="page-header my-5" id="banner">
<div class="row">
<div class="col-lg-8 col-md-7 col-sm-6">
<h1>Status Page</h1>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div class="page-header">
<h2 id="typography">Hosts</h2>
</div>
</div>
</div>
{hosts}
<div class="row">
<div class="col-lg-12">
<div class="page-header">
<h2 id="typography">Services</h2>
</div>
</div>
</div>
{services}
</div>
</body>
</html>