Merge pull request 'Some improvements' (#6) from kunsi-improvements into main
Reviewed-on: https://git.kunsmann.eu/sophie/simple-icinga-dashboard/pulls/6
This commit is contained in:
commit
3ab0a05b59
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
|
38
service.py
38
service.py
|
@ -1,35 +1,41 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import json
|
|
||||||
import requests
|
import requests
|
||||||
import tomlkit
|
import tomlkit
|
||||||
import urllib3
|
import urllib3
|
||||||
urllib3.disable_warnings()
|
urllib3.disable_warnings()
|
||||||
|
|
||||||
class StatusPage:
|
class StatusPage:
|
||||||
def do_api_calls(self):
|
def get_api_result(self):
|
||||||
#services
|
if self.services:
|
||||||
request_url = "{}/v1/objects/services".format(self.config['icinga2_api']['baseurl'])
|
return self.services
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
'Accept': 'application/json',
|
'Accept': 'application/json',
|
||||||
'X-HTTP-Method-Override': 'GET'
|
'X-HTTP-Method-Override': 'GET'
|
||||||
}
|
}
|
||||||
|
|
||||||
requestbody = {
|
requestbody = {
|
||||||
"attrs": [ "name", "state", "last_check_result", "host_name", "display_name" ],
|
"attrs": [ "name", "state", "last_check_result", "host_name", "display_name" ],
|
||||||
"joins": [ "host.name", "host.state", "host.last_check_result", "host.vars" ],
|
"joins": [ "host.name", "host.state", "host.last_check_result", "host.vars" ],
|
||||||
"filter": self.config['filters']['services'],
|
"filter": self.config['filters']['services'],
|
||||||
}
|
}
|
||||||
r = requests.get(request_url,
|
|
||||||
|
r = requests.get(
|
||||||
|
'{}/v1/objects/services'.format(self.config['icinga2_api']['baseurl']),
|
||||||
headers=headers,
|
headers=headers,
|
||||||
data=json.dumps(requestbody),
|
json=requestbody,
|
||||||
auth=(self.config['icinga2_api']['username'], self.config['icinga2_api']['password']),
|
auth=(self.config['icinga2_api']['username'], self.config['icinga2_api']['password']),
|
||||||
verify=False)
|
verify=False
|
||||||
|
)
|
||||||
|
|
||||||
if (r.status_code == 200):
|
if (r.status_code == 200):
|
||||||
self.services = r.json()
|
self.services = r.json()['results']
|
||||||
else:
|
else:
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
|
|
||||||
|
return self.services
|
||||||
|
|
||||||
|
|
||||||
def prettify(self, text):
|
def prettify(self, text):
|
||||||
for search, replace in self.config.get('prettify', {}).items():
|
for search, replace in self.config.get('prettify', {}).items():
|
||||||
|
@ -60,7 +66,7 @@ class StatusPage:
|
||||||
<span class="badge badge-{2}">{3}</span>
|
<span class="badge badge-{2}">{3}</span>
|
||||||
</div>"""
|
</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:
|
if service['attrs']['host_name'] == host:
|
||||||
state = int(service['attrs']['state'])
|
state = int(service['attrs']['state'])
|
||||||
if state in (1, 2):
|
if state in (1, 2):
|
||||||
|
@ -101,13 +107,15 @@ class StatusPage:
|
||||||
|
|
||||||
def render_service_details(self):
|
def render_service_details(self):
|
||||||
# generate list of hosts by scanning services for unique host_name
|
# generate list of hosts by scanning services for unique host_name
|
||||||
host_names = set()
|
host_names = {}
|
||||||
for service in self.services['results']:
|
for service in self.get_api_result():
|
||||||
host_names.add(service['attrs']['host_name'])
|
host_names[service['joins']['host']['vars']['pretty_name']] = service['attrs']['host_name']
|
||||||
|
|
||||||
# render html for each host_name
|
# render html for each host_name
|
||||||
html_output = []
|
html_output = []
|
||||||
for host in sorted(host_names):
|
# Can't use .values() here, since we want to sort by prettyname
|
||||||
html_output.append(self.render_services_per_host(host))
|
for prettyname, hostname in sorted(host_names.items()):
|
||||||
|
html_output.append(self.render_services_per_host(hostname))
|
||||||
|
|
||||||
return ''.join(html_output)
|
return ''.join(html_output)
|
||||||
|
|
||||||
|
@ -132,10 +140,10 @@ class StatusPage:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.config = tomlkit.loads(open('config.toml').read())
|
self.config = tomlkit.loads(open('config.toml').read())
|
||||||
|
self.services = {}
|
||||||
self.ragecounter = 0
|
self.ragecounter = 0
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
page = StatusPage()
|
page = StatusPage()
|
||||||
page.do_api_calls()
|
|
||||||
service_details = page.render_service_details()
|
service_details = page.render_service_details()
|
||||||
page.render_index_html(service_details)
|
page.render_index_html(service_details)
|
||||||
|
|
Loading…
Reference in a new issue