7 assert sys.version_info >= (3,1)
8 import importlib.machinery
10 sys.stderr.write('python.d.plugin: Not supported python version. Needed python >= 3.1\n')
11 sys.stdout.write('DISABLE\n')
16 sys.stderr.write('python.d.plugin: Cannot find yaml library\n')
17 sys.stdout.write('DISABLE\n')
20 MODULE_EXTENSION = ".chart.py"
21 BASE_CONFIG = {'update_every' : 10,
26 class PythonCharts(object):
31 modules_path='../python.d/',
32 modules_configs='../conf.d/',
35 # set configuration directory
36 self.configs = modules_configs
39 loaded_modules = self._load_modules(modules_path,modules, modules_disabled)
41 # load configuration files
42 configured_modules = self._load_configs(loaded_modules)
44 # good economy and prosperity:
45 self.jobs = self._create_jobs(configured_modules)
46 if DEBUG_FLAG and interval is not None:
48 job._create_timetable(interval)
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
58 for module in modules:
59 for name in module.config:
61 conf = module.config[name]
63 job = module.Service(configuration=conf, name=name)
64 except Exception as e:
65 debug(module.__name__ +
66 ": Couldn't start job named " +
72 # set execution_name (needed to plot run time graphs)
73 job.execution_name = module.__name__
75 job.execution_name += "_" + name
78 return [j for j in jobs if j is not None]
80 def _import_module(self, path, name=None):
81 # try to import module using only its path
83 name = path.split('/')[-1]
84 if name[-len(MODULE_EXTENSION):] != MODULE_EXTENSION:
86 name = name[:-len(MODULE_EXTENSION)]
88 return importlib.machinery.SourceFileLoader(name, path).load_module()
89 except Exception as e:
93 def _load_modules(self, path, modules, disabled):
94 # check if plugin directory exists
95 if not os.path.isdir(path):
96 debug("cannot find charts directory ", path)
97 sys.stdout.write("DISABLE\n")
106 mod = self._import_module(path + m + MODULE_EXTENSION)
110 # scan directory specified in path and load all modules from there
111 names = os.listdir(path)
113 if mod.strip(MODULE_EXTENSION) in disabled:
114 debug("disabling:",mod.strip(MODULE_EXTENSION))
116 m = self._import_module(path + mod)
118 debug("loading chart: '" + path + mod + "'")
122 def _load_configs(self,modules):
123 # function loads configuration files to modules
125 configfile = self.configs + mod.__name__ + ".conf"
126 if os.path.isfile(configfile):
127 debug("loading chart options: '" + configfile + "'")
131 self._parse_config(mod,read_config(configfile)))
132 except Exception as e:
133 debug("something went wrong while loading configuration",e)
136 ": configuration file '" +
138 "' not found. Using defaults.")
141 def _parse_config(self,module,config):
144 for key in BASE_CONFIG:
146 # get defaults from module config
147 defaults[key] = int(config.pop(key))
148 except (KeyError,ValueError):
150 # get defaults from module source code
151 defaults[key] = getattr(module, key)
152 except (KeyError,ValueError):
153 # if above failed, get defaults from global dict
154 defaults[key] = BASE_CONFIG[key]
156 # check if there are dict in config dict
159 if type(config[name]) is dict:
163 # assign variables needed by supervisor to every job configuration
167 if key not in config[name]:
168 config[name][key] = defaults[key]
169 # if only one job is needed, values doesn't have to be in dict (in YAML)
171 config = {None: config.copy()}
172 config[None].update(defaults)
174 # return dictionary of jobs where every job has BASE_CONFIG variables
177 def _stop(self, job, reason=None): #FIXME test if Service has __name__
179 self.jobs.remove(job)
182 elif reason[:3] == "no ":
185 "' does not seem to have " +
187 "() function. Disabling it.")
188 elif reason[:7] == "failed ":
190 job.execution_name + "' " +
192 "() function reports failure.")
193 elif reason[:13] == "configuration":
194 debug(job.execution_name,
195 "configuration file '" +
198 ".conf' not found. Using defaults.")
199 elif reason[:11] == "misbehaving":
200 debug(job.execution_name, "is "+reason)
203 # try to execute check() on every job
204 for job in self.jobs:
207 self._stop(job, "failed check")
208 except AttributeError:
209 self._stop(job, "no check")
210 except (UnboundLocalError, Exception) as e:
211 self._stop(job, "misbehaving. Reason: " + str(e))
214 # try to execute create() on every job
215 for job in self.jobs:
218 self._stop(job, "failed create")
220 chart = job.execution_name
222 "CHART netdata.plugin_pythond_" +
224 " '' 'Execution time for " +
226 " plugin' 'milliseconds / run' python.d netdata.plugin_python area 145000 " +
227 str(job.timetable['freq']) +
229 sys.stdout.write("DIMENSION run_time 'run time' absolute 1 1\n\n")
231 except AttributeError:
232 self._stop(job, "no create")
233 except (UnboundLocalError, Exception) as e:
234 self._stop(job, "misbehaving. Reason: " + str(e))
236 def _update_job(self, job):
237 # try to execute update() on every job and draw run time graph
238 t_start = time.time()
239 # check if it is time to execute job update() function
240 if job.timetable['next'] > t_start:
246 since_last = int((t_start - job.timetable['last']) * 1000000)
247 if not job.update(since_last):
248 self._stop(job, "update failed")
250 except AttributeError:
251 self._stop(job, "no update")
253 except (UnboundLocalError, Exception) as e:
254 self._stop(job, "misbehaving. Reason: " + str(e))
257 job.timetable['next'] = t_end - (t_end % job.timetable['freq']) + job.timetable['freq']
258 # draw performance graph
262 dt = int((t_end - job.timetable['last']) * 1000000)
263 sys.stdout.write("BEGIN netdata.plugin_pythond_"+job.execution_name+" "+str(since_last)+'\n')
264 sys.stdout.write("SET run_time = " + str(int((t_end - t_start) * 1000)) + '\n')
265 sys.stdout.write("END\n")
267 job.timetable['last'] = t_start
268 self.first_run = False
271 # run updates (this will stay forever and ever and ever forever and ever it'll be the one...)
272 self.first_run = True
274 t_begin = time.time()
276 for job in self.jobs:
277 self._update_job(job)
279 next_runs.append(job.timetable['next'])
282 if len(next_runs) == 0:
283 debug("No plugins loaded")
284 sys.stdout.write("DISABLE\n")
286 time.sleep(min(next_runs) - time.time())
289 def read_config(path):
291 with open(path, 'r') as stream:
292 config = yaml.load(stream)
293 except IsADirectoryError:
294 debug(str(path), "is a directory")
296 except yaml.YAMLError as e:
297 debug(str(path), "is malformed:", e)
305 sys.stderr.write(PROGRAM + ":")
307 sys.stderr.write(" " + str(i))
308 sys.stderr.write("\n")
312 def parse_cmdline(directory, *commands):
313 # TODO number -> interval
319 for cmd in commands[1:]:
322 elif cmd == "debug" or cmd == "all":
324 # redirect stderr to stdout?
325 elif os.path.isfile(directory + cmd + ".chart.py") or os.path.isfile(directory + cmd):
327 mods.append(cmd.replace(".chart.py", ""))
334 debug("started from", commands[0], "with options:", *commands[1:])
335 if len(mods) == 0 and DEBUG_FLAG is False:
338 return {'interval': interval,
342 # if __name__ == '__main__':
344 global DEBUG_FLAG, PROGRAM
346 PROGRAM = sys.argv[0].split('/')[-1].split('.plugin')[0]
347 # parse env variables
348 # https://github.com/firehol/netdata/wiki/External-Plugins#environment-variables
349 main_dir = os.getenv('NETDATA_PLUGINS_DIR',
350 os.path.abspath(__file__).strip("python.d.plugin.py"))
351 config_dir = os.getenv('NETDATA_CONFIG_DIR', "/etc/netdata/")
352 interval = os.getenv('NETDATA_UPDATE_EVERY', None)
354 # read configuration file
356 if config_dir[-1] != '/':
358 configfile = config_dir + "python.d.conf"
361 conf = read_config(configfile)
363 if str(conf['enable']) is False:
364 debug("disabled in configuration file")
365 sys.stdout.write("DISABLE\n")
367 except (KeyError, TypeError):
370 modules_conf = conf['plugins_config_dir']
372 modules_conf = config_dir + "python.d/" # default configuration directory
374 modules_dir = conf['plugins_dir']
376 modules_dir = main_dir.replace("plugins.d", "python.d")
378 interval = conf['interval']
380 pass # use default interval from NETDATA_UPDATE_EVERY
382 DEBUG_FLAG = conf['debug']
385 for k, v in conf.items():
386 if k in ("plugins_config_dir", "plugins_dir", "interval", "debug"):
390 except FileNotFoundError:
391 modules_conf = config_dir
392 modules_dir = main_dir.replace("plugins.d", "python.d")
394 # directories should end with '/'
395 if modules_dir[-1] != '/':
397 if modules_conf[-1] != '/':
400 # parse passed command line arguments
401 out = parse_cmdline(modules_dir, *sys.argv)
402 modules = out['modules']
403 if out['interval'] is not None:
404 interval = out['interval']
406 # configure environement to run modules
407 sys.path.append(modules_dir+"python_modules") # append path to directory with modules dependencies
410 charts = PythonCharts(interval, modules, modules_dir, modules_conf, disabled)
415 if __name__ == '__main__':