-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·81 lines (60 loc) · 2.34 KB
/
main.py
File metadata and controls
executable file
·81 lines (60 loc) · 2.34 KB
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
#!/usr/bin/env python3
import sys
import argparse
from datetime import datetime
import json
import time
from retry import retry
import metric_descriptions
from node_tracker import NodeTracker
import requests
from result_parser import ResultParser
import config as cfg
import graphyte
class MetricGatherer():
def __init__(self):
self.node_tracker = NodeTracker()
def to_url_params(self, metric_description):
return "metrics={}&dim={}&agg={}&nodes=all".format(
metric_description.name, ",".join(metric_description.dimensions),metric_description.agg)
@retry(delay=1)
def get_metric(self, metric_description):
BASE_URL = 'http://' + cfg.elastic['es_host'] + ':9600/_plugins/_performanceanalyzer/metrics?'
url = "{}{}".format(BASE_URL, self.to_url_params(metric_description))
return requests.get(url,timeout=2)
def get_all_metrics(self):
''' Loops through all the metric descriptions, sending one at a time,
parsing the results, and returning a list of dicts '''
docs = []
for metric in metric_descriptions.get_working_metric_descriptions():
result = self.get_metric(metric)
if result.status_code != 200:
print("FAIL", metric, '\n', result.text)
else:
rp = ResultParser(metric, result.text, self.node_tracker)
for doc in rp.records():
docs.append(doc)
return docs
class MetricWriter():
def put_graphite(self, docs):
for doc in docs:
keys = list(doc)
hostname = doc['node_fqdn'].split('.')[0]
metric_path = hostname + '.opensearch.' + doc['metric'] + '.' + str(doc[keys[1]]) + '_' + doc['agg']
metric_value = (doc[doc['metric']])
graphyte.send(metric_path, float(metric_value))
def init_graphite():
''' Init Graphite '''
graphyte.init(cfg.graphite['g_host'], port=cfg.graphite['g_port'], prefix=cfg.graphite['prefix'], interval=20 )
if __name__ == '__main__':
init_graphite()
while 1:
# limit execution speed
target=(int(time.time())) + 10
print(str(int(time.time())) + ' target: ' + str(target))
docs = MetricGatherer().get_all_metrics()
MetricWriter().put_graphite(docs)
while True:
if int(time.time()) >= int(target):
break
time.sleep(1)