Some improvements #6
3 changed files with 46 additions and 25 deletions
12
.editorconfig
Normal file
12
.editorconfig
Normal file
|
@ -0,0 +1,12 @@
|
|||
root = true
|
||||
|
||||
[*]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
end_of_line = lf
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
|
||||
[*.tolm]
|
||||
indent_size = 2
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
|
@ -0,0 +1 @@
|
|||
tomlkit
|
40
service.py
40
service.py
|
@ -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,7 +66,7 @@ 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):
|
||||
|
@ -101,13 +107,15 @@ 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']:
|
||||
host_names.add(service['attrs']['host_name'])
|
||||
host_names = {}
|
||||
for service in self.get_api_result():
|
||||
host_names[service['joins']['host']['vars']['pretty_name']] = service['attrs']['host_name']
|
||||
|
||||
# render html for each host_name
|
||||
html_output = []
|
||||
for host in sorted(host_names):
|
||||
html_output.append(self.render_services_per_host(host))
|
||||
# Can't use .values() here, since we want to sort by prettyname
|
||||
for prettyname, hostname in sorted(host_names.items()):
|
||||
html_output.append(self.render_services_per_host(hostname))
|
||||
|
||||
return ''.join(html_output)
|
||||
|
||||
|
@ -132,10 +140,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)
|
||||
|
|
Loading…
Reference in a new issue