<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Linux 服务器资源仪表盘(PHP 版,含 DataDisk)</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Microsoft YaHei", Arial, sans-serif;
}
body {
background-color: #1e1e2e;
color: #f0f0f0;
padding: 20px;
}
.dashboard-container {
max-width: 1200px;
margin: 0 auto;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
}
.card {
background-color: #2d2d44;
border-radius: 10px;
padding: 20px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}
.card-title {
font-size: 18px;
color: #89cff0;
margin-bottom: 15px;
display: flex;
align-items: center;
gap: 10px;
}
.card-title i {
font-size: 20px;
}
.metric-value {
font-size: 32px;
font-weight: bold;
margin: 10px 0;
color: #ffffff;
}
.progress-container {
width: 100%;
height: 12px;
background-color: #444466;
border-radius: 6px;
margin: 15px 0 5px;
overflow: hidden;
}
.progress-bar {
height: 100%;
border-radius: 6px;
transition: width 0.5s ease, background-color 0.5s ease;
}
.progress-text {
font-size: 14px;
color: #b0b0c0;
display: flex;
justify-content: space-between;
}
.details {
font-size: 14px;
color: #b0b0c0;
margin-top: 10px;
line-height: 1.6;
}
/* 进度条颜色分级 */
.success { background-color: #4CAF50; }
.warning { background-color: #FFC107; }
.danger { background-color: #F44336; }
.refresh-time {
text-align: center;
margin-top: 30px;
color: #b0b0c0;
font-size: 14px;
}
</style>
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body>
<div class="dashboard-container">
<!-- CPU 卡片 -->
<div class="card">
<div class="card-title">
<i class="fas fa-microchip"></i>
CPU 使用率
</div>
<div class="metric-value" id="cpu-value"><?php echo get_cpu_usage(); ?>%</div>
<div class="progress-container">
<div class="progress-bar" id="cpu-progress" style="width: <?php echo get_cpu_usage(); ?>%"></div>
</div>
<div class="progress-text">
<span>使用率</span>
<span id="cpu-percent"><?php echo get_cpu_usage(); ?>%</span>
</div>
<div class="details" id="cpu-load"><?php echo get_system_load(); ?></div>
</div>
<!-- 内存卡片 -->
<div class="card">
<div class="card-title">
<i class="fas fa-memory"></i>
内存使用率
</div>
<?php
$mem_data = get_mem_data();
$mem_usage = $mem_data['usage'];
?>
<div class="metric-value" id="mem-value"><?php echo $mem_usage; ?>%</div>
<div class="progress-container">
<div class="progress-bar" id="mem-progress" style="width: <?php echo $mem_usage; ?>%"></div>
</div>
<div class="progress-text">
<span>使用率</span>
<span id="mem-percent"><?php echo $mem_usage; ?>%</span>
</div>
<div class="details" id="mem-details">
总内存: <?php echo $mem_data['total']; ?> MB | 已用: <?php echo $mem_data['used']; ?> MB | 空闲: <?php echo $mem_data['free']; ?> MB
</div>
</div>
<!-- 磁盘卡片(系统盘 /) -->
<div class="card">
<div class="card-title">
<i class="fas fa-hard-drive"></i>
磁盘使用率(系统盘 /)
</div>
<?php
$disk_root_data = get_disk_data('/'); // 监控系统盘 /
$disk_root_usage = $disk_root_data['usage'];
?>
<div class="metric-value" id="disk-root-value"><?php echo $disk_root_usage; ?>%</div>
<div class="progress-container">
<div class="progress-bar" id="disk-root-progress" style="width: <?php echo $disk_root_usage; ?>%"></div>
</div>
<div class="progress-text">
<span>使用率</span>
<span id="disk-root-percent"><?php echo $disk_root_usage; ?>%</span>
</div>
<div class="details" id="disk-root-details">
总磁盘: <?php echo $disk_root_data['total']; ?> GB | 已用: <?php echo $disk_root_data['used']; ?> GB | 空闲: <?php echo $disk_root_data['free']; ?> GB
</div>
</div>
<!-- 新增:磁盘卡片(DataDisk 目录,按需修改实际路径) -->
<div class="card">
<div class="card-title">
<i class="fas fa-hard-drive"></i>
磁盘使用率(DataDisk)
</div>
<?php
// 注意:替换为你的实际 DataDisk 路径(如 /datadisk、/mnt/datadisk 等)
$datadisk_path = '/datadisk';
$disk_datadisk_data = get_disk_data($datadisk_path);
$disk_datadisk_usage = $disk_datadisk_data['usage'];
?>
<div class="metric-value" id="disk-datadisk-value"><?php echo $disk_datadisk_usage; ?>%</div>
<div class="progress-container">
<div class="progress-bar" id="disk-datadisk-progress" style="width: <?php echo $disk_datadisk_usage; ?>%"></div>
</div>
<div class="progress-text">
<span>使用率</span>
<span id="disk-datadisk-percent"><?php echo $disk_datadisk_usage; ?>%</span>
</div>
<div class="details" id="disk-datadisk-details">
总磁盘: <?php echo $disk_datadisk_data['total']; ?> GB | 已用: <?php echo $disk_datadisk_data['used']; ?> GB | 空闲: <?php echo $disk_datadisk_data['free']; ?> GB
</div>
</div>
<!-- 网络卡片 -->
<div class="card">
<div class="card-title">
<i class="fas fa-network-wired"></i>
网络流量
</div>
<?php $net_data = get_net_data(); ?>
<div class="metric-value" id="net-value"><?php echo $net_data['rx']; ?> KB/s | <?php echo $net_data['tx']; ?> KB/s</div>
<div class="details">
<p>接收速度: <span id="net-rx"><?php echo $net_data['rx']; ?></span> KB/s</p>
<p>发送速度: <span id="net-tx"><?php echo $net_data['tx']; ?></span> KB/s</p>
</div>
</div>
</div>
<div class="refresh-time" id="refresh-time">最后刷新: <?php echo date('H:i:s'); ?></div>
<script>
// 自动刷新页面(实现实时更新,可调整间隔,单位:毫秒)
const REFRESH_INTERVAL = 2000; // 2秒刷新一次
setTimeout(() => {
window.location.reload();
}, REFRESH_INTERVAL);
// 页面加载后给进度条添加颜色类
window.onload = function() {
function setProgressClass(elemId, usage) {
const elem = document.getElementById(elemId);
const num = parseFloat(usage);
if (num < 60) {
elem.classList.add('success');
} else if (num < 90) {
elem.classList.add('warning');
} else {
elem.classList.add('danger');
}
}
// 获取PHP输出的使用率并设置颜色(新增 DataDisk 进度条)
const cpuUsage = document.getElementById('cpu-value').innerText.replace('%', '');
const memUsage = document.getElementById('mem-value').innerText.replace('%', '');
const diskRootUsage = document.getElementById('disk-root-value').innerText.replace('%', '');
const diskDatadiskUsage = document.getElementById('disk-datadisk-value').innerText.replace('%', '');
setProgressClass('cpu-progress', cpuUsage);
setProgressClass('mem-progress', memUsage);
setProgressClass('disk-root-progress', diskRootUsage);
setProgressClass('disk-datadisk-progress', diskDatadiskUsage);
};
</script>
</body>
</html>
<?php
// ==================== PHP 核心函数:采集服务器资源数据(已升级) ====================
/**
* 获取 CPU 使用率(百分比,保留1位小数)
*/
function get_cpu_usage() {
// 执行top命令获取CPU使用率(排除top自身进程)
$output = shell_exec("top -bn1 | grep 'Cpu(s)'");
preg_match('/Cpu\(s\):\s+(\d+\.?\d*)\%us,\s+(\d+\.?\d*)\%sy,\s+(\d+\.?\d*)\%ni,\s+(\d+\.?\d*)\%id/', $output, $matches);
if (isset($matches[4])) {
$idle = (float)$matches[4];
$usage = 100 - $idle;
return number_format($usage, 1);
}
return '0.0';
}
/**
* 获取系统1/5/15分钟负载
*/
function get_system_load() {
$output = shell_exec("uptime");
preg_match('/load average: (\d+\.?\d*), (\d+\.?\d*), (\d+\.?\d*)/', $output, $matches);
if (isset($matches[1], $matches[2], $matches[3])) {
return "1分钟负载: {$matches[1]} | 5分钟负载: {$matches[2]} | 15分钟负载: {$matches[3]}";
}
return '1分钟负载: 0.00 | 5分钟负载: 0.00 | 15分钟负载: 0.00';
}
/**
* 获取内存使用数据(总/已用/空闲/使用率)
* @return array
*/
function get_mem_data() {
$output = shell_exec("free -m");
$lines = explode("\n", trim($output));
$mem_line = explode(" ", preg_replace('/\s+/', ' ', $lines[1])); // 内存行
$total = (int)$mem_line[1];
$used = (int)$mem_line[2];
$free = (int)$mem_line[3];
$usage = number_format(($used / $total) * 100, 1);
return [
'total' => $total,
'used' => $used,
'free' => $free,
'usage' => $usage
];
}
/**
* 升级:支持自定义路径的磁盘使用数据采集
* @param string $disk_path 磁盘目录路径(默认 /,可传入 /datadisk 等)
* @return array
*/
function get_disk_data($disk_path = '/') {
// 执行 df 命令,指定监控目录,格式化输出为 GB 单位
if($disk_path == "/"){
$output = shell_exec(" df -BG / | grep /");
} else {
$output = shell_exec(" df -BG | grep {$disk_path} ");
}
// 处理输出结果,去除多余空格
$parts = explode(" ", preg_replace('/\s+/', ' ', trim($output)));
// 容错处理:若目录不存在/获取失败,返回默认数据
if (count($parts) < 5) {
return [
'total' => 0,
'used' => 0,
'free' => 0,
'usage' => '0.0'
];
}
$total = str_replace('G', '', $parts[1]);
$used = str_replace('G', '', $parts[2]);
$free = str_replace('G', '', $parts[3]);
$usage = str_replace('%', '', $parts[4]);
return [
'total' => $total,
'used' => $used,
'free' => $free,
'usage' => $usage
];
}
/**
* 获取网络实时收发速度(KB/s,基于eth0网卡,可修改)
*/
function get_net_data() {
$nic = 'enp1s0'; // 网卡名称,可根据实际修改(如ens33)
$tmp_dir = '/tmp/php_dashboard/';
if (!is_dir($tmp_dir)) {
mkdir($tmp_dir, 0755, true);
}
$rx_file = $tmp_dir . 'net_rx';
$tx_file = $tmp_dir . 'net_tx';
$time_file = $tmp_dir . 'net_time';
// 获取当前流量和时间
$current_rx = (int)file_get_contents("/sys/class/net/{$nic}/statistics/rx_bytes");
$current_tx = (int)file_get_contents("/sys/class/net/{$nic}/statistics/tx_bytes");
$current_time = time();
// 读取历史数据
$old_rx = file_exists($rx_file) ? (int)file_get_contents($rx_file) : 0;
$old_tx = file_exists($tx_file) ? (int)file_get_contents($tx_file) : 0;
$old_time = file_exists($time_file) ? (int)file_get_contents($time_file) : $current_time;
// 计算时间差和速度
$time_diff = max(1, $current_time - $old_time);
$rx_speed = (int)(($current_rx - $old_rx) / 1024 / $time_diff);
$tx_speed = (int)(($current_tx - $old_tx) / 1024 / $time_diff);
// 保存当前数据为历史数据
file_put_contents($rx_file, $current_rx);
file_put_contents($tx_file, $current_tx);
file_put_contents($time_file, $current_time);
return [
'rx' => $rx_speed,
'tx' => $tx_speed
];
}
?>
Powered by ddoss.cn 12.0
©2015 - 2026 ddoss
渝公网安备50011302222260号
渝ICP备2024035333号|小程序备渝ICP备2024035333号-2X
|小绿叶技术博客(ddoss.cn)简介|
【实验平台安全承诺书】
主机监控系统: 安全防火墙已开启检查cc攻击-下载文件完成后等待10s 恢复访问,检查连接数低于峰值恢复访问
您的IP:216.73.216.63,2026-02-04 08:53:45,Processed in 0.01936 second(s).