]> arthur.barton.de Git - netdata.git/blobdiff - plugins.d/python.d.plugin
bundle pyyaml
[netdata.git] / plugins.d / python.d.plugin
index 74e19063a87a0ff13ee5e8e10393beebe73175ce..45c85c804716613a528938e351a99a6134143e51 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/env bash
-'''':; exec "$(command -v python2 || command -v python3 || echo "ERROR python IS NOT AVAILABLE IN THIS SYSTEM")" "$0" "$@" # '''
+'''':; exec "$(command -v python || command -v python3 || command -v python2 || echo "ERROR python IS NOT AVAILABLE IN THIS SYSTEM")" "$0" "$@" # '''
 # -*- coding: utf-8 -*-
 
 # Description: netdata python modules supervisor
@@ -37,11 +37,10 @@ import msg
 try:
     assert sys.version_info >= (3, 1)
     import importlib.machinery
-
+    PY_VERSION = 3
     # change this hack below if we want PY_VERSION to be used in modules
     # import builtins
     # builtins.PY_VERSION = 3
-    PY_VERSION = 3
     msg.info('Using python v3')
 except (AssertionError, ImportError):
     try:
@@ -54,8 +53,15 @@ except (AssertionError, ImportError):
         msg.info('Using python v2')
     except ImportError:
         msg.fatal('Cannot start. No importlib.machinery on python3 or lack of imp on python2')
+# try:
+#     import yaml
+# except ImportError:
+#     msg.fatal('Cannot find yaml library')
 try:
-    import yaml
+    if PY_VERSION == 3:
+        import pyyaml3 as yaml
+    else:
+        import pyyaml2 as yaml
 except ImportError:
     msg.fatal('Cannot find yaml library')
 
@@ -289,8 +295,9 @@ class PythonCharts(object):
             self.jobs.remove(job)
             msg.info("Disabled", prefix)
         except Exception as e:
-            msg.debug("This shouldn't happen. NO " + prefix + " IN LIST:" + str(self.jobs))
+            msg.debug("This shouldn't happen. NO " + prefix + " IN LIST:" + str(self.jobs) + " ERROR: " + str(e))
 
+        # TODO remove section below and remove `reason`.
         prefix += ": "
         if reason is None:
             return
@@ -320,13 +327,15 @@ class PythonCharts(object):
         """
         i = 0
         overridden = []
+        msg.debug("all job objects", str(self.jobs))
         while i < len(self.jobs):
             job = self.jobs[i]
             try:
                 if not job.check():
-                    self._stop(job, "failed check")
+                    msg.error(job.chart_name, "check function failed.")
+                    self._stop(job)
                 else:
-                    msg.debug(job.chart_name, "check succeeded")
+                    msg.debug(job.chart_name, "check succeeded")
                     i += 1
                     try:
                         if job.override_name is not None:
@@ -334,6 +343,7 @@ class PythonCharts(object):
                             if new_name in overridden:
                                 msg.error(job.chart_name + " already exists. Stopping '" + job.name + "'")
                                 self._stop(job)
+                                i -= 1
                             else:
                                 job.name = job.override_name
                                 msg.debug(job.chart_name + " changing chart name to: '" + new_name + "'")
@@ -342,9 +352,13 @@ class PythonCharts(object):
                     except Exception:
                         pass
             except AttributeError:
-                self._stop(job, "no check")
+                self._stop(job)
+                msg.error(job.chart_name, "cannot find check() function.")
             except (UnboundLocalError, Exception) as e:
-                self._stop(job, "misbehaving. Reason:" + str(e))
+                msg.error(job.chart_name, str(e))
+                self._stop(job)
+        msg.debug("overridden job names:", str(overridden))
+        msg.debug("all remaining job objects:", str(self.jobs))
 
     def create(self):
         """
@@ -358,7 +372,8 @@ class PythonCharts(object):
             job = self.jobs[i]
             try:
                 if not job.create():
-                    self._stop(job, "failed create")
+                    msg.error(job.chart_name, "create function failed.")
+                    self._stop(job)
                 else:
                     chart = job.chart_name
                     sys.stdout.write(
@@ -374,9 +389,11 @@ class PythonCharts(object):
                     # sys.stdout.flush()
                     i += 1
             except AttributeError:
-                self._stop(job, "no create")
+                msg.error(job.chart_name, "cannot find create() function.")
+                self._stop(job)
             except (UnboundLocalError, Exception) as e:
-                self._stop(job, "misbehaving. Reason: " + str(e))
+                msg.error(job.chart_name, str(e))
+                self._stop(job)
 
     def update(self):
         """