#!/bin/sh set -eu SYSTEM_DIR="${1:-/data/logs/system}" REPORTER_DIR="${2:-/data/logs/reporter}" LINES="${LINES:-40}" DEVICE_UUID="${DEVICE_UUID:-}" print_usage() { cat <<'USAGE' 用法: sh scripts/check_logs.sh [系统日志目录] [上报日志目录] 示例: sh scripts/check_logs.sh sh scripts/check_logs.sh /data/logs/system /data/logs/reporter LINES=80 sh scripts/check_logs.sh DEVICE_UUID=your-device-uuid sh scripts/check_logs.sh 环境变量: LINES 每个分组显示最近多少行,默认 40 DEVICE_UUID 可选,按设备 UUID 过滤 USAGE } case "${1:-}" in -h|--help) print_usage exit 0 ;; esac has_logs() { dir="$1" [ -d "$dir" ] || return 1 find "$dir" -maxdepth 1 -type f -name '*.log' 2>/dev/null | grep -q . } grep_logs() { pattern="$1" shift for dir in "$@"; do has_logs "$dir" || continue if [ -n "$DEVICE_UUID" ]; then grep -hE "$pattern" "$dir"/*.log 2>/dev/null | grep -F "$DEVICE_UUID" || true else grep -hE "$pattern" "$dir"/*.log 2>/dev/null || true fi done } count_logs() { pattern="$1" shift grep_logs "$pattern" "$@" | wc -l | tr -d ' ' } section() { title="$1" pattern="$2" shift 2 count="$(count_logs "$pattern" "$@")" printf '\n=== %s(%s 条)===\n' "$title" "$count" if [ "$count" = "0" ]; then echo "没有匹配日志。" return fi grep_logs "$pattern" "$@" | tail -n "$LINES" } summary_item() { label="$1" pattern="$2" shift 2 printf '%-34s %s\n' "$label:" "$(count_logs "$pattern" "$@")" } echo "FireLeave 日志快速检查" echo "系统日志目录 : $SYSTEM_DIR" echo "上报日志目录 : $REPORTER_DIR" echo "每组显示行数 : $LINES" if [ -n "$DEVICE_UUID" ]; then echo "设备过滤 : $DEVICE_UUID" fi if ! has_logs "$SYSTEM_DIR" && ! has_logs "$REPORTER_DIR"; then echo "指定目录下没有找到 *.log 日志文件。" exit 1 fi printf '\n=== 汇总 ===\n' summary_item "程序启动次数" "Starting FireLeave Tool" "$SYSTEM_DIR" summary_item "WS 注册成功" "Service registration successful" "$SYSTEM_DIR" summary_item "Topic 订阅成功" "Topic subscription successful" "$SYSTEM_DIR" summary_item "收到 topic_event" "WebSocket received topic_event" "$SYSTEM_DIR" summary_item "收到声光报警命令" "Received platform alert push|Platform alert command" "$SYSTEM_DIR" summary_item "收到 forbid_time" "Received forbid time message|Forbid time config received" "$SYSTEM_DIR" summary_item "fire_leave 暂停命中" "fire_leave reporting paused|fire_leave report paused" "$SYSTEM_DIR" summary_item "火焰报警触发" "Fire detected|FIRE DETECTED ALARM TRIGGERED|Fire detected continuously" "$SYSTEM_DIR" summary_item "离人报警触发" "ALARM TRIGGERED|Alarm condition met" "$SYSTEM_DIR" summary_item "WS/上报失败" "WebSocket.*failed|Response timeout|Asynchronous reporting failed|unsupported websocket message type|reply parsing failed|cast handling failed|WebSocket消息发送失败|响应超时|上报失败" "$SYSTEM_DIR" "$REPORTER_DIR" summary_item "异常/错误" "panic|fatal|killed|segfault|failed|error|Error|timeout|abnormally|not connected|失败|错误|异常|超时|未连接|无法上报" "$SYSTEM_DIR" "$REPORTER_DIR" section "启动和设备初始化" "Starting FireLeave Tool|Initialize device data|Loaded .*device|Starting device|Device processing goroutine started|Main program startup completed|System status|初始化设备|设备数据初始化|启动设备" "$SYSTEM_DIR" section "推理服务启动" "Port allocation successful|Fire_leave inference server|Fire_check inference server|inference program started|Both inference programs started successfully|Start fire_leave inference program command|Start fire_check inference program command|端口分配成功|推理程序启动|推理服务" "$SYSTEM_DIR" section "推理心跳和重启" "推理心跳收到|Inference data processed|fire_leave.*心跳|fire_check.*心跳|heartbeat.*timeout|准备重启|StartInferenceProcess|推理程序心跳超时" "$SYSTEM_DIR" section "WebSocket 注册和订阅" "Starting WebSocket connection initialization|WebSocket connection initialized successfully|Starting service registration|Service registration successful|Starting topic subscription|Topic subscription successful|/dhlr/alert|/dhlr/forbid_time|WebSocket连接|服务注册|订阅" "$SYSTEM_DIR" section "WebSocket 二进制协议" "Sending WebSocket binary frame|Received WebSocket binary frame|Received response|Response timeout|WebSocket reply parsing failed|WebSocket cast handling failed|Ignoring non-binary|Ignoring unknown|发送WebSocket二进制帧|收到WebSocket二进制帧|响应超时" "$SYSTEM_DIR" section "WebSocket 重连" "WebSocket message reading failed|Failed to send ping|Reconnection attempt|WebSocket reconnection successful|Re-registration successful|Re-subscribed|Reconnection failed|read loop exited|重连|重新注册|重新订阅|读取失败" "$SYSTEM_DIR" section "下行 topic_event 分发" "WebSocket received topic_event|No subscriber for topic_event|Received forbid time message|Received platform alert push|Forbid time config received|Platform alert command|收到.*topic_event|没有订阅者" "$SYSTEM_DIR" section "声光报警 alert" "Received platform alert push|Outer parsing failed|Second parsing failed|Not a sound/light alert command|Platform alert command|Buzzer starting|GPIO109|sound/light alarm|声光报警|蜂鸣器|平台告警" "$SYSTEM_DIR" section "forbid_time 和 fire_leave 暂停" "Received forbid time message|Forbid time config received|fire_leave reporting paused|fire_leave report paused|fire_leave reporting pause cleared|Forbid time config saved successfully|Restored fire_leave forbid window|Invalid fire_leave_timeout|Event cycle timeout config|禁止时间|暂停|forbid_time" "$SYSTEM_DIR" section "fire_leave 离人报警链路" "Personnel quantity update|Temperature update|Alarm condition met|ALARM TRIGGERED|fire_leave report paused|报警事件已上报|数据快照已上报|level.*fire_leave|Level=fire_leave|人员数量更新|温度更新|离人|温度报警" "$SYSTEM_DIR" "$REPORTER_DIR" section "fire_check 火焰报警链路" "Received fire_check inference data|Fire detected|Fire detection started|Fire detected continuously|FIRE DETECTED ALARM TRIGGERED|火焰检测|报警事件已上报|数据快照已上报|level.*fire_check|Level=fire_check" "$SYSTEM_DIR" "$REPORTER_DIR" section "视频合并上传和事件上报" "开始合并视频|Merging .* videos|Video merged and moved successfully|开始上传视频|UploadToNginx|Nginx|upload failed|报警事件已上报|数据快照已上报|Asynchronous reporting successful|Asynchronous reporting failed|Sending WebSocket binary frame: type=0x03|视频上传|事件上报|数据快照" "$SYSTEM_DIR" "$REPORTER_DIR" section "需要重点排查的问题" "panic|fatal|killed|segfault|WebSocket.*failed|Response timeout|Asynchronous reporting failed|unsupported websocket message type|reply parsing failed|cast handling failed|not connected|无法上报|失败|错误|异常|timeout|abnormally|超时|未连接" "$SYSTEM_DIR" "$REPORTER_DIR" printf '\n检查完成。\n'