#!/usr/bin/perl -w
# $Id: check_lockerd,v 1.2 2006/03/13 16:13:17 wsnyder Exp $
######################################################################
#
# Copyright 2006-2006 by Wilson Snyder <wsnyder@wsnyder.org>.  This
# program is free software; you can redistribute it and/or modify it under
# the terms of either the GNU Lesser General Public License or the Perl
# Artistic License.
#
# 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.
#
######################################################################

require 5.006_001;
use lib '../blib/lib';        # testing
use Getopt::Long;
use Pod::Usage;
use strict;
use vars qw ($Debug);

use lib "/usr/lib/nagios/plugins" ;
use utils qw(%ERRORS &print_revision &support &usage);
use IPC::Locker;

#======================================================================
# main

our $PROGNAME = "check_lockerd";
my %opt_req_params = (
    host => "localhost",
);

autoflush STDOUT 1;
autoflush STDERR 1;

Getopt::Long::Configure('bundling');
if (! GetOptions (
		  "h|help"	=> \&print_usage,
		  "v|debug"	=> \&debug,
		  "V|version"	=> \&version,
		  "p|port=i"	=> sub {$opt_req_params{port}   = $_[1];},
		  "H|host|hostname=s"	=> sub {$opt_req_params{host}   = $_[1];},
		  )) {
    usage("Bad options passed, use --help for more information.\n");
}

check_lockerd(%opt_req_params);

#----------------------------------------------------------------------

sub print_usage {
    print_revision($PROGNAME, '$Id: check_lockerd,v 1.2 2006/03/13 16:13:17 wsnyder Exp $');
    pod2usage(-verbose=>2, -exitval => 2);
    support();
    exit $ERRORS{'OK'};
}

sub version {
    print_revision($PROGNAME, '$Id: check_lockerd,v 1.2 2006/03/13 16:13:17 wsnyder Exp $');
    exit $ERRORS{'OK'};
}

sub debug {
    $Debug = 1;
    $IPC::Locker::Debug = 1;
}

#######################################################################

sub check_lockerd {
    my %params = (@_);
    
    # Config requestor
    my $locker = new IPC::Locker (%params);

    # Send a request to the server, get reply & trap errors
    my $res = $locker->ping_status();
    if ($res && $res->{ok}) {
	print "LOCKERD OK - $res->{status}\n";
	exit $ERRORS{OK};
    } else {
	print "LOCKERD CRITICAL - $res->{status}\n";
	exit $ERRORS{CRITICAL};
    }
}

#######################################################################
__END__

=pod

=head1 NAME

check_lockerd - Under Nagios, check the lockerd daemon on the specified host.

=head1 SYNOPSIS

  check_lockerd --host hostname -p port

=head1 DESCRIPTION

Check_lockerd is a nagios plugin for the IPC::Locker daemon.

Add to Nagios's checkcommands.cfg:

    define command{
	command_name	check_lockerd
	command_line	$USER1$/check_lockerd -H $HOSTADDRESS$
	}

A quicker, but less accurate check is possible using the default Nagios
check_tcp plugin as follows:

    define command{
	command_name	check_lockerd
	command_line	$USER1$/check_tcp -H $HOSTADDRESS$ -p 1751
	}

=head1 ARGUMENTS

=over 4

=item --help

Displays this message and program version and exits.

=item --host

Specifies host name to check for a process.

=item --port

Specifies the port number to contact the "lockerd" on.  (default 1752)

=back

=head1 DISTRIBUTION

The latest version is available from CPAN and from L<http://www.veripool.com/>.

Copyright 2006-2006 by Wilson Snyder.  This package is free software; you
can redistribute it and/or modify it under the terms of either the GNU
Lesser General Public License or the Perl Artistic License.

=head1 AUTHORS

Wilson Snyder <wsnyder@wsnyder.org>

=head1 SEE ALSO

L<IPC::Locker>, L<IPC::Locker>, L<lockerd>, L<nagios>

=cut

######################################################################
### Local Variables:
### compile-command: "./check_lockerd "
### End:
