emojimood depending on ragecounter

This commit is contained in:
Sophie Schiller 2021-04-10 17:39:06 +02:00
parent 6d2279a51a
commit a10c38e85d
3 changed files with 51 additions and 34 deletions

View file

@ -39,9 +39,13 @@ class StatusPage:
def render_services_per_host(self, host): def render_services_per_host(self, host):
services_operational = '' services = []
services_warning = '' state_to_design_mapping = [
services_critical = '' ('success', 'OK'),
('warning', 'WARNING'),
('danger', 'CRITICAL'),
('info', 'UNKNOWN'),
]
card_header = '' card_header = ''
services_template = """ services_template = """
@ -58,26 +62,39 @@ class StatusPage:
for service in sorted(self.services['results'], key=lambda x: x['attrs']['display_name']): for service in sorted(self.services['results'], key=lambda x: x['attrs']['display_name']):
if service['attrs']['host_name'] == host: if service['attrs']['host_name'] == host:
if service['attrs']['state'] == 0: state = int(service['attrs']['state'])
services_operational = services_operational + services_template.format(self.prettify(service['attrs']['display_name']), 'success', 'OK') if state in (1, 2):
elif service['attrs']['state'] == 1: self.ragecounter += state
services_warning = services_warning + services_template.format(self.prettify(service['attrs']['display_name']), 'warning', 'WARNING')
else:
services_critical = services_critical + services_template.format(self.prettify(service['attrs']['display_name']), 'danger', 'CRITICAL')
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: if service['joins']['host']['state'] == 0:
card_header = services_hostname_template.format(host, service['joins']['host']['vars']['pretty_name'], 'success', 'UP') card_header = services_hostname_template.format(
host,
service['joins']['host']['vars']['pretty_name'],
'success',
'UP',
)
else: else:
card_header = services_hostname_template.format(host, service['joins']['host']['vars']['pretty_name'], 'danger', 'DOWN') 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: with open("services_template.html", "r") as f:
htmlTemplate = f.read() htmlTemplate = f.read()
htmlOutput = htmlTemplate.format( htmlOutput = htmlTemplate.format(
card_header = card_header, card_header = card_header,
services_operational = services_operational, services = ''.join(services),
services_warning = services_warning,
services_critical = services_critical
) )
return htmlOutput return htmlOutput
@ -88,18 +105,25 @@ class StatusPage:
for service in self.services['results']: for service in self.services['results']:
host_names.add(service['attrs']['host_name']) host_names.add(service['attrs']['host_name'])
# render html for each host_name # render html for each host_name
html_output = "" html_output = []
for host in sorted(host_names): for host in sorted(host_names):
html_output = html_output + self.render_services_per_host(host) html_output.append(self.render_services_per_host(host))
return html_output
return ''.join(html_output)
def render_index_html(self, service_details): def render_index_html(self, service_details):
with open("template.html", "r") as f: with open("template.html", "r") as f:
htmlTemplate = f.read() htmlTemplate = f.read()
if self.ragecounter == 0:
mood = '🆗'
elif self.ragecounter < 10:
mood = '🚨'
else:
mood = '🔥'
htmlOutput = htmlTemplate.format( htmlOutput = htmlTemplate.format(
services = service_details services = service_details,
mood = mood,
) )
with open(self.config['output']['filename'], "w") as f: with open(self.config['output']['filename'], "w") as f:
@ -107,13 +131,8 @@ class StatusPage:
def __init__(self): def __init__(self):
self.config = {} self.config = tomlkit.loads(open('config.toml').read())
self.config['icinga2_api'] = { self.ragecounter = 0
'baseurl': 'https://localhost:5665',
'username': 'root',
'password': 'foobar'
}
self.config.update(tomlkit.loads(open('config.toml').read()))
if __name__ == "__main__": if __name__ == "__main__":
page = StatusPage() page = StatusPage()

View file

@ -3,9 +3,7 @@
<div class="card text-white border-primary mb-3"> <div class="card text-white border-primary mb-3">
{card_header} {card_header}
<div class="card-body"> <div class="card-body">
<ul class="list-group">{services_critical}</ul> <ul class="list-group">{services}</ul>
<ul class="list-group">{services_warning}</ul>
<ul class="list-group">{services_operational}</ul>
</div> </div>
</div> </div>
</div> </div>

View file

@ -5,14 +5,14 @@
<title>Status Page</title> <title>Status Page</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>🥴</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 col-md-7 col-sm-6"> <div class="col-lg-8 col-md-7 col-sm-6">
<h1>Status Page</h1> <h1>Status: {mood}</h1>
</div> </div>
</div> </div>
</div> </div>