功能说明:通过SNMP协议获取托管服务器的实时带宽使用率,并以进度条形式展示在网页上,适合IDC服务商提供给客户自助查看。
<?php
/**
* 服务器带宽使用率实时监控面板
* 适用场景:IDC服务器出租业务,客户自助查看带宽使用情况
* 前置条件:服务器需开启SNMP服务,已知community string
*/
// 配置参数
$servers = [
['ip' => '192.168.1.10', 'name' => '客户A-游戏服务器', 'community' => 'public'],
['ip' => '192.168.1.11', 'name' => '客户B-企业官网', 'community' => 'public'],
['ip' => '192.168.1.12', 'name' => '客户C-数据库服务器', 'community' => 'public'],
];
// 获取带宽数据函数
function getBandwidthUsage($ip, $community) {
// 获取流入流出字节数(需snmpget支持)
$inOctets = snmp2_get($ip, $community, '.1.3.6.1.2.1.2.2.1.10.1');
$outOctets = snmp2_get($ip, $community, '.1.3.6.1.2.1.2.2.1.16.1');
if ($inOctets === false || $outOctets === false) {
return false;
}
// 提取数值(SNMP返回格式如 "Counter32: 123456789")
preg_match('/\d+/', $inOctets, $inMatch);
preg_match('/\d+/', $outOctets, $outMatch);
$inBytes = intval($inMatch[0]);
$outBytes = intval($outMatch[0]);
// 假设带宽上限为100Mbps(可根据实际情况调整)
$maxBandwidth = 100 * 1024 * 1024 / 8; // 转换为字节/秒
$usagePercent = min(100, round(($inBytes + $outBytes) / $maxBandwidth * 100, 1));
return [
'percent' => $usagePercent,
'in' => round($inBytes / 1024 / 1024, 2), // MB
'out' => round($outBytes / 1024 / 1024, 2), // MB
];
}
// 颜色映射
function getColor($percent) {
if ($percent < 50) return '#4CAF50'; // 绿色
if ($percent < 80) return '#FF9800'; // 橙色
return '#f44336'; // 红色
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>服务器带宽使用率实时监控 - XXIDC</title>
<style>
body { font-family: Arial, sans-serif; background: #f5f5f5; margin: 20px; }
.container { max-width: 900px; margin: auto; }
h1 { color: #333; border-bottom: 2px solid #2196F3; padding-bottom: 10px; }
.server-card { background: white; border-radius: 8px; padding: 20px; margin: 15px 0; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
.server-name { font-size: 18px; font-weight: bold; color: #333; margin-bottom: 10px; }
.progress-bar { height: 26px; background: #e0e0e0; border-radius: 13px; overflow: hidden; margin: 8px 0; }
.progress-fill { height: 100%; line-height: 26px; text-align: center; color: white; font-weight: bold; transition: width 0.5s; }
.detail { font-size: 14px; color: #666; margin-top: 5px; }
.refresh-time { color: #999; font-size: 12px; text-align: right; margin-top: 20px; }
.footer { text-align: center; color: #999; margin-top: 30px; font-size: 13px; }
</style>
<meta http-equiv="refresh" content="30"> <!-- 每30秒自动刷新 -->
</head>
<body>
<div>
<h1>📊 服务器带宽使用率实时监控</h1>
<p style="color:#666;">以下数据每30秒自动更新,反映各托管服务器的实时带宽占用情况。</p>
<?php foreach ($servers as $server):
$data = getBandwidthUsage($server['ip'], $server['community']);
?>
<div>
<div>🖥️ <?php echo htmlspecialchars($server['name']); ?></div>
<div>IP:<?php echo $server['ip']; ?></div>
<?php if ($data === false): ?>
<div style="color: #f44336;">⚠️ 无法获取数据,请检查SNMP配置或网络连通性。</div>
<?php else: ?>
<div>
<div style="width: <?php echo $data['percent']; ?>%; background: <?php echo getColor($data['percent']); ?>;">
<?php echo $data['percent']; ?>%
</div>
</div>
<div>
流入:<?php echo $data['in']; ?> MB | 流出:<?php echo $data['out']; ?> MB
</div>
<?php endif; ?>
</div>
<?php endforeach; ?>
<div>⏱️ 上次更新时间:<?php echo date('Y-m-d H:i:s'); ?></div>
<div>
© 2026 XXIDC 服务器出租 · 如需开通监控功能请联系客服<br>
注:带宽上限默认为100Mbps,实际以合同约定为准。
</div>
</div>
</body>
</html>
使用说明:
将代码保存为bandwidth_monitor.php 修改$servers数组中的IP、名称和SNMP community 上传到支持PHP+SNMP扩展的Web服务器 访问该页面即可查看实时带宽使用率
