can haz some formatting?

This commit is contained in:
Franzi 2023-05-20 20:09:16 +02:00
parent 0a0ee1bbfc
commit 9f4f2818c5
Signed by untrusted user: kunsi
GPG key ID: 12E3D2136B818350

View file

@ -1,19 +1,21 @@
#!/usr/bin/env python3
import requests
import urllib3
from os import environ
import sys
import logging
import tomlkit
from mako.template import Template
import shutil
import sys
from datetime import datetime
from os import environ
import requests
import tomlkit
import urllib3
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:
@ -21,14 +23,17 @@ class StatusPage:
return self.services
headers = {
'Accept': 'application/json',
'X-HTTP-Method-Override': 'GET'
}
headers = {'Accept': 'application/json', 'X-HTTP-Method-Override': 'GET'}
requestbody = {
"attrs": [ "name", "state", "last_check_result", "host_name", "display_name" ],
"joins": [ "host", "host.state", "host.last_check_result", "host.vars" ],
"attrs": [
"name",
"state",
"last_check_result",
"host_name",
"display_name",
],
"joins": ["host", "host.state", "host.last_check_result", "host.vars"],
"filter": self.config['filters']['services'],
}
@ -36,14 +41,17 @@ class StatusPage:
'{}/v1/objects/services'.format(self.config['icinga2_api']['baseurl']),
headers=headers,
json=requestbody,
auth=(self.config['icinga2_api']['username'], self.config['icinga2_api']['password']),
verify=False
auth=(
self.config['icinga2_api']['username'],
self.config['icinga2_api']['password'],
),
verify=False,
)
self.logger.info(f'got http status code {r.status_code}')
self.logger.debug(r.text)
if (r.status_code == 200):
if r.status_code == 200:
self.services = r.json()['results']
else:
r.raise_for_status()
@ -52,14 +60,12 @@ class StatusPage:
return self.services
def prettify(self, text):
for search, replace in self.config.get('prettify', {}).items():
text = text.replace(search, replace)
return text
def get_services_per_host(self):
state_to_design_mapping = [
('success', 'OK'),
@ -70,7 +76,9 @@ class StatusPage:
result = {}
for service in self.get_api_result():
self.logger.info(f'now processing {service["attrs"]["host_name"]} "{service["attrs"]["display_name"]}"')
self.logger.info(
f'now processing {service["attrs"]["host_name"]} "{service["attrs"]["display_name"]}"'
)
self.logger.debug(service)
host = service['joins']['host']['vars']['pretty_name']
@ -93,7 +101,9 @@ class StatusPage:
if state in (1, 2):
self.ragecounter += state
result[host]['services'][self.prettify(service['attrs']['display_name'])] = {
result[host]['services'][
self.prettify(service['attrs']['display_name'])
] = {
'badge': state_to_design_mapping[state][0],
'state': state_to_design_mapping[state][1],
}
@ -101,7 +111,6 @@ class StatusPage:
return result
def render_html(self, service_details):
if self.ragecounter == 0:
mood = '🆗'
@ -113,7 +122,9 @@ class StatusPage:
self.logger.info('rendering output html')
start = datetime.now()
template = Template(filename=self.config['output'].get('template', 'template.html'))
template = Template(
filename=self.config['output'].get('template', 'template.html')
)
output = template.render(
title=self.config['output'].get('page_title', 'Status Page'),
mood=mood,
@ -126,7 +137,6 @@ class StatusPage:
with open(self.config['output']['filename'], 'w') as f:
f.write(output)
def __init__(self):
self.config = tomlkit.loads(open(CONFIGFILE).read())
self.services = {}
@ -134,11 +144,14 @@ class StatusPage:
self.logger = logging.getLogger('StatusPage')
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(levelname)s {%(filename)s:%(lineno)d} %(message)s')
formatter = logging.Formatter(
'%(levelname)s {%(filename)s:%(lineno)d} %(message)s'
)
handler.setFormatter(formatter)
self.logger.addHandler(handler)
self.logger.setLevel(self.config.get('loglevel', 'INFO'))
if __name__ == "__main__":
page = StatusPage()