]> arthur.barton.de Git - netdata.git/commitdiff
service inheritance
authorpaulfantom <paulfantom@gmail.com>
Fri, 17 Jun 2016 12:20:45 +0000 (14:20 +0200)
committerpaulfantom <paulfantom@gmail.com>
Fri, 17 Jun 2016 12:20:45 +0000 (14:20 +0200)
python.d/mysql.chart.py
python.d/python_modules/base.py [new file with mode: 0644]

index 3513d886b2ba60cc602d98235be9545b96fa28ae..acbfba714e09ca83dab24b95716f54103f7ce090 100644 (file)
@@ -1,32 +1,38 @@
+# Description: MySQL netdata python.d plugin
+# Author: Pawel Krupa (paulfantom)
 
 NAME = "mysql.chart.py"
-from sys import stderr
+import sys
+
+# import 3rd party library to handle MySQL communication
 try:
     import MySQLdb
-    stderr.write(NAME + ": using MySQLdb")
     # https://github.com/PyMySQL/mysqlclient-python
+    sys.stderr.write(NAME + ": using MySQLdb")
 except ImportError:
     try:
         import pymysql as MySQLdb
         # https://github.com/PyMySQL/PyMySQL
-        stderr.write(NAME + ": using pymysql")
+        sys.stderr.write(NAME + ": using pymysql")
     except ImportError:
-        stderr.write(NAME + ": You need to install PyMySQL module to use mysql.chart.py plugin\n")
+        sys.stderr.write(NAME + ": You need to install PyMySQL module to use mysql.chart.py plugin\n")
 
+# default configuration (overriden by python.d.plugin)
 config = [
     {
         'name'     : 'local',
         'user'     : 'root',
         'password' : '',
-        'socket'   : '/var/run/mysqld/mysqld.sock'
+        'socket'   : '/var/run/mysqld/mysqld.sock',
+        'update_every' : 3,
+        'retries'  : 4
     }
 ]
 
-update_every = 3
-priority = 90000
-
-#query = "SHOW GLOBAL STATUS WHERE value REGEX '^[0-9]'"
+# query executed on MySQL server
 QUERY = "SHOW GLOBAL STATUS"
