Mail Alert Notification for MariaDB Service is Down
This script can be used to check on mariaDB, apache2, 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=('mysql')
#### 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" ]])
then
##TRY TO RESTART THAT SERVICE###
service $i start
##IF RESTART WORKED###
if ([[ "$(service $i status)" =~ "MariaDB running" ]] || [[ "$(service $i status)" =~ "is started" ]] || [[ $"$(service $i status)" =~ "start" ]])
then
##SEND AN EMAIL###
MESSAGE="$(tail -15 /var/lib/mysql/publishman.com.err)"
SUBJECT="MariaDB 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 /var/lib/mysql/publishman.com.err)"
SUBJECT="MariaDB down, Restarted did not work on $(date) "
echo -e " LOGS : \n$MESSAGE . \n>>>>> Script tried to restart but it did not work" | mail -s "$SUBJECT" "$EMAIL"
fi
fi
done
################### DOWN APACHE 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
EMAILA="nijamutheen@gmail.com"
##list your services you want to check
##you can add as many as you like
SERVICESA=('httpd')
#### DO NOT CHANGE anything BELOW ####
for a in "${SERVICESA[@]}"
do
###IF SERVICE IS NOT RUNNING####
if ([[ "$(service $a status)" =~ "not running" ]] || [[ "$(service $a status)" =~ "is stopped" ]] || [[ $"$(service $a status)" =~ "stop/waiting" ]])
#if [[ "$(service $a status)" =~ "$a is stopped" ]]
then
##TRY TO RESTART THAT SERVICE###
service $a start
##IF RESTART WORKED###
#if [[ "$(service $a status)" =~ "is running" ]]
if ([[ "$(service $a status)" =~ "is running" ]] || [[ "$(service $a status)" =~ "is started" ]] || [[ $"$(service $a status)" =~ "start" ]])
then
##SEND AN EMAIL###
MESSAGEA="$(tail -15 /var/log/apache2/error_log)"
SUBJECTA="APACHE SERVER down But restarted successfully on $(date) "
echo -e " LOGS : \n$MESSAGEA " | mail -s "$SUBJECTA" "$EMAILA"
else
##IF RESTART DID NOT WORK SEND A DIFFERENT EMAIL###
MESSAGEA="$(tail -15 /var/log/apache2/error_log)"
SUBJECTA="APACHE SERVER down restarted did not work on $(date) "
echo -e " LOGS: \n$MESSAGEA . \n>>>>> Script tried to restart but it did not work" | mail -s "$SUBJECTA" "$EMAILA"
fi
fi
done
Tag:mail notification, MariaDB, script