#!/usr/bin/perl -w

# check_telalert Nagios Plugin - Version 1.0
# Last Updated: 5/12/2003
#
# Report any bugs/questions to Russell Scibetti at nagios@quadrix.com

use Getopt::Long;
&Getopt::Long::config('bundling');

# MUST SET YOUR PATH!!
$ENV{PATH}="/usr/local/bin:/usr/bin:$ENV{PATH}";

GetOptions
        ("V"   => \$version,    "version"       => \$version,
         "h"   => \$help,       "help"          => \$help,
         "v"   => \$verbose,    "verbose"       => \$verbose,
         "H=s" => \$host,       "host=s"        => \$host,
	 "C=s" => \$contact,	"contact=s"	=> \$contact,
	 "m=s" => \$message,	"message=s"	=> \$message,
	 "t=i" => \$timeout,	"timeout=i"	=> \$timeout);

if ($version) {
  printVersion();
  exit 3;
}

if ($help) {
  printVersion();
  printHelp();
  exit 3;
}

if (!$host) {
  printf "No Host Specified!\n";
  exit 2;
}
if (!$contact) {
  printf "No Contact Specified!\n";
  exit 2;
}

if (!$message) {
  $message = "Automated Message from Nagios check_telalert plugin";
}
if (!$timeout) {
  $timeout = 30;
}

$cmd = "telalertc -monitor -monitorwait $timeout -host $host -ig $contact -m '$message'";

my @checks;
my @step;

for ($i = 0; $i < 4; $i++) {
  $checks[$i] = 0;
}

$step[0] = "Couldn't connect to host";
$step[1] = "Didn't start alert";
$step[2] = "Didn't start send";
$step[3] = "Alert Wasn't Completed";

foreach $_ (`$cmd`) { 
  if ($verbose) {
    printf $_;
  }
  if (/Connected to host/) {
    $checks[0] = 1; 
  }
  elsif (/Alert Started/) {
    $checks[1] = 1;
  }  
  elsif (/Send Started/) {
    $checks[2] = 1;
  }
  elsif (/Alert Completed/) {
    $checks[3] = 1;
  }  
}

for ($i = 0; $i < 4; $i++) {
  if (!$checks[$i]) {
    print "TelAlert Error: $step[$i]\n";
    exit 2;
  }
}

print "TelAlert OK\n";
exit 0;

################################
#
# Version and Help Information
#
################################

sub printVersion {
  printf <<EndVersion;
$0 (nagios-plugins) 1.0
The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute
copies of the plugins under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.
EndVersion
}

sub printHelp {
  printf <<EOF;

This plugin runs the telalertc TelAlert client program with the -monitor
tag, which sends an alert to the specific contact and watched the output
returned by TelAlert.  It watched for certain known steps, and reports if
any of these steps does not occur.

Usage: $0 -H <Status File> -C <Contact>
          (-m <Message> -t <Timeout>)
       $0 --help
       $0 --version
NOTE: -H and -C must BOTH be specified

Options:

 -H, --host=HOSTNAME
   Name of host that the TelAlert daemon is running on
 -C, --contact=CONTACTNAME
   Name of the contact (individual or group) to send the page do 
   (Generally, you want to create a NULL user so someone doesn't get
    a lot of test emails or pages)
 -m, --message="MESSAGE_STRING"
   Message to send in the alert
   (Default = "Automated Message from Nagios check_telalert plugin")
 -t, --timeout=Timeout(in s)
   How long to allow telalertc to try to send the alert before timing out
   (Default = 30)
 -v, --verbose
   Show each line of output from telalertc
 -h, --help
   Print detailed help screen
 -V, --version
   Print version information

EOF
}