+
+# charts order (can be overriden if you want less charts, or different order)
 ORDER = ['net', 
          'queries', 
          'handlers', 
@@ -63,23 +69,32 @@ ORDER = ['net',
          'binlog_stmt_cache',
          'connection_errors']
 
+# charts definitions in format:
+# CHARTS = {
+#    'chart_name_in_netdata': (
+#        "parameters defining chart (passed to CHART statement)",
+#        [ # dimensions (lines) definitions
+#            ("dimension_name", "dimension parameters (passed to DIMENSION statement)", "additional parameter (optional)")
+#        ])
+#    }
+
 CHARTS = {
     'net' : (
         "'' 'mysql Bandwidth' 'kilobits/s' bandwidth mysql.net area",
-        (
+        [
             ("Bytes_received", "in incremental 8 1024"),
             ("Bytes_sent",     "out incremental -8 1024")
-        )),
+        ]),
     'queries' : (
         "'' 'mysql Queries' 'queries/s' queries mysql.queries line",
-        (
+        [
             ("Queries",      "queries incremental 1 1"),
             ("Questions",    "questions incremental 1 1"),
             ("Slow_queries", "slow_queries incremental -1 1")
-        )),
+        ]),
     'handlers' : (
         "'' 'mysql Handlers' 'handlers/s' handlers mysql.handlers line",
-        (
+        [
             ("Handler_commit", "commit incremental 1 1"),
             ("Handler_delete", "delete incremental 1 1"),
             ("Handler_prepare", "prepare incremental 1 1"),
@@ -94,274 +109,312 @@ CHARTS = {
             ("Handler_savepoint_rollback", "savepoint_rollback incremental 1 1"),
             ("Handler_update", "update incremental 1 1"),
             ("Handler_write", "write incremental 1 1")
-        )),
+        ]),
     'table_locks' : (
         "'' 'mysql Tables Locks' 'locks/s' locks mysql.table_locks line",
-        (
+        [
             ("Table_locks_immediate", "immediate incremental 1 1"),
             ("Table_locks_waited", "waited incremental -1 1")
-        )),
+        ]),
     'join_issues' : (
         "'' 'mysql Select Join Issues' 'joins/s' issues mysql.join_issues line",
-        (
+        [
             ("Select_full_join", "full_join incremental 1 1"),
             ("Select_full_range_join", "full_range_join incremental 1 1"),
             ("Select_range", "range incremental 1 1"),
             ("Select_range_check", "range_check incremental 1 1"),
             ("Select_scan", "scan incremental 1 1"),
-        )),
+        ]),
     'sort_issues' : (
         "'' 'mysql Sort Issues' 'issues/s' issues mysql.sort.issues line",
-        (
+        [
             ("Sort_merge_passes", "merge_passes incremental 1 1"),
             ("Sort_range", "range incremental 1 1"),
             ("Sort_scan", "scan incremental 1 1"),
-        )),
+        ]),
     'tmp' : (
         "'' 'mysql Tmp Operations' 'counter' temporaries mysql.tmp line",
-        (
+        [
             ("Created_tmp_disk_tables", "disk_tables incremental 1 1"),
             ("Created_tmp_files", "files incremental 1 1"),
             ("Created_tmp_tables", "tables incremental 1 1"),
-        )),
+        ]),
     'connections' : (
         "'' 'mysql Connections' 'connections/s' connections mysql.connections line",
-        (
+        [
             ("Connections", "all incremental 1 1"),
             ("Aborted_connects", "aborted incremental 1 1"),
-        )),
+        ]),
     'binlog_cache' : (
         "'' 'mysql Binlog Cache' 'transactions/s' binlog mysql.binlog_cache line",
-        (
+        [
             ("Binlog_cache_disk_use", "disk incremental 1 1"),
             ("Binlog_cache_use", "all incremental 1 1"),
-        )),
+        ]),
     'threads' : (
         "'' 'mysql Threads' 'threads' threads mysql.threads line",
-        (
+        [
             ("Threads_connected", "connected absolute 1 1"),
             ("Threads_created", "created incremental 1 1"),
             ("Threads_cached", "cached absolute -1 1"),
             ("Threads_running", "running absolute 1 1"),
-        )),
+        ]),
     'thread_cache_misses' : (
         "'' 'mysql Threads Cache Misses' 'misses' threads mysql.thread_cache_misses area",
-        (
+        [
             ("Thread_cache_misses", "misses misses absolute 1 100"),
-        )),
+        ]),
     'innodb_io' : (
         "'' 'mysql InnoDB I/O Bandwidth' 'kilobytes/s' innodb mysql.innodb_io area",
-        (
+        [
             ("Innodb_data_read", "read incremental 1 1024"),
             ("Innodb_data_written", "write incremental -1 1024"),
-        )),
+        ]),
     'innodb_io_ops' : (
         "'' 'mysql InnoDB I/O Operations' 'operations/s' innodb mysql.innodb_io_ops line",
-        (
+        [
             ("Innodb_data_reads", "reads incremental 1 1"),
             ("Innodb_data_writes", "writes incremental -1 1"),
             ("Innodb_data_fsyncs", "fsyncs incremental 1 1"),
-        )),
+        ]),
     'innodb_io_pending_ops' : (
         "'' 'mysql InnoDB Pending I/O Operations' 'operations' innodb mysql.innodb_io_pending_ops line",
-        (
+        [
             ("Innodb_data_pending_reads", "reads absolute 1 1"),
             ("Innodb_data_pending_writes", "writes absolute -1 1"),
             ("Innodb_data_pending_fsyncs", "fsyncs absolute 1 1"),
-        )),
+        ]),
     'innodb_log' : (
         "'' 'mysql InnoDB Log Operations' 'operations/s' innodb mysql.innodb_log line",
-        (
+        [
             ("Innodb_log_waits", "waits incremental 1 1"),
             ("Innodb_log_write_requests", "write_requests incremental -1 1"),
             ("Innodb_log_writes", "incremental -1 1"),
-        )),
+        ]),
     'innodb_os_log' : (
         "'' 'mysql InnoDB OS Log Operations' 'operations' innodb mysql.innodb_os_log line",
-        (
+        [
             ("Innodb_os_log_fsyncs", "fsyncs incremental 1 1"),
             ("Innodb_os_log_pending_fsyncs", "pending_fsyncs absolute 1 1"),
             ("Innodb_os_log_pending_writes", "pending_writes absolute -1 1"),
-        )),
+        ]),
     'innodb_os_log_io' : (
         "'' 'mysql InnoDB OS Log Bandwidth' 'kilobytes/s' innodb mysql.innodb_os_log_io area",
-        (
+        [
             ("Innodb_os_log_written", "write incremental -1 1024"),
-        )),
+        ]),
     'innodb_cur_row_lock' : (
         "'' 'mysql InnoDB Current Row Locks' 'operations' innodb mysql.innodb_cur_row_lock area",
-        (
+        [
             ("Innodb_row_lock_current_waits", "current_waits absolute 1 1"),
-        )),
+        ]),
     'innodb_rows' : (
         "'' 'mysql InnoDB Row Operations' 'operations/s' innodb mysql.innodb_rows area",
-        (
+        [
             ("Innodb_rows_inserted", "read incremental 1 1"),
             ("Innodb_rows_read", "deleted incremental -1 1"),
             ("Innodb_rows_updated", "inserted incremental 1 1"),
             ("Innodb_rows_deleted", "updated incremental -1 1"),
-        )),
+        ]),
     'innodb_buffer_pool_pages' : (
         "'' 'mysql InnoDB Buffer Pool Pages' 'pages' innodb mysql.innodb_buffer_pool_pages line",
-        (
+        [
             ("Innodb_buffer_pool_pages_data", "data absolute 1 1"),
             ("Innodb_buffer_pool_pages_dirty", "dirty absolute -1 1"),
             ("Innodb_buffer_pool_pages_free", "free absolute 1 1"),
             ("Innodb_buffer_pool_pages_flushed", "flushed incremental -1 1"),
             ("Innodb_buffer_pool_pages_misc", "misc absolute -1 1"),
             ("Innodb_buffer_pool_pages_total", "total absolute 1 1"),
-        )),
+        ]),
     'innodb_buffer_pool_bytes' : (
         "'' 'mysql InnoDB Buffer Pool Bytes' 'MB' innodb mysql.innodb_buffer_pool_bytes area",
-        (
+        [
             ("Innodb_buffer_pool_bytes_data", "data absolute 1"),
             ("Innodb_buffer_pool_bytes_dirty", "dirty absolute -1"),
-        )),
+        ]),
     'innodb_buffer_pool_read_ahead' : (
         "'' 'mysql InnoDB Buffer Pool Read Ahead' 'operations/s' innodb mysql.innodb_buffer_pool_read_ahead area",
-        (
+        [
             ("Innodb_buffer_pool_read_ahead", "all incremental 1 1"),
             ("Innodb_buffer_pool_read_ahead_evicted", "evicted incremental -1 1"),
             ("Innodb_buffer_pool_read_ahead_rnd", "random incremental 1 1"),
-        )),
+        ]),
     'innodb_buffer_pool_reqs' : (
         "'' 'mysql InnoDB Buffer Pool Requests' 'requests/s' innodb mysql.innodb_buffer_pool_reqs area",
-        (
+        [
             ("Innodb_buffer_pool_read_requests", "reads incremental 1 1"),
             ("Innodb_buffer_pool_write_requests", "writes incremental -1 1"),
-        )),
+        ]),
     'innodb_buffer_pool_ops' : (
         "'' 'mysql InnoDB Buffer Pool Operations' 'operations/s' innodb mysql.innodb_buffer_pool_ops area",
-        (
+        [
             ("Innodb_buffer_pool_reads", "'disk reads' incremental 1 1"),
             ("Innodb_buffer_pool_wait_free", "'wait free' incremental -1 1"),
-        )),
+        ]),
     'qcache_ops' : (
         "'' 'mysql QCache Operations' 'queries/s' qcache mysql.qcache_ops line",
-        (
+        [
             ("Qcache_hits", "hits incremental 1 1"),
             ("Qcache_lowmem_prunes", "'lowmem prunes' incremental -1 1"),
             ("Qcache_inserts", "inserts incremental 1 1"),
             ("Qcache_not_cached", "'not cached' incremental -1 1"),
-        )),
+        ]),
     'qcache' : (
         "'' 'mysql QCache Queries in Cache' 'queries' qcache mysql.qcache line",
-        (
+        [
             ("Qcache_queries_in_cache", "queries absolute 1 1"),
-        )),
+        ]),
     'qcache_freemem' : (
         "'' 'mysql QCache Free Memory' 'MB' qcache mysql.qcache_freemem area",
-        (
+        [
             ("Qcache_free_memory", "free absolute 1"),
-        )),
+        ]),
     'qcache_memblocks' : (
         "'' 'mysql QCache Memory Blocks' 'blocks' qcache mysql.qcache_memblocks line",
-        (
+        [
             ("Qcache_free_blocks", "free absolute 1"),
             ("Qcache_total_blocks", "total absolute 1 1"),
-        )),
+        ]),
     'key_blocks' : (
         "'' 'mysql MyISAM Key Cache Blocks' 'blocks' myisam mysql.key_blocks line",
-        (
+        [
             ("Key_blocks_unused", "unused absolute 1 1"),
             ("Key_blocks_used", "used absolute -1 1"),
             ("Key_blocks_not_flushed", "'not flushed' absolute 1 1"),
-        )),
+        ]),
     'key_requests' : (
         "'' 'mysql MyISAM Key Cache Requests' 'requests/s' myisam mysql.key_requests area",
-        (
+        [
             ("Key_read_requests", "reads incremental 1 1"),
             ("Key_write_requests", "writes incremental -1 1"),
-        )),
+        ]),
     'key_disk_ops' : (
         "'' 'mysql MyISAM Key Cache Disk Operations' 'operations/s' myisam mysql.key_disk_ops area",
-        (
+        [
             ("Key_reads", "reads incremental 1 1"),
             ("Key_writes", "writes incremental -1 1"),
-        )),
+        ]),
     'files' : (
         "'' 'mysql Open Files' 'files' files mysql.files line",
-        (
+        [
             ("Open_files", "files absolute 1 1"),
-        )),
+        ]),
     'files_rate' : (
         "'' 'mysql Opened Files Rate' 'files/s' files mysql.files_rate line",
-        (
+        [
             ("Opened_files", "files incremental 1 1"),
-        )),
+        ]),
     'binlog_stmt_cache' : (
         "'' 'mysql Binlog Statement Cache' 'statements/s' binlog mysql.binlog_stmt_cache line",
-        (
+        [
             ("Binlog_stmt_cache_disk_use", "disk incremental 1 1"),
             ("Binlog_stmt_cache_use", "all incremental 1 1"),
-        )),
+        ]),
     'connection_errors' : (
         "'' 'mysql Connection Errors' 'connections/s' connections mysql.connection_errors line",
-        (
+        [
             ("Connection_errors_accept", "accept incremental 1 1"),
             ("Connection_errors_internal", "internal incremental 1 1"),
             ("Connection_errors_max_connections", "max incremental 1 1"),
             ("Connection_errors_peer_address", "peer_addr incremental 1 1"),
             ("Connection_errors_select", "select incremental 1 1"),
             ("Connection_errors_tcpwrap", "tcpwrap incremental 1 1")
-        ))
+        ])
 }
 
 
