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
|
||||
|
|
117
service.py
117
service.py
|
@ -1,10 +1,16 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import requests
|
||||
import tomlkit
|
||||
import urllib3
|
||||
from os import environ
|
||||
|
||||
import tomlkit
|
||||
from mako.template import Template
|
||||
|
||||
urllib3.disable_warnings()
|
||||
|
||||
CONFIGFILE = environ.get('STATUSPAGE_CONFIG', 'config.toml')
|
||||
|
||||
class StatusPage:
|
||||
def get_api_result(self):
|
||||
if self.services:
|
||||
|
@ -44,106 +50,73 @@ class StatusPage:
|
|||
return text
|
||||
|
||||
|
||||
def render_services_per_host(self, host):
|
||||
services = []
|
||||
def get_services_per_host(self):
|
||||
state_to_design_mapping = [
|
||||
('success', 'OK'),
|
||||
('warning', 'WARNING'),
|
||||
('danger', 'CRITICAL'),
|
||||
('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 self.get_api_result():
|
||||
host = service['joins']['host']['vars']['pretty_name']
|
||||
|
||||
if host not in result:
|
||||
result[host] = {
|
||||
'hostname': service['attrs']['host_name'],
|
||||
'services': {},
|
||||
}
|
||||
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
|
||||
|
||||
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],
|
||||
))
|
||||
result[host]['services'][self.prettify(service['attrs']['display_name'])] = {
|
||||
'badge': state_to_design_mapping[state][0],
|
||||
'state': 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
|
||||
return result
|
||||
|
||||
|
||||
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():
|
||||
host_names[service['joins']['host']['vars']['pretty_name']] = service['attrs']['host_name']
|
||||
|
||||
# render html for each host_name
|
||||
html_output = []
|
||||
# Can't use .values() here, since we want to sort by prettyname
|
||||
for prettyname, hostname in sorted(host_names.items()):
|
||||
html_output.append(self.render_services_per_host(hostname))
|
||||
|
||||
return ''.join(html_output)
|
||||
|
||||
|
||||
def render_index_html(self, service_details):
|
||||
with open("template.html", "r") as f:
|
||||
htmlTemplate = f.read()
|
||||
def render_html(self, service_details):
|
||||
if self.ragecounter == 0:
|
||||
mood = '🆗'
|
||||
elif self.ragecounter < 10:
|
||||
mood = '🚨'
|
||||
else:
|
||||
mood = '🔥'
|
||||
htmlOutput = htmlTemplate.format(
|
||||
services = service_details,
|
||||
|
||||
template = Template(filename=self.config['output'].get('template', 'template.html'))
|
||||
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:
|
||||
f.write(htmlOutput)
|
||||
with open(self.config['output']['filename'], 'w') as f:
|
||||
f.write(output)
|
||||
|
||||
|
||||
def __init__(self):
|
||||
self.config = tomlkit.loads(open('config.toml').read())
|
||||
self.config = tomlkit.loads(open(CONFIGFILE).read())
|
||||
self.services = {}
|
||||
self.ragecounter = 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
page = StatusPage()
|
||||
service_details = page.render_service_details()
|
||||
page.render_index_html(service_details)
|
||||
service_details = page.get_services_per_host()
|
||||
|
||||
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>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Status Page</title>
|
||||
<title>${title}</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<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>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="page-header my-5" id="banner">
|
||||
<div class="row">
|
||||
<div class="col-lg-8">
|
||||
<h1>Status: {mood}</h1>
|
||||
<h1>Status: ${mood}</h1>
|
||||
</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>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Reference in a new issue