]> arthur.barton.de Git - netdata.git/commitdiff
Merge remote-tracking branch 'upstream/master'
authorpaulfantom <paulfantom@gmail.com>
Mon, 13 Jun 2016 21:34:32 +0000 (23:34 +0200)
committerpaulfantom <paulfantom@gmail.com>
Mon, 13 Jun 2016 21:34:32 +0000 (23:34 +0200)
conf.d/Makefile.am
conf.d/python.d.conf [new file with mode: 0644]
conf.d/python.d/example.conf [new file with mode: 0644]
plugins.d/python.d.plugin
python.d/Makefile.am
python.d/python_modules/__init__.py [new file with mode: 0755]

index 381b546e3642adf0cc0675e94fe8aa513dcae586..c8c518fabd244c3edf95b5639afb5612a6e0d1a1 100644 (file)
@@ -6,4 +6,20 @@ MAINTAINERCLEANFILES= $(srcdir)/Makefile.in
 dist_config_DATA = \
        apps_groups.conf \
        charts.d.conf \
+       python.d.conf \
        $(NULL)
+
+chartsconfigdir=$(configdir)/charts.d
+dist_chartsconfig_DATA = \
+       $(NULL)
+
+nodeconfigdir=$(configdir)/node.d
+dist_nodeconfig_DATA = \
+       $(NULL)
+
+pythonconfigdir=$(configdir)/python.d
+dist_pythonconfig_DATA = \
+       python.d/example.conf
+       $(NULL)
+
+
diff --git a/conf.d/python.d.conf b/conf.d/python.d.conf
new file mode 100644 (file)
index 0000000..4803e89
--- /dev/null
@@ -0,0 +1,11 @@
+# This is the configuration for python.d.plugin
+
+# It sets custom configuration directory for python modules
+# plugins_config_dir=
+
+# This is a directory with python modules
+# plugins_dir=
+
+# By default python.d.plugin enables all modules stored in python.d
+# Modules can be disabled with setting "module_name = no"
+example=no
diff --git a/conf.d/python.d/example.conf b/conf.d/python.d/example.conf
new file mode 100644 (file)
index 0000000..a8f26d2
--- /dev/null
@@ -0,0 +1 @@
+update_every=2
index 5e720a904b233713395fdff6d780546392bb8e45..bb7468d8f6648fdd1fba6a643d038a76ace3a5d3 100755 (executable)
@@ -3,8 +3,6 @@
 import os
 import sys
 import time
-#import configparser
-#import importlib.machinery
 try:
     assert sys.version_info >= (3,1)
     import configparser
@@ -21,40 +19,28 @@ class PythonCharts(object):
                  interval=None,
                  modules=[],
                  modules_path='../python.d/',
-                 modules_configs='../conf.d/'):
+                 modules_configs='../conf.d/',
+                 modules_disabled=[]):
         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)
+        # set configuration directory
         self.configs = modules_configs
 
-        self.modules = []
-        if len(modules) > 0:
-            for m in modules:
-                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.exit(1)
-
-        self.load_configs()
+        # load modules
+        self.modules = self._load_modules(modules_path,modules)
+        
+        # check if loaded modules are on disabled modules list
+        for mod in self.modules:
+            if mod.__name__ in modules_disabled:
+                self.modules.remove(mod)
+        
+        # load configuration files
+        self._load_configs()
 
         # set timetable dict (last execution, next execution and frequency)
         # set priorities
         self.timetable = {}
+        freq = 1
         for m in self.modules:
             try:
                 m.priority = int(m.priority)
@@ -63,16 +49,17 @@ class PythonCharts(object):
              
             if interval is None:
                 try:
-                    interval = int(m.update_every)
+                    freq = int(m.update_every)
                 except (AttributeError, ValueError):
-                    interval = 1
+                    freq = 1
             
             now = time.time()
             self.timetable[m.__name__] = {'last' : now,
-                                          'next' : now - (now % interval) + interval,
-                                          'freq' : interval}
+                                          'next' : now - (now % freq) + freq,
+                                          'freq' : freq}
 
-    def import_plugin(self, path, name=None):
+    def _import_plugin(self, path, name=None):
+    # try to import module using only its path
         if name is None:
             name = path.split('/')[-1]
             if name[-9:] != ".chart.py":
@@ -84,15 +71,32 @@ class PythonCharts(object):
             debug(str(e))
             return None
 
-    def load_modules(self, path):
-        names = os.listdir(path)
-        for mod in names:
-            m = self.import_plugin(path + mod)
-            if m is not None:
-                debug("loading chart: '" + path + mod + "'")
-                self.modules.append(m)
+    def _load_modules(self, path, modules):
+        # check if plugin directory exists
+        if not os.path.isdir(path):
+            debug("cannot find charts directory ", path)
+            sys.stdout.write("DISABLE\n")
+            sys.exit(1)
 
-    def load_configs(self):
+        # load modules
+        loaded = []
+        if len(modules) > 0:
+            for m in modules:
+                mod = self._import_plugin(path + m + ".chart.py")
+                if mod is not None:
+                    loaded.append(mod)
+        else:
+            # scan directory specified in path and load all modules from there
+            names = os.listdir(path)
+            for mod in names:
+                m = self._import_plugin(path + mod)
+                if m is not None:
+                    debug("loading chart: '" + path + mod + "'")
+                    loaded.append(m)
+        return loaded
+
+    def _load_configs(self):
+    # function modifies every loaded module in self.modules
         for m in self.modules:
             configfile = self.configs + m.__name__ + ".conf"
             if os.path.isfile(configfile):
@@ -105,7 +109,8 @@ class PythonCharts(object):
                       configfile +
                       "' not found. Using defaults.")
 
