#!/usr/bin/perl 
#Author:
#        Otavio Honorio (otavio.honorio@opservices.com.br)
use strict;
use Getopt::Long;
use POSIX;
use File::Basename;
use DBI;
# Setting environment
$ENV{"USER"}="opuser";
$ENV{"HOME"}="/home/opuser";
# Global variables
our $name = basename($0, ".pl");
our $path = "/usr/local/opmon/libexec";
our ($oHelp, $oVerbose);
#--------------------------------------------------------------------------------------
sub main {
	getOption();
	my %hash = mysql("127.0.0.1","root","");
	open(FILE,">$name.csv");
	print FILE "HOST;NRPE;NRPE-n;NT;SNMP\n";
	for my $key ( keys %hash ) {
		my $nrpe_ssl = nrpe($hash{$key}->{'address'},"");
		my $nrpe_nossl = nrpe($hash{$key}->{'address'},"-n");
		my $nt = nt($hash{$key}->{'address'});
		my $snmp = snmp($hash{$key}->{'address'},$hash{$key}->{'snmp_version'},$hash{$key}->{'community'},$hash{$key}->{'snmp_port'});
		print FILE $key.";".$nrpe_ssl.";".$nrpe_nossl.";".$nt.";".$snmp."\n";
		print $key." ->\nNPRE SSL: ".$nrpe_ssl."\nNRPE NOSSL: ".$nrpe_nossl."\nNSCLIENT: ".$nt."\nSNMP: ".$snmp."\n\n" if ($oVerbose);
	}
	close(FILE);
}
#--------------------------------------------------------------------------------------
sub snmp {
	my $host = shift;
	my $version = shift;
	my $community = shift;
	my $port = shift;
	return "fail" if ((!$community) or ($community eq "undef") or (!$version) or ($version eq "undef") or (!$port) or ($port eq "undef"));
	my $snmp = "/usr/bin/snmpwalk";
	my $check = `$snmp -v$version -c $community $host:$port sysname -Ovq`;
	my $code = $?;
	chomp($check);
	if ($code == 0) { return $check }
	else{ return "fail" }
}
#--------------------------------------------------------------------------------------
sub nrpe {
	my $host = shift;
	my $ssl = shift;
	my $port = "5666";
	my $nrpe = $path."/check_nrpe";
	my $check = `$nrpe -H $host -p $port $ssl`;
	my $code = $?;
	chomp($check);
	if ($code == 0) { return $check }
	else{ return "fail" }
}
#--------------------------------------------------------------------------------------
sub nt {
	my $host = shift;
	my $port = "5667";
	my $nt = $path."/check_nt";
	my $check = `$nt -H $host -p $port -v CLIENTVERSION`;
	my $code = $?;
	chomp($check);
	if ($code == 0) { return $check }
	else{ return "fail" }
}
#--------------------------------------------------------------------------------------
sub mysql {
	my $host = shift;
	my $user = shift;
	my $pass = shift;
	my $database = "opcfg";
	my $sql = "select host_name, address, community, snmp_port, snmp_version from nagios_hosts";
	my %key;
	my %hash;
	my $dbh = DBI->connect("dbi:mysql:dbname=$database;host=$host",$user,$pass) or quit("Can't connect in database: $DBI::errstr",3);
	my $sth = $dbh->prepare($sql);
	quit("Query in database does not return the expected: $DBI::errstr",3) if (!defined($sth));
	$sth->execute;
	while (my ($hostname, $address, $community, $snmp_port, $snmp_version) = $sth->fetchrow_array()) {
		next if ($address =~ /^127\.0\.0\.1$|^localhost$/);
		$hash{$hostname}{'address'} = $address;
		$hash{$hostname}{'community'} = $community;
		$hash{$hostname}{'snmp_port'} = $snmp_port;
		$hash{$hostname}{'snmp_version'} = $snmp_version;
	}
	$sth->finish;
	return %hash
}
#--------------------------------------------------------------------------------------
sub quit {
	my $mgs = shift;
	my $code = shift;
	print $mgs,"\n";
	exit($code);
}
#--------------------------------------------------------------------------------------
sub getOption  {
	Getopt::Long::Configure('bundling');
	GetOptions(
		'h|help' => \$oHelp,
		'v|verbose' => \$oVerbose,
        );
	if($oHelp){
		printUsage();
		exit(1);
	}
}
#--------------------------------------------------------------------------------------
sub logger {
        my $msg = shift (@_);
        my $log = "$path/$name.log";
        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
        $wday++;
        $yday++;
        $mon++;
        $year+=1900;
        $isdst++;
        open(LOG, ">>$log");
        printf LOG ("%02i/%02i/%i - %02i:%02i:%02i => %s\n",$mday,$mon,$year,$hour,$min,$sec,$msg);
        close(LOG);
}
#--------------------------------------------------------------------------------------
sub printUsage {
       print <<EOB
Usage: $name.pl [OPTION]...

-h, --help
-v, --verbose

	Execute sem parâmetros, será criado um arquivo (csv) no local onde foi executado com as informações de testes de NRPE (com e sem SSL), NSCLIENT e SNMP.
	Para acompanhar o processo, basta utilizar o -v.

EOB
}
#--------------------------------------------------------------------------------------
&main;
