【发布时间】: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");希望它可以帮助某人。