-
Notifications
You must be signed in to change notification settings - Fork 19
/
PrometheusController.py
140 lines (113 loc) · 3.98 KB
/
PrometheusController.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# -*- coding: utf-8 -*-
from flask import Flask, request, render_template, make_response, Response
import sys
import json
from PrometheusService import create_rules_model, get_rule, get_rule_detail, update_rules_model, delete_rules_model
import PrometheusConfig
import os
reload(sys)
sys.setdefaultencoding('utf8')
app = Flask(__name__)
@app.route('/prometheus/add', methods=['POST'])
def add():
content = json.dumps('创建成功')
try:
name = request.form['name']
expr = request.form['expr']
level = request.form['level']
_for = request.form['_for']
desc = request.form['desc']
model = request.form['model']
service = request.form['service']
rs = create_rules_model(name, expr, level, _for, desc, model, service)
if rs == 1:
return json.dumps('error! please check the path is exist and Permission!')
except Exception, e:
print e
content = json.dumps('error')
resp = Response(content)
return resp
@app.route('/prometheus/update', methods=['POST'])
def update():
content = json.dumps('修改成功')
try:
_name = request.form['_name']
name = request.form['name']
expr = request.form['expr']
level = request.form['level']
_for = request.form['_for']
desc = request.form['desc']
model = request.form['model']
service = request.form['service']
rs = update_rules_model(name, expr, level, _for, desc, model, _name, service)
if rs == 1:
return json.dumps('error! please check the path is exist and Permission!')
except Exception, e:
print e
content = json.dumps('error!')
resp = Response(content)
return resp
@app.route('/prometheus/delete', methods=['POST'])
def delete():
content = json.dumps('删除成功')
try:
_name = request.form['_name']
delete_rules_model(_name)
except Exception, e:
print e
content = json.dumps('error! please check the path is exist and Permission!')
resp = Response(content)
return resp
@app.route('/prometheus/getRulesList', methods=['POST'])
def get_rules_list():
try:
content = get_rule()
except Exception, e:
print e
content = []
resp = Response(json.dumps(content))
return resp
@app.route('/prometheus/getRulesDetail', methods=['POST'])
def get_rules_detail():
try:
name = request.form['name']
content = get_rule_detail(name)
except Exception, e:
print e
content = {'name': '', 'alert': '', 'expr': '', '_for': '', 'level': '', 'summary': '', 'description': ''}
resp = Response(json.dumps(content))
return resp
@app.route('/', methods=['GET'])
def open_web():
# return render_template('rulesWeb.html')
return app.send_static_file('pages/rulesWeb.html')
@app.route('/prometheus/AddRules', methods=['GET'])
def open_html():
# return render_template('rulesWeb.html')
return app.send_static_file('pages/rulesWeb.html')
@app.route('/prometheus/UpdateRules')
def update_html():
# return render_template('rulesWeb.html')
return app.send_static_file('pages/update.html')
@app.route('/prometheus/GetInfo', methods=['POST'])
def get_info():
result = {}
try:
result['servers'] = PrometheusConfig.services
result['alerm'] = PrometheusConfig.alarms
except Exception, e:
print e
return json.dumps(result)
@app.route('/prometheus/refresh', methods=['POST'])
def refresh():
try:
# os.system("curl -X POST --connect-timeout 10 -m 20 http://172.16.0.143:9090/-/reload >/dev/null")
rs = os.popen("curl -X POST --connect-timeout 10 -m 20 http://127.0.0.1:9090/-/reload").read()
if rs.strip() != '':
return json.dumps("refresh fail, please check the prometheus.yml")
return json.dumps("refresh success!")
except Exception, e:
print e
return json.dumps("refresh fail")
if __name__ == '__main__':
app.run("0.0.0.0", 8888)