#!/usr/bin/perl -w

=pod

=head1 NAME

check_tina.pl - nagios plugin for TiNa

=head1 SYNOPSIS

Check alerts from Time Navigator and return a netsaint/nagios compatible result

=head1 OPTIONS

check_tina.pl <outputfile> [options]

=item B<-verbose>

more times for more debugging info

=item B<-warning>

warning limit: number of severity 2 errors that generate a WARNING error

=item B<-critical>

critical limit: number of severity 2 errors that generate a CRITICAL error

=head1 DESCRIPTION

This is a nagios plugin to retrieve active alerts from a local instance of Atempo Time Navigator.
It uses the tina_shell scripting utility. Note that severity 3 errors always return CRITICAL.

=head1 FILES

F</dev/null> input file

=head1 RESTRICTIONS

Severity 1 errors are not considered

=head1 CAVEATS

tina acknowledgements are ignored

=head1 AUTHOR

(c) William Preston, noris network AG, 2005 (william.preston@noris.net)

=head1 LICENSE

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.

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 (see the file COPYING included with this
distribution); if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

=head1 HISTORY

=cut


use strict;
use Getopt::Long;
use Pod::Usage;
use POSIX;

my $help = 0;
my $warn_limit = 1;
my $crit_limit = 1;
my $error_count = 0;
my @tina_errors;
my $tmpfile;

# exit values;
# 0 = OK
# 1 = WARNING
# 2 = CRITICAL
my $returnvalue = 0;
my $returntext = "";

# debug - 0 = none, >0 = more
my $debug = 0;

# check the command line options
GetOptions('h|help|?' => \$help,
           'v|verbose|debug+' => \$debug,
	   'w|warning=s' => \$warn_limit,
	   'c|critical=s' => \$crit_limit);
	   
if ($#ARGV!=-1) {$help=1;} # wrong number of command line options
pod2usage(1) if $help;

if (! $ENV{TINA_SERVICE_NAME}) { $ENV{TINA_SERVICE_NAME} = "tina" };
if (! $ENV{TINA_HOME}) { $ENV{TINA_HOME} = "/usr/tina" };
$ENV{LD_LIBRARY_PATH}=$ENV{LD_LIBRARY_PATH}?"$ENV{LD_LIBRARY_PATH}:$ENV{TINA_HOME}/Lib":"$ENV{TINA_HOME}/Lib";

do {$tmpfile = tmpnam();} until sysopen(FH, $tmpfile, O_RDWR|O_CREAT|O_EXCL, 0666);


print FH <<EOL;
list Alarm in alarmlist
foreach myalarm in alarmlist
 assign AlarmMessage &mymessage
 assign AlarmSeverity &myseverity
 get myalarm
 echo myseverity \\\"mymessage\\\"
endfor
EOL

close FH;

open (FH2, "$ENV{TINA_HOME}/Bin/tina_shell -identity tina -file $tmpfile |") or die "tina shell not found";

while (<FH2>) {
  my $alarm;

  if (! /([0-9]*) "(.*)"/ms) {
    # concatenate multi-line errors
    chomp;
    $_ = $_.<FH2>;
    redo;
  }
  if ($debug>1) { print "$1,\"$2\"\n"; }
  $alarm->{level}=$1;
  $alarm->{text}=$2;
  push @tina_errors,($alarm);
} 

# sort errors by severity
my @newarray = sort {$a->{level} <=> $b->{level}} (@tina_errors);

while (my $alarm = pop (@newarray)) {

  if ($alarm->{level}>2) {
    # always return critical if severity 3 alarms occur
    $returnvalue = 2;
    $returntext .= $alarm->{text}."<br>";
  }

  if ($alarm->{level}==2) {
    $error_count++;
    $returntext .= $alarm->{text}."<br>";
  }

  ## ignore severity 1 errors
  #if ($alarm->{level}==1) {last;}
  if ($alarm->{level}==1) {
    # ignore licence key warnings
    next if $alarm->{text} =~ /current license key/;
    $returntext .= $alarm->{text}."<br>";
  }

  
}

if ($error_count >= $crit_limit) {
  if ($debug) { print "Number of warnings $error_count passed CRITICAL limit $crit_limit\n"; }
  $returnvalue = 2;
} elsif ($error_count >= $warn_limit) {
  if ($debug) { print "Number of warnings $error_count passed WARNING limit $warn_limit\n"; }
  $returnvalue = $returnvalue<1?1:$returnvalue;
}

close FH2;

unlink $tmpfile;

if ($returntext eq "") {$returntext = "OK - no alerts";}
print $returntext;
exit $returnvalue;
