0 like 0 dislike
28 views

Please log in or register to answer this question.

0 like 0 dislike
answered by (1.2k points)  

This script can be used to check on postgres apache,mysql, or whatever service you want. This is not meant to be a SOLUTION to services that are crashing, but rather a notification and a temporary restart until you can solve the real issue.

 

The script will check the status of each service. If the service is stopped, it tries to restart the service. If the service starts, it sends you an email saying the service stopped but was restarted.

If the service does not start for some reason, it sends you an email telling you it was not started.

 

After that, it will continue to try and start, but not send any more emails until the service is finally started.

NOTE*: This script should be run as your root user, so it would be added to the crontab like so:

  1. put it into your scripts folder
  2. set your email address
  3. set the services you want to keep an eye on (by default it has postgresql, mysql , mariadb and apache2..you can add or take away whatever you need)
  4. save your changes
  5. create a cronjob as root (sudo crontab -e) and add something like this, which runs every minute (adjust to your needs):
#check on services
*/1 *  * * * sh /root/script/dbalert.sh
################### DOWN DB server######################
#!/bin/bash
#ver. 2

##this script will check whatever services
##you want to keep an eye on
##if that service is not running
##it will (try to) start the service and send
##an email to you

##set your email address
EMAIL="nijamutheen@gmail.com"

##list your services you want to check
##you can add as many as you like
SERVICES=('postgresql')
logdate=$(date +%a)  # our log file format is postgresql-Fri.log choose depend on logfile format
#### DO NOT CHANGE anything BELOW ####


 for i in "${SERVICES[@]}"
  do
    ###IF SERVICE IS NOT RUNNING####
if ([[ "$(service $i status)" =~ "not running" ]] || [[ "$(service $i status)" =~ "is stopped" ]] || [[ $"$(service $i status)" =~ "stop/waiting/down" ]] || [[ $"$(service $i status)" =~ "no server running"]] )

    then
       ##TRY TO RESTART THAT SERVICE###
    service $i start  


        ##IF RESTART WORKED###
if ([[ "$(service $i status)" =~ "server is running" ]] || [[ "$(service $i status)" =~ "is started" ]] || [[ $"$(service $i status)" =~ "start" ]])
then
        ##SEND AN EMAIL###   
    MESSAGE="$(tail -15 /$PGDATA/pg_log/postgresql-$logdate.log)"
    SUBJECT="PostgreSQL Down But restarted Successfully on $(date) "
    echo -e "   LOGS :  \n$MESSAGE "  | mail -s "$SUBJECT" "$EMAIL"
       else

        ##IF RESTART DID NOT WORK SEND A DIFFERENT EMAIL###
    MESSAGE="$(tail -15 /$PGDATA/pg_log/postgresql-$logdate.log)"
    SUBJECT="PostgreSQL down, Restarted did not work on $(date) "
    echo -e "   LOGS :  \n$MESSAGE . \n>>>>> Script tried to restart the Postgres Server but it did not work"  | mail -s "$SUBJECT" "$EMAIL"

       fi
    fi
done

#  Better schedule  it as root user  because root user only having mail service access in some environment if postgres user having  mail services access then you can schedule it as postgres user and change pg_ctl instead of service command

Related questions

0 like 0 dislike
1 answer
Welcome to PostgreSQL Database Discussion Forum where you can ask questions and receive answers from other members of the community. Can discuss here Oracle, Postgresql, mariadb , mySQL , AWS , Linux , MSSQL , MongoDB , Greenplum databases related queries ...etc.
...