#!/usr/local/bin/perl
# Checks if a MySQL-server is online
# Copyright (c) 2000 Mark van Eijk <mark@luon.net>
# Notes:
# Make sure that the remote host accepts MySQL-connections from
# the host this plugin runs on.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# use strict here to ensure 'decent' coding
use strict;
use Getopt::Std;
# added for database interfacing
use DBI;
use DBD::mysql;
use vars qw($opt_p $opt_t $opt_h);
getopts("p:t:h:");
# standard port for mysql is 3306
my $port = $opt_p || 3306;
my $TIMEOUT = $opt_t || 5;
my $host = $opt_h || "localhost";
exit &serviceGET($host, $port, $TIMEOUT);
sub serviceGET
{
my ($server, $port, $serviceTIMEOUT) = @_;
# return failure status code by default
my $serviceOK = 1;
eval
{
# set timeout alarm
local $SIG{ALRM} = sub { die "Timeout Alarm" };
alarm $serviceTIMEOUT;
# load MySQL database driver
my $mysqlDriverHandle = DBI->install_driver("mysql");
# query MySQL-server for list of databases
my @databases = $mysqlDriverHandle->func($server, $port, '_ListDBs');
# cancel alarm
alarm 0;
if (defined @databases)
{
# change status code to OK
$serviceOK = 0;
}
};
#check for timeout
if ($@ and ($@ =~ /Timeout Alarm/))
{
# change status code to TIMEOUT
$serviceOK = 2;
}
# exit with service status code
return $serviceOK;
}
syntax highlighted by Code2HTML, v. 0.9.1