Quantcast
Channel: Syed Jahanzaib – Personal Blog to Share Knowledge !
Viewing all articles
Browse latest Browse all 408

[LAB TEST] BASH: Linux Script to check Internet/Gateway status and SMS via kannel

$
0
0

net_sms_details

Personnel notes for reference. LAB tests only. Generally not for production usage. Make sure you modify overall script to use at least two hosts for monitoring.

Testing Script to check internet /gateway status. and send alert via kannel sms, while preventing repeated alert for same status.


#!/bin/bash
# Script to check Internet Status and alert (trigger one time for each status changed)
# Host to monitor
HOST1="4.2.2.1"

# Server / This Computer name
HOSTNAME=`hostname`

COMPANY="ZAIB"
DATE=`date`

# How many PING attempts
ping_attempts=30

# Temporary file holder for host status
HOST1_STATUS="/tmp/down_hosts.txt"

# Create temp file if not already present, usually for 1st time execution
touch $HOST1_STATUS

# SMS RELATED and KANNEL INFO

# KANNEL SMS Gateway Info
KANNELURL="127.0.0.1:13013"
KANNELID="kannel"
KANNELPASS="kannel"
CELL1="03333021909"

# SMS Messages for UP / DOWN
SMS_DOWN="$COMPANY ALERT: $DATE

$HOSTNAME: Internet / $HOST1 not responding to ping request. Check internet connectivity."

SMS_UP="$COMPANY INFO: $DATE

$HOSTNAME: Internet / $HOST1 is reachable now. OK!"

SMSDOWNHOLDER="/tmp/$HOST1_down.sms"
SMSUPHOLDER="/tmp/$HOST1_up.sms"

# Run the script
echo -e "Trying to ping $HOST1 / 30 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 0 ]; then
echo "$HOST is down (ping failed) at $(date) .. SENDING DOWN SMS ..."
echo "$SMS_DOWN" > /tmp/$HOST1_down.sms
echo "$HOST" >> $HOST1_STATUS

# Sending DOWN SMS via KANNEL
cat $SMSDOWNHOLDER | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-

fi
else
echo "$HOST is alive"
if  [ $(grep -c "$HOST" "$HOST1_STATUS") -eq 1 ]; then
echo "$HOST is up (ping ok) at $(date)... SENDING UP SMS ..."
echo "$SMS_UP" > /tmp/$HOST1_down.sms
# Sending UP SMS via KANNEL
cat $SMSUPHOLDER | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-

sed -i "/$HOST/d" "$HOST1_STATUS"
fi
fi
done

Schedule it to run after very x minutes.

Regard’s
Syed Jahanzaib


Filed under: Linux Related

Viewing all articles
Browse latest Browse all 408

Trending Articles