Unix每小时grep多个模式

时间:2016-06-26 21:05:38

标签: unix

假设日志中有4种不同类型的模式(错误),每种模式都可能不时发生。例如:"超时异常"," ldap错误"," db error","错误四"。任何一个地方都可以为我提供一个脚本: - 如何每小时在日志中grep多个模式,如果脚本找到任何模式,那么它应该只向我发送一次警报,没有重复警报。请帮我。谢谢

2 个答案:

答案 0 :(得分:0)

#!/bin/bash

while true; do
    export ERRORS=`cat YOUR_LOG_FILE | grep -e "(timeout exception)|(ldap error)|(db error)|(error four)"
    if [ $ERRORS ]; then
        # sendmail or any other kind of "alert" you prefer.
        echo $ERRORS | sendmail "your@email.com"
    fi
    sleep 1h
done

答案 1 :(得分:0)

创建一个每小时运行一次的crontab条目。该条目可以调用您的脚本:

logfile=/path/to/logfile/application.out

function send_alert {
   # Some sendmail or other tool to send your alert using the args
   printf "I want to alert about %s" "$*"
}

# Solution only announcing errors without sending them
grep -qE "timeout exception|ldap error|db error|error four" ${logfile} && 
        send_alert "grep found something"

# Solution sending number of errorlines
errorlinecount=$(grep -c "timeout exception|ldap error|db error|error four" )
if [ ${errorcount} -gt 0 ]; then
    send_alert "grep found ${errorcount} disturbing lines"
fi