add modules path to environement; add a possibility to disable plugins; code cleanup; comments
paulfantom committed
Jun 13, 2016 at 09:49 UTC
870819929e016642a3003275253469678001e525
1 file changed
+41
-22
plugins.d/python.d.plugin
+41
-22
@@ -3,8 +3,6 @@
3
import os
4
import sys
5
import time
6
-#import configparser
7
-#import importlib.machinery
6
try:
7
assert sys.version_info >= (3,1)
8
import configparser
@@ -21,7 +19,8 @@ class PythonCharts(object):
19
interval=None,
20
modules=[],
21
modules_path='../python.d/',
24
- modules_configs='../conf.d/'):
22
+ modules_configs='../conf.d/',
23
+ modules_disabled=[]):
24
self.first_run = True
25
if interval is None:
26
interval = 1
@@ -31,25 +30,31 @@ class PythonCharts(object):
30
debug("cannot find charts directory ", modules_path)
31
sys.stdout.write("DISABLE\n")
32
sys.exit(1)
33
+ # set configuration directory
34
self.configs = modules_configs
35
36
+ # load modules
37
self.modules = []
38
if len(modules) > 0:
39
for m in modules:
40
mod = self.import_plugin(modules_path + m + ".chart.py")
41
if mod is not None:
42
self.modules.append(mod)
42
- if len(self.modules) == 0:
43
- debug("cannot find provided module(s)", modules_path)
44
- sys.stdout.write("DISABLE\n")
45
- sys.exit(1)
43
else:
47
- self.load_modules(modules_path)
48
- if len(self.modules) == 0:
49
- debug("cannot find modules directory", modules_path)
50
- sys.stdout.write("DISABLE\n")
51
- sys.exit(1)
44
+ self.autoload_modules(modules_path)
45
+
46
+ # check if there are any modules loaded
47
+ if len(self.modules) == 0:
48
+ debug("cannot find modules", modules_path)
49
+ sys.stdout.write("DISABLE\n")
50
+ sys.exit(1)
51
52
+ # check if loaded modules is on disabled modules list
53
+ for mod in self.modules:
54
+ if mod.__name__ in modules_disabled:
55
+ self.modules.remove(mod)
56
+
57
+ # load configuration files
58
self.load_configs()
59
60
# set timetable dict (last execution, next execution and frequency)
@@ -73,6 +78,7 @@ class PythonCharts(object):
78
'freq' : interval}
79
80
def import_plugin(self, path, name=None):
81
+ # try to import module using only its path
82
if name is None:
83
name = path.split('/')[-1]
84
if name[-9:] != ".chart.py":
@@ -84,7 +90,9 @@ class PythonCharts(object):
90
debug(str(e))
91
return None
92
87
- def load_modules(self, path):
93
+ def autoload_modules(self, path):
94
+ # scan directory specified in path and load all modules from there
95
+ # function modifies self.modules
96
names = os.listdir(path)
97
for mod in names:
98
m = self.import_plugin(path + mod)
@@ -93,6 +101,7 @@ class PythonCharts(object):
101
self.modules.append(m)
102
103
def load_configs(self):
104
+ # function modifies every loaded module in self.modules
105
for m in self.modules:
106
configfile = self.configs + m.__name__ + ".conf"
107
if os.path.isfile(configfile):
@@ -106,6 +115,7 @@ class PythonCharts(object):
115
"' not found. Using defaults.")
116
117
def disable_module(self, mod, reason=None):
118
+ # modifies self.modules
119
self.modules.remove(mod)
120
del self.timetable[mod.__name__]
121
if reason is None:
@@ -131,6 +141,7 @@ class PythonCharts(object):
141
debug(mod.__name__, "is "+reason)
142
143
def check(self):
144
+ # try to execute check() on every loaded module
145
for mod in self.modules:
146
try:
147
if not mod.check():
@@ -141,6 +152,7 @@ class PythonCharts(object):
152
self.disable_module(mod, "misbehaving. Reason: " + str(e))
153
154
def create(self):
155
+ # try to execute create() on every loaded module
156
for mod in self.modules:
157
try:
158
if not mod.create():
@@ -163,6 +175,7 @@ class PythonCharts(object):
175
self.disable_module(mod, "misbehaving. Reason: " + str(e))
176
177
def update_module(self, mod):
178
+ # try to execute update() on every module and draw run time graph
179
t_start = time.time()
180
# check if it is time to execute module update() function
181
if self.timetable[mod.__name__]['next'] > t_start:
@@ -183,6 +196,7 @@ class PythonCharts(object):
196
return
197
t_end = time.time()
198
self.timetable[mod.__name__]['next'] = t_end - (t_end % self.timetable[mod.__name__]['freq']) + self.timetable[mod.__name__]['freq']
199
+ # draw performance graph
200
if self.first_run:
201
dt = 0
202
else:
@@ -195,6 +209,7 @@ class PythonCharts(object):
209
self.first_run = False
210
211
def update(self):
212
+ # run updates (this will stay forever and ever and ever forever and ever it'll be the one...)
213
self.first_run = True
214
while True:
215
t_begin = time.time()
@@ -280,6 +295,7 @@ def run():
295
interval = int(os.getenv('NETDATA_UPDATE_EVERY', 1))
296
297
# read configuration file
298
+ disabled = []
299
if config_dir[-1] != '/':
300
configfile = config_dir + '/' + "python.d.conf"
301
else:
@@ -296,20 +312,24 @@ def run():
312
pass
313
try:
314
modules_conf = conf['plugins_config_dir']
299
- except (KeyError, TypeError):
315
+ except (KeyError):
316
modules_conf = config_dir # default configuration directory
317
try:
318
modules_dir = conf['plugins_dir']
303
- except (KeyError, TypeError):
319
+ except (KeyError):
320
modules_dir = main_dir.replace("plugins.d", "python.d")
321
try:
322
interval = int(conf['interval'])
323
except (KeyError, TypeError):
308
- pass # default interval
324
+ pass # use default interval from NETDATA_UPDATE_EVERY
325
try:
326
DEBUG_FLAG = bool(conf['debug'])
327
except (KeyError, TypeError):
328
pass
329
+ try:
330
+ disabled = conf['disabled'].split(',')
331
+ except (KeyError):
332
+ pass
333
except FileNotFoundError:
334
modules_conf = config_dir
335
modules_dir = main_dir.replace("plugins.d", "python.d")
@@ -323,15 +343,14 @@ def run():
343
# parse passed command line arguments
344
out = parse_cmdline(modules_dir, *sys.argv)
345
modules = out['modules']
326
-# if out['interval'] is not None:
327
-# interval = out['interval']
346
+ if out['interval'] is not None:
347
+ interval = out['interval']
348
349
# configure environement to run modules
330
- #sys.path.append(modules_dir+"python_modules") # append path to directory with modules dependencies
331
-
350
+ sys.path.append(modules_dir+"python_modules") # append path to directory with modules dependencies
351
+
352
# run plugins
333
-# charts = PythonCharts(interval, modules, modules_dir, modules_conf)
334
- charts = PythonCharts(out['interval'], modules, modules_dir, modules_conf)
353
+ charts = PythonCharts(interval, modules, modules_dir, modules_conf, disabled)
354
charts.check()
355
charts.create()
356
charts.update()