#! /usr/bin/perl -w
#
# check_ifstatus.pl - nagios plugin 
# 
#
# Copyright (C) 2000 Christoph Kron
# Modified 5/2002 to conform to updated Nagios Plugin Guidelines (S. Ghosh)
#
# 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.
#
#
# Report bugs to: ck@zet.net, nagiosplug-help@lists.sf.net
# 
# 11.01.2000 Version 1.0
#
# $Id: check_ifstatus.pl,v 1.3 2003/02/03 05:29:54 sghosh Exp $

use POSIX;
use strict;
use lib "/usr/lib/nagios/plugins" ;
use utils qw($TIMEOUT %ERRORS &print_revision &support);

use Getopt::Long;
Getopt::Long::Configure('bundling');

my $PROGNAME = "check_ifstatus";



#my $state = "UNKNOWN";
#my $answer = "";

my $hostname;

my $opt_h ;
my $opt_V ;
my $opt_v;




# Just in case of problems, let's not hang Nagios
$SIG{'ALRM'} = sub {
     print ("ERROR: alarm timeout\n");
     exit $ERRORS{"UNKNOWN"};
};
alarm($TIMEOUT);

#Option checking
my $status = GetOptions(
		"v"   => \$opt_v, "verbose"    => \$opt_v,
		"V"   => \$opt_V, "version"    => \$opt_V,
		"h"   => \$opt_h, "help"       => \$opt_h,
		     "H=s" => \$hostname, "hostname=s" => \$hostname);
		
if ($status == 0 or !$hostname)
{
	print_help() ;
	exit $ERRORS{'OK'};
}


if ($opt_V) {
	print_revision($PROGNAME,'$Revision: 1.3 $ ');
	exit $ERRORS{'OK'};
}

if ($opt_h) {
	print_help();
	exit $ERRORS{'OK'};
}

my @hosts = split /\s*,\s*/, $hostname;

for (@hosts) {
    if (! utils::is_hostname($_)){
	print "ERROR: Not a valid hostname: $_\n";
	usage();
	exit $ERRORS{"UNKNOWN"};
    }
}

#
# For now do this in series, later reimplement as parallel checks
#

my $host;
my $state="CRITICAL";

for (@hosts) {
    $host = $_;
    my $msg = `/bin/ping -l 3 -c 3 -q -n $host`;
    $opt_v and print ">>>$msg<<<\n";
    $msg =~ /100% packet loss/ and next;
    print ("OK: $host\n");
    exit $ERRORS{OK};
}

print "CRITICAL: All hosts timed out ($hostname)\n";
exit $ERRORS{CRITICAL};


sub usage {
	printf "\nMissing arguments!\n";
	printf "\n";
	printf "check_multiple [-v|--verbose] -H <HOSTNAME>\n";
	printf "check_multiple -V|--version\n";
	printf "check_multiple -h|--help\n";
	printf "Copyright (C) 2004 Martijn Lievaart\n";
	printf "\n\n";
#	support();
	exit $ERRORS{"UNKNOWN"};
}

sub print_help {
    printf "check_multiple plugin for Nagios checks multiple hosts.\n";
    printf "If any of the hosts replies to any ping packet, this test declares success.\n";
    printf "\nUsage:\n";
    printf "   -H (--hostname)   Comma seperated list of hostnames to query - (required)\n";
    printf "   -v (--verbose)    Print ping output\n";
    printf "   -V (--version)    Plugin version\n";
    printf "   -h (--help)       usage help \n\n";
    print_revision($PROGNAME, '$Revision: 1.3 $');
	
}
