7 assert sys.version_info >= (3,1)
9 import importlib.machinery
10 except AssertionError:
11 sys.stderr.write('Not supported python version. Needed python >= 3.1\n')
12 sys.stdout.write('DISABLE\n')
16 class PythonCharts(object):
21 modules_path='../python.d/',
22 modules_configs='../conf.d/',
25 self.default_priority = 90000
26 # set configuration directory
27 self.configs = modules_configs
30 modules = self._load_modules(modules_path,modules)
32 # check if loaded modules are on disabled modules list
33 self.modules = [ m for m in modules if m.__name__ not in modules_disabled ]
35 # load configuration files
38 # set timetable dict (last execution, next execution and frequency)
42 for m in self.modules:
44 m.priority = int(m.priority)
45 except (AttributeError,ValueError):
46 m.priority = self.default_priority
50 freq = int(m.update_every)
51 except (AttributeError, ValueError):
55 self.timetable[m.__name__] = {'last' : now,
56 'next' : now - (now % freq) + freq,
59 def _import_plugin(self, path, name=None):
60 # try to import module using only its path
62 name = path.split('/')[-1]
63 if name[-9:] != ".chart.py":
67 return importlib.machinery.SourceFileLoader(name, path).load_module()
68 except Exception as e:
72 def _load_modules(self, path, modules):
73 # check if plugin directory exists
74 if not os.path.isdir(path):
75 debug("cannot find charts directory ", path)
76 sys.stdout.write("DISABLE\n")
83 mod = self._import_plugin(path + m + ".chart.py")
87 # scan directory specified in path and load all modules from there
88 names = os.listdir(path)
90 m = self._import_plugin(path + mod)
92 debug("loading chart: '" + path + mod + "'")
96 def _load_configs(self):
97 # function modifies every loaded module in self.modules
98 for m in self.modules:
99 configfile = self.configs + m.__name__ + ".conf"
100 if os.path.isfile(configfile):
101 debug("loading chart options: '" + configfile + "'")
102 for k, v in read_config(configfile).items():
105 except AttributeError:
106 self._disable_module(m,"misbehaving having bad configuration")
109 ": configuration file '" +
111 "' not found. Using defaults.")
113 def _disable_module(self, mod, reason=None):
114 # modifies self.modules
115 self.modules.remove(mod)
116 del self.timetable[mod.__name__]
119 elif reason[:3] == "no ":
122 "' does not seem to have " +
124 "() function. Disabling it.")
125 elif reason[:7] == "failed ":
127 mod.__name__ + "' " +
129 "() function reports failure.")
130 elif reason[:13] == "configuration":
132 "configuration file '" +
135 ".conf' not found. Using defaults.")
136 elif reason[:11] == "misbehaving":
137 debug(mod.__name__, "is "+reason)
140 # try to execute check() on every loaded module
141 for mod in self.modules:
144 self._disable_module(mod, "failed check")
145 except AttributeError:
146 self._disable_module(mod, "no check")
147 except (UnboundLocalError, Exception) as e:
148 self._disable_module(mod, "misbehaving. Reason: " + str(e))
151 # try to execute create() on every loaded module
152 for mod in self.modules:
155 self._disable_module(mod, "failed create")
159 "CHART netdata.plugin_pythond_" +
161 " '' 'Execution time for " +
163 " plugin' 'milliseconds / run' python.d netdata.plugin_python area 145000 " +
164 str(self.timetable[mod.__name__]['freq']) +
166 sys.stdout.write("DIMENSION run_time 'run time' absolute 1 1\n\n")
168 except AttributeError:
169 self._disable_module(mod, "no create")
170 except (UnboundLocalError, Exception) as e:
171 self._disable_module(mod, "misbehaving. Reason: " + str(e))
173 def _update_module(self, mod):
174 # try to execute update() on every module and draw run time graph
175 t_start = time.time()
176 # check if it is time to execute module update() function
177 if self.timetable[mod.__name__]['next'] > t_start:
183 since_last = int((t_start - self.timetable[mod.__name__]['last']) * 1000000)
184 if not mod.update(since_last):
185 self._disable_module(mod, "update failed")
187 except AttributeError:
188 self._disable_module(mod, "no update")
190 except (UnboundLocalError, Exception) as e:
191 self._disable_module(mod, "misbehaving. Reason: " + str(e))
194 self.timetable[mod.__name__]['next'] = t_end - (t_end % self.timetable[mod.__name__]['freq']) + self.timetable[mod.__name__]['freq']
195 # draw performance graph
199 dt = int((t_end - self.timetable[mod.__name__]['last']) * 1000000)
200 sys.stdout.write("BEGIN netdata.plugin_pythond_"+mod.__name__+" "+str(since_last)+'\n')
201 sys.stdout.write("SET run_time = " + str(int((t_end - t_start) * 1000)) + '\n')
202 sys.stdout.write("END\n")
204 self.timetable[mod.__name__]['last'] = t_start
205 self.first_run = False
208 # run updates (this will stay forever and ever and ever forever and ever it'll be the one...)
209 self.first_run = True
211 t_begin = time.time()
213 for mod in self.modules:
214 self._update_module(mod)
216 next_runs.append(self.timetable[mod.__name__]['next'])
219 if len(next_runs) == 0:
220 debug("No plugins loaded")
221 sys.stdout.write("DISABLE\n")
223 time.sleep(min(next_runs) - time.time())
226 def read_config(path):
227 config = configparser.ConfigParser()
230 with open(path, 'r', encoding="utf_8") as f:
231 config_str = '[config]\n' + f.read()
232 except IsADirectoryError:
233 debug(str(path), "is a directory")
236 config.read_string(config_str)
237 except configparser.ParsingError as e:
238 debug("Malformed configuration file: "+str(e))
240 return dict(config.items('config'))
246 sys.stderr.write(PROGRAM + ":")
248 sys.stderr.write(" " + str(i))
249 sys.stderr.write("\n")
253 def parse_cmdline(directory, *commands):
254 # TODO number -> interval
260 for cmd in commands[1:]:
263 elif cmd == "debug" or cmd == "all":
265 # redirect stderr to stdout?
266 elif os.path.isfile(directory + cmd + ".chart.py") or os.path.isfile(directory + cmd):
268 mods.append(cmd.replace(".chart.py", ""))
275 debug("started from", commands[0], "with options:", *commands[1:])
276 if len(mods) == 0 and DEBUG_FLAG is False:
279 return {'interval': interval,
283 # if __name__ == '__main__':
285 global DEBUG_FLAG, PROGRAM
287 PROGRAM = sys.argv[0].split('/')[-1].split('.plugin')[0]
288 # parse env variables
289 # https://github.com/firehol/netdata/wiki/External-Plugins#environment-variables
290 main_dir = os.getenv('NETDATA_PLUGINS_DIR',
291 os.path.abspath(__file__).strip("python.d.plugin.py"))
292 config_dir = os.getenv('NETDATA_CONFIG_DIR', "/etc/netdata/")
293 interval = os.getenv('NETDATA_UPDATE_EVERY', None)
295 # read configuration file
297 if config_dir[-1] != '/':
299 configfile = config_dir + "python.d.conf"
302 conf = read_config(configfile)
304 if str(conf['enable']) == "no":
305 debug("disabled in configuration file")
306 sys.stdout.write("DISABLE\n")
308 except (KeyError, TypeError):
311 modules_conf = conf['plugins_config_dir']
313 modules_conf = config_dir + "python.d/" # default configuration directory
315 modules_dir = conf['plugins_dir']
317 modules_dir = main_dir.replace("plugins.d", "python.d")
319 interval = int(conf['interval'])
320 except (KeyError, TypeError):
321 pass # use default interval from NETDATA_UPDATE_EVERY
323 DEBUG_FLAG = bool(conf['debug'])
324 except (KeyError, TypeError):
326 for k, v in conf.items():
327 if k in ("plugins_config_dir", "plugins_dir", "interval", "debug"):
331 except FileNotFoundError:
332 modules_conf = config_dir
333 modules_dir = main_dir.replace("plugins.d", "python.d")
335 # directories should end with '/'
336 if modules_dir[-1] != '/':
338 if modules_conf[-1] != '/':
341 # parse passed command line arguments
342 out = parse_cmdline(modules_dir, *sys.argv)
343 modules = out['modules']
344 if out['interval'] is not None:
345 interval = out['interval']
347 # configure environement to run modules
348 sys.path.append(modules_dir+"python_modules") # append path to directory with modules dependencies
351 charts = PythonCharts(interval, modules, modules_dir, modules_conf, disabled)
356 if __name__ == '__main__':