【问题标题】:Curl isn't woking with NagiosCurl 不适用于 Nagios
【发布时间】:2014-04-16 07:13:51
【问题描述】:

我目前正在用 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");希望它可以帮助某人

【问题讨论】:

  • 嗯,我找到了答案。这是因为代理基本上是在我的操作系统(centOS)上设置的,但 Nagios 并没有使用它而不是 PHP。所以我只需要输入: curl_setopt($ch, CURLOPT_PROXY, 'myproxy:8080'); curl_setopt($ch, CURLOPT_PROXYUSERPWD, "user:pass");希望它可以帮助某人。

标签: php curl centos nagios


【解决方案1】:
curl_setopt($ch, CURLOPT_PROXY, 'myproxy:8080'); 
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "user:pass")

【讨论】:

    【解决方案2】:

    你能尝试像这样发出 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);
    

    上面会输出以下内容:

    【讨论】:

    • 我试过了,但没有任何改变,Nagios 和 PHP 上的 HTTP 代码不一样...
    猜你喜欢
    • 1970-01-01
    • 2015-06-05
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 2014-08-17
    相关资源
    最近更新 更多