]> arthur.barton.de Git - netdata.git/blobdiff - plugins.d/python.d.plugin
notify user when sth wrong in update()
[netdata.git] / plugins.d / python.d.plugin
index 66bb239b92531800249ca387d403f3b570688dd9..167e6e3db2943320d3c373cad54f8f0a7a958da6 100755 (executable)
@@ -3,49 +3,74 @@
 import os
 import sys
 import time
-import importlib.machinery
-import configparser
+#import configparser
+#import importlib.machinery
+try:
+    assert sys.version_info >= (3,1)
+    import configparser
+    import importlib.machinery
+except AssertionError:
+    sys.stderr.write('Not supported python version. Needed python >= 3.1\n')
+    sys.stdout.write('DISABLE\n')
+    sys.exit(1)
 
 
 class PythonCharts(object):
-
+    
     def __init__(self,
-                 interval=1,
+                 interval=None,
                  modules=[],
                  modules_path='../python.d/',
                  modules_configs='../conf.d/'):
-        self.interval = interval
+        self.first_run = True
+        if interval is None:
+            interval = 1
+        self.default_priority = 90000
         # check if plugin directory exists
         if not os.path.isdir(modules_path):
             debug("cannot find charts directory ", modules_path)
+            sys.stdout.write("DISABLE\n")
             sys.exit(1)
         self.configs = modules_configs
 
         self.modules = []
         if len(modules) > 0:
             for m in modules:
-                self.modules.append(
-                    self.import_plugin(
-                        modules_path + m + ".chart.py"))
+                mod = self.import_plugin(modules_path + m + ".chart.py")
+                if mod is not None:
+                    self.modules.append(mod)
+            if len(self.modules) == 0:
+                debug("cannot find provided module(s)", modules_path)
+                sys.stdout.write("DISABLE\n")
+                sys.exit(1)
         else:
             self.load_modules(modules_path)
             if len(self.modules) == 0:
                 debug("cannot find modules directory", modules_path)
-                sys.stdout.write("DISABLE\n")
+                sys.stdout.write("DISABLE\n")
                 sys.exit(1)
 
         self.load_configs()
 
-        # set last execution and execution frequency dict
+        # set timetable dict (last execution, next execution and frequency)
+        # set priorities
         self.timetable = {}
-        #last_exec = time.time()
-        last_exec = 0
         for m in self.modules:
             try:
-                interval = m.update_every
-            except AttributeError:
-                interval = self.interval
-            self.timetable[m.__name__] = [last_exec, int(interval)]
+                m.priority = int(m.priority)
+            except (AttributeError,ValueError):
+                m.priority = self.default_priority
+             
+            if interval is None:
+                try:
+                    interval = int(m.update_every)
+                except (AttributeError, ValueError):
+                    interval = 1
+            
+            now = time.time()
+            self.timetable[m.__name__] = {'last' : now,
+                                          'next' : now - (now % interval) + interval,
+                                          'freq' : interval}
 
     def import_plugin(self, path, name=None):
         if name is None:
@@ -75,11 +100,10 @@ class PythonCharts(object):
                 for k, v in read_config(configfile).items():
                     setattr(m, k, v)
             else:
-                debug(
-                    m.__name__ +
-                    ": configuration file '" +
-                    configfile +
-                    "' not found. Using defaults.")
+                debug(m.__name__ +
+                      ": configuration file '" +
+                      configfile +
+                      "' not found. Using defaults.")
 
     def disable_module(self, mod, reason=None):
         self.modules.remove(mod)
@@ -87,28 +111,24 @@ class PythonCharts(object):
         if reason is None:
             return
         elif reason[:3] == "no ":
-            debug(
-                "chart '" +
-                mod.__name__,
-                "' does not seem to have " +
-                reason[3:] +
-                "() function. Disabling it.")
+            debug("chart '" +
+                  mod.__name__,
+                  "' does not seem to have " +
+                  reason[3:] +
+                  "() function. Disabling it.")
         elif reason[:7] == "failed ":
