Some improvements #6
1 changed files with 27 additions and 21 deletions
28
service.py
28
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):
|
||||
|
@ -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()
|
||||
page.render_index_html(service_details)
|
||||
|
|
Loading…
Reference in a new issue