Check POST variable with check_http

Hi

I’m using icinga2 with nagios plugins for several years and is really great. Thanks for that!

I’m having trouble to setup a check_http plugin with post data. I 'don’t know what I’m doing wrong.

I’ve spend several days trying to make http_check works to post data to check a web service. Php server variable $_POST is empty.

In curl it works like this:

curl -H X-Api-Key:xxxxxxxxxxxxxxxxxxxxxx -H "Content-Type: multipart/form-data" -X POST -F 'dni=22222222' -F 'action=consultar' https://testconsulta.xxxx.com.ar/ws/dniconsultar.php

I 'm trying to replicate curl behavior calling check_http plugin like this:

/usr/lib/nagios/plugins/check_http -S -H testconsulta.xxxx.com.ar -k 'X-Api-Key:xxxxxxxxxxxxxxxxxxxxxx' -u "/ws/dniconsultar.php" -P "dni%3D22222222%3B%20action%3Dconsultar" -T "multipart/form-data" -v

But always _POST server variable in server side is empty.

¿What I’m missing to post data be sent to the server?

Best regards
Mariano

Hi @marianoacc

Welcome to the community!

I moved your post to a new topic, as the “Ask me Anything” thread is not suitable for that.

I also added some code formatting :slight_smile:
See Create topics and master Markdown formatting for formatting tips for future posts :slight_smile:

Best regards
Logic

1 Like

You may need to explicitly specify the method to check_http, can you try it with -j POST?

 -j, --method=STRING  (for example: HEAD, OPTIONS, TRACE, PUT, DELETE, CONNECT)
    Set HTTP method.

since you are familiar with curl you could use check_curl

Hi Lee

Thanks a lot by your response. I tried to add “-j POST” and the result is the same thing, the _POST variable is empty on server.

Thanks regards
Mariano

Thanks Moreamazingnick for your response, as I understand by de syntax of check_curl2:

Syntax:
    -U URL (s)
    -A Agent (s)(default: Mozilla/5.0 ... )
    -G Grep page on STRING (s)
    -L Show page (-)
    -F Follow redirects (-)
    -I Ignore SSL certificate errors (-)
    -X Exclude performance data (default: include)
    -Tc Ccritical page return time (i)
    -Tw Warning page return time (i)
    -Sbc Critical page size below SIZE (i)
    -Soc Critical page size over SIZE (i)
    -Sbw Warning page size below SIZE (i)
    -Sow Warning page size over SIZE (i)
    -S Find string between ARG1 and ARG2, return first match (s s) (example: value=\" \" )
    -T Timeout (i)(default: 10sec)
    -O Output Driven Check - Page Should respond with \"Status: OK\" or otherwise
\n\n Example:
  check_curl.php -U http://test.example.net\n\n";

It not include the posibility to add post form data.

Regards
Mariano

yes but since it’s php you can modify the code and add new parameters to check_curl

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "postvar1=value1&postvar2=value2&postvar3=value3");

Thanks a lot moreamazingnick, I followed your suggestion and finally I could send post fields correct. Here what I added to original check_curl.php code:

     case '-P':
          if ($Pocet>$i+1){
              parse_str($argv[++$i], $P);
              curl_setopt($ch, CURLOPT_POST, count($P));
              curl_setopt($ch, CURLOPT_POSTFIELDS,$P);
          if ($Debug)echo "\nDEBUG: -P ".$argv[$i];
        }else{
          echo "ERROR in parsing argument -P: missing value";
          Manual();
          exit(2);
        }
        break;
     case '-H':
         if ($Pocet>$i+1){
            curl_setopt($ch, CURLOPT_HEADER, true);
            $H=str_getcsv($argv[++$i]);


              /*              parse_str($argv[++$i], $H);*/
              curl_setopt($ch, CURLOPT_HTTPHEADER,$H);
          if ($Debug)echo "\nDEBUG: -H ".$argv[$i];
        }else{
          echo "ERROR in parsing argument -H: missing value";
          Manual();
          exit(2);
        }
        break;

I also added the option to send http headers because I need to pass with the posts parameters an API key for authorization.

