Tip: Shell/cron script to remedy weird MariaDB connection problem

Discussion in 'Tips/Tricks/Mods' started by macguru, Aug 12, 2019.

  1. macguru

    macguru Member HowtoForge Supporter

    Hi!

    I wrote simple shell script being invoked from cron (each min), which monitors weird problem on ISPConfig 3/Debian Stretch - dovecot and postfix loosing connection with MariaDB db for user authentication at random intervals (while MariaDB is 100% OK). Script writes logs in /var/log if any problem detected.
    Code:
    # nano /etc/crontab
    # run every 1 minute. Edit your script path...
    # * * * * * root /home/andrei/scripts/anvcheck_services.sh
    
    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, restarting service" >> $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, restarting service" >> $LOGFILE
            service postfix restart
            mv $MAILERRLOG "$MAILERRLOG-$NOW"
            exit 0
        fi
    done
    
    
     
    till likes this.

Share This Page