-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathwrite_json.py
128 lines (112 loc) · 4.08 KB
/
write_json.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
"""
*******************
*Copyright 2017, MapleLabs, All Rights Reserved.
*
********************
"""
import os
import json
import collectd
# user imports
from constants import *
class WriteJson:
def __init__(self):
self.max_entries = 10
self.path = PATH
def read_config(self, cfg):
for children in cfg.children:
if children.key == MAX_LOG_ENTRIES:
self.max_entries = int(children.values[0])
def datadict_to_dirname(self, data):
if PLUGIN not in data:
collectd.error("Plugin values not set")
return None
# path = os.path.join(self.path, DATADIR, data[PLUGIN])
if data[PLUGIN].startswith('prometheus'):
path = os.path.join(self.path, DATADIR, data[PLUGIN])
if(data[PLUGIN] not in ["mysql","mongod", "postgres", "tpcc", "jmeter", "jvm"]):
path = os.path.join(self.path, DATADIR, data[PLUGIN] + "/" + data[PLUGINTYPE])
else:
path = os.path.join(self.path, DATADIR, data[PLUGIN])
if data[PLUGIN] == "elasticsearch":
path = os.path.join(self.path, DATADIR, "elasticsearchagent" + "/" + data[PLUGINTYPE])
if PLUGIN_INS in data:
path = os.path.join(path, data[PLUGIN_INS])
return path
# creates index file in the required directory if it doesn't exits
# otherwise just returns index
def get_index(self, dirname):
index = ERRNO
filename = os.path.join(dirname, INDEX_FILE)
if not os.path.exists(os.path.dirname(filename)):
try:
os.makedirs(os.path.dirname(filename))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
collectd.error("Creation of directory failed")
return index
with open(filename, "w") as f:
f.write(INDEX_NEW_FILE)
f.write("\n")
f.write(str(self.max_entries))
f.write("\n")
with open(filename, "r") as f:
try:
index = int(f.readline())
collectd.debug("reading index.txt index = %s" % index)
except Exception as e:
collectd.debug("reading index.txt index = %s" % str(e))
index = ERRNO
return index
def get_filename_from_index(self, index):
new_index = (index + 1) % self.max_entries
filename = str(new_index) + LOG_EXTENSION
return filename, new_index
def write(self, data, dirname):
index = self.get_index(dirname)
if index == ERRNO:
return
(filename, new_index) = self.get_filename_from_index(index)
fpath_log = os.path.join(dirname, filename)
fpath_index = os.path.join(dirname, INDEX_FILE)
try:
fh = open(fpath_log, "w")
data = json.dumps(data)
fh.write(data)
fh.write("\n")
fh.close()
fh = open(fpath_index, "w")
fh.write(str(new_index))
fh.write("\n")
fh.write(str(self.max_entries))
fh.write("\n")
fh.close()
except:
fh.close()
def format_json(self, data):
delete_list = []
update_dict = {}
for key, value in data.items():
if key == ACTUALPLUGINTYPE:
delete_list.append(key)
if isinstance(value, str):
if len(value) > 0 and value[0] == "[":
delete_list.append(key)
value = value.replace("\\", "")
update_dict[key] = json.loads(value)
for key in delete_list:
data.pop(key, None)
data.update(update_dict)
return data
def write_json(self, ds):
data_dict = self.format_json(ds)
dirname = self.datadict_to_dirname(data_dict)
if dirname is None:
return
self.write(data_dict, dirname)
def write(data):
if not WRITE_JSON_ENABLED:
return
collectd.info("write_json invoked")
obj = WriteJson()
obj.write_json(data)