And this is my Icinga2 command definition


object CheckCommand "check_api_service" {
        command = [PluginDir + "/check_curl.php" ]
        arguments = {
                "-U" = {
                        value = "$curl_apiURL$"
                        description = "URL for curl"
                            }
                "-G" = {
                        value = "$curl_apiGREP$"
                        description = "GREP for curl"
                    }
                "-P" = {
                        value = "$curl_apiPOSTF$"
                        description = "Post fields"
                    }
                "-H" = {
                        value = "$curl_apiHEADERS$"
                        description = "Http Headers"
                        }

       }
}

And this is my complete check_curl modified:

#!/usr/bin/php -q
<?php

// Changeable variables
$Debug=0;
$Timeout=10;
// --------------------

$Continue=1;
$Agent="Mozilla/5.0 (X11; U; Linux i686; cs; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7";
$Status=0;
$InludePerf=1;
$Msg='';
$ShowPage=0;

$ch = curl_init();  
$Pocet=count($argv);
if ($Pocet>1){
  for ($i=1;$i<$Pocet;$i++){
    switch ($argv[$i]){
      case '-U':    
        if ($Pocet>$i+1){
          curl_setopt($ch, CURLOPT_URL, $argv[++$i]);
          if ($Debug)echo "\nDEBUG: -U ".$argv[$i];
        }else{
          echo "ERROR in parsing argument -U: missing value";
          Manual();
          exit(2);
        }
        break;

      case '-A':    
        if ($Pocet>$i+1){
          $Agent=$argv[++$i];
          if ($Debug)echo "\nDEBUG: -A ".$argv[$i];
        }else{
          echo "ERROR in parsing argument -A: missing value";
          Manual();
          exit(2);
        }
        break;

      case '-T':    
        if ($Pocet>$i+1){
          $Timeout=$argv[++$i];
          if ($Debug)echo "\nDEBUG: -T ".$argv[$i];
        }else{
          echo "ERROR in parsing argument -T: missing value";
          Manual();
          exit(2);
        }
        break;

      case '-G':    
        if ($Pocet>$i+1){
          $Grep=$argv[++$i];
          if ($Debug)echo "\nDEBUG: -G ".$argv[$i];
        }else{
          echo "ERROR in parsing argument -G: missing value";
          Manual();
          exit(2);
        }
        break;

      case '-L':    
        $ShowPage=1;
        if ($Debug)echo "\nDEBUG: -L";
        break;

      case '-F':    
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        if ($Debug)echo "\nDEBUG: -F";
        break;

      case '-X':    
        $InludePerf=0;
        if ($Debug)echo "\nDEBUG: -X";
        break;

      case '-I':    
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,  0);
        if ($Debug)echo "\nDEBUG: -I";
        break;

      case '-Tc':    
        if ($Pocet>$i+1){
          $Tc=$argv[++$i];
          if ($Debug)echo "\nDEBUG: -Tc ".$argv[$i];
        }else{
          echo "ERROR in parsing argument -Tc: missing value";
          Manual();
          exit(2);
        }
        break;

      case '-Tw':    
        if ($Pocet>$i+1){
          $Tw=$argv[++$i];
          if ($Debug)echo "\nDEBUG: -Tw ".$argv[$i];
        }else{
          echo "ERROR in parsing argument -Tw: missing value";
          Manual();
          exit(2);
        }
        break;

      case '-Sbc':    
        if ($Pocet>$i+1){
          $Sbc=$argv[++$i];
          if ($Debug)echo "\nDEBUG: -Sbc ".$argv[$i];
        }else{
          echo "ERROR in parsing argument -Sbc: missing value";
          Manual();
          exit(2);
        }
        break;

      case '-Sbw':    
        if ($Pocet>$i+1){
          $Sbw=$argv[++$i];
          if ($Debug)echo "\nDEBUG: -Sbw ".$argv[$i];
        }else{
          echo "ERROR in parsing argument -Sbw: missing value";
          Manual();
          exit(2);
        }
        break;

      case '-Soc':    
        if ($Pocet>$i+1){
          $Soc=$argv[++$i];
          if ($Debug)echo "\nDEBUG: -Soc ".$argv[$i];
        }else{
          echo "ERROR in parsing argument -Soc: missing value";
          Manual();
          exit(2);
        }
        break;

      case '-Sow':    
        if ($Pocet>$i+1){
          $Sow=$argv[++$i];
          if ($Debug)echo "\nDEBUG: -Sow ".$argv[$i];
        }else{
          echo "ERROR in parsing argument -Sow: missing value";
          Manual();
          exit(2);
        }
        break;

      case '-S':    
        if ($Pocet>$i+2){
          $String1=$argv[++$i];
          $String2=$argv[++$i];
          if ($Debug)echo "\nDEBUG: -S ".$String1.' '.$String2;
        }else{
          echo "ERROR in parsing argument -S: missing values";
          Manual();
          exit(2);
        }
        break;
		
	  case '-O':    
        $UseOutput=1;
        if ($Debug)echo "\nDEBUG: -O";
        break;
      case '-P':
          if ($Pocet>$i+1){
              parse_str($argv[++$i], $P);
              curl_setopt($ch, CURLOPT_POST, count($P));
              curl_setopt($ch, CURLOPT_POSTFIELDS,$P);
          if ($Debug)echo "\nDEBUG: -P ".$argv[$i];
        }else{
          echo "ERROR in parsing argument -P: missing value";
          Manual();
          exit(2);
        }
        break;
     case '-H':
         if ($Pocet>$i+1){
            curl_setopt($ch, CURLOPT_HEADER, true);
            $H=str_getcsv($argv[++$i]);
 
            
              /*              parse_str($argv[++$i], $H);*/
              curl_setopt($ch, CURLOPT_HTTPHEADER,$H);
          if ($Debug)echo "\nDEBUG: -H ".$argv[$i];
        }else{
          echo "ERROR in parsing argument -H: missing value";
          Manual();
          exit(2);
        }
        break;
 
      case '--help':
      case '-h':
        Manual();
        exit(2);
        break; 
      default:
        echo "ERROR in argument parsing: ".$argv[$i];
        Manual();
        exit(2);
        break;
    }
  }
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  /*  curl_setopt($ch, CURLOPT_HEADER, 0);*/
  curl_setopt($ch, CURLOPT_USERAGENT, $Agent );
  curl_setopt($ch, CURLOPT_TIMEOUT, $Timeout);  
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  if (isset($Tc) and isset($Tw) and  ($Tw >= $Tc) ){
    echo "ERROR in arguments Tw ($Tw) >= Tc ($Tc) !!!";
    Manual();
    exit(2);
  } 
  if (isset($Sbc) and isset($Sbw) and  ($Sbw >= $Sbc) ){
    echo "ERROR in arguments Sbw ($Sbw) >= Sbc ($Sbc) !!!";
    Manual();
    exit(2);
  } 
  if (isset($Soc) and isset($Sow) and  ($Sow >= $Soc) ){
    echo "ERROR in arguments Sbw ($Sow) >= Sbc ($Soc) !!!";
    Manual();
    exit(2);
  } 

  // EXEC    
  $time_start = getmicrotime();
  $Buff=@curl_exec($ch);
  $Time = round(getmicrotime() - $time_start,3);
  $Size=strlen($Buff);
  $errnum=curl_errno($ch);
  if ($errnum){
    if ($errnum==28){
      echo 'Timeout '.$Timeout.'sec exceeded.';
      exit(2);
    }else{
      echo "ERROR in service! Err:".curl_error($ch);
      exit(2);
    }
  }
  @curl_close($ch);
  
  if (isset($Sbc) and ($Size > $Sbc)){
    $Status=2;
    $Msg.='Size '.$Size.'B below limit '.$Sbc.'B';
  }elseif (isset($Sbw) and ($Size > $Sbw)){
    $Status=1;
    $Msg.='Size '.$Size.'B below limit '.$Sbw.'B';
  }

  if (isset($Soc) and ($Size < $Soc)){
    $Status=2;
    $Msg.='Size '.$Size.'B over limit '.$Soc.'B';
  }elseif (isset($Sow) and ($Size < $Sow)){
    $Status=1;
    $Msg.='Size '.$Size.'B over limit '.$Sow.'B';
  }

  if (isset($Tc) and ($Time > $Tc)){
    $Status=2;
    $Msg.='Download time '.$Time.'sec exceeded time limit '.$Tc.'sec';
  }elseif (isset($Tw) and ($Time > $Tw)){
    $Status=1;
    $Msg.='Download time '.$Time.'sec exceeded time limit '.$Tw.'sec';
  }

  if (isset($Grep)){
    if (!strstr($Buff,$Grep)){
      $Status=2;
      $Msg.='String '.$Grep.' not found!';
    }
  }

  if (isset($String1) or isset($String2)){
    if (isset($String1) and isset($String2)){
      echo "\n".$String1."\n".$String2."\n";
      $First=strpos($Buff,$String1);
      if ($First){
        $Last=strpos($Buff,$String2,($First+strlen($String1)));
        if ($Last){
          echo $First.'-'.$Last.' - '.substr($Buff,($First+strlen($String1)),($Last-$First-strlen($String1)) );
        }else{
          echo "ERROR in arguments -S. Second string ".$String2.' not found!!!';
          Manual();
          exit(2);
        }
      }else{
        echo "ERROR in arguments -S. First string ".$String1.' not found!!!';
        Manual();
        exit(2);
      }
    }else{
      echo "ERROR in arguments -S. Must be two strings! Before and after.";
      Manual();
      exit(2);
    }
  }

  if (isset($UseOutput)){
  	$StatusHeader = "Status:";
	$StatusSeperator = "-";
	$First=strpos($Buff,$StatusHeader);
	$Last=strpos($Buff,$StatusSeperator,($First+strlen($StatusHeader)));
	$OutputStatus = trim(substr($Buff,($First+strlen($StatusHeader)),($Last-$First-strlen($StatusHeader)) ) );
    if ( strtoupper($OutputStatus) == "CRITICAL" ) {
		$Status=2;
    	$Msg.=substr($Buff,$First+8);
  	} elseif ( strtoupper($OutputStatus) == "WARNING" ){
    	$Status=1;
    	$Msg.=substr($Buff,$First+8);
	} else {
		$Status=0;
    	$Msg.=substr($Buff,$First+8);
	}
  }



  if (empty($Msg))$Msg='Service OK';
  if ($InludePerf)$Msg.= " |time=$Time size=$Size";
  echo $Msg;
  if ($ShowPage)echo "\n\n----------------------------- Page content ----------------------------\n\n".$Buff;
  exit($Status);
    
}else{
  Manual();
}

