move stuff to class, add prettifier, use pretty names
This commit is contained in:
parent
e03a10abb6
commit
f899e6e13b
1 changed files with 95 additions and 101 deletions
80
service.py
80
service.py
|
@ -6,44 +6,39 @@ import configparser
|
||||||
import urllib3
|
import urllib3
|
||||||
urllib3.disable_warnings()
|
urllib3.disable_warnings()
|
||||||
|
|
||||||
|
class StatusPage:
|
||||||
def do_api_calls(config):
|
def do_api_calls(self):
|
||||||
data = {}
|
|
||||||
|
|
||||||
#services
|
#services
|
||||||
request_url = "{}/v1/objects/services".format(config['icinga2_api']['baseurl'])
|
request_url = "{}/v1/objects/services".format(self.config['icinga2_api']['baseurl'])
|
||||||
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" ],
|
"joins": [ "host.name", "host.state", "host.last_check_result", "host.vars" ],
|
||||||
"filter": config['filters']['services'],
|
"filter": self.config['filters']['services'],
|
||||||
}
|
}
|
||||||
r = requests.get(request_url,
|
r = requests.get(request_url,
|
||||||
headers=headers,
|
headers=headers,
|
||||||
data=json.dumps(requestbody),
|
data=json.dumps(requestbody),
|
||||||
auth=(config['icinga2_api']['username'], 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):
|
||||||
data['services'] = r.json()
|
self.services = r.json()
|
||||||
else:
|
else:
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
|
|
||||||
return data
|
|
||||||
|
def prettify(self, text):
|
||||||
|
for search, replace in self.config['prettify'].items():
|
||||||
|
text = text.replace(search.upper(), replace)
|
||||||
|
|
||||||
|
return text
|
||||||
|
|
||||||
|
|
||||||
def render_text_output(data):
|
def render_services_per_host(self, host):
|
||||||
print("{:50s} {:10s}".format("host", "status"))
|
|
||||||
for host in data['hosts']['results']:
|
|
||||||
print("{:50s} {}".format(host['name'], host['attrs']['state']))
|
|
||||||
for service in data['services']['results']:
|
|
||||||
print("{:50s} {}".format(service['name'], service['attrs']['state']))
|
|
||||||
|
|
||||||
|
|
||||||
def render_services_per_host(host, data):
|
|
||||||
services_operational = ''
|
services_operational = ''
|
||||||
services_warning = ''
|
services_warning = ''
|
||||||
services_critical = ''
|
services_critical = ''
|
||||||
|
@ -57,23 +52,23 @@ def render_services_per_host(host, data):
|
||||||
"""
|
"""
|
||||||
services_hostname_template = """
|
services_hostname_template = """
|
||||||
<div id="{0}" class="card-header d-flex justify-content-between align-items-center">
|
<div id="{0}" class="card-header d-flex justify-content-between align-items-center">
|
||||||
<h4><a href="#{0}">{0}</a></h4>
|
<h4><a href="#{0}">{1}</a></h4>
|
||||||
<span class="badge badge-{1}">{2}</span>
|
<span class="badge badge-{2}">{3}</span>
|
||||||
</div>"""
|
</div>"""
|
||||||
|
|
||||||
for service in sorted(data['services']['results'], key=lambda x: x['attrs']['display_name']):
|
for service in sorted(self.services['results'], key=lambda x: x['attrs']['display_name']):
|
||||||
if service['attrs']['host_name'] == host:
|
if service['attrs']['host_name'] == host:
|
||||||
if service['attrs']['state'] == 0:
|
if service['attrs']['state'] == 0:
|
||||||
services_operational = services_operational + services_template.format(service['attrs']['display_name'], 'success', 'OK')
|
services_operational = services_operational + services_template.format(self.prettify(service['attrs']['display_name']), 'success', 'OK')
|
||||||
elif service['attrs']['state'] == 1:
|
elif service['attrs']['state'] == 1:
|
||||||
services_warning = services_warning + services_template.format(service['attrs']['display_name'], 'warning', 'WARNING')
|
services_warning = services_warning + services_template.format(self.prettify(service['attrs']['display_name']), 'warning', 'WARNING')
|
||||||
else:
|
else:
|
||||||
services_critical = services_critical + services_template.format(service['attrs']['display_name'], 'danger', 'CRITICAL')
|
services_critical = services_critical + services_template.format(self.prettify(service['attrs']['display_name']), 'danger', 'CRITICAL')
|
||||||
|
|
||||||
if service['joins']['host']['state'] == 0:
|
if service['joins']['host']['state'] == 0:
|
||||||
card_header = services_hostname_template.format(host, 'success', 'UP')
|
card_header = services_hostname_template.format(host, service['joins']['host']['vars']['pretty_name'], 'success', 'UP')
|
||||||
else:
|
else:
|
||||||
card_header = services_hostname_template.format(host, 'danger', 'DOWN')
|
card_header = services_hostname_template.format(host, service['joins']['host']['vars']['pretty_name'], 'danger', 'DOWN')
|
||||||
|
|
||||||
with open("services_template.html", "r") as f:
|
with open("services_template.html", "r") as f:
|
||||||
htmlTemplate = f.read()
|
htmlTemplate = f.read()
|
||||||
|
@ -87,20 +82,19 @@ def render_services_per_host(host, data):
|
||||||
return htmlOutput
|
return htmlOutput
|
||||||
|
|
||||||
|
|
||||||
def render_service_details(data):
|
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 = []
|
host_names = set()
|
||||||
for service in data['services']['results']:
|
for service in self.services['results']:
|
||||||
if service['attrs']['host_name'] not in host_names:
|
host_names.add(service['attrs']['host_name'])
|
||||||
host_names.append(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):
|
for host in sorted(host_names):
|
||||||
html_output = html_output + render_services_per_host(host, data)
|
html_output = html_output + self.render_services_per_host(host)
|
||||||
return html_output
|
return html_output
|
||||||
|
|
||||||
|
|
||||||
def render_index_html(filename, service_details):
|
def render_index_html(self, service_details):
|
||||||
with open("template.html", "r") as f:
|
with open("template.html", "r") as f:
|
||||||
htmlTemplate = f.read()
|
htmlTemplate = f.read()
|
||||||
|
|
||||||
|
@ -108,22 +102,22 @@ def render_index_html(filename, service_details):
|
||||||
services = service_details
|
services = service_details
|
||||||
)
|
)
|
||||||
|
|
||||||
with open(filename, "w") as f:
|
with open(self.config['output']['filename'], "w") as f:
|
||||||
f.write(htmlOutput)
|
f.write(htmlOutput)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def __init__(self):
|
||||||
config = configparser.ConfigParser()
|
self.config = configparser.ConfigParser()
|
||||||
config['icinga2_api'] = {
|
self.config['icinga2_api'] = {
|
||||||
'baseurl': 'https://localhost:5665',
|
'baseurl': 'https://localhost:5665',
|
||||||
'username': 'root',
|
'username': 'root',
|
||||||
'password': 'foobar'
|
'password': 'foobar'
|
||||||
}
|
}
|
||||||
with open('config.conf', 'r') as configfile:
|
with open('config.conf', 'r') as configfile:
|
||||||
config.read('config.conf')
|
self.config.read('config.conf')
|
||||||
data = do_api_calls(config)
|
|
||||||
service_details = render_service_details(data)
|
|
||||||
render_index_html(config['output']['filename'], service_details)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
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