kunsi-mako-templating #7

Merged
sophie merged 3 commits from kunsi-mako-templating into kunsi-improvements 2021-04-11 08:59:36 +00:00
4 changed files with 75 additions and 90 deletions

View file

@ -1 +1,2 @@
Mako
tomlkit tomlkit

View file

@ -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 = """ for service in self.get_api_result():
<li class="list-group-item d-flex justify-content-between align-items-center"> host = service['joins']['host']['vars']['pretty_name']
{}
<span class="badge badge-{}">{}</span> if host not in result:
</li> result[host] = {
""" 'hostname': service['attrs']['host_name'],
services_hostname_template = """ 'services': {},
<div id="{0}" class="card-header d-flex justify-content-between align-items-center"> }
<h4><a href="#{0}">{1}</a></h4> if service['joins']['host']['state'] == 0:
<span class="badge badge-{2}">{3}</span> result[host]['host_badge'] = 'success'
</div>""" 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']) state = int(service['attrs']['state'])
if state in (1, 2): if state in (1, 2):
self.ragecounter += state self.ragecounter += state
services.append(services_template.format( result[host]['services'][self.prettify(service['attrs']['display_name'])] = {
self.prettify(service['attrs']['display_name']), 'badge': state_to_design_mapping[state][0],
state_to_design_mapping[state][0], 'state': state_to_design_mapping[state][1],
state_to_design_mapping[state][1], }
))
if not card_header: return result
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): def render_html(self, service_details):
# 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()
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'))
output = template.render(
title=self.config['output'].get('page_title', 'Status Page'),
mood=mood, 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)

View file

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

View file

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