-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·73 lines (59 loc) · 2.07 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import datetime
from flask import Flask, render_template
from flask_humanize import Humanize
from jinja2 import StrictUndefined
from requests.exceptions import RequestException
from helpers import State, StateCssClass
from models import MonitoringStatus
from icinga2api.client import Client as Icinga2Client
app = Flask(__name__, static_url_path='/naglite4/static', static_folder='static')
app.jinja_env.undefined = StrictUndefined
humanize = Humanize(app)
icinga2api = Icinga2Client(config_file='icinga2-api.ini')
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
REFRESH = os.getenv('REFRESH', 15)
@app.template_filter('icinga_status')
def status_filter(check_result):
if check_result is None:
return 'N/A'
try:
return State(int(check_result['state'])).name
except ValueError:
return 'N/A'
@app.template_filter('icinga_status_css_class')
def status_css_class_filter(check_result):
if check_result is None:
return ''
try:
return getattr(StateCssClass, State(int(check_result['state'])).name).value
except ValueError:
return ''
@app.route('/')
def view_index():
try:
context = dict(
monitoring=MonitoringStatus(apiclient=icinga2api),
current_time=datetime.datetime.now().isoformat(sep=' ', timespec='seconds')
)
body = render_template('index.html', **context)
except RequestException as e:
import sys
sys.stderr.write(str(e))
context = dict(
current_time=datetime.datetime.now().isoformat(sep=' ', timespec='seconds')
)
body = render_template('api_error.html', **context)
headers = dict(
Refresh='5'
)
return (body, 200, headers)
headers = dict(
Refresh='{}'.format(REFRESH)
)
return (body, 200, headers)
if __name__ == '__main__':
import sys
sys.stderr.write("# This should be run using UWSGI container or `flask run` \n")
sys.stderr.write("# FLASK_APP=main FLASK_DEBUG=1 pipenv run flask run \n")
raise ValueError("Invalid invocation")