#!/usr/bin/perl -w

# check_telalert_status 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);

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

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

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

$cmd = "telalertc -status -host $host";

my @checks;
my @step;

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

$conn = 0;

$step[0] = "POPClient";
$step[1] = "EngineMini";
$step[2] = "Internet";

foreach $_ (`$cmd`) {
  if ($verbose) {
    printf $_;
  }
  if (/Connected to host/) {
    $conn = 1;
  }
  if (/POPClient Status/ && /Offline/) {
    $checks[0] = 1; 
  }
  elsif (/EngineMini1 Status/ && /Offline/) {
    $checks[1] = 1;
  }  
  elsif (/Internet Status/ && /Offline/) {
    $checks[2] = 1;
  }
}

$errors = "";

if (!$conn) {
  print "TelAlert Status:  Cannot connect to host\n";
  exit 2;
}
else {
  for ($i = 0; $i < 4; $i++) {
    if ($checks[$i]) {
      if (!$errors) {
        $errors = "$step[$i]";
      }
      else {
        $errors .=  ", $step[$i]";
      }
    }
  }
  if ($errors) {
    print "TelAlert Status: $errors Offline!\n";
    exit 2;
  }
}

print "TelAlert Status: Online\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 -status
tag, which gets Status information from the TelAlert daemons on the specified
host.  This output is watched for known output status lines messages and 
reports if any TelAlert daemons are offline or if the connection to the host
failed.

Usage: $0 -H <Host Name> (-v)
       $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
 -v, --verbose
   Show each line of output from telalertc -status
 -h, --help
   Print detailed help screen
 -V, --version
   Print version information

EOF
}


