Code cleanup
paulfantom committed
Jun 19, 2016 at 14:35 UTC
49b71e8140b89731bdbc2ca2bfe4886d667d2681
2 files changed
+59
-60
plugins.d/python.d.plugin
+46
-48
@@ -3,8 +3,9 @@
3
import os
4
import sys
5
import time
6
+
7
try:
7
- assert sys.version_info >= (3,1)
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')
@@ -17,14 +18,15 @@ except ImportError:
18
sys.stdout.write('DISABLE\n')
19
sys.exit(1)
20
21
+DEBUG_FLAG = False
22
+PROGRAM = "python.d.plugin"
23
MODULE_EXTENSION = ".chart.py"
21
-BASE_CONFIG = {'update_every' : 10,
24
+BASE_CONFIG = {'update_every': 10,
25
'priority': 12345,
23
- 'retries' : 0}
26
+ 'retries': 0}
27
28
29
class PythonCharts(object):
27
-
30
def __init__(self,
31
interval=None,
32
modules=[],
@@ -36,7 +38,7 @@ class PythonCharts(object):
38
self.configs = modules_configs
39
40
# load modules
39
- loaded_modules = self._load_modules(modules_path,modules, modules_disabled)
41
+ loaded_modules = self._load_modules(modules_path, modules, modules_disabled)
42
43
# load configuration files
44
configured_modules = self._load_configs(loaded_modules)
@@ -45,15 +47,15 @@ class PythonCharts(object):
47
self.jobs = self._create_jobs(configured_modules)
48
if DEBUG_FLAG and interval is not None:
49
for job in self.jobs:
48
- job._create_timetable(interval)
49
-
50
-
51
- def _create_jobs(self,modules):
52
- # module store a definition of Service class
53
- # module store configuration in module.config
54
- # configs are list of dicts or a dict
55
- # one dict is one service
56
- # iterate over list of modules and inside one loop iterate over configs
50
+ job.create_timetable(interval)
51
+
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:
@@ -64,7 +66,7 @@ class PythonCharts(object):
66
except Exception as e:
67
debug(module.__name__ +
68
": Couldn't start job named " +
67
- str(name) +
69
+ str(name) +
70
": " +
71
str(e))
72
return None
@@ -74,11 +76,12 @@ class PythonCharts(object):
76
if name is not None:
77
job.execution_name += "_" + name
78
jobs.append(job)
77
-
79
+
80
return [j for j in jobs if j is not None]
81
80
- def _import_module(self, path, name=None):
81
- # try to import module using only its path
82
+ @staticmethod
83
+ def _import_module(path, name=None):
84
+ # try to import module using only its path
85
if name is None:
86
name = path.split('/')[-1]
87
if name[-len(MODULE_EXTENSION):] != MODULE_EXTENSION:
@@ -111,7 +114,7 @@ class PythonCharts(object):
114
names = os.listdir(path)
115
for mod in names:
116
if mod.strip(MODULE_EXTENSION) in disabled:
114
- debug("disabling:",mod.strip(MODULE_EXTENSION))
117
+ debug("disabling:", mod.strip(MODULE_EXTENSION))
118
continue
119
m = self._import_module(path + mod)
120
if m is not None:
@@ -119,8 +122,8 @@ class PythonCharts(object):
122
loaded.append(m)
123
return loaded
124
122
- def _load_configs(self,modules):
123
- # function loads configuration files to modules
125
+ def _load_configs(self, modules):
126
+ # function loads configuration files to modules
127
for mod in modules:
128
configfile = self.configs + mod.__name__ + ".conf"
129
if os.path.isfile(configfile):
@@ -128,9 +131,9 @@ class PythonCharts(object):
131
try:
132
setattr(mod,
133
'config',
131
- self._parse_config(mod,read_config(configfile)))
134
+ self._parse_config(mod, read_config(configfile)))
135
except Exception as e:
133
- debug("something went wrong while loading configuration",e)
136
+ debug("something went wrong while loading configuration", e)
137
else:
138
debug(mod.__name__ +
139
": configuration file '" +
@@ -138,7 +141,7 @@ class PythonCharts(object):
141
"' not found. Using defaults.")
142
# set config if not found
143
if not hasattr(mod, 'config'):
141
- mod.config = {None:{}}
144
+ mod.config = {None: {}}
145
for var in BASE_CONFIG:
146
try:
147
mod.config[None][var] = getattr(var, mod)
@@ -146,28 +149,29 @@ class PythonCharts(object):
149
mod.config[None][var] = BASE_CONFIG[var]
150
return modules
151
149
- def _parse_config(self,module,config):
152
+ @staticmethod
153
+ def _parse_config(module, config):
154
# get default values
155
defaults = {}
156
for key in BASE_CONFIG:
157
try:
158
# get defaults from module config
159
defaults[key] = int(config.pop(key))
156
- except (KeyError,ValueError):
160
+ except (KeyError, ValueError):
161
try:
162
# get defaults from module source code
163
defaults[key] = getattr(module, key)
160
- except (KeyError,ValueError):
164
+ except (KeyError, ValueError):
165
# if above failed, get defaults from global dict
166
defaults[key] = BASE_CONFIG[key]
163
-
167
+
168
# check if there are dict in config dict
169
many_jobs = False
170
for name in config:
171
if type(config[name]) is dict:
172
many_jobs = True
173
break
170
-
174
+
175
# assign variables needed by supervisor to every job configuration
176
if many_jobs:
177
for name in config:
@@ -178,12 +182,12 @@ class PythonCharts(object):
182
else:
183
config = {None: config.copy()}
184
config[None].update(defaults)
181
-
185
+
186
# return dictionary of jobs where every job has BASE_CONFIG variables
187
return config
188
189
def _stop(self, job, reason=None):
186
- # modifies self.jobs
190
+ # modifies self.jobs
191
self.jobs.remove(job)
192
if reason is None:
193
return
@@ -205,10 +209,10 @@ class PythonCharts(object):
209
job.execution_name +
210
".conf' not found. Using defaults.")
211
elif reason[:11] == "misbehaving":
208
- debug(job.execution_name, "is "+reason)
212
+ debug(job.execution_name, "is " + reason)
213
214
def check(self):
211
- # try to execute check() on every job
215
+ # try to execute check() on every job
216
for job in self.jobs:
217
try:
218
if not job.check():
@@ -219,7 +223,7 @@ class PythonCharts(object):
223
self._stop(job, "misbehaving. Reason: " + str(e))
224
225
def create(self):
222
- # try to execute create() on every job
226
+ # try to execute create() on every job
227
for job in self.jobs:
228
try:
229
if not job.create():
@@ -242,7 +246,7 @@ class PythonCharts(object):
246
self._stop(job, "misbehaving. Reason: " + str(e))
247
248
def _update_job(self, job):
245
- # try to execute update() on every job and draw run time graph
249
+ # try to execute update() on every job and draw run time graph
250
t_start = time.time()
251
# check if it is time to execute job update() function
252
if job.timetable['next'] > t_start:
@@ -264,11 +268,7 @@ class PythonCharts(object):
268
t_end = time.time()
269
job.timetable['next'] = t_end - (t_end % job.timetable['freq']) + job.timetable['freq']
270
# draw performance graph
267
- if self.first_run:
268
- dt = 0
269
- else:
270
- dt = int((t_end - job.timetable['last']) * 1000000)
271
- sys.stdout.write("BEGIN netdata.plugin_pythond_"+job.execution_name+" "+str(since_last)+'\n')
271
+ sys.stdout.write("BEGIN netdata.plugin_pythond_" + job.execution_name + " " + str(since_last) + '\n')
272
sys.stdout.write("SET run_time = " + str(int((t_end - t_start) * 1000)) + '\n')
273
sys.stdout.write("END\n")
274
sys.stdout.flush()
@@ -276,10 +276,9 @@ class PythonCharts(object):
276
self.first_run = False
277
278
def update(self):
279
- # run updates (this will stay forever and ever and ever forever and ever it'll be the one...)
279
+ # run updates (this will stay forever and ever and ever forever and ever it'll be the one...)
280
self.first_run = True
281
while True:
282
- t_begin = time.time()
282
next_runs = []
283
for job in self.jobs:
284
self._update_job(job)
@@ -318,9 +317,7 @@ def debug(*args):
317
318
319
def parse_cmdline(directory, *commands):
321
- # TODO number -> interval
320
global DEBUG_FLAG
323
- DEBUG_FLAG = False
321
interval = None
322
323
mods = []
@@ -412,10 +409,10 @@ def run():
409
modules = out['modules']
410
if out['interval'] is not None:
411
interval = out['interval']
415
-
416
- # configure environement to run modules
417
- sys.path.append(modules_dir+"python_modules") # append path to directory with modules dependencies
418
-
412
+
413
+ # configure environment to run modules
414
+ sys.path.append(modules_dir + "python_modules") # append path to directory with modules dependencies
415
+
416
# run plugins
417
charts = PythonCharts(interval, modules, modules_dir, modules_conf, disabled)
418
charts.check()
@@ -423,5 +420,6 @@ def run():
420
charts.update()
421
sys.stdout.write("DISABLE")
422
423
+
424
if __name__ == '__main__':
425
run()
python.d/python_modules/base.py
+13
-12
@@ -6,47 +6,48 @@ import sys
6
7
8
class BaseService(object):
9
- def __init__(self,name=None,configuration=None):
9
+ def __init__(self, configuration=None):
10
if configuration is None:
11
self.error("BaseService: no configuration parameters supplied. Cannot create Service.")
12
raise RuntimeError
13
else:
14
self._extract_base_config(configuration)
15
- self._create_timetable()
15
+ self.timetable = {}
16
+ self.create_timetable()
17
self.execution_name = ""
18
18
- def _extract_base_config(self,config):
19
+ def _extract_base_config(self, config):
20
self.update_every = int(config['update_every'])
21
self.priority = int(config['priority'])
22
self.retries = int(config['retries'])
23
self.retries_left = self.retries
24
24
- def _create_timetable(self,freq=None):
25
+ def create_timetable(self, freq=None):
26
if freq is None:
27
freq = self.update_every
28
now = time()
28
- self.timetable = {'last' : now,
29
- 'next' : now - (now % freq) + freq,
30
- 'freq' : freq}
29
+ self.timetable = {'last': now,
30
+ 'next': now - (now % freq) + freq,
31
+ 'freq': freq}
32
32
-
33
- def error(self, msg, exception=""):
33
+ @staticmethod
34
+ def error(msg, exception=""):
35
if exception != "":
36
exception = " " + str(exception).replace("\n"," ")
37
sys.stderr.write(str(msg)+exception+"\n")
38
sys.stderr.flush()
39
40
def check(self):
40
- # TODO notify about not overriden function
41
+ # TODO notify about not overridden function
42
self.error("Where is your check()?")
43
return False
44
45
def create(self):
45
- # TODO notify about not overriden function
46
+ # TODO notify about not overridden function
47
self.error("Where is your create()?")
48
return False
49
50
def update(self):
50
- # TODO notify about not overriden function
51
+ # TODO notify about not overridden function
52
self.error("Where is your update()?")
53
return False