added number of microseconds since the last update of the chart; code formatting
paulfantom committed
Jun 11, 2016 at 13:36 UTC
921a1f34c0e1f800f38afbb5a3b718992520f4b3
2 files changed
+47
-45
plugins.d/python.d.plugin
+46
-44
@@ -6,18 +6,19 @@ import time
6
import importlib.machinery
7
import configparser
8
9
-
9
class PythonCharts(object):
11
-
10
+
11
def __init__(self,
12
interval=1,
13
modules=[],
14
modules_path='../python.d/',
15
modules_configs='../conf.d/'):
16
+ self.first_run = True
17
self.interval = interval
18
# check if plugin directory exists
19
if not os.path.isdir(modules_path):
20
debug("cannot find charts directory ", modules_path)
21
+ sys.stdout.write("DISABLE\n")
22
sys.exit(1)
23
self.configs = modules_configs
24
@@ -31,21 +32,29 @@ class PythonCharts(object):
32
self.load_modules(modules_path)
33
if len(self.modules) == 0:
34
debug("cannot find modules directory", modules_path)
34
- # sys.stdout.write("DISABLE\n")
35
+ sys.stdout.write("DISABLE\n")
36
sys.exit(1)
37
38
self.load_configs()
39
40
# set last execution and execution frequency dict
41
self.timetable = {}
41
- #last_exec = time.time()
42
- last_exec = 0
42
for m in self.modules:
43
try:
44
interval = m.update_every
45
+ interval = int(interval)
46
except AttributeError:
47
interval = self.interval
48
- self.timetable[m.__name__] = [last_exec, int(interval)]
48
+ except ValueError:
49
+ interval = self.interval
50
+ debug(m.__name__ +
51
+ ": wrong value of 'update_every' setting to " +
52
+ str(self.interval))
53
+ # ensure module update frequency is not less than execution of this program
54
+ if interval < self.interval:
55
+ interval = self.interval
56
+ # charts updates are twice as frequent as user specified
57
+ self.timetable[m.__name__] = [0, interval/2]
58
59
def import_plugin(self, path, name=None):
60
if name is None:
@@ -75,11 +84,10 @@ class PythonCharts(object):
84
for k, v in read_config(configfile).items():
85
setattr(m, k, v)
86
else:
78
- debug(
79
- m.__name__ +
80
- ": configuration file '" +
81
- configfile +
82
- "' not found. Using defaults.")
87
+ debug(m.__name__ +
88
+ ": configuration file '" +
89
+ configfile +
90
+ "' not found. Using defaults.")
91
92
def disable_module(self, mod, reason=None):
93
self.modules.remove(mod)
@@ -87,28 +95,24 @@ class PythonCharts(object):
95
if reason is None:
96
return
97
elif reason[:3] == "no ":
90
- debug(
91
- "chart '" +
92
- mod.__name__,
93
- "' does not seem to have " +
94
- reason[3:] +
95
- "() function. Disabling it.")
98
+ debug("chart '" +
99
+ mod.__name__,
100
+ "' does not seem to have " +
101
+ reason[3:] +
102
+ "() function. Disabling it.")
103
elif reason[:7] == "failed ":
97
- debug(
98
- "chart '" +
99
- mod.__name__ +
100
- reason[3:] +
101
- "() function. reports failure.")
104
+ debug("chart '" +
105
+ mod.__name__ +
106
+ reason[3:] +
107
+ "() function. reports failure.")
108
elif reason[:13] == "configuration":
103
- debug(
104
- mod.__name__,
105
- "configuration file '" +
106
- self.configs +
107
- mod.__name__ +
108
- ".conf' not found. Using defaults.")
109
+ debug(mod.__name__,
110
+ "configuration file '" +
111
+ self.configs +
112
+ mod.__name__ +
113
+ ".conf' not found. Using defaults.")
114
elif reason[:11] == "misbehaving":
115
debug(mod.__name__, "is misbeaving. Disabling it")
111
- # sys.stdout.write('DISABLE')
116
117
def check(self):
118
for mod in self.modules:
@@ -127,7 +131,6 @@ class PythonCharts(object):
131
self.disable_module(mod, "failed create")
132
else:
133
chart = mod.__name__
130
- # sys.stdout.write("CHART netdata.plugin_pythond_"+chart+" '' 'Execution time for "+chart+" plugin' 'milliseconds / run' python.d netdata.plugin_python area 145000 "+str(self.interval)+'\n')
134
sys.stdout.write(
135
"CHART netdata.plugin_pythond_" +
136
chart +
@@ -136,8 +139,7 @@ class PythonCharts(object):
139
" plugin' 'milliseconds / run' python.d netdata.plugin_python area 145000 " +
140
str(self.timetable[mod.__name__][1]) +
141
'\n')
139
- sys.stdout.write(
140
- "DIMENSION run_time 'run time' absolute 1 1\n\n")
142
+ sys.stdout.write("DIMENSION run_time 'run time' absolute 1 1\n\n")
143
sys.stdout.flush()
144
except AttributeError:
145
self.disable_module(mod, "no create")
@@ -150,12 +152,11 @@ class PythonCharts(object):
152
if (t1 - self.timetable[mod.__name__][0]) < self.timetable[mod.__name__][1]:
153
return
154
try:
153
- # t=int((t1- self.timetable[mod.__name__][0]) * 1000)
154
- # if t == 0: t=""
155
- t = "" # FIXME
155
+ if not self.first_run:
156
+ t = ""
157
+ else:
158
+ t=int((t1- self.timetable[mod.__name__][0]) * 1000000)
159
if not mod.update(t):
157
- # if not mod.update(int((t1- self.timetable[mod.__name__][0]) *
158
- # 1000)):
160
self.disable_module(mod, "update failed")
161
return
162
except AttributeError:
@@ -165,11 +166,11 @@ class PythonCharts(object):
166
self.disable_module(mod, "misbehaving")
167
return
168
t2 = time.time()
168
-# dt = int((t2 - self.timetable[mod.__name__][0]) * 1000)
169
-# if dt == 0: dt=""
170
- # dt="" #FIXME
171
- #sys.stdout.write("BEGIN netdata.plugin_pythond_"+mod.__name__+" "+str(dt)+'\n')
172
- sys.stdout.write("BEGIN netdata.plugin_pythond_" + mod.__name__ + "\n")
169
+ if self.first_run:
170
+ dt = ""
171
+ else:
172
+ dt = int((t2 - self.timetable[mod.__name__][0]) * 1000000)
173
+ sys.stdout.write("BEGIN netdata.plugin_pythond_"+mod.__name__+" "+str(dt)+'\n')
174
sys.stdout.write("SET run_time = " + str(int((t2 - t1) * 1000)) + '\n')
175
sys.stdout.write("END\n")
176
sys.stdout.flush()
@@ -181,10 +182,10 @@ class PythonCharts(object):
182
for mod in self.modules:
183
self.update_module(mod)
184
time.sleep((self.interval - (time.time() - t_begin)) / 2)
185
+ self.first_run = False
186
187
188
def read_config(path):
187
- # TODO make it bulletproof
189
config = configparser.ConfigParser()
190
config_str = ""
191
try:
@@ -208,6 +209,7 @@ def debug(*args):
209
210
211
def parse_cmdline(directory, *commands):
212
+ # TODO number -> interval
213
global DEBUG_FLAG, PROGRAM
214
DEBUG_FLAG = False
215
@@ -235,7 +237,6 @@ def run():
237
main_dir = os.getenv('NETDATA_PLUGINS_DIR',
238
os.path.abspath(__file__).strip("python.d.plugin.py"))
239
config_dir = os.getenv('NETDATA_CONFIG_DIR', "/etc/netdata/")
238
- # logs_dir = os.getenv('NETDATA_LOGS_DIR',None) #TODO logging?
240
interval = int(os.getenv('NETDATA_UPDATE_EVERY', 1))
241
242
# read configuration file
@@ -249,6 +250,7 @@ def run():
250
try:
251
if str(conf['enable']) == "no":
252
debug("disabled in configuration file")
253
+ sys.stdout.write("DISABLE\n")
254
sys.exit(1)
255
except (KeyError, TypeError):
256
pass
python.d/example.chart.py
+1
-1
@@ -1,6 +1,6 @@
1
import random
2
3
-update_every=10
3
+update_every=5
4
5
def check():
6
return True