rename do_api_calls to get_api_results, return early if services were already requested
This commit is contained in:
parent
cb0f3bc13c
commit
6b95291821
1 changed files with 27 additions and 21 deletions
30
service.py
30
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):
|
||||||
|
@ -102,7 +108,7 @@ 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 = set()
|
||||||
for service in self.services['results']:
|
for service in self.get_api_result():
|
||||||
host_names.add(service['attrs']['host_name'])
|
host_names.add(service['attrs']['host_name'])
|
||||||
# render html for each host_name
|
# render html for each host_name
|
||||||
html_output = []
|
html_output = []
|
||||||
|
@ -132,10 +138,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