3535use SNMP;
3636use DBI;
3737use DBD::mysql;
38+ use Pod::Usage;
3839
3940sub my_snmp_handler ($$$$);
4041netsnmp_ds_set_boolean( NETSNMP_DS_APPLICATION_ID,
4142 NETSNMP_DS_AGENT_NO_ROOT_ACCESS, 1 );
4243my $agent = new NetSNMP::agent( ' Name' => ' mysql' , ' AgentX' => 1 );
4344
4445my $VERSION = " 0.6" ;
45- my %opt = ();
46-
47- sub usage {
48- print " usage: mysql-agent.pl [*options*]\n\n " ;
49- print " --help display this help and exit\n " ;
50- print " -v, --verbose be verbose about what you do\n " ;
51- print " -V, --version output version information and exit\n " ;
52- print
53- " --daemon-pid=FILE write PID to FILE instead of /var/run/mysql-agent.pid\n " ;
54- print " -u DBUSER use DBUSER as user to connect to mysql\n " ;
55- print " -p DBPASS use DBPASS as password to connect to mysql\n " ;
56- print " -h|--host HOST connect to HOST\n " ;
57- print " -P|--port PORT port to connect (default 3306)\n " ;
58- print " -m|--master check master\n " ;
59- print " -s|--slave check slave\n " ;
60- print " --oid OID registering OID\n " ;
61- print " -i|--refresh refresh interval in seconds\n " ;
62- print " \n " ;
63- exit ;
64- }
46+
47+ my %opt = (
48+ daemon_pid => ' /var/run/mysql-agent.pid' ,
49+ host => ' localhost' ,
50+ oid => ' 1.3.6.1.4.1.20267.200.1' ,
51+ pass => ' ' ,
52+ port => ' 3306' ,
53+ refresh => ' 300' ,
54+ user => ' monitor' ,
55+ );
6556
6657Getopt::Long::Configure(' no_ignore_case' );
6758GetOptions(
68- \%opt , ' help' ,
69- ' version|V' , ' verbose|v+' ,
70- ' u|user=s' , ' p|password=s' ,
71- ' h|host=s' , ' P|port=i' ,
72- ' no-daemon' , ' master|m' ,
73- ' slave|s' , ' oid' ,
74- ' i|refresh=i' , ' daemon_pid|daemon-pid=s' ,
75- ) or exit (1);
76-
77- usage() if $opt {help };
59+ \%opt ,
60+ ' daemon_pid|daemon-pid=s' ,
61+ ' help|?' ,
62+ ' host=s' ,
63+ ' man' ,
64+ ' master|m' ,
65+ ' no-daemon' ,
66+ ' oid' ,
67+ ' password=s' ,
68+ ' port|P=i' ,
69+ ' refresh|i=i' ,
70+ ' slave' ,
71+ ' user=s' ,
72+ ' usage' ,
73+ ' verbose|v+' ,
74+ ' version|V' ,
75+ ) or pod2usage(-verbose => 0);
76+
77+ pod2usage(-verbose => 0) if $opt {usage };
78+ pod2usage(-verbose => 1) if $opt {help };
79+ pod2usage(-verbose => 2) if $opt {man };
7880
7981if ( $opt {version } ) {
8082 print " mysql-agent.pl $VERSION by brice.figureau\@ daysofwonder.com\n " ;
8183 exit ;
8284}
8385
84- my $user = $opt {u } || " monitor" ;
85- my $pass = $opt {p } || " " ;
86- my $host = $opt {h } || ' localhost' ;
87- my $port = $opt {P } || ' 3306' ;
8886my $debugging = $opt {verbose };
89- my $daemon_pidfile = $opt {' daemon-pid' } || ' /var/run/mysql-agent.pid' ;
9087my $subagent = 0;
9188my $chk_innodb = 1; # Do you want to check InnoDB statistics?
9289my $chk_master = 1; # Do you want to check binary logging?
9390my $chk_slave = 0; # Do you want to check slave status?
94- my $refresh_interval = $opt {i } || 300;
9591
96- my $dsn = " DBI:mysql:host=${host} ;port=${port} " ;
92+ my $dsn = " DBI:mysql:host=$opt {host};port=$opt {port}" ;
9793my $running = 0;
9894my $error = 0;
9995
@@ -109,9 +105,7 @@ sub usage {
109105my $global_last_refresh = 0;
110106
111107# enterprises.20267.200.1
112- my $startOID = $opt {oid } || " .1.3.6.1.4.1.20267.200.1" ;
113- my $regOID = new NetSNMP::OID($startOID );
114-
108+ my $regOID = new NetSNMP::OID($opt {oid });
115109$agent -> register( " mysql" , $regOID , \&my_snmp_handler );
116110
117111# various types & definitions
314308 if ($pid ) {
315309
316310 # parent
317- open PIDFILE, " > $ daemon_pidfile"
318- or die " mysql-agent : can't write to $daemon_pidfile : $! \n " ;
311+ open PIDFILE, ' > ' , $opt { daemon_pidfile }
312+ or die " $0 : can't write to $opt { daemon_pidfile} : $! \n " ;
319313 print PIDFILE " $pid \n " ;
320314 close (PIDFILE);
321315 exit ;
@@ -635,17 +629,17 @@ sub refresh_status {
635629 my $now = time ();
636630
637631 # Check if we have been called quicker than once every $refresh_interval
638- if ( ( $now - $global_last_refresh ) < $refresh_interval ) {
632+ if ( ( $now - $global_last_refresh ) < $opt { refresh_interval } ) {
639633
640634 # if yes, do not do anything
641635 dolog( LOG_DEBUG,
642636 " not refreshing: "
643637 . ( $now - $global_last_refresh )
644- . " < $refresh_interval " )
638+ . " < $opt { refresh_interval} " )
645639 if ($debugging );
646640 return ;
647641 }
648- my ( $oid , $types , $status ) = fetch_mysql_data( $dsn , $user , $pass );
642+ my ( $oid , $types , $status ) = fetch_mysql_data( $dsn , $opt { user } , $opt { pass } );
649643 if ($oid ) {
650644 dolog( LOG_DEBUG, " Setting error to 0" ) if ($debugging );
651645 $error = 0;
@@ -789,9 +783,102 @@ ($$)
789783$SIG {' TERM' } = \&shut_it_down;
790784$running = 1;
791785while ($running ) {
792- refresh_status($startOID );
786+ refresh_status($opt { oid } );
793787 $agent -> agent_check_and_process(1); # 1 = block
794788}
795789$agent -> shutdown ();
796790
797791dolog( LOG_INFO, " agent shutdown" );
792+
793+ __END__
794+ =head1 NAME
795+
796+ mysql-agent - report mysql statistics via SNMP
797+
798+ =head1 SYNOPSIS
799+
800+ mysql-agent.pl [options]
801+
802+ -h HOST, --host=HOST connect to MySQL DB on HOST
803+ -u USER, --user=USER use USER as user to connect to mysql
804+ -p PASS, --password=PASS use PASS as password to connect to mysql
805+ -P PORT, --port=PORT port to connect (default 3306)
806+ --daemon-pid=FILE write PID to FILE instead of $default{pid}
807+ -n, --no-daemon do not detach and become a daemon
808+ -m, --master check master
809+ -s, --slave check slave
810+ -o OID, --oid=OID registering OID
811+ -i INT, --refresh=INT set refresh interval to INT (seconds)
812+ -?, --help display this help and exit
813+ --man display program man page
814+ -v, --verbose be verbose about what you do
815+ -V, --version output version information and exit
816+
817+ =head1 OPTIONS
818+
819+ =over 8
820+
821+ =item B<-h HOST, --host=HOST >
822+
823+ connect to MySQL DB on HOST
824+
825+ =item B<-u USER, --user=USER >
826+
827+ use USER as user to connect to mysql
828+
829+ =item B<-p PASS, --password=PASS >
830+
831+ use PASS as password to connect to mysql
832+
833+ =item B<-P PORT, --port=PORT >
834+
835+ port to connect (default 3306)
836+
837+ =item B<--daemon-pid=FILE >
838+
839+ write PID to FILE instead of $default{pid}
840+
841+ =item B<-n, --no-daemon >
842+
843+ do not detach and become a daemon
844+
845+ =item B<-m, --master >
846+
847+ check master
848+
849+ =item B<-s, --slave >
850+
851+ check slave
852+
853+ =item B<-o OID, --oid=OID >
854+
855+ registering OID
856+
857+ =item B<-i INT, --refresh=INT >
858+
859+ refresh interval in seconds
860+
861+ =item B<-?, --help >
862+
863+ Print a brief help message and exits.
864+
865+ =item B<--man >
866+
867+ Prints the manual page and exits.
868+
869+ =item B<-v, --verbose >
870+
871+ be verbose about what you do
872+
873+ =item B<-V, --version >
874+
875+ output version information and exit
876+
877+ =back
878+
879+ =head1 DESCRIPTION
880+
881+ B<mysql-agent > is a small daemon that connects to a local snmpd daemon
882+ to report statistics on a local or remote MySQL server.
883+
884+ =cut
0 commit comments