-            debug(
-                "chart '" +
-                mod.__name__ +
-                reason[3:] +
-                "() function. reports failure.")
+            debug("chart '" +
+                  mod.__name__ +
+                  reason[3:] +
+                  "() function. reports failure.")
         elif reason[:13] == "configuration":
-            debug(
-                mod.__name__,
-                "configuration file '" +
-                self.configs +
-                mod.__name__ +
-                ".conf' not found. Using defaults.")
+            debug(mod.__name__,
+                  "configuration file '" +
+                  self.configs +
+                  mod.__name__ +
+                  ".conf' not found. Using defaults.")
         elif reason[:11] == "misbehaving":
-            debug(mod.__name__, "is misbeaving. Disabling it")
-        # sys.stdout.write('DISABLE')
+            debug(mod.__name__, "is "+reason)
 
     def check(self):
         for mod in self.modules:
@@ -127,17 +147,15 @@ class PythonCharts(object):
                     self.disable_module(mod, "failed create")
                 else:
                     chart = mod.__name__
-                    # sys.stdout.write("CHART netdata.plugin_pythond_"+chart+" '' 'Execution time for "+chart+" plugin' 'milliseconds / run' python.d netdata.plugin_python area 145000 "+str(self.interval)+'\n')
                     sys.stdout.write(
                         "CHART netdata.plugin_pythond_" +
                         chart +
                         " '' 'Execution time for " +
                         chart +
                         " plugin' 'milliseconds / run' python.d netdata.plugin_python area 145000 " +
-                        str(self.timetable[mod.__name__][1]) +
+                        str(self.timetable[mod.__name__]['freq']) +
                         '\n')
-                    sys.stdout.write(
-                        "DIMENSION run_time 'run time' absolute 1 1\n\n")
+                    sys.stdout.write("DIMENSION run_time 'run time' absolute 1 1\n\n")
                     sys.stdout.flush()
             except AttributeError:
                 self.disable_module(mod, "no create")
@@ -145,46 +163,56 @@ class PythonCharts(object):
                 self.disable_module(mod, "misbehaving")
 
     def update_module(self, mod):
-        t1 = time.time()
+        t_start = time.time()
         # check if it is time to execute module update() function
-        if (t1 - self.timetable[mod.__name__][0]) < self.timetable[mod.__name__][1]:
+        if self.timetable[mod.__name__]['next'] > t_start:
             return
         try:
-            #            t=int((t1- self.timetable[mod.__name__][0]) * 1000)
-            #            if t == 0: t=""
-            t = ""  # FIXME
-            if not mod.update(t):
-                # if not mod.update(int((t1- self.timetable[mod.__name__][0]) *
-                # 1000)):
+            if self.first_run:
+                since_last = 0
+            else:
+                since_last = int((t_start - self.timetable[mod.__name__]['last']) * 1000000)
+            if not mod.update(since_last):
                 self.disable_module(mod, "update failed")
                 return
         except AttributeError:
             self.disable_module(mod, "no update")
             return
-        except UnboundLocalError:
-            self.disable_module(mod, "misbehaving")
+        except (UnboundLocalError, Exception) as e:
+            self.disable_module(mod, "misbehaving. Reason: " + str(e))
             return
-        t2 = time.time()
-#        dt = int((t2 - self.timetable[mod.__name__][0]) * 1000)
-#        if dt == 0: dt=""
-        # dt="" #FIXME
-        #sys.stdout.write("BEGIN netdata.plugin_pythond_"+mod.__name__+" "+str(dt)+'\n')
-        sys.stdout.write("BEGIN netdata.plugin_pythond_" + mod.__name__ + "\n")
-        sys.stdout.write("SET run_time = " + str(int((t2 - t1) * 1000)) + '\n')
+        t_end = time.time()
+        self.timetable[mod.__name__]['next'] = t_end - (t_end % self.timetable[mod.__name__]['freq']) + self.timetable[mod.__name__]['freq']
+        if self.first_run:
+            dt = 0
+        else:
+            dt = int((t_end - self.timetable[mod.__name__]['last']) * 1000000)
+        sys.stdout.write("BEGIN netdata.plugin_pythond_"+mod.__name__+" "+str(since_last)+'\n')
+        sys.stdout.write("SET run_time = " + str(int((t_end - t_start) * 1000)) + '\n')
         sys.stdout.write("END\n")
         sys.stdout.flush()