-    def disable_module(self, mod, reason=None):
+    def _disable_module(self, mod, reason=None):
+    # modifies self.modules
         self.modules.remove(mod)
         del self.timetable[mod.__name__]
         if reason is None:
@@ -131,20 +136,22 @@ class PythonCharts(object):
             debug(mod.__name__, "is "+reason)
 
     def check(self):
+    # try to execute check() on every loaded module
         for mod in self.modules:
             try:
                 if not mod.check():
-                    self.disable_module(mod, "failed check")
+                    self._disable_module(mod, "failed check")
             except AttributeError:
-                self.disable_module(mod, "no check")
+                self._disable_module(mod, "no check")
             except (UnboundLocalError, Exception) as e:
-                self.disable_module(mod, "misbehaving. Reason: " + str(e))
+                self._disable_module(mod, "misbehaving. Reason: " + str(e))
 
     def create(self):
+    # try to execute create() on every loaded module
         for mod in self.modules:
             try:
                 if not mod.create():
-                    self.disable_module(mod, "failed create")
+                    self._disable_module(mod, "failed create")
                 else:
                     chart = mod.__name__
                     sys.stdout.write(
@@ -158,11 +165,12 @@ class PythonCharts(object):
                     sys.stdout.write("DIMENSION run_time 'run time' absolute 1 1\n\n")
                     sys.stdout.flush()
             except AttributeError:
-                self.disable_module(mod, "no create")
+                self._disable_module(mod, "no create")
             except (UnboundLocalError, Exception) as e:
-                self.disable_module(mod, "misbehaving. Reason: " + str(e))
+                self._disable_module(mod, "misbehaving. Reason: " + str(e))
 
-    def update_module(self, mod):
+    def _update_module(self, mod):
+    # try to execute update() on every module and draw run time graph 
         t_start = time.time()
         # check if it is time to execute module update() function
         if self.timetable[mod.__name__]['next'] > t_start:
@@ -173,16 +181,17 @@ class PythonCharts(object):
             else:
                 since_last = int((t_start - self.timetable[mod.__name__]['last']) * 1000000)
             if not mod.update(since_last):
-                self.disable_module(mod, "update failed")
+                self._disable_module(mod, "update failed")
                 return
         except AttributeError:
-            self.disable_module(mod, "no update")
+            self._disable_module(mod, "no update")
             return
         except (UnboundLocalError, Exception) as e:
-            self.disable_module(mod, "misbehaving. Reason: " + str(e))
+            self._disable_module(mod, "misbehaving. Reason: " + str(e))
             return
         t_end = time.time()
         self.timetable[mod.__name__]['next'] = t_end - (t_end % self.timetable[mod.__name__]['freq']) + self.timetable[mod.__name__]['freq']
+        # draw performance graph
         if self.first_run:
             dt = 0
         else:
@@ -195,12 +204,13 @@ class PythonCharts(object):
         self.first_run = False
 
     def update(self):
+    # run updates (this will stay forever and ever and ever forever and ever it'll be the one...)
         self.first_run = True
         while True:
             t_begin = time.time()
             next_runs = []
             for mod in self.modules:
-                self.update_module(mod)
+                self._update_module(mod)
                 try:
                     next_runs.append(self.timetable[mod.__name__]['next'])
                 except KeyError:
@@ -277,9 +287,10 @@ def run():
     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/")
-    interval = int(os.getenv('NETDATA_UPDATE_EVERY', 1))
+    interval = os.getenv('NETDATA_UPDATE_EVERY', None)
 
     # read configuration file
+    disabled = []
     if config_dir[-1] != '/':
         configfile = config_dir + '/' + "python.d.conf"
     else:
@@ -296,20 +307,25 @@ def run():
             pass
         try:
             modules_conf = conf['plugins_config_dir']
-        except (KeyError, TypeError):
-            modules_conf = config_dir  # default configuration directory
+        except (KeyError):
+            modules_conf = config_dir + "python.d/"  # default configuration directory
         try:
             modules_dir = conf['plugins_dir']
-        except (KeyError, TypeError):
+        except (KeyError):
             modules_dir = main_dir.replace("plugins.d", "python.d")
         try:
             interval = int(conf['interval'])
         except (KeyError, TypeError):
-            pass  # default interval
+            pass  # use default interval from NETDATA_UPDATE_EVERY
         try:
             DEBUG_FLAG = bool(conf['debug'])
         except (KeyError, TypeError):
             pass
+        for k, v in conf.items():
+            if k in ("plugins_config_dir", "plugins_dir", "interval", "debug"):
+                continue
+            if v == 'no':
+                disabled.append(k)
     except FileNotFoundError:
         modules_conf = config_dir
         modules_dir = main_dir.replace("plugins.d", "python.d")
@@ -323,15 +339,14 @@ def run():
     # parse passed command line arguments
     out = parse_cmdline(modules_dir, *sys.argv)
     modules = out['modules']
-#    if out['interval'] is not None:
-#        interval = out['interval']
-
+    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
-
+    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(out['interval'], modules, modules_dir, modules_conf)
+    charts = PythonCharts(interval, modules, modules_dir, modules_conf, disabled)
     charts.check()
     charts.create()
     charts.update()
index f10b8e91adae27f3aec4dfd804d4447664183406..8c84254b8f93af846b8367939938e9d56b782add 100644 (file)
@@ -7,3 +7,7 @@ dist_python_SCRIPTS = \
 dist_python_DATA = \
        README.md \
        $(NULL)
+
+pythonmodulesdir=$(pythondir)/python_modules
+dist_pythonmodules_DATA = \
+       $(NULL)
diff --git a/python.d/python_modules/__init__.py b/python.d/python_modules/__init__.py
new file mode 100755 (executable)
index 0000000..8d1c8b6
--- /dev/null
@@ -0,0 +1 @@