【发布时间】:2015-07-29 11:42:32
【问题描述】:
我一直在使用 PHP + cURL 进行自制服务器监控的有趣项目。现在,我一直在尝试使用 cURL 通过以下命令将 POST 数据发送到 PHP file:
echo "temp=`sensors | grep 'Core 1' | cut -c9-21 | tr -d ' '`" | curl -s -d @- http://10.0.0.201/statusboard/temp.php
问题是,它似乎没有发布任何数据 PHP:
<?php
//add your server aliases here
$servers = array(
"10.0.0.201" => "Larry",
"10.0.0.56" => "Le Mac Pro",
);
if(isset( $_POST['temp'], $_POST['df'] )){
preg_match('/\d+\.\d{2}/', $_POST['temp'],$temp);
preg_match('/\d+%/', $_POST['df'],$df);
$stats = array(
"temp" => $temp[0],
"ip" => $_SERVER["REMOTE_ADDR"]
);
save_to_stats($stats);
}else{
output_stats_table();
echo "empty";
echo "<table>";
foreach ($_POST as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
function save_to_stats($stats){
$data = json_decode( file_get_contents("temps.json"), true );
$data[ $stats['ip'] ] = $stats;
file_put_contents("stats.json", json_encode($data), LOCK_EX);
}
function output_stats_table(){
global $servers;
//display data
$data = json_decode( file_get_contents("temps.json"), true );
?>
<table id="projects">
<?php foreach($data as $server => $stats): ?>
<tr>
<td class="server-status" style="width:48px;" ><?php if (time() - (int) $stats['time'] > $timeinsecondstoalert )
{
}
else
{
} ?></td>
<td class="server-name" style="width:200px; text-transform:lowercase;"><?php echo $servers[$stats['ip']] ; ?></td>
<td class="server-load" style="width:72px;"><?php echo $servers[$stats['temp']] ; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php
};
function number_of_bars($df){
$value = (int) str_replace('%', '', $df) / 10;
return round( ($value > 8 ? 8 : $value) * .8 );
}
我完全不知道问题出在哪里。如果没有生成 POST 数据,JSON 文件也不是,因此没有数据。
【问题讨论】:
-
echo "temp=
sensors | grep 'Core 1' | cut -c9-21 | tr -d ' '" 的输出是什么? -
@AkshayHegde 只是输出,即 +38.0°