function getmicrotime(){ 
  list($usec, $sec) = explode(" ",microtime()); 
  return ((float)$usec + (float)$sec); 
} 

function Manual(){
echo "------- check_curl.php written by Vit Safar (CZ), v1.0, 20.10.2006 ---------
----------------- modified by Donald Fellow (US), v1.1, 08.06.2007 ---------\n
 Syntax:
    -U URL (s)
    -A Agent (s)(default: Mozilla/5.0 ... )
    -G Grep page on STRING (s)
    -L Show page (-)
    -F Follow redirects (-)
    -I Ignore SSL certificate errors (-)
    -X Exclude performance data (default: include)
    -Tc Ccritical page return time (i)
    -Tw Warning page return time (i)
    -Sbc Critical page size below SIZE (i)
    -Soc Critical page size over SIZE (i)
    -Sbw Warning page size below SIZE (i)
    -Sow Warning page size over SIZE (i)
    -S Find string between ARG1 and ARG2, return first match (s s) (example: value=\" \" )
    -T Timeout (i)(default: 10sec)
    -O Output Driven Check - Page Should respond with \"Status: OK\" or otherwise
    -P Allow to send post fields. (example: -P 'field1=value&field2=value')
    -H Allow to send http headers separated by ','. (example: -H 'X-Api-Key:xxxxxxxxxxxxxxxxxxxxx,Content-Type: multipart/form-data'
\n\n Example:
  check_curl.php -U http://test.example.net\n\n";
}

?>


Regards
Mariano