You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP中嵌入HTML标签实现树莓派playlog.csv监控?

没问题,我来帮你搞定这个树莓派Playlog日志可视化监控的需求!下面是一套结合PHP+HTML的实现方案,完全贴合你的需求:

树莓派Playlog监控:PHP嵌入HTML可视化方案

1. 先明确日志规则

从你给出的示例来看,每行日志格式为:时间戳,文件名,状态码 // PI标识,我们先约定状态映射逻辑(你可以根据实际需求调整):

  • 状态码0 → 运行正常(绿色圆圈)
  • 状态码-2 → 运行异常(红色圆圈)

2. PHP核心逻辑:获取并解析多台PI的日志

首先要确保PHP能访问到每台树莓派的playlog.csv——可以通过SAMBA共享挂载、SSH远程拉取(用tail -n 1获取最新一行)等方式。下面是代码示例:

<?php
// 定义所有树莓派的信息,按需修改PI标识和日志路径/获取方式
$raspberryPis = [
    ['id' => 'PI 1', 'log_source' => '/mnt/pi_shared/pi1/playlog.csv'],
    ['id' => 'PI 2', 'log_source' => '/mnt/pi_shared/pi2/playlog.csv'],
    ['id' => 'PI 3', 'log_source' => '/mnt/pi_shared/pi3/playlog.csv'],
];

$piStatusList = [];
foreach ($raspberryPis as $pi) {
    // 获取日志最新一行:本地挂载用tail,远程SSH可以替换成exec('ssh pi@xxx "tail -n 1 /path/to/playlog.csv"')
    $latestLogLine = trim(shell_exec("tail -n 1 " . escapeshellarg($pi['log_source'])));
    
    // 解析日志内容
    $logParts = explode(',', $latestLogLine);
    if (count($logParts) >= 3) {
        $timestamp = $logParts[0];
        $fileName = $logParts[1];
        $statusCode = intval($logParts[2]);
        
        // 匹配状态样式
        $statusClass = $statusCode === 0 ? 'circleGreen' : 'circleRed';
        $statusText = $statusCode === 0 ? '正常运行' : '出现异常';
        
        $piStatusList[] = [
            'pi_id' => $pi['id'],
            'time' => $timestamp,
            'file' => $fileName,
            'status_class' => $statusClass,
            'status_text' => $statusText
        ];
    } else {
        // 日志格式异常的兜底处理
        $piStatusList[] = [
            'pi_id' => $pi['id'],
            'time' => '无法获取',
            'file' => '无法获取',
            'status_class' => 'circleRed',
            'status_text' => '日志格式异常'
        ];
    }
}
?>

3. 嵌入HTML实现可视化展示

把PHP处理好的状态数据,嵌入到你提供的HTML样式中,完成可视化:

<!DOCTYPE html>
<html>
<head>
    <title>树莓派Playlog实时监控</title>
    <style>
        .circleRed { background: #ff0000; width: 15px; height: 15px; border-radius: 50%; display: inline-block; margin-right: 8px; }
        .circleGreen { background: #00ff00; width: 15px; height: 15px; border-radius: 50%; display: inline-block; margin-right: 8px; }
        .pi-monitor-item { margin: 18px 0; padding: 12px; border-bottom: 1px solid #e0e0e0; }
        .pi-details { display: inline-block; vertical-align: middle; }
        h1 { color: #333; font-family: Arial, sans-serif; }
    </style>
</head>
<body>
    <h1>树莓派Playlog监控面板</h1>
    
    <?php foreach ($piStatusList as $status): ?>
    <div class="pi-monitor-item">
        <div class="<?php echo $status['status_class']; ?>"></div>
        <div class="pi-details">
            <strong><?php echo $status['pi_id']; ?></strong><br>
            最新日志时间: <?php echo $status['time']; ?><br>
            当前播放文件: <?php echo $status['file']; ?><br>
            状态: <?php echo $status['status_text']; ?>
        </div>
    </div>
    <?php endforeach; ?>
</body>
</html>

4. 实用优化建议

  • 权限保障:确保PHP运行用户有访问PI日志文件的权限(本地挂载要开读写权限,远程SSH要配置免密登录)
  • 实时刷新:如果需要自动更新监控状态,可以在HTML的</body>前加一段JS:setTimeout(() => location.reload(), 5000)(每5秒刷新一次)
  • 安全优化:远程获取日志时,建议用PHP的专业SSH扩展(比如phpseclib)替代shell_exec,避免命令注入风险

内容的提问来源于stack exchange,提问作者sar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:01:00