#==========================================================================
# Author      : alex lanz - konzept.it
# Description : nagios plugin to check the kaspersky admin kit
# Date        : 29-08-2011
# Version	  : 1.1
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   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.  If not, see <http://www.gnu.org/licenses/>.
#==========================================================================
use warnings;
use strict;



#server configuration
my $server="WS1";
my $instancename="KAV_CS_ADMIN_KIT";
my $databasename="KAV";


#configuration
my $passive=0;										#passive check: 1=Yes, 0=No
my @groups= (0);									#you can find out the groups with the following sql command: SELECT strDisplayName, nGroup FROM dbo.hosts


my $check_update=1;									#check updates: 1=Yes, 0=No
my $check_virus=1;									#check virus: 1=Yes, 0=No
my $check_expirationtime=1;							#check expiration time: 1=Yes, 0=No


my $lastvisible=1;									#check updates if host is not older than $lastvisible (days)
my $update_warning=4;								#warning if last update is older than $update_warning (days)
my $update_critical=7;								#critical if last update is older than $update_critical(days)
my $expirationtime_warning=14;						#warning if licenz expires in $expirationtime_warning (days)
my $expirationtime_critical=2;						#critical if licenz expires in $expirationtime_critical (days)


my @exceptions_update = ();							#hosts that won't be checked (update)
my @exceptions_virus = ("STATION6", "SERVER1");		#hosts that won't be checked (virus)
my @exceptions_expirationtime = ();					#hosts that won't be checked (expirationtime)




#possible nagios states
my $OK=0;																		
my $WARNING=1;
my $CRITICAL=2;
my $UNKNOWN=3;
my $DEPENDENT=4;


#variables
my $i;
my $j;
my $k=0;
my $size;
my @output;


#groups
my $group;
$size=$#groups + 1;

if($size > 0){
	$group='WHERE nGroup=' . $groups[0];
}
if($size > 1){
	for ($i=1;$i<$size;$i++){
		$group.=' OR nGroup=' . $groups[$i];
	}
}


#sql query
chomp (my @res_tmp=`sqlcmd -S $server\\$instancename -d $databasename -Q "SELECT h.strwinhostname as WinHostName, datediff (day, h.tmLastVisible, getdate()) as LastVisible, datediff (day, h.tmLastUpdate, getdate()) as LastUpdate, h.nVirusCount - h.nVirusCountBase as VirusCount, datediff (day, getdate(), k.tmExpirationTime) as Expirationtime FROM dbo.Hosts as h JOIN dbo.apphostskeys as k ON h.nId = k.nIdhost $group"`);


#save result into array
$i=2;
$j=0;
my @values;
my @tmp;


while (defined($res_tmp[$i])){
	if(($res_tmp[$i] !~ /[()]/) && ($res_tmp[$i] ne "")){
		
		@tmp = split(/[ \n]+/, $res_tmp[$i]);
		$values[$j++]=$tmp[0];	#WinHostName
		$values[$j++]=$tmp[1];	#LastVisible
		$values[$j++]=$tmp[2];  #LastUpdate
		$values[$j++]=$tmp[3];	#VirusCount
		$values[$j++]=$tmp[4];	#ExpirationTime
	}
	$i++;
}


#items
$size=$#values + 1;


#check last update
my $result;
my $state=$OK;

if($check_update == 1){
	for ($i=0;$i<$size;$i+=5){

		if( grep{$_ eq $values[$i]} @exceptions_update){
			next;
		}
		
		if($values[$i+1] > $lastvisible){
			next;
		}
		
		if($values[$i+2] > $update_warning){
			$state = $WARNING if $state == $OK;
			$output[$k] = $values[$i] . " had the last update " . $values[$i+2] . " days ago.";
			$k++;
		}
		
		$state = $CRITICAL if $values[$i+2] > $update_critical;
	}
}


#check expirationtime
if($check_expirationtime == 1){
	for ($i=0;$i<$size;$i+=5){

		if( grep{$_ eq $values[$i]} @exceptions_expirationtime){
			next;
		}
		
		if($values[$i+4] <= $expirationtime_warning){
			$state = $WARNING if $state == $OK;
		}
		
		if($values[$i+4] <= $expirationtime_critical){
			$state = $CRITICAL;
			
			if($values[$i+4] > 0){
				$output[$k] = $values[$i] . " licenz expires in " . $values[$i+4] . " days.";
				$k++;
			}else{
				$output[$k] = $values[$i] . " licenz expired.";
				$k++;
			}
		}
		
	}
}


#check virus count
if($check_virus == 1){
	for ($i=0;$i<$size;$i+=5){

		if( grep{$_ eq $values[$i]} @exceptions_virus){
			next;
		}
		
		
		if($values[$i+3] > 0){
			$state=$CRITICAL;
			$output[$k] = $values[$i] . " has " . $values[$i+3] . " viruses.";
			$k++;
		}
	}
}


#print messages and return state
if($state == $OK){
	print "OK - Kaspersky Administation Kit";
}else{
	if($k == 1){
		print "1 message";
	}else{
		print $k, " messages";
	}
	
	print " from the Kaspersky Administration Kit. ";
	
	for ($i=0;$i<$k;$i++){
		print "\n" if $passive != 1;
		print $output[$i], " ";
	}
}

exit $state;