#!/usr/bin/perl -w

#Script zur Anzeige der angemeldeten Agenten und KeyUser in OTRS
#Copyright (c) 2008 by Michael Glaess


use strict;
use Getopt::Long;
use vars qw($opt_t $opt_q $opt_c $opt_w $opt_v $opt_h);
my(%ERRORS) = ( OK=>0, WARNING=>1, CRITICAL=>2, UNKNOWN=>3, WARN=>1, CRIT=>2 );
use DBI;
use DBD::mysql;
use Net::SMTP;
sub print_help();

my $VERSION ="1.1";
my $DBuser  ="nagios";
my $DBpass  ="nagios";
my $opt_v;
my $opt_h;
my $opt_w;
my $opt_c;
my $opt_H;
my $opt_U;
my $opt_P;
my $status  ="0";	
my $anzahl;
my $anzahla;
my $anzahlu;
my $sql;
my $result;
my $message;

sub check_otrs {
#Check DatabaseConnection
my $dbhm=DBI->connect("dbi:mysql:otrs:$opt_H","$opt_U","$opt_P",
    {
    PrintError=>1,
    }
);
unless ( $dbhm ) {
    die("No Connection to Database");
    }
my $dbhm2=DBI->connect("dbi:mysql:otrs:$opt_H","$opt_U","$opt_P",
    {
    PrintError=>1,
    }
);


# Anzahl Agenten
    $sql = "Select count(*) as Anzahl from sessions where left(session_value,4)='User'";
    
my $sqlp=$dbhm->prepare($sql);
if (!$sqlp->execute()){
    print "CRITICAL - Unable to Execute SQL-Query";
    $status = $ERRORS{'CRITICAL'};
    }    
    $result=$sqlp->fetchrow_hashref();
    $anzahla=$result->{Anzahl};

# Anzahl KeyUser
    $sql = "Select count(*) as Anzahl from sessions where left(session_value,4)<>'User'";

$sqlp=$dbhm->prepare($sql);
if (!$sqlp->execute()){
    print "CRITICAL - Unable to Execute SQL-Query";
    $status = $ERRORS{'CRITICAL'};
    }    
    $result=$sqlp->fetchrow_hashref();
    $anzahlu=$result->{Anzahl};

    $anzahl=$anzahla + $anzahlu;
#Set Optimize Text for Nagios
    $message = "Anzahl Agenten $anzahla<br>";
    $message .= "Anzahl KeyUser $anzahlu<br>";
    $message .= "Gesamt $anzahl";    
    $message = $message." |Agenten=$anzahla;KeyUser=$anzahlu;Gesamt=$anzahl";
	
	
if    ($anzahl >= $opt_c) 
	{$status = $ERRORS{'CRITICAL'};}
elsif ($anzahl >= $opt_w) 
	{$status = $ERRORS{'WARNING'};}
else { $status = $ERRORS{'OK'};}


$sqlp->finish();

}
####################################


sub print_help () {
    printf "$0 plugin for Nagios check for count of Agents/KeyUsers loggedin in OTRS\n";
    printf "Copyright (c) 2008 Michael Glaess\n";
    printf "Usage:\n";
    printf "   -w (--warn)   Warning-Level, Default: 0\n";
    printf "   -c (--crit)   Criticle-Level, Default: 2\n";
    printf "   -v            Version\n";
    printf "   -h (--help)   Help\n";
    printf "\n";
    print_usage();    
}
##############################################
sub print_usage () {
	print "Usage: $0 \n";
	print "       $0 -w 2 -c 3\n";
	print "       $0 -w 2 -c 4\n";
}
###############################################
$ENV{'BASH_ENV'}=''; 
$ENV{'ENV'}='';

Getopt::Long::Configure('bundling');
GetOptions
	("v" => \$opt_v, "version"    	=> \$opt_v,
	 "h" => \$opt_h, "help"       	=> \$opt_h,
	 "w:i" => \$opt_w, "warn" 	=> \$opt_w,
	 "c:i" => \$opt_c, "crit" 	=> \$opt_c,
	 "H:s" => \$opt_H, "U:s"	=> \$opt_U,
	 "P:s" => \$opt_P );

#Set default Values
if ( !$opt_w){$opt_w=1;} #Warning at just one ticket
if ( !$opt_c){$opt_c=2;} #Set Critical even on two tickets
if ( !$opt_H){$opt_H="localhost";}
if ( !$opt_U){$opt_U=$DBuser;}
if ( !$opt_P){$opt_P=$DBpass;}

#printf "OPT_t = $opt_t,OPT_w= $opt_w, OPT_c = $opt_c, OPT_q = $opt_q\n";
if ($opt_v) {
	print "$0: $VERSION\n" ;
	exit $ERRORS{'OK'};
}
if ($opt_h) {print_help(); exit $ERRORS{'OK'};}

$status = $ERRORS{OK}; $message = '';

#Call CheckUp Routine
check_otrs;

#Give System a feedback what have we done
if( $message ) {
	if( $status == $ERRORS{OK} ) {
		print "OK: ";
	} elsif( $status == $ERRORS{WARNING} ) {
		print "WARNING: ";
	} elsif( $status == $ERRORS{CRITICAL} ) {
		print "CRITICAL: ";
	}
	print "$message\n";
} else {
	$status = $ERRORS{UNKNOWN};
	print "No Data yet\n";
}
exit $status;
