2020-11-21 21:35:43 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import json
|
|
|
|
import requests
|
2021-04-10 14:05:09 +00:00
|
|
|
import tomlkit
|
2020-11-21 21:35:43 +00:00
|
|
|
import urllib3
|
|
|
|
urllib3.disable_warnings()
|
|
|
|
|
2021-04-10 13:34:41 +00:00
|
|
|
class StatusPage:
|
|
|
|
def do_api_calls(self):
|
|
|
|
#services
|
|
|
|
request_url = "{}/v1/objects/services".format(self.config['icinga2_api']['baseurl'])
|
|
|
|
headers = {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'X-HTTP-Method-Override': 'GET'
|
|
|
|
}
|
|
|
|
requestbody = {
|
|
|
|
"attrs": [ "name", "state", "last_check_result", "host_name", "display_name" ],
|
|
|
|
"joins": [ "host.name", "host.state", "host.last_check_result", "host.vars" ],
|
|
|
|
"filter": self.config['filters']['services'],
|
|
|
|
}
|
|
|
|
r = requests.get(request_url,
|
|
|
|
headers=headers,
|
|
|
|
data=json.dumps(requestbody),
|
|
|
|
auth=(self.config['icinga2_api']['username'], self.config['icinga2_api']['password']),
|
|
|
|
verify=False)
|
|
|
|
|
|
|
|
if (r.status_code == 200):
|
|
|
|
self.services = r.json()
|
|
|
|
else:
|
|
|
|
r.raise_for_status()
|
|
|
|
|
|
|
|
|
|
|
|
def prettify(self, text):
|
2021-04-10 14:05:09 +00:00
|
|
|
for search, replace in self.config.get('prettify', {}).items():
|
|
|
|
text = text.replace(search, replace)
|
2021-04-10 13:34:41 +00:00
|
|
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
|
|
|
def render_services_per_host(self, host):
|
2021-04-10 15:39:06 +00:00
|
|
|
services = []
|
|
|
|
state_to_design_mapping = [
|
|
|
|
('success', 'OK'),
|
|
|
|
('warning', 'WARNING'),
|
|
|
|
('danger', 'CRITICAL'),
|
|
|
|
('info', 'UNKNOWN'),
|
|
|
|
]
|
2021-04-10 13:34:41 +00:00
|
|
|
card_header = ''
|
|
|
|
|
|
|
|
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.services['results'], key=lambda x: x['attrs']['display_name']):
|
|
|
|
if service['attrs']['host_name'] == host:
|
2021-04-10 15:39:06 +00:00
|
|
|
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
|
2021-04-10 13:34:41 +00:00
|
|
|
|
|
|
|
with open("services_template.html", "r") as f:
|
|
|
|
htmlTemplate = f.read()
|
|
|
|
|
|
|
|
htmlOutput = htmlTemplate.format(
|
|
|
|
card_header = card_header,
|
2021-04-10 15:39:06 +00:00
|
|
|
services = ''.join(services),
|
2021-04-10 13:34:41 +00:00
|
|
|
)
|
|
|
|
return htmlOutput
|
|
|
|
|
|
|
|
|
|
|
|
def render_service_details(self):
|
|
|
|
# generate list of hosts by scanning services for unique host_name
|
|
|
|
host_names = set()
|
|
|
|
for service in self.services['results']:
|
|
|
|
host_names.add(service['attrs']['host_name'])
|
|
|
|
# render html for each host_name
|
2021-04-10 15:39:06 +00:00
|
|
|
html_output = []
|
2021-04-10 13:34:41 +00:00
|
|
|
for host in sorted(host_names):
|
2021-04-10 15:39:06 +00:00
|
|
|
html_output.append(self.render_services_per_host(host))
|
|
|
|
|
|
|
|
return ''.join(html_output)
|
2021-04-10 13:34:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def render_index_html(self, service_details):
|
|
|
|
with open("template.html", "r") as f:
|
|
|
|
htmlTemplate = f.read()
|
2021-04-10 15:39:06 +00:00
|
|
|
if self.ragecounter == 0:
|
|
|
|
mood = '🆗'
|
|
|
|
elif self.ragecounter < 10:
|
|
|
|
mood = '🚨'
|
|
|
|
else:
|
|
|
|
mood = '🔥'
|
2021-04-10 13:34:41 +00:00
|
|
|
htmlOutput = htmlTemplate.format(
|
2021-04-10 15:39:06 +00:00
|
|
|
services = service_details,
|
|
|
|
mood = mood,
|
2021-04-10 13:34:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
with open(self.config['output']['filename'], "w") as f:
|
|
|
|
f.write(htmlOutput)
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
2021-04-10 15:39:06 +00:00
|
|
|
self.config = tomlkit.loads(open('config.toml').read())
|
|
|
|
self.ragecounter = 0
|
2020-11-21 21:35:43 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-04-10 13:34:41 +00:00
|
|
|
page = StatusPage()
|
|
|
|
page.do_api_calls()
|
2021-04-10 15:39:06 +00:00
|
|
|
service_details = page.render_service_details()
|
2021-04-10 13:34:41 +00:00
|
|
|
page.render_index_html(service_details)
|