Merge pull request 'kunsi-mako-templating' (#7) from kunsi-mako-templating into kunsi-improvements
Reviewed-on: https://git.kunsmann.eu/sophie/simple-icinga-dashboard/pulls/7
This commit is contained in:
commit
755b649197
4 changed files with 75 additions and 90 deletions
|
@ -1 +1,2 @@
|
||||||
|
Mako
|
||||||
tomlkit
|
tomlkit
|
||||||
|
|
125
service.py
125
service.py
|
@ -1,10 +1,16 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import tomlkit
|
|
||||||
import urllib3
|
import urllib3
|
||||||
|
from os import environ
|
||||||
|
|
||||||
|
import tomlkit
|
||||||
|
from mako.template import Template
|
||||||
|
|
||||||
urllib3.disable_warnings()
|
urllib3.disable_warnings()
|
||||||
|
|
||||||
|
CONFIGFILE = environ.get('STATUSPAGE_CONFIG', 'config.toml')
|
||||||
|
|
||||||
class StatusPage:
|
class StatusPage:
|
||||||
def get_api_result(self):
|
def get_api_result(self):
|
||||||
if self.services:
|
if self.services:
|
||||||
|
@ -44,106 +50,73 @@ class StatusPage:
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
|
||||||
def render_services_per_host(self, host):
|
def get_services_per_host(self):
|
||||||
services = []
|
|
||||||
state_to_design_mapping = [
|
state_to_design_mapping = [
|
||||||
('success', 'OK'),
|
('success', 'OK'),
|
||||||
('warning', 'WARNING'),
|
('warning', 'WARNING'),
|
||||||
('danger', 'CRITICAL'),
|
('danger', 'CRITICAL'),
|
||||||
('info', 'UNKNOWN'),
|
('info', 'UNKNOWN'),
|
||||||
]
|
]
|
||||||
card_header = ''
|
result = {}
|
||||||
|
|
||||||
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 id="{0}" class="card-header d-flex justify-content-between align-items-center">
|
|
||||||
<h4><a href="#{0}">{1}</a></h4>
|
|
||||||
<span class="badge badge-{2}">{3}</span>
|
|
||||||
</div>"""
|
|
||||||
|
|
||||||
for service in sorted(self.get_api_result(), key=lambda x: x['attrs']['display_name']):
|
|
||||||
if service['attrs']['host_name'] == host:
|
|
||||||
state = int(service['attrs']['state'])
|
|
||||||
if state in (1, 2):
|
|
||||||
self.ragecounter += state
|
|
||||||
|
|
||||||
services.append(services_template.format(
|
|
||||||
self.prettify(service['attrs']['display_name']),
|
|
||||||
state_to_design_mapping[state][0],
|
|
||||||
state_to_design_mapping[state][1],
|
|
||||||
))
|
|
||||||
|
|
||||||
if not card_header:
|
|
||||||
if service['joins']['host']['state'] == 0:
|
|
||||||
card_header = services_hostname_template.format(
|
|
||||||
host,
|
|
||||||
service['joins']['host']['vars']['pretty_name'],
|
|
||||||
'success',
|
|
||||||
'UP',
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
card_header = services_hostname_template.format(
|
|
||||||
host,
|
|
||||||
service['joins']['host']['vars']['pretty_name'],
|
|
||||||
'danger',
|
|
||||||
'DOWN',
|
|
||||||
)
|
|
||||||
self.ragecounter += 100
|
|
||||||
|
|
||||||
with open("services_template.html", "r") as f:
|
|
||||||
htmlTemplate = f.read()
|
|
||||||
|
|
||||||
htmlOutput = htmlTemplate.format(
|
|
||||||
card_header = card_header,
|
|
||||||
services = ''.join(services),
|
|
||||||
)
|
|
||||||
return htmlOutput
|
|
||||||
|
|
||||||
|
|
||||||
def render_service_details(self):
|
|
||||||
# generate list of hosts by scanning services for unique host_name
|
|
||||||
host_names = {}
|
|
||||||
for service in self.get_api_result():
|
for service in self.get_api_result():
|
||||||
host_names[service['joins']['host']['vars']['pretty_name']] = service['attrs']['host_name']
|
host = service['joins']['host']['vars']['pretty_name']
|
||||||
|
|
||||||
# render html for each host_name
|
if host not in result:
|
||||||
html_output = []
|
result[host] = {
|
||||||
# Can't use .values() here, since we want to sort by prettyname
|
'hostname': service['attrs']['host_name'],
|
||||||
for prettyname, hostname in sorted(host_names.items()):
|
'services': {},
|
||||||
html_output.append(self.render_services_per_host(hostname))
|
}
|
||||||
|
if service['joins']['host']['state'] == 0:
|
||||||
|
result[host]['host_badge'] = 'success'
|
||||||
|
result[host]['host_state'] = 'UP'
|
||||||
|
else:
|
||||||
|
result[host]['host_badge'] = 'danger'
|
||||||
|
result[host]['host_state'] = 'DOWN'
|
||||||
|
self.ragecounter += 10
|
||||||
|
|
||||||
return ''.join(html_output)
|
state = int(service['attrs']['state'])
|
||||||
|
|
||||||
|
if state in (1, 2):
|
||||||
|
self.ragecounter += state
|
||||||
|
|
||||||
|
result[host]['services'][self.prettify(service['attrs']['display_name'])] = {
|
||||||
|
'badge': state_to_design_mapping[state][0],
|
||||||
|
'state': state_to_design_mapping[state][1],
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
def render_index_html(self, service_details):
|
def render_html(self, service_details):
|
||||||
with open("template.html", "r") as f:
|
|
||||||
htmlTemplate = f.read()
|
|
||||||
if self.ragecounter == 0:
|
if self.ragecounter == 0:
|
||||||
mood = '🆗'
|
mood = '🆗'
|
||||||
elif self.ragecounter < 10:
|
elif self.ragecounter < 10:
|
||||||
mood = '🚨'
|
mood = '🚨'
|
||||||
else:
|
else:
|
||||||
mood = '🔥'
|
mood = '🔥'
|
||||||
htmlOutput = htmlTemplate.format(
|
|
||||||
services = service_details,
|
template = Template(filename=self.config['output'].get('template', 'template.html'))
|
||||||
mood = mood,
|
output = template.render(
|
||||||
|
title=self.config['output'].get('page_title', 'Status Page'),
|
||||||
|
mood=mood,
|
||||||
|
hosts=service_details,
|
||||||
)
|
)
|
||||||
|
|
||||||
with open(self.config['output']['filename'], "w") as f:
|
with open(self.config['output']['filename'], 'w') as f:
|
||||||
f.write(htmlOutput)
|
f.write(output)
|
||||||
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.config = tomlkit.loads(open('config.toml').read())
|
self.config = tomlkit.loads(open(CONFIGFILE).read())
|
||||||
self.services = {}
|
self.services = {}
|
||||||
self.ragecounter = 0
|
self.ragecounter = 0
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
page = StatusPage()
|
page = StatusPage()
|
||||||
service_details = page.render_service_details()
|
service_details = page.get_services_per_host()
|
||||||
page.render_index_html(service_details)
|
|
||||||
|
from pprint import pprint
|
||||||
|
pprint(service_details)
|
||||||
|
|
||||||
|
page.render_html(service_details)
|
||||||
|
|
|
@ -1,10 +0,0 @@
|
||||||
<div class="row">
|
|
||||||
<div class="col">
|
|
||||||
<div class="card text-white border-primary mb-3">
|
|
||||||
{card_header}
|
|
||||||
<div class="card-body">
|
|
||||||
<ul class="list-group">{services}</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
|
@ -2,21 +2,42 @@
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<title>Status Page</title>
|
<title>${title}</title>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<link rel="stylesheet" href="bootstrap.min.css">
|
<link rel="stylesheet" href="bootstrap.min.css">
|
||||||
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>{mood}</text></svg>">
|
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>${mood}</text></svg>">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="page-header my-5" id="banner">
|
<div class="page-header my-5" id="banner">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-lg-8">
|
<div class="col-lg-8">
|
||||||
<h1>Status: {mood}</h1>
|
<h1>Status: ${mood}</h1>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{services}
|
% for prettyname, details in sorted(hosts.items()):
|
||||||
|
<div class="row">
|
||||||
|
<div class="col">
|
||||||
|
<div class="card text-white border-primary mb-3">
|
||||||
|
<div id="${details['hostname']}" class="card-header d-flex justify-content-between align-items-center">
|
||||||
|
<h4><a href="#${details['hostname']}">${prettyname}</a></h4>
|
||||||
|
<span class="badge badge-${details['host_badge']}">${details['host_state']}</span>
|
||||||
|
</div>
|
||||||
|
<div class="card-body">
|
||||||
|
<ul class="list-group">
|
||||||
|
% for service_name, service_details in sorted(details['services'].items()):
|
||||||
|
<li class="list-group-item d-flex justify-content-between align-items-center">
|
||||||
|
${service_name}
|
||||||
|
<span class="badge badge-${service_details['badge']}">${service_details['state']}</span>
|
||||||
|
</li>
|
||||||
|
% endfor
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
% endfor
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Reference in a new issue