cat > /home/system/zabbix_agent/ps/check_heartbeat.sh << 'EOF' #!/bin/bash # 获取当前小时和分钟 current_hour=$(date +"%H"|sed 's/^0//') current_minute=$(date +"%M"|sed 's/^0//') # 转换为24小时制的时间格式 current_time=$((current_hour * 60 + current_minute)) # 设定时间段(09:15到14:57)的开始和结束时间(以分钟计) start_time=$((9*60+15)) # 09:15 end_time=$((14*60+57)) # 14:57 #展示strategy_date_*.log的文件路径 strategy_dir=$(find /home/system/app_* -name "strategy_`date '+%Y%m%d'`_*.log" -regextype posix-extended ! -regex '.*/test_log/.*') #在9:00-14:57循环判断日志时间和系统时间相差是否超过30s if [[ $current_time -ge $start_time ]] && [[ $current_time -lt $end_time ]]; then if [[ -z "${strategy_dir}" ]]; then echo 0 #心跳正常 else array_error=() for dir_path in ${strategy_dir}; do time_heart=$(# 取最后1000行,直接匹配最后一个时间 time 或 timestamp 字段并提取日期时间 tail -n 1000 ${dir_path} | grep -E '"time"|"timestamp"' | tail -2 | head -1 | sed -E ' # 格式1: "time": "2025-12-12 14:56:25:010" -> 2025-12-12 14:56:25 s/.*"time"[[:space:]]*:[[:space:]]*"([0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2}):[0-9]+".*/\1 \2/p # 格式2: "timestamp": "12/12/2025, 14:56:26" -> 2025-12-12 14:56:26 s/.*"timestamp"[[:space:]]*:[[:space:]]*"([0-9]{2})\/([0-9]{2})\/([0-9]{4}), ([0-9]{2}:[0-9]{2}:[0-9]{2})".*/\3-\1-\2 \4/p ' -n) ct=$(date +%s) lt=$(date -d "${time_heart}" +%s) minus=$((${ct} - ${lt})) if [[ -z "${time_heart}" ]]; then continue else if [[ ${minus} -ge 180 ]]; then array_error+=(${dir_path}) fi fi done if [[ ${#array_error[@]} -gt 0 ]]; then echo ${array_error[@]} #列出数组的所有元素 else echo 0 #心跳正常 fi fi else echo 0 #心跳正常 fi EOF sh /home/system/zabbix_agent/ps/check_heartbeat.sh