
該腳本監視日誌檔案中的模式“ora”,提取錯誤詳細資訊並發送電子郵件。我將 crontab 設定為每 5 分鐘一次,因此 grep 會找出導致錯誤警報的相同舊錯誤。
mailto=xyz.email.com
logdirectory=/location/to/logfile
cd $logdirectory
grep "ORA" logfile
if [ $? = 0 ]; then
mailx -s "errors" $mailto
fi
我的日誌檔:
Fri Jun 07 05:09:32 2019 Archived Log entry 93 added for thread 1 sequence 59 ID 0xf10d426f dest 1:
Fri Jun 07 11:08:20 2019 07-JUN-19 ORA-1100: Testing, Please Ignore
答案1
awk -v parseLog='/some/where/filename' '
BEGIN {
prevNR=( (getline line < parseLog) > 0 ? line : 0 )
}
(NR>prevNR) && /ORA/{ print; found=1 }
END {
print NR > parseLog
exit !found
}
' /location/to/logfile
或在外殼:
parseLog='/some/where/filename'
IFS= read -r line < "$parseLog"
if [[ -n "$line" ]]; then
prevNR="$line"
else
prevNR=0
fi
nr=$(wc -l < /tmp/logfile)
head -"$nr" /location/to/logfile |
tail +"$(( "$prevNR" + 1 ))" |
grep ORS
rslt=$?
printf '%d\n' "$nr" > "$parseLog"
請注意,您需要先獲取行數,然後對該行數wc
執行 a並將其通過管道傳輸到 tail,然後再傳輸到 grep,否則日誌檔案可能會在和之間增長,然後您會錯過下一行或者如果你交換了這些迭代的順序,那麼你可以解析同一行兩次。head
grep
wc
請注意,上面假設寫入日誌檔案的內容在每行 1 個原子操作中執行 - 如果它單獨寫入部分行,然後在完成後附加換行符,那麼您需要忽略最後一行(可能是部分行)上述解析的日誌檔。