-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.py
126 lines (110 loc) · 3.56 KB
/
app.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
from flask import Flask, request
from flask_restful import Resource, Api
from jsonschema import validate
from pymongo import MongoClient
from bson.objectid import ObjectId
import json, os
MONGO_HOST = os.environ.get('MONGO_HOST', 'db')
MONGO_PORT = os.environ.get('MONGO_PORT', 27017)
app = Flask(__name__)
api = Api(app)
schema = {
"type" : "object",
"properties" : {
"target" : {"type" : "string"},
"labels" : {"type" : "object"}
},
"required": ["target"]
}
delete_schema = {
"type" : "object",
"properties" : {
"id" : {"type" : "string"},
},
"required": ["id"]
}
class IndexPage(Resource):
def get(self):
return { "message": "Need Web UI, Please add UI support https://github.com/narate/prom-file-sd"}
class PromTargets(Resource):
def get(self):
client = MongoClient(MONGO_HOST, MONGO_PORT)
db = client.prom
col = db.targets
targets = []
for o in col.find():
targets.append({'target': o['target'], 'labels': o.get('labels',{}), 'id': str(o['_id'])})
return { 'targets': targets }
def post(self):
body = request.get_json()
try:
validate(body, schema)
except:
return {
'message': 'Input data invalid or miss some value, required: {}'.format(schema['required'])
}, 400
client = MongoClient(MONGO_HOST, MONGO_PORT)
db = client.prom
col = db.targets
labels = body.get('labels', {})
doc = {
'target': body['target'],
'labels': labels
}
sel = {
'target': body['target']
}
metrics_path = labels.get('__metrics_path__')
if metrics_path is not None:
sel['labels.__metrics_path__'] = metrics_path
else:
doc['labels']['__metrics_path__'] = '/metrics'
col.replace_one(sel, doc, True)
with open('/prom/conf/targets.json', 'w') as f:
targets = []
for o in col.find({}, projection={'_id': False}):
targets.append(
{
'targets': [o['target']],
'labels': o.get('labels',{})
}
)
f.write(json.dumps(targets, indent=2))
f.flush()
os.fsync(f.fileno())
return {
'status': 'created',
'data': doc
}, 201
def delete(self):
body = request.get_json()
try:
validate(body, delete_schema)
except:
return {
'message': 'Input data invalid or miss some value, required: {}'.format(schema['required'])
}, 400
client = MongoClient(MONGO_HOST, MONGO_PORT)
db = client.prom
col = db.targets
sel = {
'_id': ObjectId(body['id'])
}
col.delete_one(sel)
with open('/prom/conf/targets.json', 'w') as f:
targets = []
for o in col.find({}, projection={'_id': False}):
targets.append(
{
'targets': [o['target']],
'labels': o.get('labels',{})
}
)
f.write(json.dumps(targets, indent=2))
f.flush()
os.fsync(f.fileno())
return None, 204
api.add_resource(IndexPage, '/')
api.add_resource(PromTargets, '/targets')
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0")