2323# License: MIT (http://www.opensource.org/licenses/mit-license.php)
2424#
2525
26- import collectd
26+ import sys
27+
28+ COLLECTD_ENABLED = True
29+ try :
30+ import collectd
31+ except ImportError :
32+ # We're not running in CollectD, set this to False so we can make some changes
33+ # accordingly for testing/development.
34+ COLLECTD_ENABLED = False
2735import re
2836import MySQLdb
2937
@@ -488,9 +496,12 @@ def fetch_innodb_stats(conn):
488496 return stats
489497
490498def log_verbose (msg ):
491- if MYSQL_CONFIG ['Verbose' ] == False :
499+ if not MYSQL_CONFIG ['Verbose' ]:
492500 return
493- collectd .info ('mysql plugin: %s' % msg )
501+ if COLLECTD_ENABLED :
502+ collectd .info ('mysql plugin: %s' % msg )
503+ else :
504+ print ('mysql plugin: %s' % msg )
494505
495506def dispatch_value (prefix , key , value , type , type_instance = None ):
496507 if not type_instance :
@@ -504,11 +515,12 @@ def dispatch_value(prefix, key, value, type, type_instance=None):
504515 except ValueError :
505516 value = float (value )
506517
507- val = collectd .Values (plugin = 'mysql' , plugin_instance = prefix )
508- val .type = type
509- val .type_instance = type_instance
510- val .values = [value ]
511- val .dispatch ()
518+ if COLLECTD_ENABLED :
519+ val = collectd .Values (plugin = 'mysql' , plugin_instance = prefix )
520+ val .type = type
521+ val .type_instance = type_instance
522+ val .values = [value ]
523+ val .dispatch ()
512524
513525def configure_callback (conf ):
514526 global MYSQL_CONFIG
@@ -565,7 +577,21 @@ def read_callback():
565577 if key not in innodb_status : continue
566578 dispatch_value ('innodb' , key , innodb_status [key ], MYSQL_INNODB_STATUS_VARS [key ])
567579
568- collectd .register_read (read_callback )
569- collectd .register_config (configure_callback )
580+ if COLLECTD_ENABLED :
581+ collectd .register_read (read_callback )
582+ collectd .register_config (configure_callback )
583+
584+ if __name__ == "__main__" and not COLLECTD_ENABLED :
585+ print "Running in test mode, invoke with"
586+ print sys .argv [0 ] + " Host Port User Password "
587+ MYSQL_CONFIG ['Host' ] = sys .argv [1 ]
588+ MYSQL_CONFIG ['Port' ] = int (sys .argv [2 ])
589+ MYSQL_CONFIG ['User' ] = sys .argv [3 ]
590+ MYSQL_CONFIG ['Password' ] = sys .argv [4 ]
591+ MYSQL_CONFIG ['Verbose' ] = True
592+ from pprint import pprint as pp
593+ pp (MYSQL_CONFIG )
594+ read_callback ()
595+
570596
571597# vim:noexpandtab ts=8 sw=8 sts=8
0 commit comments