-
Notifications
You must be signed in to change notification settings - Fork 12
/
hxconnect.py
115 lines (96 loc) · 3.76 KB
/
hxconnect.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
"""
*******************
*Copyright 2017, MapleLabs, All Rights Reserved.
*
********************
"""
"""
A collectd plugin for analysing hx controller cluster
test results
"""
import collectd
import time
import json
import os
import requests
from constants import *
from utils import *
from copy import deepcopy
class Hx_controllerResults:
def __init__(self):
self.interval = HX_INTERVAL
self.pollCounter = 0
self.end_time = int(time.time())
self.start_time = self.end_time - self.interval
self.timestamp_format = '%H:%M_%Y%m%d'
def read_config(self, cfg):
for children in cfg.children:
if children.key == INTERVAL:
self.interval = children.values[0]
if children.key == HX_CONNECT_IP:
self.hx_cluster_ip = children.values[0]
@staticmethod
def add_common_params(hx_cluster_data, doc_type):
hostname = gethostname()
timestamp = int(round(time.time() * 1000))
hx_cluster_data[HOSTNAME] = hostname
hx_cluster_data[TIMESTAMP] = timestamp
hx_cluster_data[PLUGIN] = HXCONNECT
hx_cluster_data[ACTUALPLUGINTYPE] = HXCONNECT
hx_cluster_data[PLUGINTYPE] = HXCONNECT
@staticmethod
def dispatch_data(hx_cluster_data):
collectd.info("Plugin HX_CONNECT: Values: " + json.dumps(hx_cluster_data))
dispatch(hx_cluster_data)
def format_time(self):
cur_time = time.strftime(self.timestamp_format,time.localtime(self.end_time))
prev_time = time.strftime(self.timestamp_format,time.localtime(self.start_time))
self.fromTime = prev_time
self.until = cur_time
def collect_hx_cluster_data(self):
"""
# Collecting the hx_cluster data
"""
try:
basic_url = "https://" + self.hx_cluster_ip + "/render"
data_dict = {}
self.format_time()
date_time = subprocess.check_output("timedatectl", shell=True)
date_time = date_time.split("\n")
for each_line in date_time:
if "Time zone" in each_line:
time_zone = (each_line.strip()).split(" ")
tz = time_zone[2]
for each_val in PARAMS_LIST:
params = {"format":"json"}
params["tz"] = tz
params["from"] = self.fromTime
params["until"] = self.until
params["target"] = APIVALS_DICT[each_val]["target"]
res = requests.get(basic_url, params=params, verify=False)
res = res.json()
if res:
for each_datapoint in res[0]["datapoints"]:
if each_datapoint[0]:
#data_dict[APIVALS_DICT[each_val]["key"]] = res[0]["datapoints"][0][0]
data_dict[APIVALS_DICT[each_val]["key"]] = each_datapoint[0]
else:
pass
self.start_time = self.start_time + int(self.interval)
self.end_time = self.end_time + int(self.interval)
return data_dict
except Exception as e:
collectd.debug("Couldn't collect hx controller data for the host %s due to %s" % (self.hx_cluster_ip, e))
def collect_results(self):
hx_cluster_data = self.collect_hx_cluster_data()
self.add_common_params(hx_cluster_data, "hx_cluster")
self.dispatch_data(hx_cluster_data)
def read(self):
self.pollCounter += 1
self.collect_results()
def read_temp(self):
collectd.unregister_read(self.read_temp)
collectd.register_read(self.read, interval=int(self.interval))
obj = Hx_controllerResults()
collectd.register_config(obj.read_config)
collectd.register_read(obj.read_temp)