check_mysqlslave.pl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. use strict;
  30. use lib "/usr/local/nagios/libexec";
  31. use utils qw($TIMEOUT %ERRORS &print_revision &support);
  32. use DBI;
  33. use DBD::mysql;
  34. use Getopt::Long;
  35. Getopt::Long::Configure('bundling');
  36. # Predeclare some variables
  37. my $PROGNAME = 'check_mysqlslave';
  38. my $REVISION = '0.1';
  39. my $status;
  40. my $state = 'UNKNOWN';
  41. my $opt_V;
  42. my $opt_h;
  43. my $port = 3306;
  44. my $hostname;
  45. my $user = 'root';
  46. my $pass = '';
  47. my $driver;
  48. my $dbh;
  49. my $query;
  50. my $result;
  51. my $data;
  52. # Just in case of problems, let's not hang Nagios
  53. $SIG{'ALRM'} = sub {
  54. print ("ERROR: No response from $hostname (alarm timeout)\n");
  55. exit $ERRORS{"UNKNOWN"};
  56. };
  57. alarm($TIMEOUT);
  58. $status = GetOptions(
  59. "V" => \$opt_V, "version" => \$opt_V,
  60. "h" => \$opt_h, "help" => \$opt_h,
  61. "p=i" => \$port, "port=i" => \$port,
  62. "H=s" => \$hostname, "hostname=s" => \$hostname,
  63. "u=s" => \$user, "user=s" => \$user,
  64. "P=s" => \$pass, "pass=s" => \$pass,
  65. );
  66. if ($status == 0) {
  67. print_help() ;
  68. exit $ERRORS{'OK'};
  69. }
  70. if ($opt_V) {
  71. print_revision($PROGNAME,'$Revision$REVISION .' $ ');
  72. exit $ERRORS{'OK'};
  73. }
  74. if ($opt_h) {
  75. print_help();
  76. exit $ERRORS{'OK'};
  77. }
  78. if (! utils::is_hostname($hostname)){
  79. usage();
  80. exit $ERRORS{"UNKNOWN"};
  81. }
  82. $driver = 'DBI:mysql::'. $hostname;
  83. eval {
  84. $dbh = DBI->connect($driver, $user, $pass, { RaiseError => 1, PrintError => 0});
  85. };
  86. if ($@) {
  87. $status = $@;
  88. if ($status =~ /^.*failed:\ (.+)\ at\ $0/i) { $status = $1; }
  89. $state='CRITICAL';
  90. print $state .': Connect failed: '."$status\n";
  91. exit ($ERRORS{$state});
  92. }
  93. eval {
  94. $query = 'SHOW SLAVE STATUS';
  95. $result = $dbh->prepare($query);
  96. $result->execute;
  97. $data = $result->fetchrow_hashref();
  98. $result->finish();
  99. $dbh->disconnect();
  100. };
  101. if ($@) {
  102. $status = $@;
  103. $status =~ s/\n/ /g;
  104. if ($status =~ /^DB[ID].*(failed|prepare):\ (.+)\ at\ $0/i) { $status = $2; }
  105. $state = 'CRITICAL';
  106. print $state .': Couldn\'t check slave: '."$status\n";
  107. exit($ERRORS{$state});
  108. }
  109. if ($data->{'Slave_Running'} eq 'Yes') {
  110. $status = 'Replicating from '. $data->{'Master_Host'};
  111. $state = 'OK';
  112. print $state .': '. $status ."\n";
  113. exit($ERRORS{$state});
  114. } elsif ($data->{'Slave_Running'} eq 'No') {
  115. if (length($data->{'Last_error'}) > 0) {
  116. $status = 'Slave stopped with error message';
  117. $state = 'CRITICAL';
  118. print $state .': '. $status ."\n";
  119. exit($ERRORS{$state});
  120. } else {
  121. $status = 'Slave stopped without errors';
  122. $state = 'WARNING';
  123. print $state .': '. $status ."\n";
  124. exit($ERRORS{$state});
  125. }
  126. } else {
  127. $status = 'Unknown slave status: (Running: '. $data->{'Slave_Running'} .')';
  128. $state = 'UNKNOWN';
  129. print $state .': '. $status ."\n";
  130. exit($ERRORS{$state});
  131. }
  132. sub usage {
  133. printf "\nMissing arguments!\n";
  134. printf "\n";
  135. printf "check_mysqlslave -H <hostname> [-p <port> -u <username> -P <password>]\n";
  136. printf "Copyright 2002 Mario Witte\n";
  137. printf "\n\n";
  138. support();
  139. exit $ERRORS{"UNKNOWN"};
  140. }
  141. sub print_help {
  142. printf "check_mysqlslave plugin for Nagios checks \n";
  143. printf "if the replication on a backup mysql-server\n";
  144. printf "is up and running\n";
  145. printf "\nUsage:\n";
  146. printf " -H (--hostname) Hostname to query\n";
  147. printf " -p (--port) mysql port (default: 3306)\n";
  148. printf " -u (--user) username for accessing mysql host\n";
  149. printf " (default: root)\n";
  150. printf " -P (--pass) password for accessing mysql host\n";
  151. printf " (default: '')\n";
  152. printf " -V (--version) Plugin version\n";
  153. printf " -h (--help) usage help \n\n";
  154. print_revision($PROGNAME, '$Revision$REVISION .' $');
  155. }