PyBot/monitor.php
2026-05-24 19:54:12 +08:00

431 lines
12 KiB
PHP

<?php
// monitor.php - 系统文件监控页面
// 设置字符编码
header('Content-Type: text/html; charset=utf-8');
// 格式化文件大小函数
function formatFileSize($bytes) {
if ($bytes === 0) return '0 Bytes';
$k = 1024;
$sizes = ['Bytes', 'KB', 'MB', 'GB'];
$i = floor(log($bytes) / log($k));
return round($bytes / pow($k, $i), 2) . ' ' . $sizes[$i];
}
// 格式化时间函数
function formatTime($timestamp) {
if (!$timestamp) return '未知';
return date("Y-m-d H:i:s", $timestamp);
}
// 获取目录信息的函数
function getDirectoryInfo($dirPath, $displayName) {
$files = [];
if (!is_dir($dirPath)) {
return [
'name' => $displayName,
'path' => $dirPath,
'exists' => false,
'files' => [],
'totalSize' => 0,
'fileCount' => 0
];
}
$items = glob($dirPath . '/*');
$totalSize = 0;
$fileCount = 0;
foreach ($items as $item) {
if (is_file($item)) {
$fileInfo = [
'name' => basename($item),
'path' => $item,
'size' => filesize($item),
'modified' => filemtime($item),
'extension' => strtolower(pathinfo($item, PATHINFO_EXTENSION))
];
$files[] = $fileInfo;
$totalSize += $fileInfo['size'];
$fileCount++;
}
}
// 按修改时间降序排列
usort($files, function($a, $b) {
return $b['modified'] - $a['modified'];
});
return [
'name' => $displayName,
'path' => $dirPath,
'exists' => true,
'files' => $files,
'totalSize' => $totalSize,
'fileCount' => $fileCount
];
}
// 定义要监控的目录
$directories = [
getDirectoryInfo('./resources/log', 'SecPulse运行日志'),
getDirectoryInfo('./resources/db', '数据库文件'),
getDirectoryInfo('./resources/JSON', '中转JSON文件')
];
// 计算总体统计信息
$totalFiles = 0;
$totalSize = 0;
foreach ($directories as $dir) {
$totalFiles += $dir['fileCount'];
$totalSize += $dir['totalSize'];
}
$currentTime = date("Y-m-d H:i:s");
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="./static/favicon.ico" type="image/x-icon">
<title>SecPulse系统文件监控</title>
<!-- 引入 Layui 的 CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/layui-src/dist/css/layui.css">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: "Microsoft YaHei", Arial, sans-serif;
background-color: #f5f5f5;
padding: 20px;
line-height: 1.6;
}
.container {
max-width: 1200px;
margin: 0 auto;
background: white;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
overflow: hidden;
}
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px;
text-align: center;
}
.header h1 {
font-size: clamp(1.5rem, 4vw, 2.5rem);
margin-bottom: 10px;
}
.header p {
font-size: clamp(0.9rem, 2vw, 1.1rem);
opacity: 0.9;
}
.stats-bar {
background: #f8f9fa;
padding: 15px 20px;
border-bottom: 1px solid #eee;
display: flex;
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
gap: 15px;
}
.stat-item {
text-align: center;
min-width: 120px;
}
.stat-value {
font-size: 1.5rem;
font-weight: bold;
color: #667eea;
}
.stat-label {
font-size: 0.85rem;
color: #666;
margin-top: 5px;
}
.directory-section {
margin: 20px;
border: 1px solid #eee;
border-radius: 6px;
overflow: hidden;
}
.directory-header {
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
color: white;
padding: 15px 20px;
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 10px;
}
.directory-title {
font-size: 1.2rem;
font-weight: bold;
}
.directory-info {
font-size: 0.9rem;
opacity: 0.9;
}
.file-table {
width: 100%;
border-collapse: collapse;
}
.file-table th {
background: #f8f9fa;
padding: 12px 15px;
text-align: left;
font-weight: 600;
color: #333;
border-bottom: 2px solid #eee;
}
.file-table td {
padding: 12px 15px;
border-bottom: 1px solid #f0f0f0;
color: #666;
}
.file-table tr:hover {
background: #f8f9ff;
}
.file-name {
font-weight: 500;
color: #333;
}
.file-size {
font-family: 'Consolas', monospace;
color: #667eea;
}
.file-time {
font-size: 0.85rem;
color: #888;
}
.empty-directory {
padding: 30px;
text-align: center;
color: #999;
font-style: italic;
}
.footer {
text-align: center;
padding: 15px;
background: #f8f9fa;
color: #666;
font-size: 0.85rem;
border-top: 1px solid #eee;
}
/* 响应式设计 */
@media (max-width: 768px) {
body {
padding: 10px;
}
.header {
padding: 15px;
}
.stats-bar {
flex-direction: column;
gap: 10px;
}
.stat-item {
min-width: auto;
}
.directory-section {
margin: 15px;
}
.directory-header {
flex-direction: column;
align-items: flex-start;
gap: 8px;
}
.file-table {
font-size: 0.85rem;
}
.file-table th,
.file-table td {
padding: 8px 10px;
}
}
@media (max-width: 480px) {
.header h1 {
font-size: 1.3rem;
}
.header p {
font-size: 0.85rem;
}
.directory-title {
font-size: 1rem;
}
.file-table {
font-size: 0.8rem;
}
.file-table th,
.file-table td {
padding: 6px 8px;
}
}
/* 滚动条样式 */
::-webkit-scrollbar {
width: 8px;
height: 8px;
}
::-webkit-scrollbar-track {
background: #f1f1f1;
border-radius: 4px;
}
::-webkit-scrollbar-thumb {
background: #c1c1c1;
border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
background: #a8a8a8;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>📊 系统文件监控</h1>
<p>实时监控系统资源文件状态</p>
</div>
<div class="stats-bar">
<div class="stat-item">
<div class="stat-value"><?php echo count($directories); ?></div>
<div class="stat-label">监控目录</div>
</div>
<div class="stat-item">
<div class="stat-value"><?php echo $totalFiles; ?></div>
<div class="stat-label">文件总数</div>
</div>
<div class="stat-item">
<div class="stat-value"><?php echo formatFileSize($totalSize); ?></div>
<div class="stat-label">总大小</div>
</div>
<div class="stat-item">
<div class="stat-value"><?php echo $currentTime; ?></div>
<div class="stat-label">更新时间</div>
</div>
</div>
<?php foreach ($directories as $dir): ?>
<div class="directory-section">
<div class="directory-header">
<div class="directory-title">📁 <?php echo htmlspecialchars($dir['name']); ?></div>
<div class="directory-info">
<?php if ($dir['exists']): ?>
<?php echo $dir['fileCount']; ?> 个文件 |
总大小: <?php echo formatFileSize($dir['totalSize']); ?>
<?php else: ?>
目录不存在
<?php endif; ?>
</div>
</div>
<?php if ($dir['exists'] && !empty($dir['files'])): ?>
<table class="file-table">
<thead>
<tr>
<th>文件名称</th>
<th>文件大小</th>
<th>更新时间</th>
<th>文件类型</th>
</tr>
</thead>
<tbody>
<?php foreach ($dir['files'] as $file): ?>
<tr>
<td class="file-name"><?php echo htmlspecialchars($file['name']); ?></td>
<td class="file-size"><?php echo formatFileSize($file['size']); ?></td>
<td class="file-time"><?php echo formatTime($file['modified']); ?></td>
<td><?php echo strtoupper(htmlspecialchars($file['extension'])); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php elseif ($dir['exists']): ?>
<div class="empty-directory">
该目录下暂无文件
</div>
<?php else: ?>
<div class="empty-directory">
目录路径: <?php echo htmlspecialchars($dir['path']); ?><br>
该目录不存在或无法访问
</div>
<?php endif; ?>
</div>
<?php endforeach; ?>
<div class="footer">
<p>© 2026 SecPulse 文件监控系统 | 自动刷新间隔: 60秒</p>
</div>
</div>
<script>
// 自动刷新功能
setTimeout(function() {
location.reload();
}, 60000); // 60秒自动刷新
// 添加加载动画效果
window.addEventListener('load', function() {
const container = document.querySelector('.container');
container.style.opacity = '0';
container.style.transform = 'translateY(20px)';
container.style.transition = 'all 0.5s ease';
setTimeout(() => {
container.style.opacity = '1';
container.style.transform = 'translateY(0)';
}, 100);
});
</script>
</body>
</html>