-        self.timetable[mod.__name__][0] = t1
+        self.timetable[mod.__name__]['last'] = t_start
+        self.first_run = False
 
     def update(self):
+        self.first_run = True
         while True:
             t_begin = time.time()
+            next_runs = []
             for mod in self.modules:
                 self.update_module(mod)
-            time.sleep((self.interval - (time.time() - t_begin)) / 2)
+                try:
+                    next_runs.append(self.timetable[mod.__name__]['next'])
+                except KeyError:
+                    pass
+            if len(next_runs) == 0:
+                debug("No plugins loaded")
+                sys.stdout.write("DISABLE\n")
+                sys.exit(1)
+            time.sleep(min(next_runs) - time.time())
 
 
 def read_config(path):
-    # TODO make it bulletproof
     config = configparser.ConfigParser()
     config_str = ""
     try:
@@ -208,8 +236,10 @@ def debug(*args):
 
 
 def parse_cmdline(directory, *commands):
-    global DEBUG_FLAG, PROGRAM
+    # TODO number -> interval
+    global DEBUG_FLAG
     DEBUG_FLAG = False
+    interval = None
 
     mods = []
     for cmd in commands[1:]:
@@ -221,21 +251,28 @@ def parse_cmdline(directory, *commands):
         elif os.path.isfile(directory + cmd + ".chart.py") or os.path.isfile(directory + cmd):
             DEBUG_FLAG = True
             mods.append(cmd.replace(".chart.py", ""))
+        else:
+            try:
+                interval = int(cmd)
+            except ValueError:
+                pass
 
-    PROGRAM = commands[0].split('/')[-1].split('.plugin')[0]
     debug("started from", commands[0], "with options:", *commands[1:])
 
-    return mods
+    return {'interval': interval,
+            'modules': mods}
 
 
 # if __name__ == '__main__':
 def run():
+    global DEBUG_FLAG, PROGRAM
+    DEBUG_FLAG = True
+    PROGRAM = sys.argv[0].split('/')[-1].split('.plugin')[0]
     # parse env variables
     # https://github.com/firehol/netdata/wiki/External-Plugins#environment-variables
     main_dir = os.getenv('NETDATA_PLUGINS_DIR',
                          os.path.abspath(__file__).strip("python.d.plugin.py"))
     config_dir = os.getenv('NETDATA_CONFIG_DIR', "/etc/netdata/")
-  #  logs_dir = os.getenv('NETDATA_LOGS_DIR',None) #TODO logging?
     interval = int(os.getenv('NETDATA_UPDATE_EVERY', 1))
 
     # read configuration file
@@ -249,6 +286,7 @@ def run():
         try:
             if str(conf['enable']) == "no":
                 debug("disabled in configuration file")
+                sys.stdout.write("DISABLE\n")
                 sys.exit(1)
         except (KeyError, TypeError):
             pass
@@ -264,7 +302,6 @@ def run():
             interval = int(conf['interval'])
         except (KeyError, TypeError):
             pass  # default interval
-        global DEBUG_FLAG
         try:
             DEBUG_FLAG = bool(conf['debug'])
         except (KeyError, TypeError):
@@ -280,10 +317,17 @@ def run():
         modules_conf += "/"
 
     # parse passed command line arguments
-    modules = parse_cmdline(modules_dir, *sys.argv)
+    out = parse_cmdline(modules_dir, *sys.argv)
+    modules = out['modules']
+#    if out['interval'] is not None:
+#        interval = out['interval']
+
+    # configure environement to run modules
+    #sys.path.append(modules_dir+"python_modules") # append path to directory with modules dependencies
 
     # run plugins
-    charts = PythonCharts(interval, modules, modules_dir, modules_conf)
+#    charts = PythonCharts(interval, modules, modules_dir, modules_conf)
+    charts = PythonCharts(out['interval'], modules, modules_dir, modules_conf)
     charts.check()
     charts.create()
     charts.update()