]> arthur.barton.de Git - netdata.git/blobdiff - python.d/mysql.chart.py
Merge branch 'objects'
[netdata.git] / python.d / mysql.chart.py
index f962beea2e455052e7fdaa8b1658075541ab4bca..04ccd4c4637be4b0f530d46d78e3d995fad43488 100644 (file)
@@ -4,8 +4,6 @@
 NAME = "mysql.chart.py"
 import sys
 
-sys.path.append("./python_modules") #FIXME it is here for debug only
-
 # import 3rd party library to handle MySQL communication
 try:
     import MySQLdb
@@ -17,22 +15,23 @@ except ImportError:
         # https://github.com/PyMySQL/PyMySQL
         sys.stderr.write(NAME + ": using pymysql\n")
     except ImportError:
-        sys.stderr.write(NAME + ": You need to install PyMySQL module to use mysql.chart.py plugin\n")
+        sys.stderr.write(NAME + ": You need to install MySQLdb or PyMySQL module to use mysql.chart.py plugin\n")
+        raise ImportError
 
 from base import BaseService
 
 # default configuration (overriden by python.d.plugin)
 # FIXME change password
-config = [
-    {
-        'name'     : 'local',
+config = {
+    'local': {
         'user'     : 'root',
-        'password' : 'a',
+        'password' : '',
         'socket'   : '/var/run/mysqld/mysqld.sock',
         'update_every' : 3,
-        'retries'  : 4
+        'retries'  : 4,
+        'priority' : 100
     }
-]
+}
 
 # default module values (can be overridden per job in `config`)
 update_every = 3
@@ -334,16 +333,17 @@ CHARTS = {
 
 
 class Service(BaseService):
-    def __init__(self,configuration=None):
-        super().__init__(configuration)
+    def __init__(self,configuration=None,name=None):
+        super().__init__(configuration=configuration)
+        self.name = name
         self.configuration = self._parse_config(configuration)
         self.connection = None
         self.defs = {}
 
     def _parse_config(self,configuration):
         # parse configuration to collect data from mysql server
-        if 'name' not in configuration:
-            configuration['name'] = 'local'
+        if self.name is None:
+            self.name = 'local'
         if 'user' not in configuration:
             configuration['user'] = 'root'
         if 'password' not in configuration:
@@ -404,7 +404,6 @@ class Service(BaseService):
 
     def create(self):
         for name in ORDER:
-            print(name)
             self.defs[name] = []
             for line in CHARTS[name][1]:
                 self.defs[name].append(line[0])
@@ -413,7 +412,7 @@ class Service(BaseService):
         data = self._get_data()
         for name in ORDER:
             header = "CHART mysql_" + \
-                     str(self.configuration['name']) + "." + \
+                     str(self.name) + "." + \
                      name + " " + \
                      CHARTS[name][0] + " " + \
                      str(self.priority + idx) + " " + \
@@ -441,7 +440,7 @@ class Service(BaseService):
         except Exception:
             pass
         for chart, dimensions in self.defs.items():
-            header = "BEGIN mysql_" + str(self.configuration['name']) + "." + chart + " " + str(interval) + '\n'
+            header = "BEGIN mysql_" + str(self.name) + "." + chart + " " + str(interval) + '\n'
             lines = ""
             for d in dimensions:
                 try:
@@ -452,10 +451,3 @@ class Service(BaseService):
                 print(header + lines + "END")
         
         return True
-
-#FIXME debug only:
-if __name__ == "__main__":
-    my = Service(config[0])
-    my.check()
-    my.create()
-    my.update(1)