Error log monitoring alarms script

EDITORIAL:


In practice, regularly check the database error log is essential work items. But check the time is not necessarily produced a new error message, or that we hope never to have more information go wrong, because it means that our database is robust. In this case, you can not predict when an error message is generated, and if we check the error log and often is normal, it is easy to form the inertia of thinking, the passage of time is not being given the information, we can easily ignore this work item. But for us, the error log is essential, at this time, we hope, generate an error message when, on direct mail alert notice me, I can free up time to do more meaningful things. If you have this demand, then this article might help you.


Script file:


monitor_errorlog.sh CAT
#! / bin / bash
# script error log monitoring alarm

# monitor Date
Time = +% F` `DATE
# Time = 2018-06-02

cd / Home / dbalzb

# generated by date error log file monitoring, first of all detecting error log is generated over the day
LS errorlog- $ time.txt &> / dev / null

# the return value is non-zero state, it means that the day is not generated through the
iF [$ 0 -ne?]; the then
    CAT / Home / Data / MySQL / error.log | grep "$ Time" | egrep -i 'error | warning'> errorlog- $ time.txt
    # determine whether there is an error log that day
    NUM = `CAT errorlog- $ time.txt | WC -l`
    iF [$ NUM -ne 0]; the then
        mutt [email protected] -s "errorlog3306" <$ time.txt errorlog-
    Fi
    the md5sum errorlog- time.txt $> $ time.txt MD5-errorlog-
the else
    cat /home/data/mysql/error.log | grep "$ time" | egrep -i 'error | warning'> errorlog- $ time-new.txt
    script file # second and subsequent execution will generate a new errorlog file, and generate a new value md5sum
    md5sum errorlog- $ Time-MD5-errorlog- new.txt >> $ time.txt
    
    str1 = MD5-CAT errorlog- $ `time.txt | -n Sed '1P' | awk '{ } $. 1 Print ' `
    str2 = MD5-CAT errorlog- $` time.txt | -n Sed' 2P '| awk' {}. 1 Print $ ' `

    # num1 = $` Time-CAT errorlog- new.txt | -l WC `
    #cat errorlog- time.txt >> errorlog- $ $ Time-History.txt

    # md5sum comparative values of the two files to determine whether a new error log generated
    iF [$ str2 str1 = $!]; the then
        mutt lizibin_1 @ -s 126.com "new new-errorlog3306" <errorlog- $ Time-new.txt
        #cat errorlog- $ Time-new.txt >> errorlog- $ Time-History.a .txt -
    fi
    
    /bin/rm -f errorlog-$time-new.txt
    sed -i '2d' md5-errorlog-$time.txt
fi


Add Scheduled Task: (performed once per hour, can be adjusted)

5 */1 * * * sh /home/dbalzb/monitor_errorlog.sh


All roads lead to Rome, you is how dynamic monitoring database error log it? Leave a comment below.

Guess you like

Origin blog.51cto.com/20131104/2409319