0

I have a following script scheduled to run every 5 minute that checks for two services and if it found any service status down,it sends email. Its working ok.

But the issue is I want it to send UP alert as well when service goes up. I can add line to send email but it repeats the up alert. how can i add UP alert function but it should send alert just one time (like it do for down alert)

#!/bin/bash
if pgrep "mysql" > /dev/null
then
    echo "MYSQL Running"
    rm -f /var/run/.mysql_mail_sent
else
    echo "ALERT: mysqld Stopped."
    if [ ! -f /var/run/.mysql_mail_sent ]; then
echo "Sending MYSQL DOWN Email..."
echo "DOWN ALERT! sending email" > /var/run/.mysql_mail_sent

    fi
fi

if pgrep "radiusd" > /dev/null
then
    echo "radiusd Running"
    rm -f /var/run/.radiusd_mail_sent
else
    echo "ALERT: RADIUSD Stopped"
    if [ ! -f /var/run/.radiusd_mail_sent ]; then
echo "RADIUSD DOWN: Sending Email."
echo "DOWN ALERT! sending email" > /var/run/.radiusd_mail_sent
    fi
fi
0

Ok I finally made it. BASH is quicker and doesn't requires any 3rd part tool. [updated 4:43pm]

#!/bin/bash
# Scheduled Script to check linux service status after every 5 minutes.
# If found stopped, send sms or email Alerts, but donot repeat it untill next status change.
# Syed Jahanzaib


# Run script with service name like
# ./status.sh mysqld


# Check if no service name is given
if [ "$1" == "" ]; then
echo No service name have been provided.
echo Usage exmaple:
echo
echo -e "./status.sh mysqld"
echo
fi

DATE=`date`
COMPANY="MYISP"


SERVICE1="$1"
SUBJECT="ALERT: $SERVICE1 is Down..."
STATUS_HOLDER="/tmp/$SERVICE1_STATUS_HOLDER.txt"

# KANNEL Gateway Info
KANNELURL="127.0.0.1:13013"
KANNELID="kannel"
KANNELPASS="password"
CELL1="0333xxxxxx"

# SMS Msgs test
MSG_UP="$COMPANY Info: $SERVICE1 is now UP @ $DATE"
MSG_DOWN="$COMPANY Alert: $SERVICE1 is now DOWN @ $DATE"

touch $STATUS_HOLDER

for SRVCHK in $SERVICE1
do
        PID=$(pgrep $SERVICE1)
        if [ "$PID" == "" ]; then
                echo "$SRVCHK is down"
                if  [ $(grep -c "$SRVCHK" "$STATUS_HOLDER") -eq 0 ]; then
                        echo "ALERT: $SERVICE1 is down at $(date) / SENDING SMS ...."
            echo "$MSG_DOWN" > /tmp/$SERVICE1_down.sms
# Sending DOWN SMS via KANNEL
cat /tmp/$SERVICE1_up.sms | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-
                        echo "$SRVCHK" >> $STATUS_HOLDER
                fi
        else
                echo -e "$SRVCHK is alive and its PID are as follows...\n$PID"
                if  [ $(grep -c "$SRVCHK" "$STATUS_HOLDER") -eq 1 ]; then
                        echo "INFO ALERT : $SERVICE1 is UP at $(date) / SENDING SMS ...."
            echo "$MSG_UP" > /tmp/$SERVICE1_up.sms
# Sending UP SMS via KANNEL
cat /tmp/$SERVICE1_up.sms | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-
                        sed -i "/$SRVCHK/d" "$STATUS_HOLDER"
                fi
        fi
done
|improve this answer|||||
  • 1
    bash is quicker than what? – mikeserv Nov 25 '15 at 11:25
  • @mikeserv I meant to say BASH is fun :) – Syed Jahanzaib Nov 25 '15 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.