35
35
use SNMP;
36
36
use DBI;
37
37
use DBD::mysql;
38
+ use Pod::Usage;
38
39
39
40
sub my_snmp_handler ($$$$);
40
41
netsnmp_ds_set_boolean( NETSNMP_DS_APPLICATION_ID,
41
42
NETSNMP_DS_AGENT_NO_ROOT_ACCESS, 1 );
42
43
my $agent = new NetSNMP::agent( ' Name' => ' mysql' , ' AgentX' => 1 );
43
44
44
45
my $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
+ );
65
56
66
57
Getopt::Long::Configure(' no_ignore_case' );
67
58
GetOptions(
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 };
78
80
79
81
if ( $opt {version } ) {
80
82
print " mysql-agent.pl $VERSION by brice.figureau\@ daysofwonder.com\n " ;
81
83
exit ;
82
84
}
83
85
84
- my $user = $opt {u } || " monitor" ;
85
- my $pass = $opt {p } || " " ;
86
- my $host = $opt {h } || ' localhost' ;
87
- my $port = $opt {P } || ' 3306' ;
88
86
my $debugging = $opt {verbose };
89
- my $daemon_pidfile = $opt {' daemon-pid' } || ' /var/run/mysql-agent.pid' ;
90
87
my $subagent = 0;
91
88
my $chk_innodb = 1; # Do you want to check InnoDB statistics?
92
89
my $chk_master = 1; # Do you want to check binary logging?
93
90
my $chk_slave = 0; # Do you want to check slave status?
94
- my $refresh_interval = $opt {i } || 300;
95
91
96
- my $dsn = " DBI:mysql:host=${host} ;port=${port} " ;
92
+ my $dsn = " DBI:mysql:host=$opt {host};port=$opt {port}" ;
97
93
my $running = 0;
98
94
my $error = 0;
99
95
@@ -109,9 +105,7 @@ sub usage {
109
105
my $global_last_refresh = 0;
110
106
111
107
# 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 });
115
109
$agent -> register( " mysql" , $regOID , \&my_snmp_handler );
116
110
117
111
# various types & definitions
314
308
if ($pid ) {
315
309
316
310
# 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 " ;
319
313
print PIDFILE " $pid \n " ;
320
314
close (PIDFILE);
321
315
exit ;
@@ -635,17 +629,17 @@ sub refresh_status {
635
629
my $now = time ();
636
630
637
631
# 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 } ) {
639
633
640
634
# if yes, do not do anything
641
635
dolog( LOG_DEBUG,
642
636
" not refreshing: "
643
637
. ( $now - $global_last_refresh )
644
- . " < $refresh_interval " )
638
+ . " < $opt { refresh_interval} " )
645
639
if ($debugging );
646
640
return ;
647
641
}
648
- my ( $oid , $types , $status ) = fetch_mysql_data( $dsn , $user , $pass );
642
+ my ( $oid , $types , $status ) = fetch_mysql_data( $dsn , $opt { user } , $opt { pass } );
649
643
if ($oid ) {
650
644
dolog( LOG_DEBUG, " Setting error to 0" ) if ($debugging );
651
645
$error = 0;
@@ -789,9 +783,102 @@ ($$)
789
783
$SIG {' TERM' } = \&shut_it_down;
790
784
$running = 1;
791
785
while ($running ) {
792
- refresh_status($startOID );
786
+ refresh_status($opt { oid } );
793
787
$agent -> agent_check_and_process(1); # 1 = block
794
788
}
795
789
$agent -> shutdown ();
796
790
797
791
dolog( 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