emojimood depending on ragecounter
This commit is contained in:
parent
6d2279a51a
commit
a10c38e85d
3 changed files with 51 additions and 34 deletions
77
service.py
77
service.py
|
@ -39,9 +39,13 @@ class StatusPage:
|
|||
|
||||
|
||||
def render_services_per_host(self, host):
|
||||
services_operational = ''
|
||||
services_warning = ''
|
||||
services_critical = ''
|
||||
services = []
|
||||
state_to_design_mapping = [
|
||||
('success', 'OK'),
|
||||
('warning', 'WARNING'),
|
||||
('danger', 'CRITICAL'),
|
||||
('info', 'UNKNOWN'),
|
||||
]
|
||||
card_header = ''
|
||||
|
||||
services_template = """
|
||||
|
@ -58,26 +62,39 @@ class StatusPage:
|
|||
|
||||
for service in sorted(self.services['results'], key=lambda x: x['attrs']['display_name']):
|
||||
if service['attrs']['host_name'] == host:
|
||||
if service['attrs']['state'] == 0:
|
||||
services_operational = services_operational + services_template.format(self.prettify(service['attrs']['display_name']), 'success', 'OK')
|
||||
elif service['attrs']['state'] == 1:
|
||||
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')
|
||||
state = int(service['attrs']['state'])
|
||||
if state in (1, 2):
|
||||
self.ragecounter += state
|
||||
|
||||
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')
|
||||
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_operational = services_operational,
|
||||
services_warning = services_warning,
|
||||
services_critical = services_critical
|
||||
services = ''.join(services),
|
||||
)
|
||||
return htmlOutput
|
||||
|
||||
|
@ -88,18 +105,25 @@ class StatusPage:
|
|||
for service in self.services['results']:
|
||||
host_names.add(service['attrs']['host_name'])
|
||||
# render html for each host_name
|
||||
html_output = ""
|
||||
html_output = []
|
||||
for host in sorted(host_names):
|
||||
html_output = html_output + self.render_services_per_host(host)
|
||||
return html_output
|
||||
html_output.append(self.render_services_per_host(host))
|
||||
|
||||
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:
|
||||
mood = '🆗'
|
||||
elif self.ragecounter < 10:
|
||||
mood = '🚨'
|
||||
else:
|
||||
mood = '🔥'
|
||||
htmlOutput = htmlTemplate.format(
|
||||
services = service_details
|
||||
services = service_details,
|
||||
mood = mood,
|
||||
)
|
||||
|
||||
with open(self.config['output']['filename'], "w") as f:
|
||||
|
@ -107,16 +131,11 @@ class StatusPage:
|
|||
|
||||
|
||||
def __init__(self):
|
||||
self.config = {}
|
||||
self.config['icinga2_api'] = {
|
||||
'baseurl': 'https://localhost:5665',
|
||||
'username': 'root',
|
||||
'password': 'foobar'
|
||||
}
|
||||
self.config.update(tomlkit.loads(open('config.toml').read()))
|
||||
self.config = tomlkit.loads(open('config.toml').read())
|
||||
self.ragecounter = 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
page = StatusPage()
|
||||
page.do_api_calls()
|
||||
service_details = page.render_service_details()
|
||||
service_details = page.render_service_details()
|
||||
page.render_index_html(service_details)
|
||||
|
|
|
@ -3,9 +3,7 @@
|
|||
<div class="card text-white border-primary mb-3">
|
||||
{card_header}
|
||||
<div class="card-body">
|
||||
<ul class="list-group">{services_critical}</ul>
|
||||
<ul class="list-group">{services_warning}</ul>
|
||||
<ul class="list-group">{services_operational}</ul>
|
||||
<ul class="list-group">{services}</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -5,14 +5,14 @@
|
|||
<title>Status Page</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>🥴</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 col-md-7 col-sm-6">
|
||||
<h1>Status Page</h1>
|
||||
<h1>Status: {mood}</h1>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
Loading…
Reference in a new issue