-def Service(object):
-    def __init__(self,config=None):
-        if config is None:
-            pass # TODO use defaults
-        if 'name' not in config:
-            from random import randint
-            config['name'] = "srv_"+str(randint(0,99)
-        if 'user' not in config:
-            config['user'] = 'root'
-        if 'password' not in config:
-            config['password'] = ''
-        if 'my.cnf' in config:
-            config['socket'] = ''
-            config['host'] = ''
-            config['port'] = 0
-        elif 'socket' in config:
-            config['my.cnf'] = ''
-            config['host'] = ''
-            config['port'] = 0
-        elif 'host' in config:
-            config['my.cnf'] = ''
-            config['socket'] = ''
-            if 'port' in config:
-                config['port'] = int(config['port'])
-            else:
-                config['port'] = 3306
+class BaseService(object):
+    def __init__(self,configuration,update_every,priority,retries):
+        if configuration is None:
+            # use defaults
+            configuration = config
+            self.error(NAME+": no configuration supplied. using defaults.")
 
-        self.config = config
-        self.connection = None
-        self.mysql_def = {}
+        self._parse_base_config(configuration)
 
-    def check(self):
+    def _parse_base_config(self,config,update_every,priority,retries):
+        # parse configuration options to run this Service
         try:
-            self.connection = self._connect()
-            return True
-        except RuntimeError:
-            self.connection = None
-            return False
+            self.update_every = int(config['update_every'])
+        except (KeyError, ValueError):
+            self.update_every = update_every
+        try:
+            self.priority = int(config['priority'])
+        except (KeyError, ValueError):
+            self.priority = priority
+        try:
+            self.retries = int(config['retries'])
+        except (KeyError, ValueError):
+            self.retries = retries
+        self.retries_left = self.retries
 
-    
+    def error(self, msg, exception=""):
+        if exception != "":
+            exception = " " + str(exception).replace("\n"," ")
+        sys.stderr.write(str(msg)+exception+"\n")
+        sys.stderr.flush()
+
+    def check(self):
+        # TODO notify about not overriden function
+        self.error("Where is your check()?")
+        return False
+
+    def create(self):
+        # TODO notify about not overriden function
+        self.error("Where is your create()?")
+        return False
+
+    def update(self):
+        # TODO notify about not overriden function
+        self.error("Where is your update()?")
+        return False
+
+
+class Service(BaseService):
+    def __init__(self,configuration=None,update_every=3,priority=90000,retries=2):
+        super().__init__(*args,**kwargs)
+        self.configuration = self._parse_config(configuration)
+        self.connection = None
+        self.defs = None
+
+    def _parse_config(configuration):
+        # parse configuration to collect data from mysql server
+        if 'name' not in configuration:
+            configuration['name'] = 'local'
+        if 'user' not in configuration:
+            configuration['user'] = 'root'
+        if 'password' not in configuration:
+            configuration['password'] = ''
+        if 'my.cnf' in configuration:
+            configuration['socket'] = ''
+            configuration['host'] = ''
+            configuration['port'] = 0
+        elif 'socket' in configuration:
+            configuration['my.cnf'] = ''
+            configuration['host'] = ''
+            configuration['port'] = 0
+        elif 'host' in configuration:
+            configuration['my.cnf'] = ''
+            configuration['socket'] = ''
+            if 'port' in configuration:
+                configuration['port'] = int(configuration['port'])
+            else:
+                configuration['port'] = 3306
+
+        return configuration
 
     def _connect(self):
         try:
-            self.connection = MySQLdb.connect(user=self.config['user'],
-                                              passwd=self.config['password'],
-                                              read_default_file=self.config['my.cnf'],
-                                              unix_socket=self.config['socket'],
-                                              host=self.config['host'],
-                                              port=self.config['port'],
-                                              connect_timeout=int(update_every))
+            self.connection = MySQLdb.connect(user=self.configuration['user'],
+                                              passwd=self.configuration['password'],
+                                              read_default_file=self.configuration['my.cnf'],
+                                              unix_socket=self.configuration['socket'],
+                                              host=self.configuration['host'],
+                                              port=self.configuration['port'],
+                                              connect_timeout=self.configuration['update_every'])
         except Exception as e:
-            stderr.write(NAME + " has problem connecting to server: "+str(e).replace("\n"," ")+"\n")
+            self.error(NAME + " has problem connecting to server:", e)
             raise RuntimeError #stop creating module, need to catch it in supervisor
-
     
     def _get_data(self):
         try:
@@ -369,22 +422,28 @@ def Service(object):
                 cursor.execute(QUERY)
                 raw_data = cursor.fetchall()
         except Exception as e:
-            stderr.write(NAME + ": cannot execute query." + str(e) + "\n")
+            self.error(NAME + ": cannot execute query.", e)
             self.connection.close()
             return None
         
         return dict(raw_data)
 
-
+    def check(self):
+        try:
+            self.connection = self._connect()
+            return True
+        except RuntimeError:
+            self.connection = None
+            return False
 
     def create(self):
         for name in ORDER:
             self.mysql_def[name] = []
             for line in CHARTS[name][1]:
-                self.mysql_def[name].append(line[0])
+                self.defs[name].append(line[0])
    
         idx = 0
-        data = self._get_data(self.config)
+        data = self._get_data(self.configuration)
         for name in ORDER:
             header = "CHART mysql_" + \
                      str(srv['name']) + "." + \
@@ -406,17 +465,16 @@ def Service(object):
             return False
         return True
 
-
     def update(self,interval):
-        data = self._get_data(self.config)
+        data = self._get_data(self.configuration)
         if data is None:
             return False
         try:
             data['Thread cache misses'] = int( int(data['Threads_created']) * 10000 / int(data['Connections']))
         except Exception:
             pass
-        for chart, dimensions in self._mysql_def.items():
-            header = "BEGIN mysql_" + str(self.config['name']) + "." + chart + " " + str(interval) + '\n'
+        for chart, dimensions in self.defs.items():
+            header = "BEGIN mysql_" + str(self.configuration['name']) + "." + chart + " " + str(interval) + '\n'
             lines = ""
             for d in dimensions:
                 try:
@@ -427,3 +485,9 @@ def Service(object):
                 print(header + lines + "END")
         
         return True
+
+if __name__ == "__main__":
+    my = Service(config)
+    my.check()
+    my.create()
+    my.update(1)
diff --git a/python.d/python_modules/base.py b/python.d/python_modules/base.py
new file mode 100644 (file)
index 0000000..8aa2ee6
--- /dev/null
@@ -0,0 +1,48 @@
+# Description: base for netdata python.d plugins
+# Author: Pawel Krupa (paulfantom)
+
+class BaseService(object):
+    def __init__(self,configuration,update_every,priority,retries):
+        if configuration is None:
+            # use defaults
+            configuration = config
+            self.error(NAME+": no configuration supplied. using defaults.")
+
+        self._parse_base_config(configuration)
+
+    def _parse_base_config(self,config,update_every,priority,retries):
+        # parse configuration options to run this Service
+        try:
+            self.update_every = int(config['update_every'])
+        except (KeyError, ValueError):
+            self.update_every = update_every
+        try:
+            self.priority = int(config['priority'])
+        except (KeyError, ValueError):
+            self.priority = priority
+        try:
+            self.retries = int(config['retries'])
+        except (KeyError, ValueError):
+            self.retries = retries
+        self.retries_left = self.retries
+
+    def error(self, msg, exception=""):
+        if exception != "":
+            exception = " " + str(exception).replace("\n"," ")
+        sys.stderr.write(str(msg)+exception+"\n")
+        sys.stderr.flush()
+
+    def check(self):
+        # TODO notify about not overriden function
+        self.error("Where is your check()?")
+        return False
+
+    def create(self):
+        # TODO notify about not overriden function
+        self.error("Where is your create()?")
+        return False
+
+    def update(self):
+        # TODO notify about not overriden function
+        self.error("Where is your update()?")
+        return False