This repository has been archived by the owner on Sep 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
web.py
103 lines (90 loc) · 3.12 KB
/
web.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'''
Automatron: API Service
* Act as backend to user interface
* Provide REST API for Automatron
'''
import json
import sys
from flask import Flask, g, request, render_template
import core.common
import core.logs
import core.db
app = Flask(__name__)
def connect_db(config):
''' Connect to DB '''
db = core.db.SetupDatastore(config=config)
try:
return db.get_dbc()
except Exception as e:
logger.error("Failed to connect to datastore: {0}".format(e.message))
return None
@app.before_request
def before_request():
''' Pre-request hander '''
logger.debug("Incoming Web Request: {0}".format(request.full_path))
g.dbc = connect_db(app.config)
@app.teardown_request
def teardown_request(exc=None):
''' Post request handler '''
if hasattr(g, 'dbc'):
g.dbc.disconnect()
# Web UI Routes
@app.route('/', methods=['GET'])
def get_index():
''' Returns dashboard '''
data = {
'theme': 'slate'
}
# Set Bootswatch theme
if 'theme' in app.config['web']:
data['theme'] = app.config['web']['theme']
return render_template("index.html", data=data), 200
# API Routes
@app.route('/api/status', methods=['GET'])
def get_status():
''' Returns JSON string summarizing current Automatron status '''
status = {
'targets': 0,
'runbooks': {
'OK': 0,
'CRITICAL': 0,
'WARNING': 0,
'UNKNOWN': 0
},
'events': []
}
targets = g.dbc.get_target()
for target in targets.keys():
status['targets'] = status['targets'] + 1
if 'runbooks' in targets[target]:
for runbook in targets[target]['runbooks'].keys():
if 'status' in targets[target]['runbooks'][runbook]:
for s in targets[target]['runbooks'][runbook]['status'].keys():
if targets[target]['runbooks'][runbook]['status'][s] > 0:
status['runbooks'][s] = status['runbooks'][s] + 1
status['events'].append({
'status': s,
'target': target,
'runbook': targets[target]['runbooks'][runbook]['name'],
'count': targets[target]['runbooks'][runbook]['status'][s]
})
return json.dumps(status), 200
@app.route('/api/targets/<target>', methods=['GET'])
@app.route('/api/targets', methods=['GET'])
def get_targets(target=None):
''' Returns JSON string of target information from the database '''
targets = g.dbc.get_target(target_id=target)
return json.dumps(targets), 200
if __name__ == '__main__':
config = core.common.get_config(description="Automatron: Web")
if config is False:
print("Could not get configuration")
sys.exit(1)
app.config.update(config)
# Setup Logging
if app.config['logging']['debug']:
app.debug = True
logs = core.logs.Logger(config=config, proc_name="api")
logger = logs.getLogger()
# Start Flask
app.run(host=app.config['web']['listen'], port=app.config['web']['port'])