1 #!/usr/bin/perl -w
2 #
3 # check_mysqlslave.pl - nagios plugin
4 #
5 #
6 # Copyright 2002 Mario Witte
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #
22 # Credits:
23 # - Thanks to Christoph Kron <ck@zet.net> for check_ifstatus.pl
24 # I used check_ifstatus.pl as a layout when writing this
25 #
26 # Report bugs to: chengfu@users.sourceforge.net
27 #
28 # 20.09.2002 Version 0.1
31 use strict;
32 use lib "/usr/local/nagios/libexec";
33 use utils qw($TIMEOUT %ERRORS &print_revision &support);
35 use DBI;
36 use DBD::mysql;
37 use Getopt::Long;
38 Getopt::Long::Configure('bundling');
40 # Predeclare some variables
41 my $PROGNAME = 'check_mysqlslave';
42 my $REVISION = '0.1';
43 my $status;
44 my $state = 'UNKNOWN';
45 my $opt_V;
46 my $opt_h;
47 my $port = 3306;
48 my $hostname;
49 my $user = 'root';
50 my $pass = '';
51 my $driver;
52 my $dbh;
53 my $query;
54 my $result;
55 my $data;
57 # Just in case of problems, let's not hang Nagios
58 $SIG{'ALRM'} = sub {
59 print ("ERROR: No response from $hostname (alarm timeout)\n");
60 exit $ERRORS{"UNKNOWN"};
61 };
62 alarm($TIMEOUT);
64 $status = GetOptions(
65 "V" => \$opt_V, "version" => \$opt_V,
66 "h" => \$opt_h, "help" => \$opt_h,
67 "p=i" => \$port, "port=i" => \$port,
68 "H=s" => \$hostname, "hostname=s" => \$hostname,
69 "u=s" => \$user, "user=s" => \$user,
70 "P=s" => \$pass, "pass=s" => \$pass,
71 );
74 if ($status == 0) {
75 print_help() ;
76 exit $ERRORS{'OK'};
77 }
79 if ($opt_V) {
80 print_revision($PROGNAME,'$Revision$REVISION .' $ ');
81 exit $ERRORS{'OK'};
82 }
84 if ($opt_h) {
85 print_help();
86 exit $ERRORS{'OK'};
87 }
89 if (! utils::is_hostname($hostname)){
90 usage();
91 exit $ERRORS{"UNKNOWN"};
92 }
95 $driver = 'DBI:mysql::'. $hostname;
97 eval {
98 $dbh = DBI->connect($driver, $user, $pass, { RaiseError => 1, PrintError => 0});
99 };
100 if ($@) {
101 $status = $@;
102 if ($status =~ /^.*failed:\ (.+)\ at\ $0/i) { $status = $1; }
103 $state='CRITICAL';
104 print $state .': Connect failed: '."$status\n";
105 exit ($ERRORS{$state});
106 }
108 eval {
109 $query = 'SHOW SLAVE STATUS';
110 $result = $dbh->prepare($query);
111 $result->execute;
112 $data = $result->fetchrow_hashref();
113 $result->finish();
114 $dbh->disconnect();
115 };
116 if ($@) {
117 $status = $@;
118 $status =~ s/\n/ /g;
119 if ($status =~ /^DB[ID].*(failed|prepare):\ (.+)\ at\ $0/i) { $status = $2; }
120 $state = 'CRITICAL';
121 print $state .': Couldn\'t check slave: '."$status\n";
122 exit($ERRORS{$state});
123 }
125 if ($data->{'Slave_Running'} eq 'Yes') {
126 $status = 'Replicating from '. $data->{'Master_Host'};
127 $state = 'OK';
128 print $state .': '. $status ."\n";
129 exit($ERRORS{$state});
130 } elsif ($data->{'Slave_Running'} eq 'No') {
131 if (length($data->{'Last_error'}) > 0) {
132 $status = 'Slave stopped with error message';
133 $state = 'CRITICAL';
134 print $state .': '. $status ."\n";
135 exit($ERRORS{$state});
136 } else {
137 $status = 'Slave stopped without errors';
138 $state = 'WARNING';
139 print $state .': '. $status ."\n";
140 exit($ERRORS{$state});
141 }
142 } else {
143 $status = 'Unknown slave status: (Running: '. $data->{'Slave_Running'} .')';
144 $state = 'UNKNOWN';
145 print $state .': '. $status ."\n";
146 exit($ERRORS{$state});
147 }
149 sub usage {
150 printf "\nMissing arguments!\n";
151 printf "\n";
152 printf "check_mysqlslave -H <hostname> [-p <port> -u <username> -P <password>]\n";
153 printf "Copyright 2002 Mario Witte\n";
154 printf "\n\n";
155 support();
156 exit $ERRORS{"UNKNOWN"};
157 }
159 sub print_help {
160 printf "check_mysqlslave plugin for Nagios checks \n";
161 printf "if the replication on a backup mysql-server\n";
162 printf "is up and running\n";
163 printf "\nUsage:\n";
164 printf " -H (--hostname) Hostname to query\n";
165 printf " -p (--port) mysql port (default: 3306)\n";
166 printf " -u (--user) username for accessing mysql host\n";
167 printf " (default: root)\n";
168 printf " -P (--pass) password for accessing mysql host\n";
169 printf " (default: '')\n";
170 printf " -V (--version) Plugin version\n";
171 printf " -h (--help) usage help \n\n";
172 print_revision($PROGNAME, '$Revision$REVISION .' $');
174 }