Syed Jahanzaib Personal Blog to Share Knowledge !

November 26, 2015

Linux bash script for IP monitoring status / SMS alerting via KANNEL sms Gateway

Filed under: Linux Related — Tags: , — Syed Jahanzaib / Pinochio~:) @ 12:10 PM

net_sms_details

Bash script to check any HOST IP status status & send alert via kannel sms, while preventing repeated alert for same status.

#!/bin/bash
# Linux BASH Script to check any HOST/Internet Status & send SMS alert #(trigger one time for Per status changed)
# Syed Jahanzaib
# Created: 26-NOV-2015
#set -x
HOSTNAME=`hostname`
COMPANY="zaib"
DATE=`date`
TEMP="temp"
HOST1="$1"
# Temporary file holder for host status
HOST1_STATUS="/$TEMP/$1.ping.status"
# Create temp file if not already present, usually for 1st time execution
touch $HOST1_STATUS
# SMS RELATED and KANNEL INFO
# KANNEL SMS Gateway Info
# KANNEL SECTION
KURL="127.0.0.1:13013"
KID="kannel"
KPASS="KANNEL_PASS"
CELL1="03333021909"
FOOTER="Powered by $COMPANY"
MNT="10"
# How many PING attempts
PING_ATTEMPTS="10"
# SMS Messages for UP / DOWN
SMS_DOWN="$COMPANY ALERT: $DATE
$HOST1 not responding to $PING_ATTEMPTS ping request. Check connectivity
$FOOTER"
SMS_UP="$COMPANY INFO: $DATE
$HOSTNAME: $HOST1 is reachable now. OK
$FOOTER"
SMSDOWNHOLDER="/$TEMP/$HOST1.ping.down.sms"
SMSUPHOLDER="/$TEMP/$HOST1.ping.up.sms"

###########################################################
# CHECKING IF SYSTEM IS JUST RESTARTED, TO AVOID SENDING SMS
upSeconds="$(cat /proc/uptime | grep -o '^[0-9]\+')"
upMins=$((${upSeconds} / 60))
if [ "${upMins}" -lt "$MNT" ]
then
echo "System BOOT time is less then $MNT Minutes, so nothing to do, ignoring and exiting the SCRIPT"
exit 1
fi
if [ ! -d "/$TEMP" ]; then
echo
echo
echo "/$TEMP folder not found, Creating it so all ping results should be saved there . . ."
mkdir /$TEMP
fi

# Run the script
echo -e "Trying to ping - $HOST1 / $PING_ATTEMPTS times"
for HOST in $HOST1
do
count=$(ping -c $PING_ATTEMPTS $HOST | awk -F, '/received/{print $2*1}')
if [ $count -eq 0 ]; then
echo "$HOST is down."

if [ $(grep -c "$HOST" "$HOST1_STATUS") -eq 1 ]; then
echo "SMS alert have already been sent ..."
fi

if [ $(grep -c "$HOST" "$HOST1_STATUS") -eq 0 ]; then
echo "$HOST is down (ping failed) at $(date) .. SENDING DOWN SMS ..."
echo "$SMS_DOWN" > /$TEMP/$HOST1.down.sms
echo "$HOST" >> $HOST1_STATUS
# Sending DOWN SMS via KANNEL
cat /$TEMP/$HOST1.down.sms| curl "http://$KURL/cgi-bin/sendsms?username=$KID&password=$KPASS&to=$CELL1" -G --data-urlencode text@-
fi
else
echo "$HOST is alive"
if [ $(grep -c "$HOST" "$HOST1_STATUS") -eq 1 ]; then
DATE=`date`
echo "$HOST is now up (ping ok) at $(date)... SENDING UP SMS ..."
echo "$SMS_UP" > /$TEMP/$HOST1.up.sms
# Sending UP SMS via KANNEL
cat /$TEMP/$HOST1.up.sms| curl "http://$KURL/cgi-bin/sendsms?username=$KID&password=$KPASS&to=$CELL1" -G --data-urlencode text@-
sed -i "/$HOST/d" "$HOST1_STATUS"
fi
fi
done

Schedule it to run after every x minutes in cron , example:


*/1 * * * * /temp/net_mon.sh 8.8.8.8 >/dev/null 2>&1

 

Regard’s
Syed Jahanzaib

2 Comments »

  1. When i try to run the script, “Trying to ping – / 10 times” with or without internet connections

    Like

    Comment by alyainec — February 24, 2019 @ 6:17 PM


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: