#!/usr/bin/perl
#
# File name: 		check_sim_healthstatus.pl
# Purpose:	    	Poll HP System Insight Manager SQL Database for events and incidents for a specific host
# Author:		    Sven Bauknecht
# License:			GPLv2
# Contact:			sbauknecht@gmx.net
# LastChangeDate:	2011-02-08

use DBI;
use Getopt::Long;
use POSIX qw(strftime);

# define vars
my ($server,$all,$help);

# Database information
$dsn = 'DBI:Sybase:server=hpsim'; # check /etc/freetds/freetds.conf for details
$db_name = "Insight_vXXXXXXXXXXXXXXXXX";

# default exit code (icinga state unkown)
my $ec=3;

# handle command line options
my $opts = GetOptions("server=s" => \$server,
		      "all=s" => \$all,
		      "help=s" => \$help);
if ( $help ) {
   print "Usage: check_sim_eventstatus.pl --server=servernameinSIMDatabase\n";
   exit;
}


# Database control
my $dbh = DBI->connect($dsn, "SERVER\\USER", 'MYTOPSECRETPASSWORD');
die "unable to connect to server $DBI::errstr" unless $dbh;
$dbh->do("use $db_name");
$query = "SELECT timestamp, DeviceName, evStatus FROM dbo.icinga_status2 WHERE DeviceName LIKE '$server'";
if ($all) {
   $query = "SELECT timestamp, DeviceName, evStatus FROM dbo.icinga_status_event";
}
$sth = $dbh->prepare ($query) or die "prepare failed\n";
$sth->execute( ) or die "unable to execute query $query error $DBI::errstr";


# process HP SIM Database results
while ( @first = $sth->fetchrow_array ) {
   my $seconds = ( $first[0] / 1000 );
   my $incident_time = strftime "%Y-%m-%d_%H:%M:%S", localtime($seconds);
   if ($first[2] == 5) {
	print "CRITICAL incident at " . $incident_time . " " . $first[1] . " Statuscode " . $first[2] . "|SIMEventStatus=$first[2];3;5;0;5";
	print "\n";
        $ec = 2;
   }
   elsif ($first[2] == 4) {
        print "MAJOR incident at " . $incident_time . " " . $first[1] . " Statuscode " . $first[2] . "|SIMEventStatus=$first[2];3;5;0;5";
	print "\n";
        $ec = 1;
   }
   elsif ($first[2] == 3) {
        print "MINOR incident at " . $incident_time . " " . $first[1] . " Statuscode " . $first[2] . "|SIMEventStatus=$first[2];3;5;0;5";
	print "\n";
        $ec = 1;
   }
   elsif ($first[2] == 2) {
        print "WARNING incident at " . $incident_time . " " . $first[1] . " Statuscode " . $first[2] . "|SIMEventStatus=$first[2];3;5;0;5";
	print "\n";
        $ec = 1;
   } 
   elsif ($first[2] == 1) {
        print "NORMAL " . $first[1] . " at " . $incident_time . " " . $first[2] . " Statuscode " . $first[2] .  "|SIMEventStatus=$first[2];3;5;0;5"; 
	print "\n";
        $ec = 0;
   }
   elsif ($first[2] == 16) {
        print "INFO " . $first[1] . " at " . $incident_time . " " . $first[2] . " Statuscode " . $first[2] .  "|SIMEventStatus=$first[2];3;5;0;5"; 
	print "\n";
        $ec = 0;
   }
   else {
        print "UNKNOWN: incident at " . $incident_time . " " . $first[1] . " Statuscode " . $first[2] . "|SIMEventStatus=$first[2];3;5;0;5";
	print "\n";
        $ec = 3;
   } 
   #else { 
   #     print "OK " . $first[1] . " at " . $incident_time . " " . $first[2] .  "\n";
   #     $ec = 0;
   #} 
}
exit $ec;
