#!/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
# BasedOn       : senddata_bycurl_nrdp.pl
# Versión      	: 0.1 - send result check or nagios plugin to nrdp based on code senddata_bycurl_nrdp.pl
#                 
# **************************************************************************************
# Librerias
use POSIX;
use Getopt::Long;
Getopt::Long::config('auto_abbrev');
use Switch;

# **************************************************************************************
# 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: ./sendexec_bycurl_nrdp.pl -wu <str> -t <str> -h <str> -s <str> -o <int> -pt <str> -pr <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
    -pt path to script
    -pr parameters 
    -u  url for nrdp web ending with / example: http://x.x.x.x/nrdp/
Example 1 http/ssl without user web:
./sendexec_bycurl_nrdp.pl -wu \"\" -t token_otro -h host-pasive -s check_pasivo -o 2 -pt /usr/local/nagios/lib/check_users -pr \"-w 0 -c 0\" -u http://192.168.0.60/nrdp/
./sendexec_bycurl_nrdp.pl -wu \"\" -t token_otro -h host-pasive -s check_pasivo -o 2 -pt /usr/local/nagios/lib/check_users -pr \"-w 0 -c 0\" -u https://192.168.0.60/nrdp/
Example 2 http/ssl with user web:
./sendexec_bycurl_nrdp.pl -wu \"webusr:pass\" -t token_otro -h host-pasive -s check_pasivo -o 2 -pt /usr/local/nagios/lib/check_users -pr \"-w 0 -c 0\" -u http://192.168.0.60/nrdp/
./sendexec_bycurl_nrdp.pl -wu \"webusr:pass\" -t token_otro -h host-pasive -s check_pasivo -o 2 -pt /usr/local/nagios/lib/check_users -pr \"-w 0 -c 0\" -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,
						"pt=s" => \$path,
						"pr=s" => \$parameters,
						"u=s" => \$url);

# print parameters
# print "\n$namescript -wu \"$httpuser\" -t $token -h $hostname -s $servicename -o $optcheck -pt $path -pr \"$parameters\" -u $url\n"

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

$command="$path $parameters";
# print "\ncommand=$command\n"
$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";
        exit(-1);
    }
else
    {
        #print "\nexit_code=$exit_code\nResult=$result\n";
        # Asign the correct exit code
        # 0=OK,1=Warning,2=Critical,3=Unknown
        switch ($exit_code) 
        {
            case 0      { $exit_code=0 } # OK
            case 1      { $exit_code=1 } # WARNING
            case 256    { $exit_code=1 } # WARNING
            case 2      { $exit_code=2 } # CRITICAL
            case 512    { $exit_code=2 } # CRITICAL
            else        { $exit_code=3 } # UNKNOWN
        }
        $status=$exit_code;
        $message=$result;
        # print "\nstatus=$status\nmessage=$result\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 transfer 


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

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


