2016-06-26 19 views
0

假設,日誌中有4種不同類型的模式(錯誤),每種模式都可能不時出現。例如:「超時異常」,「ldap錯誤」,「db錯誤」,「錯誤四」。任何一個地方都可以提供關於以下內容的腳本: - 如何每小時查看一個日誌中的多個模式,如果腳本找到任何模式,那麼它應該只向我發送一次警報,而不是重複的警報。請幫幫我。謝謝Unix每小時grep多種模式

+0

StackOverflow不是代碼服務工廠,我投票將此問題標記爲脫離主題。 –

回答

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 "[email protected]" 
    fi 
    sleep 1h 
done 
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