-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector.py
executable file
·70 lines (52 loc) · 1.87 KB
/
collector.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import config
from flask import Flask
from flask import request
from flask.views import MethodView
from lib.flask_pbj import api, json, protobuf
from lib.metric_pb2 import MetricBatch, Status
from influxdb import InfluxDBClient
class MetricEndpoint(MethodView):
decorators = [api(json, protobuf(receives=MetricBatch, sends=Status))]
def _get_prefix(self, path):
return path.split('.')[0]
def _get_value(self, path):
return path.split('.')[-1:][0]
def _get_measurement(self, path):
items = path.split('.')
if len(items) == 5:
return ".".join(path.split('.')[-3:-1])
else:
return ".".join(path.split('.')[-2:-1])
def post(self):
metricbatch = request.data_dict
influx_points = []
for metric in metricbatch['metric']:
influx_points.append({
'measurement': self._get_measurement(metric['path']),
'tags': {
'hostname': metric['host'],
'type': self._get_value(metric['path'])
},
'time': int(metric['timestamp']),
'fields': {
'value': float(metric['value'])
}
})
client = InfluxDBClient(config.db_host, config.db_port, config.db_user, config.db_pass, config.db_name)
client.write_points(influx_points, time_precision='s')
return {
'code': 200,
'message': 'OK',
}
class MetricCollector(Flask):
def __init__(self, name):
Flask.__init__(self, name)
metric_endpoint = MetricEndpoint.as_view('metric')
self.add_url_rule('/', view_func=metric_endpoint)
def main():
app = MetricCollector(__name__)
app.run(debug=config.debug, host='0.0.0.0')
if __name__ == '__main__':
main()