python2 compatibility
paulfantom committed
Jun 19, 2016 at 17:16 UTC
ebca5181e8282e7d4cc0a15664560bfd64c05f1e
4 files changed
+79
-53
plugins.d/python.d.plugin
+76
-49
@@ -7,10 +7,25 @@ import time
7
try:
8
assert sys.version_info >= (3, 1)
9
import importlib.machinery
10
-except AssertionError:
11
- sys.stderr.write('python.d.plugin: Not supported python version. Needed python >= 3.1\n')
12
- sys.stdout.write('DISABLE\n')
13
- sys.exit(1)
10
+
11
+ # change this hack below if we want PY_VERSION to be used in modules
12
+ # import builtins
13
+ # builtins.PY_VERSION = 3
14
+ PY_VERSION = 3
15
+ sys.stderr.write('python.d.plugin: Using python 3\n')
16
+except (AssertionError, ImportError):
17
+ try:
18
+ import imp
19
+
20
+ # change this hack below if we want PY_VERSION to be used in modules
21
+ # import __builtin__
22
+ # __builtin__.PY_VERSION = 2
23
+ PY_VERSION = 2
24
+ sys.stderr.write('python.d.plugin: Using python 2\n')
25
+ except (AssertionError, ImportError):
26
+ sys.stderr.write('python.d.plugin: Not supported python version. Needed python >= 3.1\n')
27
+ sys.stdout.write('DISABLE\n')
28
+ sys.exit(1)
29
try:
30
import yaml
31
except ImportError:
@@ -29,10 +44,16 @@ BASE_CONFIG = {'update_every': 10,
44
class PythonCharts(object):
45
def __init__(self,
46
interval=None,
32
- modules=[],
47
+ modules=None,
48
modules_path='../python.d/',
49
modules_configs='../conf.d/',
35
- modules_disabled=[]):
50
+ modules_disabled=None):
51
+
52
+ if modules is None:
53
+ modules = []
54
+ if modules_disabled is None:
55
+ modules_disabled = []
56
+
57
self.first_run = True
58
# set configuration directory
59
self.configs = modules_configs
@@ -49,36 +70,6 @@ class PythonCharts(object):
70
for job in self.jobs:
71
job.create_timetable(interval)
72
52
- @staticmethod
53
- def _create_jobs(modules):
54
- # module store a definition of Service class
55
- # module store configuration in module.config
56
- # configs are list of dicts or a dict
57
- # one dict is one service
58
- # iterate over list of modules and inside one loop iterate over configs
59
- jobs = []
60
- for module in modules:
61
- for name in module.config:
62
- # register a new job
63
- conf = module.config[name]
64
- try:
65
- job = module.Service(configuration=conf, name=name)
66
- except Exception as e:
67
- debug(module.__name__ +
68
- ": Couldn't start job named " +
69
- str(name) +
70
- ": " +
71
- str(e))
72
- return None
73
- else:
74
- # set execution_name (needed to plot run time graphs)
75
- job.execution_name = module.__name__
76
- if name is not None:
77
- job.execution_name += "_" + name
78
- jobs.append(job)
79
-
80
- return [j for j in jobs if j is not None]
81
-
73
@staticmethod
74
def _import_module(path, name=None):
75
# try to import module using only its path
@@ -88,7 +79,11 @@ class PythonCharts(object):
79
return None
80
name = name[:-len(MODULE_EXTENSION)]
81
try:
91
- return importlib.machinery.SourceFileLoader(name, path).load_module()
82
+ if PY_VERSION == 3:
83
+ return importlib.machinery.SourceFileLoader(name, path).load_module()
84
+ else:
85
+ return imp.load_source(name, path)
86
+ # return importlib.import_module(path, name)
87
except Exception as e:
88
debug(str(e))
89
return None
@@ -109,6 +104,10 @@ class PythonCharts(object):
104
mod = self._import_module(path + m + MODULE_EXTENSION)
105
if mod is not None:
106
loaded.append(mod)
107
+ else: # exit if plugin is not found
108
+ sys.stdout.write("DISABLE")
109
+ sys.stdout.flush()
110
+ sys.exit(1)
111
else:
112
# scan directory specified in path and load all modules from there
113
names = os.listdir(path)
@@ -186,6 +185,35 @@ class PythonCharts(object):
185
# return dictionary of jobs where every job has BASE_CONFIG variables
186
return config
187
188
+ @staticmethod
189
+ def _create_jobs(modules):
190
+ # module store a definition of Service class
191
+ # module store configuration in module.config
192
+ # configs are list of dicts or a dict
193
+ # one dict is one service
194
+ # iterate over list of modules and inside one loop iterate over configs
195
+ jobs = []
196
+ for module in modules:
197
+ for name in module.config:
198
+ # register a new job
199
+ conf = module.config[name]
200
+ try:
201
+ job = module.Service(configuration=conf, name=name)
202
+ except Exception as e:
203
+ debug(module.__name__ +
204
+ ": Couldn't start job named " +
205
+ str(name) +
206
+ ": " +
207
+ str(e))
208
+ return None
209
+ else:
210
+ # set execution_name (needed to plot run time graphs)
211
+ job.execution_name = module.__name__
212
+ if name is not None:
213
+ job.execution_name += "_" + name
214
+ jobs.append(job)
215
+ return [j for j in jobs if j is not None]
216
+
217
def _stop(self, job, reason=None):
218
# modifies self.jobs
219
self.jobs.remove(job)
@@ -297,8 +325,8 @@ def read_config(path):
325
try:
326
with open(path, 'r') as stream:
327
config = yaml.load(stream)
300
- except IsADirectoryError:
301
- debug(str(path), "is a directory")
328
+ except (OSError, IOError):
329
+ debug(str(path), "is not a valid configuration file")
330
return None
331
except yaml.YAMLError as e:
332
debug(str(path), "is malformed:", e)
@@ -331,6 +359,7 @@ def parse_cmdline(directory, *commands):
359
DEBUG_FLAG = True
360
mods.append(cmd.replace(".chart.py", ""))
361
else:
362
+ DEBUG_FLAG = False
363
try:
364
interval = int(cmd)
365
except ValueError:
@@ -346,8 +375,7 @@ def parse_cmdline(directory, *commands):
375
376
# if __name__ == '__main__':
377
def run():
349
- global DEBUG_FLAG, PROGRAM
350
- DEBUG_FLAG = True
378
+ global PROGRAM, DEBUG_FLAG
379
PROGRAM = sys.argv[0].split('/')[-1].split('.plugin')[0]
380
# parse env variables
381
# https://github.com/firehol/netdata/wiki/External-Plugins#environment-variables
@@ -362,9 +390,8 @@ def run():
390
config_dir += '/'
391
configfile = config_dir + "python.d.conf"
392
365
- try:
366
- conf = read_config(configfile)
367
- print(conf)
393
+ conf = read_config(configfile)
394
+ if conf is not None:
395
try:
396
if str(conf['enable']) is False:
397
debug("disabled in configuration file")
@@ -374,26 +401,26 @@ def run():
401
pass
402
try:
403
modules_conf = conf['plugins_config_dir']
377
- except KeyError:
404
+ except (KeyError, TypeError):
405
modules_conf = config_dir + "python.d/" # default configuration directory
406
try:
407
modules_dir = conf['plugins_dir']
381
- except KeyError:
408
+ except (KeyError, TypeError):
409
modules_dir = main_dir.replace("plugins.d", "python.d")
410
try:
411
interval = conf['interval']
385
- except KeyError:
412
+ except (KeyError, TypeError):
413
pass # use default interval from NETDATA_UPDATE_EVERY
414
try:
415
DEBUG_FLAG = conf['debug']
389
- except KeyError:
416
+ except (KeyError, TypeError):
417
pass
418
for k, v in conf.items():
419
if k in ("plugins_config_dir", "plugins_dir", "interval", "debug"):
420
continue
421
if v is False:
422
disabled.append(k)
396
- except FileNotFoundError:
423
+ else:
424
modules_conf = config_dir + "python.d/"
425
modules_dir = main_dir.replace("plugins.d", "python.d")
426
python.d/example.chart.py
+1
-1
@@ -13,7 +13,7 @@ retries = 7
13
14
class Service(BaseService):
15
def __init__(self, configuration=None, name=None):
16
- super().__init__(configuration=configuration)
16
+ super(self.__class__,self).__init__(configuration=configuration)
17
18
def check(self):
19
return True
python.d/mysql.chart.py
+1
-2
@@ -21,7 +21,6 @@ except ImportError:
21
from base import BaseService
22
23
# default configuration (overridden by python.d.plugin)
24
-# FIXME change password
24
config = {
25
'local': {
26
'user' : 'root',
@@ -334,7 +333,7 @@ CHARTS = {
333
334
class Service(BaseService):
335
def __init__(self,configuration=None,name=None):
337
- super().__init__(configuration=configuration)
336
+ super(self.__class__, self).__init__(configuration=configuration)
337
self.name = name
338
self.configuration = self._parse_config(configuration)
339
self.connection = None
python.d/python_modules/base.py
+1
-1
@@ -6,7 +6,7 @@ import sys
6
7
8
class BaseService(object):
9
- def __init__(self, configuration=None):
9
+ def __init__(self, configuration=None, name=None):
10
if configuration is None:
11
self.error("BaseService: no configuration parameters supplied. Cannot create Service.")
12
raise RuntimeError