summaryrefslogtreecommitdiffstats
path: root/contrib/check_mysqlslave.pl
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/check_mysqlslave.pl')
-rw-r--r--contrib/check_mysqlslave.pl174
1 files changed, 174 insertions, 0 deletions
diff --git a/contrib/check_mysqlslave.pl b/contrib/check_mysqlslave.pl
new file mode 100644
index 0000000..ab7af89
--- /dev/null
+++ b/contrib/check_mysqlslave.pl
@@ -0,0 +1,174 @@
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
29
30
31use strict;
32use lib "/usr/local/nagios/libexec";
33use utils qw($TIMEOUT %ERRORS &print_revision &support);
34
35use DBI;
36use DBD::mysql;
37use Getopt::Long;
38Getopt::Long::Configure('bundling');
39
40# Predeclare some variables
41my $PROGNAME = 'check_mysqlslave';
42my $REVISION = '0.1';
43my $status;
44my $state = 'UNKNOWN';
45my $opt_V;
46my $opt_h;
47my $port = 3306;
48my $hostname;
49my $user = 'root';
50my $pass = '';
51my $driver;
52my $dbh;
53my $query;
54my $result;
55my $data;
56
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};
62alarm($TIMEOUT);
63
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 );
72
73
74if ($status == 0) {
75 print_help() ;
76 exit $ERRORS{'OK'};
77}
78
79if ($opt_V) {
80 print_revision($PROGNAME,'$Revision$REVISION .' $ ');
81 exit $ERRORS{'OK'};
82}
83
84if ($opt_h) {
85 print_help();
86 exit $ERRORS{'OK'};
87}
88
89if (! utils::is_hostname($hostname)){
90 usage();
91 exit $ERRORS{"UNKNOWN"};
92}
93
94
95$driver = 'DBI:mysql::'. $hostname;
96
97eval {
98 $dbh = DBI->connect($driver, $user, $pass, { RaiseError => 1, PrintError => 0});
99};
100if ($@) {
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}
107
108eval {
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};
116if ($@) {
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}
124
125if ($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}
148
149sub 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}
158
159sub 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 .' $');
173
174}