#!/usr/bin/perl


# ==================== Load Perl modules ====================
use strict;
use Getopt::Long;
use DBI;
use Data::Dumper;

# ============ Database connect information ==================
my $dbOpcfg = "opcfg";
my $dbOpmon4 = "opmon4";
my $dbUser = "root";
my $dbPass = "";
my $opt_host = $ARGV[0]; 
# ==================== Functions =============================
sub print_usage(){
	printf "\nUsage:\n";
	printf "  Usage: check_ic_status.pl [host_name]\n";
	exit(2);
}


if (not defined $opt_host){
	print_usage();
}

my $dbh = DBI->connect("DBI:mysql:database=$dbOpmon4;host=localhost", $dbUser, $dbPass) || die "Could not connect to database: $DBI::errstr";



#verifica se ic possui kpis de disponibilidade
my $sti = $dbh->prepare("select opcfg.nagios_services.service_description, opcfg.nagios_services.process_perf_data from opcfg.nagios_services left join opmon4.service_status ON opmon4.service_status.service_id = opcfg.nagios_services.service_id where opmon4.service_status.host_name = '$opt_host'");

$sti->execute();
my $check_kpis = $sti->rows;

if (!$check_kpis){
		$sti->finish();
		$dbh->disconnect();
		print "IC Nao Possui KPIs\n";
		exit(2);
} 



my @ics_avail;
our $avail;
our $name;
our $perf;
our $count_kpis;

while (($name,$perf) = $sti->fetchrow_array()) {
	push @ics_avail, ($name,$perf);

	#print "$name - $perf\n";
	
	if ($perf eq '0'){
		$avail++;
	#	print "$avail\n";
	}
	$count_kpis++;
}

#print "$avail\n";



$sti->finish();

our @data;

if ($avail){


	my $stk = $dbh->prepare("select opcfg.nagios_services.service_description from opcfg.nagios_services left join opmon4.service_status ON opmon4.service_status.service_id = opcfg.nagios_services.service_id where opmon4.service_status.host_name = '$opt_host' and opcfg.nagios_services.process_perf_data = '0'");
	$stk->execute();
	my $rows = $stk->rows;
    $stk->finish();	


	#print "$rows\n"; 


	my $sth = $dbh->prepare("select opcfg.nagios_services.service_description,opmon4.service_status.current_state from opcfg.nagios_services left join opmon4.service_status ON opmon4.service_status.service_id = opcfg.nagios_services.service_id where opmon4.service_status.host_name = '$opt_host' and opcfg.nagios_services.process_perf_data = '0' and opcfg.nagios_services.process_perf_data is not NULL and opmon4.service_status.current_state not in ('0','1') and opmon4.service_status.state_type = 1 and opmon4.service_status.problem_has_been_acknowledged = 0 and opmon4.service_status.host_id not in (SELECT opmon4.host_downtime.host_id  FROM opmon4.host_downtime where opmon4.host_downtime.end_time >= now() and opmon4.host_downtime.start_time <= now())");
	
	$sth->execute();
    my $count_avail;
	my $count_ics_avail;
	
	while (my $row = $sth->fetchrow_hashref()) {
		my $status = $row->{current_state};
			if ($status > 2){
				$count_avail++;
			}
			if ($status == 2){	
				push @data, $row;
			}
			
	}
	$sth->finish();

	if ($count_avail == $rows){
		$dbh->disconnect();
		print "IC - Desconhecido (Verificar possiveis problemas de checagem)\n";
		exit(2)
	}
	

}else{

    my $sta = 	$dbh->prepare("select opcfg.nagios_services.service_description from opcfg.nagios_services left join opmon4.service_status ON opmon4.service_status.service_id = opcfg.nagios_services.service_id where opmon4.service_status.host_name = '$opt_host' and opmon4.service_status.current_state not in ('0','1','2') and opmon4.service_status.state_type = 1");
	
	$sta->execute();
	
	my $count_desc;
	
	while (my $row = $sta->fetchrow_hashref()) {
		push @data, $row;
		$count_desc++;
	}
	$sta->finish();
	$dbh->disconnect();
	
	#print "$count_kpis == $count_desc\n";
	
	if ($count_kpis == $count_desc){
		print "IC - Desconhecido (Verificar possiveis problemas de checagem)\n";
		exit(2)
	}else{
		print "IC - OK" . "\n";
		exit(0);
	}
		

}
# Disconnect from the database.
$dbh->disconnect();

#print Dumper @data; 
my $msg_ok = "IC - Ok";
#Se não tiver nenhum problema já manda o print OK
if (!@data){
	print $msg_ok."\n";
	exit(0);
}

my $msg_aics_erro = 'IC - Critical: ';
my $count_erros = 0;
foreach my $aic (@data){
    	$msg_aics_erro .=  ", "if($count_erros != 0 );
    	$msg_aics_erro .= $aic->{'service_description'};
    	$count_erros++;
}

if($count_erros > 0 ){
	print $msg_aics_erro."\n";
	exit(2);
}else{
	print $msg_ok."\n";
	exit(0);
}
