Search
j0ke.net Open Build Service
>
Projects
>
server:monitoring
>
nagios-plugins-various
> check_curl2
Sign Up
|
Log In
Username
Password
Cancel
Overview
Repositories
Revisions
Requests
Users
Advanced
Attributes
Meta
File check_curl2 of Package nagios-plugins-various
#!/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 '--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); 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 opening page! 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='Page 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 \n\n Example: check_curl.php -U http://test.example.net\n\n"; } ?>