OK, I'm game. Thanks for your interest in helping.

First off, here's the script:

#!/usr/bin/env perl

#
# mysql-slave-check.pl
#
# Nagios script for checking the replication
# status of a slave MySQL server.
#
# Michal Ludvig <mic...@logix.cz> (c) 2006
#               http://www.logix.cz/michal
#
# Run with --help to get some hints about usage or
# look at subroutine usage() near the end of this file.
#

use strict;
use DBI;
use Getopt::Long;

my $db_host = "localhost";
my $db_port = "";
my $db_sock = "";
my $db_user = "";
my $db_pass = "";
my $db_name = "";

# Nagios codes
my %ERRORS=('OK'=>0, 'WARNING'=>1, 'CRITICAL'=>2, 'UNKNOWN'=>3,
'DEPENDENT'=>4);

GetOptions(
'host=s' => \$db_host,
'port=i' => \$db_port,
'user=s' => \$db_user,
'password=s' => \$db_pass,
'socket=s' => \$db_sock,
'name|dbname|database=s' => \$db_name,
'help' => sub { &usage(); },
);

&nagios_return("UNKNOWN", "Either set --host/--port or --sock, not both!")
if (($db_port || $db_host ne "localhost") && $db_sock);

my $db_conn_string = "DBI:mysql:";
$db_conn_string .= "database=$db_name;";
$db_conn_string .= "host=$db_host;";
$db_conn_string .= "port=$db_port;";
$db_conn_string .= "mysql_socket=$db_sock;";

## Connect to the database.
my $dbh = DBI->connect($db_conn_string, $db_user, $db_pass,
                       {'RaiseError' => 0, 'PrintError' => 0});

&nagios_return("UNKNOWN", "Connect failed: $DBI::errstr") if (!$dbh);

## Now retrieve data from the table.
my $sth = $dbh->prepare("SHOW SLAVE STATUS");
&nagios_return("UNKNOWN", "[1] $DBI::errstr") if (!$sth);

$sth->execute();

&nagios_return("UNKNOWN", "[2] $DBI::errstr") if ($sth->err);
&nagios_return("CRITICAL", "Query returned ".scalar($sth->rows)." rows") if
(scalar($sth->rows) < 1);

## Query should return one row only
my $result = $sth->fetchrow_hashref();

&nagios_return("UNKNOWN", "[3] $DBI::errstr") if (!$result);

## Print all results? No thanks.
# while (my ($key, $val) = each %$result) {
# print "$key = $val\n";
# }

