Q: Log monitoring shell/cron script

Discussion in 'Programming/Scripts' started by macguru, Aug 9, 2019.

  1. macguru

    macguru Member HowtoForge Supporter

    Hi !

    I have simple shell script being invoked from cron which monitors weird problem on ISPConfig - dovecot and postfix loosing connection with mysql db for user authentication (while mysql is 100% OK).

    I short - dovecot monitoring works, postfix does not - its seems there is a bug in my grep.
    Log lines:
    Code:
    Aug  9 08:51:21 mail postfix/smtpd[3233]: fatal: no SASL authentication mechanisms
    Aug  9 08:51:21 mail postfix/smtpd[3234]: fatal: no SASL authentication mechanisms
    Aug  9 08:51:21 mail postfix/smtpd[3235]: fatal: no SASL authentication mechanisms
    
    Shell Script:

    Code:
    #!/bin/bash
    
    NOW=$(date +'%Y-%m-%d %T')
    LOGFILE='/var/log/anvcheck_services.log'
    MAILERRLOG='/var/log/mail.err'
    
    # Create log file if it doesn't exist.
    if [ ! -f $LOGFILE ]; then
        touch $LOGFILE
        echo "$NOW : log file created" >> $LOGFILE
    fi
    
    if [ ! -f $MAILERRLOG ]; then
        exit 0
    fi
    
    ERRSTRING1="dovecot/|lda\|Fatal\|Internal\|error\|occurred"
    ERRSTRING3="postfix/|smtpd\|fatal\|no\|SASL\|authentication\|mechanisms"
    
    cat $MAILERRLOG | \
    while read LINE
    do
        echo "$LINE" | grep -q "$ERRSTRING1"
        if [ $? = 0 ]
        then
            echo "$NOW : Dovecot problem detected" >> $LOGFILE
            service dovecot restart
            mv $MAILERRLOG "$MAILERRLOG-$NOW"
            exit 0
        fi
    done
    
    
    cat $MAILERRLOG | \
    while read LINE
    do
        echo "$LINE" | grep -q "$ERRSTRING3"
        if [ $? = 0 ]
        then
            echo "$NOW : Postfix problem detected" >> $LOGFILE
            service postfix restart
            mv $MAILERRLOG "$MAILERRLOG-$NOW"
            exit 0
        fi
    done
    
    Thanks in advance !
     
  2. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    In what way does it not work? What was the expected result of your code?
    You get better help if you make the work of person helping you easier. I am not fond of deciphering strange code.

    From what I understand of your code, this might work. Have not tested.
    Code:
    ERRSTRING3="postfix/smtpd.*fatal:"
    
    Then use grep -E, like this:
    echo "$LINE" | grep -E -q "$ERRSTRING3"
    
    Do read this:
    https://www.regular-expressions.info/
     
  3. macguru

    macguru Member HowtoForge Supporter

    Scanning log file with grep (for specific Postfix error related to MySQL db user lookup) doesn't trigger true even if these error messages are presented in mail.err.
    This works
    ERRSTRING1="dovecot/|lda\|Fatal\|Internal\|error\|occurred"
    This do not
    ERRSTRING3="postfix/|smtpd\|fatal\|no\|SASL\|authentication\|mechanisms"
    Although they both very similar. This is why I'm concluded there is a bug in ERRSTRING3 definition.
     
  4. macguru

    macguru Member HowtoForge Supporter

    Get it worked, syntax error was in another part of script
     

Share This Page