PostgreSQL Health checkup script with mail notification
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:
- put it into your scripts folder
- set your email address
- set the services you want to keep an eye on (by default it has MariaDB and apache2..you can add or take away whatever you need)
- save your changes
- 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