#!/usr/bin/perl 

# **************************************************************************************
# Info
# Realizado por : Jaime Andres Cardona
# Realizado por : jacardona@outlook.com
# Modificado    : Septiembre 7 de 2016
# Objetivo      : Send data to nrdp web for reporting into nagios check
# Versión      	: 0.1 - send data http/https, including web user and password
#                 
# **************************************************************************************
# Librerias
use POSIX;
use Getopt::Long;
Getopt::Long::config('auto_abbrev');

# **************************************************************************************
# TEST CURL COMMAND PATH

$curl_command = `which curl`;
chomp($curl_command);
# print "$curl_command\n";
if (!-e $curl_command) 
{ 
    print "Command curl don't set\n";
    exit(-1);
}


# **************************************************************************************
# Capturando parametros de entrada

my $namescript   = $0;
my $numbarg = @ARGV;

if ($numbarg < 16)
{
printf("Use: ./senddata_bycurl_nrdp.pl -wu <str> -t <str> -h <str> -s <str> -o <int> -sr <int> -m <str> -u <str>
options:
    -wu http/https user:password, use \"\" for empty web user
    -t  token string for nrdp
    -h  hostname into nagios
    -s  servicename into nagios
    -o  option type for report check, 1=host, 2=service
    -sr status to report: 0=OK,1=Warning,2=Critical,3=Unknown
    -m  message | perfdata to report
    -u  url for nrdp web ending with / example: http://x.x.x.x/nrdp/
Example 1 http/ssl without user web:
./senddata_bycurl_nrdp.pl -wu \"\" -t token_otro -h host-pasive -s check_pasivo -o 2 -sr 0 -m \"OK: Data | perfdata\" -u http://192.168.0.60/nrdp/
./senddata_bycurl_nrdp.pl -wu \"\" -t token_otro -h host-pasive -s check_pasivo -o 2 -sr 0 -m \"OK: Data | perfdata\" -u https://192.168.0.60/nrdp/
Example 2 http/ssl with user web:
./senddata_bycurl_nrdp.pl -wu \"webusr:pass\" -t token_otro -h host-pasive -s check_pasivo -o 2 -sr 0 -m \"OK: Data | perfdata\" -u http://192.168.0.60/nrdp/
./senddata_bycurl_nrdp.pl -wu \"webusr:pass\" -t token_otro -h host-pasive -s check_pasivo -o 2 -sr 0 -m \"OK: Data | perfdata\" -u https://192.168.0.60/nrdp/
");
exit($RETCODES{"UNKNOWN"});
}

# GetOptions ("length=i" => \$length,   # numeric
#       "file=s" => \$data,     # string
#       "verbose" => \$verbose);  # flag

$status_opt = GetOptions(	"wu=s" => \$httpuser,
						"t=s" => \$token,
						"h=s" => \$hostname,
						"s=s" => \$servicename,
						"o=i" => \$optcheck,
						"sr=i" => \$status,
						"m=s" => \$message,
						"u=s" => \$url);

# print parameters
# print "\n$namescript -wu \"$httpuser\" -t $token -h $hostname -s $servicename -o $optcheck -sr $status -m \"$message\" -u $url\n"


# **************************************************************************************
# Format CURL Command

# curl -H headres -f --silent --insecure -d $postdata $url
#
# Parameters
# -f, --fail		Fail silently (no output at all) on HTTP errors (H)
# -H 				"Headers"
# -s, --silent		Silent mode. Don't output anything
# -u, --user 		USER[:PASSWORD]  Server user and password
# -k, --insecure 	SSL
# -d, --data data 	HTTP POST data (H)
# --connect-timeout SECONDS  Maximum time allowed for connection

$curl_headers="-H 'Accept: text/xml,application/xml,application/xhtml+xml,' -H 'text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5' -H 'Cache-Control: max-age=0' -H 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'";
$curl_options="--connect-timeout 60 -f --silent --insecure -d";

$type_check='service';
if($optcheck==1)
    {
    	$type_check='host';
    }
chomp($httpuser);
$httpuser_size=length $httpuser;
if($httpuser_size==0)
    {
        $curl_authweb="";
    }
else
    {
        $curl_authweb="--user $httpuser";
    }

$curl_xmldata="XMLDATA=<?xml version='1.0'?> <checkresults> <checkresult type='$type_check'> <hostname>$hostname</hostname><servicename>$servicename</servicename><state>$status</state> <output>$message</output> </checkresult> </checkresults>";
$curl_datapost="cmd=submitcheck&token=$token&$curl_xmldata";

$command="$curl_command $curl_authweb $curl_headers $curl_options \"$curl_datapost\" $url";
# print "\n$command\n";

# **************************************************************************************
# Execute command 

$result=qx($command 2>&1);
$exit_code=$?;
$result_size=length $result;
# print "result_size=$result_size\n$result\n";

if($result_size==0)
    {
        print "Error [$exit_code] - No data\n";
    }
else
    {
        print "$result\n";
    }
