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をログファイルの場所に変更します