]> arthur.barton.de Git - netdata.git/blob - plugins.d/python.d.plugin
better handling of disabled modules
[netdata.git] / plugins.d / python.d.plugin
1 #!/usr/bin/env python3
2
3 import os
4 import sys
5 import time
6 try:
7     assert sys.version_info >= (3,1)
8     import importlib.machinery
9 except AssertionError:
10     sys.stderr.write('python.d.plugin: Not supported python version. Needed python >= 3.1\n')
11     sys.stdout.write('DISABLE\n')
12     sys.exit(1)
13 try:
14     import yaml
15 except ImportError:
16     sys.stderr.write('python.d.plugin: Cannot find yaml library\n')
17     sys.stdout.write('DISABLE\n')
18     sys.exit(1)
19
20 MODULE_EXTENSION = ".chart.py"
21 BASE_CONFIG = {'update_every' : 10,
22                'priority': 12345,
23                'retries' : 0}
24
25
26 class PythonCharts(object):
27     
28     def __init__(self,
29                  interval=None,
30                  modules=[],
31                  modules_path='../python.d/',
32                  modules_configs='../conf.d/',
33                  modules_disabled=[]):
34         self.first_run = True
35         # set configuration directory
36         self.configs = modules_configs
37
38         # load modules
39         loaded_modules = self._load_modules(modules_path,modules, modules_disabled)
40
41         # load configuration files
42         configured_modules = self._load_configs(loaded_modules)
43
44         # good economy and prosperity:
45         self.jobs = self._create_jobs(configured_modules)
46         if DEBUG_FLAG and interval is not None:
47             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
57         jobs = []
58         for module in modules:
59             for name in module.config:
60                 # register a new job
61                 conf = module.config[name]
62                 try:
63                     job = module.Service(configuration=conf, name=name)
64                 except Exception as e:
65                     debug(module.__name__ +
66                           ": Couldn't start job named " +
67                           str(name) + 
68                           ": " +
69                           str(e))
70                     return None
71                 else:
72                     # set execution_name (needed to plot run time graphs)
73                     job.execution_name = module.__name__
74                     if name is not None:
75                         job.execution_name += "_" + name
76                 jobs.append(job)
77         
78         return [j for j in jobs if j is not None]
79
80     def _import_module(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[-len(MODULE_EXTENSION):] != MODULE_EXTENSION:
85                 return None
86             name = name[:-len(MODULE_EXTENSION)]
87         try:
88             return importlib.machinery.SourceFileLoader(name, path).load_module()
89         except Exception as e:
90             debug(str(e))
91             return None
92
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")
98             sys.exit(1)
99
100         # load modules
101         loaded = []
102         if len(modules) > 0:
103             for m in modules:
104                 if m in disabled:
105                     continue
106                 mod = self._import_module(path + m + MODULE_EXTENSION)
107                 if mod is not None:
108                     loaded.append(mod)
109         else:
110             # scan directory specified in path and load all modules from there
111             names = os.listdir(path)
112             for mod in names:
113                 if mod.strip(MODULE_EXTENSION) in disabled:
114                     debug("disabling:",mod.strip(MODULE_EXTENSION))
115                     continue
116                 m = self._import_module(path + mod)
117                 if m is not None:
118                     debug("loading chart: '" + path + mod + "'")
119                     loaded.append(m)
120         return loaded
121
122     def _load_configs(self,modules):
123     # function loads configuration files to modules
124         for mod in modules:
125             configfile = self.configs + mod.__name__ + ".conf"
126             if os.path.isfile(configfile):
127                 debug("loading chart options: '" + configfile + "'")
128                 try:
129                     setattr(mod,
130                             'config',
131                             self._parse_config(mod,read_config(configfile)))
132                 except Exception as e:
133                     debug("something went wrong while loading configuration",e)
134             else:
135                 debug(mod.__name__ +
136                       ": configuration file '" +
137                       configfile +
138                       "' not found. Using defaults.")
139         return modules
140
141     def _parse_config(self,module,config):
142         # get default values
143         defaults = {}
144         for key in BASE_CONFIG:
145             try:
146                 # get defaults from module config
147                 defaults[key] = int(config.pop(key))
148             except (KeyError,ValueError):
149                 try:
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]
155       
156         # check if there are dict in config dict
157         many_jobs = False
158         for name in config:
159             if type(config[name]) is dict:
160                 many_jobs = True
161                 break
162         
163         # assign variables needed by supervisor to every job configuration
164         if many_jobs:
165             for name in config:
166                 for key in defaults:
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)
170         else:
171             config = {None: config.copy()}
172             config[None].update(defaults)
173             
174         # return dictionary of jobs where every job has BASE_CONFIG variables
175         return config
176
177     def _stop(self, job, reason=None): #FIXME test if Service has __name__
178     # modifies self.jobs
179         self.jobs.remove(job)
180         if reason is None:
181             return
182         elif reason[:3] == "no ":
183             debug("chart '" +
184                   job.execution_name,
185                   "' does not seem to have " +
186                   reason[3:] +
187                   "() function. Disabling it.")
188         elif reason[:7] == "failed ":
189             debug("chart '" +
190                   job.execution_name + "' " +
191                   reason[7:] +
192                   "() function reports failure.")
193         elif reason[:13] == "configuration":
194             debug(job.execution_name,
195                   "configuration file '" +
196                   self.configs +
197                   job.execution_name +
198                   ".conf' not found. Using defaults.")
199         elif reason[:11] == "misbehaving":
200             debug(job.execution_name, "is "+reason)
201
202     def check(self):
203     # try to execute check() on every job
204         for job in self.jobs:
205             try:
206                 if not job.check():
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))
212
213     def create(self):
214     # try to execute create() on every job
215         for job in self.jobs:
216             try:
217                 if not job.create():
218                     self._stop(job, "failed create")
219                 else:
220                     chart = job.execution_name
221                     sys.stdout.write(
222                         "CHART netdata.plugin_pythond_" +
223                         chart +
224                         " '' 'Execution time for " +
225                         chart +
226                         " plugin' 'milliseconds / run' python.d netdata.plugin_python area 145000 " +
227                         str(job.timetable['freq']) +
228                         '\n')
229                     sys.stdout.write("DIMENSION run_time 'run time' absolute 1 1\n\n")
230                     sys.stdout.flush()
231             except AttributeError:
232                 self._stop(job, "no create")
233             except (UnboundLocalError, Exception) as e:
234                 self._stop(job, "misbehaving. Reason: " + str(e))
235
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:
241             return
242         try:
243             if self.first_run:
244                 since_last = 0
245             else:
246                 since_last = int((t_start - job.timetable['last']) * 1000000)
247             if not job.update(since_last):
248                 self._stop(job, "update failed")
249                 return
250         except AttributeError:
251             self._stop(job, "no update")
252             return
253         except (UnboundLocalError, Exception) as e:
254             self._stop(job, "misbehaving. Reason: " + str(e))
255             return
256         t_end = time.time()
257         job.timetable['next'] = t_end - (t_end % job.timetable['freq']) + job.timetable['freq']
258         # draw performance graph
259         if self.first_run:
260             dt = 0
261         else:
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")
266         sys.stdout.flush()
267         job.timetable['last'] = t_start
268         self.first_run = False
269
270     def update(self):
271     # run updates (this will stay forever and ever and ever forever and ever it'll be the one...)
272         self.first_run = True
273         while True:
274             t_begin = time.time()
275             next_runs = []
276             for job in self.jobs:
277                 self._update_job(job)
278                 try:
279                     next_runs.append(job.timetable['next'])
280                 except KeyError:
281                     pass
282             if len(next_runs) == 0:
283                 debug("No plugins loaded")
284                 sys.stdout.write("DISABLE\n")
285                 sys.exit(1)
286             time.sleep(min(next_runs) - time.time())
287
288
289 def read_config(path):
290     try:
291         with open(path, 'r') as stream:
292             config = yaml.load(stream)
293     except IsADirectoryError:
294         debug(str(path), "is a directory")
295         return None
296     except yaml.YAMLError as e:
297         debug(str(path), "is malformed:", e)
298         return None
299     return config
300
301
302 def debug(*args):
303     if not DEBUG_FLAG:
304         return
305     sys.stderr.write(PROGRAM + ":")
306     for i in args:
307         sys.stderr.write(" " + str(i))
308     sys.stderr.write("\n")
309     sys.stderr.flush()
310
311
312 def parse_cmdline(directory, *commands):
313     # TODO number -> interval
314     global DEBUG_FLAG
315     DEBUG_FLAG = False
316     interval = None
317
318     mods = []
319     for cmd in commands[1:]:
320         if cmd == "check":
321             pass
322         elif cmd == "debug" or cmd == "all":
323             DEBUG_FLAG = True
324             # redirect stderr to stdout?
325         elif os.path.isfile(directory + cmd + ".chart.py") or os.path.isfile(directory + cmd):
326             DEBUG_FLAG = True
327             mods.append(cmd.replace(".chart.py", ""))
328         else:
329             try:
330                 interval = int(cmd)
331             except ValueError:
332                 pass
333
334     debug("started from", commands[0], "with options:", *commands[1:])
335     if len(mods) == 0 and DEBUG_FLAG is False:
336         interval = None
337
338     return {'interval': interval,
339             'modules': mods}
340
341
342 # if __name__ == '__main__':
343 def run():
344     global DEBUG_FLAG, PROGRAM
345     DEBUG_FLAG = True
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)
353
354     # read configuration file
355     disabled = []
356     if config_dir[-1] != '/':
357         config_dir += '/'
358     configfile = config_dir + "python.d.conf"
359
360     try:
361         conf = read_config(configfile)
362         try:
363             if str(conf['enable']) is False:
364                 debug("disabled in configuration file")
365                 sys.stdout.write("DISABLE\n")
366                 sys.exit(1)
367         except (KeyError, TypeError):
368             pass
369         try:
370             modules_conf = conf['plugins_config_dir']
371         except KeyError:
372             modules_conf = config_dir + "python.d/"  # default configuration directory
373         try:
374             modules_dir = conf['plugins_dir']
375         except KeyError:
376             modules_dir = main_dir.replace("plugins.d", "python.d")
377         try:
378             interval = conf['interval']
379         except KeyError:
380             pass  # use default interval from NETDATA_UPDATE_EVERY
381         try:
382             DEBUG_FLAG = conf['debug']
383         except KeyError:
384             pass
385         for k, v in conf.items():
386             if k in ("plugins_config_dir", "plugins_dir", "interval", "debug"):
387                 continue
388             if v is False:
389                 disabled.append(k)
390     except FileNotFoundError:
391         modules_conf = config_dir
392         modules_dir = main_dir.replace("plugins.d", "python.d")
393
394     # directories should end with '/'
395     if modules_dir[-1] != '/':
396         modules_dir += "/"
397     if modules_conf[-1] != '/':
398         modules_conf += "/"
399
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']
405     
406     # configure environement to run modules
407     sys.path.append(modules_dir+"python_modules") # append path to directory with modules dependencies
408     
409     # run plugins
410     charts = PythonCharts(interval, modules, modules_dir, modules_conf, disabled)
411     charts.check()
412     charts.create()
413     charts.update()
414
415 if __name__ == '__main__':
416     run()