Skip to content

Instantly share code, notes, and snippets.

@soulflyman
Last active March 16, 2016 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soulflyman/4500e3b001472f9c22a7 to your computer and use it in GitHub Desktop.
Save soulflyman/4500e3b001472f9c22a7 to your computer and use it in GitHub Desktop.

Description

This combination of scripts monitors the services on an linux system, if a service is down it writes a log and sends a push notification via pushover (http://pushover.net). After all tests it flushes the mailq.

Files

monitor_services.sh - Monitors the services and writes the log entry if one is down

bo.sh - small api implementation for pushover push notification service

monitor_services.cron - setup line for crontab

Setup

  1. Place both .sh files in the home directory of the root user
  2. allow root to execute them
  3. Also copy the content of the .cron file into your crontab

Now all is setup and the system will check every 10 minutes if the services are still up.

Notice

The log file is locate in /var/log/monitor_services.log

#!/bin/sh
# Bash script which implements a simple version of the pushover api to send notifications
# Credits https://github.com/2bytes/Bashover
LOGGING=true # This will print the message and response to the syslog.
URL="https://api.pushover.net/1/messages.json"
APP_KEY="aPTFZVhzgiqzm2yMd6LARZN8iAU8iz" #token
USER_KEY="ujkGtmTupCGqWgfQFZXPtKDcmxKeA3" #user
TITLE=$1
PRIORITY=1
SOUND="siren"
MESSAGE=$2
if [ ${#APP_KEY} == 0 ]
then
echo "Error: You need to supply an app token"
exit 1;
fi
if [ ${#USER_KEY} == 0 ]
then
echo "Error: You need to supply a user key"
exit 1;
fi
if [ ${#MESSAGE} == 0 ]
then
MESSAGE=$(cat)
fi
RESPONSE=`curl -s --data token=$APP_KEY --data user=$USER_KEY --data-urlencode title="$TITLE" --data sound=$SOUND -
-data priority=$PRIORITY --data-urlencode message="$MESSAGE" $URL`
if [ $LOGGING == true ]
then
logger "PUSH"
logger "|"
logger "| MESSAGE: $MESSAGE"
logger "| RESPONSE: $RESPONSE"
logger "|___________________________________________________________________________________________"
fi
*/10 * * * * /root/monitor_services.sh >> /var/log/monitor_services.log 2>&1
#!/bin/sh
SERVICES="mysql
dovecot
amavis
clamav-daemon
clamav-freshclam
postfix"
SUCCESS_MSG="Restarting the service was SUCCESSFUL!"
FAILED_MSG="Restarting the service has FAILED!"
send_alert ()
{
if [ 0 -eq $1 ]; then
MSG="$SUCCESS_MSG"
else
MSG="$FAILED_MSG"
fi;
DATE=`date +"%Y-%m-%d %H:%M:%S"`
echo "[$DATE] $2 - $MSG" >> /var/log/monitor_services.log
/root/bo.sh "$2" "$MSG" &>/dev/null
}
is_service_running ()
{
# service $1 status | grep "active (running)" > /dev/null
`/etc/init.d/$1 status | grep "active (running)" > /dev/null`
return $?
}
for service in $SERVICES
do
is_service_running $service
if [ 0 -ne $? ]; then
`/etc/init.d/$service restart`
is_service_running $service
send_alert $? "$service service down!"
fi;
done
/usr/sbin/postfix flush
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment