#!/usr/bin/perl -w # # # check_ifpingok -H -C # # Do the given check_command if the ping check of hostname is OK. # If ping is not OK than an OK is assumed. If ping is OK the statuscode # and message of the check_command is returned. # # Nagios command example: # command_line $USER1$/check_ifpingok -H $HOSTADDRESS$ -C "$USER1$/check_printer -H $HOSTADDRESS$" # # Changes and Modifications # ========================= # 18.12.2007 - 1.0.0 R. Kaiser autinform # Created # use Time::Local; use POSIX; use strict; use Getopt::Long; use vars qw($opt_C $opt_H $opt_h $opt_V $verbose); use vars qw($PROGNAME); use vars qw($REVISION); # Place to find "utils.pm", change is necessary use lib "/usr/lib/nagios/plugins" ; use utils qw($TIMEOUT %ERRORS &print_revision &support &usage); # Programname and version $PROGNAME = "check_ifpingok"; $REVISION = "\$Revision: 1.0.0 \$"; # Definition of my defaults, change if necessary my $def_pingcommand="/usr/lib/nagios/plugins/check_ping"; my $def_pingparams="-w 100.0,20% -c 500.0,60% -p 3"; sub print_help (); sub print_usage (); sub zeit_wandeln_in_sek (); Getopt::Long::Configure('bundling'); GetOptions ("V" => \$opt_V, "version" => \$opt_V, "h" => \$opt_h, "help" => \$opt_h, "v" => \$verbose, "verbose" => \$verbose, "H=s" => \$opt_H, "hostname=s" => \$opt_H, "C=s" => \$opt_C, "check_command=s" => \$opt_C); if ($opt_V) { print_revision($PROGNAME,$REVISION); exit $ERRORS{'OK'}; } if ($opt_h) {print_help(); exit 0;} ($opt_H) || ($opt_H = shift) || usage("Server name not specified\n"); my $mhost = $1 if ($opt_H =~ /^([-_.A-Za-z0-9]+\$?)$/); ($mhost) || usage("Invalid hostname: $opt_H\n"); ($opt_C) || ($opt_C = shift) || usage("Check Command not specified\n"); my $mcmd = $opt_C; ($mcmd) || usage("Invalid check command: $opt_C\n"); print "Server: $mhost\n" if $verbose; print "Check : $mcmd\n" if $verbose; # end of params checking my $state = "OK"; my $answer = undef; # Just in case of problems, let's not hang Nagios $SIG{'ALRM'} = sub { print "No Answer from Client\n"; exit $ERRORS{"UNKNOWN"}; }; alarm($TIMEOUT); ########## Action # Try to ping the given hostname. my $ping_check = qx($def_pingcommand -H $mhost $def_pingparams); my $ping_code = $?; $ping_check =~ s/\n$//; # remove trailing linefeed print "PING: $ping_code - $ping_check\n" if $verbose; if ( $ping_code == 0 ) { my $cmd_check = qx($mcmd); my $cmd_code = $?; $cmd_check =~ s/^ +//g; # remove leading blanks $cmd_check =~ s/\n$//; # remove trailing linefeed print "Check: $cmd_code - $cmd_check\n" if $verbose; $answer = $cmd_check; if ( $cmd_code == 0 ) { $state = "OK"; } elsif ( $cmd_code == 256 ) { $state = "WARNING"; } elsif ( $cmd_code == 768 ) { $state = "UNKNOWN"; } else { $state = "CRITICAL"; } } else { $answer = "Assumed to be OK, Ping fails, no further check done.\n"; $state = "OK"; } #Turn off alarm alarm(0); print $answer; exit $ERRORS{$state}; ############################################################################ sub print_usage () { print_revision($PROGNAME,$REVISION); print "Usage: $PROGNAME -H -C \n"; } ### sub print_help () { print "Do the check_command if ping of hostname is ok. "; print_usage(); print " -H, --hostname=STRING name or ip of the srver to be checked -C, --check_command=STRING Nagios check command to be executed if ping was ok -v --verbose debugging output "; }