## Check the returned values
&nagios_return("CRITICAL", "Replication problem: Slave IO not running!") if
($result->{'Slave_IO_Running'} ne "Yes");
&nagios_return("CRITICAL", "Replication problem: Slave SQL not running!") if
($result->{'Slave_SQL_Running'} ne "Yes");
&nagios_return("CRITICAL", "Replication problem:
Seconds_Behind_Master=".$result->{'Seconds_Behind_Master'}) if
($result->{'Seconds_Behind_Master'} > 120);
&nagios_return("WARNING",  "Replication problem:
Seconds_Behind_Master=".$result->{'Seconds_Behind_Master'}) if
($result->{'Seconds_Behind_Master'} > 60);
$sth->finish();

# Disconnect from the database.
$dbh->disconnect();

&nagios_return("OK", "$result->{'Slave_IO_State'}, replicating host
$result->{'Master_Host'}:$result->{'Master_Port'}");
exit 0;

###

sub nagios_return($$) {
my ($ret, $message) = @_;
my ($retval, $retstr);
if (defined($ERRORS{$ret})) {
$retval = $ERRORS{$ret};
$retstr = $ret;
} else {
$retstr = 'UNKNOWN';
$retval = $ERRORS{$retstr};
$message = "WTF is return code '$ret'??? ($message)";
}
$message = "$retstr - $message\n";
$! = $retval;
die ($message);
}

sub usage() {
print("
Nagios script for checking the replication status of a
slave MySQL server.

Michal Ludvig <michal\@logix.cz> (c) 2006
              http://www.logix.cz/michal

  --host=<host>  Hostname or IP address to connect to.
  --port=<port>   TCP port where the server listens
  --socket=</path/to/mysqld.sock>
                  Path and filename of the Unix socket

  --user=<user>
  --password=<password>
                  Username and password of a user with
  REPLICATION CLIENT privileges. See
  below for details.

  --dbname=<dbname>
                  Name od database to open on connect.
  Should normaly not be needed.

  --help          Guess what ;-)

The script needs to connect as a MySQL user (say 'monitor')
with privilege REPLICATION CLIENT. Use this GRANT
command to create such a user:

mysql> GRANT REPLICATION CLIENT ON *.* \\
TO monitor\@localhost IDENTIFIED BY 'SecretPassword';

To access the script over SNMP put the following line
into your /etc/snmpd.conf:

extend mysql-slave /path/to/mysql-slave-check.pl \\
--user monitor --pass 'SecretPassword';

To check retrieve the status over SNMP use check_snmp_extend.sh
from http://www.logix.cz/michal/devel/nagios



");
exit 0;

}

You have the command when I run it from the command line.

In /usr/local/icinga/etc/objects/linux.cfg I have my services defined:

define service{
        use                             generic-service
        host_name                       mysqlslave1, mysqlslave2
        service_description             Check MySQL Slave
        check_command                   check_mysql_slave.pl
        }

In /usr/local/icinga/etc/objects/commands.cfg I have:

define command{
        command_name    check_mysql_slave.pl
        command_line    $USER1$/check_mysql_slave.pl --host $HOSTADDRESS$
--user $USER3$ --password $USER4$
        }

Have also tried variations as others have suggested:  putting /usr/bin/perl
at the beginning of the command_line, as well as putting the userid and
password in directly instead of using the macros

In /usr/local/icinga/etc/resources.cfg I have at the bottom of the file:

# Store some usernames and passwords (hidden from the CGIs)
$USER3$=myuserid
$USER4$=mypassword

(obviously changed for the purpose of this email).

Let me know if you see anything or have other suggestions. Thanks again,
Mark




2011/9/14 Michael Friedrich <michael.friedr...@univie.ac.at>

> **
> On 14.09.2011 17:53, Mark Creamer wrote:
>
> It replies:
>
>  OK - Waiting for master to send event, replicating host 192.168.7.85:3306
>
>
> hm. i want to see everything, even the exact command you are putting. other
> than that it will be hard to guess what you are actually doing.
>
>
>  2011/9/14 Michael Friedrich <michael.friedr...@univie.ac.at>
>
>>  On 14.09.2011 17:33, Mark Creamer wrote:
>>
>> Yep, both checks are true. Icinga user is running the script and $USER1$
>> points to /usr/local/icinga/libexec which is where the script resides.
>>
>>
>> and what is the shell output when running it manually as icinga user?
>>
>
>  --
> DI (FH) Michael Friedrich
>
> Vienna University Computer Center
> Universitaetsstrasse 7 A-1010 Vienna, Austria
>
> email:     michael.friedr...@univie.ac.at
> phone:     +43 1 4277 14359
> mobile:    +43 664 60277 14359
> fax:     +43 1 4277 14338
> web:       http://www.univie.ac.at/zid
>            http://www.aco.net
>
> Icinga Core & IDOUtils Developerhttp://www.icinga.org
>
>
>
> ------------------------------------------------------------------------------
> BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
> Learn about the latest advances in developing for the
> BlackBerry&reg; mobile platform with sessions, labs & more.
> See new tools and technologies. Register for BlackBerry&reg; DevCon today!
> http://p.sf.net/sfu/rim-devcon-copy1
> _______________________________________________
> icinga-users mailing list
> icinga-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/icinga-users
>
>


-- 
Mark
------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
Learn about the latest advances in developing for the 
BlackBerry&reg; mobile platform with sessions, labs & more.
See new tools and technologies. Register for BlackBerry&reg; DevCon today!
http://p.sf.net/sfu/rim-devcon-copy1 
_______________________________________________
icinga-users mailing list
icinga-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/icinga-users

Reply via email to