Curl并没有和Nagios打交道

时间:2014-04-16 07:13:52

标签: php curl centos nagios

我目前正在使用PHP和cURL开发一个nagios插件。

我的问题是,当我使用PHP时,我的脚本运行良好:

#php /usr/local/nagios/plugins/script.php

我的意思是它返回200个HTTP代码。

但是对于nagios,它返回一个0 HTTP代码。这很奇怪因为php正在使用NAGIOS(我可以读取变量......)。所以问题是Nagios不能使用cURL。

有人能给我一些线索吗?感谢。

在这里你可以看到我的代码。

<?php
$widgeturl = "http://google.com";
$agent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12";
    if (!function_exists("curl_init")) die("pushMeTo needs CURL module, please install CURL on your php.");

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $widgeturl);

    $page = curl_exec($ch); //or die("Curl exe failed"); 
    $code=curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($code==200) {
          fwrite(STDOUT, $page.'Working well : '.$code);
          exit(0);
    } 
    else {
          fwrite(STDOUT, $page.'not working : '.$code);
          exit(1);
    }
    curl_close($ch);

解决方案: 这是因为代理基本上是在我的操作系统(centOS)上设置的,但是Nagios没有使用它而不是PHP。所以我只需要把:curl_setopt($ ch,CURLOPT_PROXY,'myproxy:8080'); curl_setopt($ ch,CURLOPT_PROXYUSERPWD,“user:pass”);希望它可以帮助某人

2 个答案:

答案 0 :(得分:0)

您可以尝试像这样制作CURL请求(即仅标题请求):

<?php

// config
$url = 'http://www.google.com/';

// make request & parse response
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FILETIME, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response_header = curl_exec($curl);
$response_info = curl_getinfo($curl);
curl_close($curl);

// debug
echo "<b>response_header</b>\r\n";
var_dump($response_header);
echo "<b>response_info</b>\r\n";
var_dump($response_info);

以上将输出以下内容:

enter image description here

答案 1 :(得分:0)

curl_setopt($ch, CURLOPT_PROXY, 'myproxy:8080'); 
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "user:pass")