2016-06-26 5 views
0

ログには、時々発生する可能性のあるパターン(エラー)が4種類あるとします。例:「タイムアウト例外」、「LDAPエラー」、「dbエラー」、「エラー4」どのような場所でも私にスクリプトを提供できます: - 毎時ログに複数のパターンをgrepする方法、スクリプトが任意のパターンを見つけた場合は、警告を重複して送信する必要はありません。私を助けてください。ありがとう毎時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

1時間に1回実行される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 
関連する問題