rename do_api_calls to get_api_results, return early if services were already requested

This commit is contained in:
Franzi 2021-04-11 08:00:36 +02:00
parent cb0f3bc13c
commit 6b95291821
Signed by untrusted user: kunsi
GPG key ID: 12E3D2136B818350

View file

@ -1,35 +1,41 @@
#!/usr/bin/env python3
import json
import requests
import tomlkit
import urllib3
urllib3.disable_warnings()
class StatusPage:
def do_api_calls(self):
#services
request_url = "{}/v1/objects/services".format(self.config['icinga2_api']['baseurl'])
def get_api_result(self):
if self.services:
return self.services
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,
r = requests.get(
'{}/v1/objects/services'.format(self.config['icinga2_api']['baseurl']),
headers=headers,
data=json.dumps(requestbody),
json=requestbody,
auth=(self.config['icinga2_api']['username'], self.config['icinga2_api']['password']),
verify=False)
verify=False
)
if (r.status_code == 200):
self.services = r.json()
self.services = r.json()['results']
else:
r.raise_for_status()
return self.services
def prettify(self, text):
for search, replace in self.config.get('prettify', {}).items():
@ -60,31 +66,31 @@ class StatusPage:
<span class="badge badge-{2}">{3}</span>
</div>"""
for service in sorted(self.services['results'], key=lambda x: x['attrs']['display_name']):
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'])
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],
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',
host,
service['joins']['host']['vars']['pretty_name'],
'success',
'UP',
)
else:
card_header = services_hostname_template.format(
host,
service['joins']['host']['vars']['pretty_name'],
'danger',
host,
service['joins']['host']['vars']['pretty_name'],
'danger',
'DOWN',
)
self.ragecounter += 100
@ -102,7 +108,7 @@ class StatusPage:
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']:
for service in self.get_api_result():
host_names.add(service['attrs']['host_name'])
# render html for each host_name
html_output = []
@ -132,10 +138,10 @@ class StatusPage:
def __init__(self):
self.config = tomlkit.loads(open('config.toml').read())
self.services = {}
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)