我想监控我的 Linux Debian 机器上的日志文件 (syslog)。因此,如果有带有特定字符串的新条目,我想执行一个脚本。
字符串为“闹钟开始”和“闹钟结束”
所以它应该看起来像这样(没有程序语言,只是我的俚语;-)):
if logfile has changed
get last line (or all new lines)
search for string
if string = "alarm start" found then /foo/bar/script_alarm_start.sh
if string = "alarm end" found then /foo/bar/script_alarm_end.sh
有没有我可以使用的守护进程之类的东西?
日志文件如下所示:
Jul 7 06:38:17 zma_m1[8075]: INF [DoorCam: 898051 - Opening new event 14, alarm start]
Jul 7 06:38:23 zma_m1[8075]: INF [DoorCam: 898056 - Gone into alert state]
Jul 7 06:38:31 zmc_m1[8047]: INF [DoorCam: 900000 - Capturing at 25.00 fps]
Jul 7 06:38:33 zma_m1[8075]: INF [DoorCam: 898116 - Left alarm state (14) - 125(5) images]
Jul 7 06:38:33 zma_m1[8075]: INF [DoorCam: 898116 - Closing event 14, alarm end]
...
Jul 7 06:40:38 zma_m1[8075]: INF [DoorCam: 901286 - Opening new event 15, alarm start]
Jul 7 06:40:44 zma_m1[8075]: INF [DoorCam: 901289 - Gone into alert state]
Jul 7 06:40:53 zma_m1[8075]: INF [DoorCam: 901349 - Left alarm state (15) - 123(3) images]
Jul 7 06:40:53 zma_m1[8075]: INF [DoorCam: 901349 - Closing event 15, alarm end]
干杯!
答案1
尝试这个:
#!/bin/bash
function fs { ls -l LOGFILE | awk '{print $5}' }
function lc { wc -l LOGFILE | awk '{print $1}' }
function sv { fileSize=$(fs) fileLength=$(lc) }
function ov { nFileSize=$(fs) nFileLeng=$(fs) }
sv
while true; do
ov
if [[ $nFileSize != $fileSize ]]; then
newLines=$(tail -n $(($nFileLeng-$fileLength)))
if [[ $(echo "$newLines" | grep "alarm start") ]]; then
/foo/bar/script_alarm_start.sh
elif [[ $(echo "$newLines" | grep "alarm end") ]]; then
/foo/bar/script_alarm_end.sh
fi
fi
sv
done
将 LOGFILE 更改为日志文件的位置