Syed Jahanzaib Personal Blog to Share Knowledge !

June 9, 2016

BASH Script to check modem status and alert by email/sms accordingly

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

teltonika

wavewcom

 

Disclaimer:

This is a neat hack to query modem status and alert admin if found Down. This is no way a standard method to perform this task but I used it as it worked fine for some specific environment. I made this because first , I was unable to find any good solution for this, second, making our customized solution is always far better then ready made tools. because with customization, we can get our desired results by folding/molding and twisting about anything that gets in our way :~)


SCENARIO!

Serial modem is attached to Linux (Ubuntu) system for sending receiving sms via locally installed KANNEL as gateway. Rarely sometimes modem stops responding to sms requests and it require physical power off/on. if system is not very actively monitored by admin, or remote administration is being done, then we require a solution that can alert us by EMAIL if modem stopped working. and when it start working again, send us Email+SMS for the event. But it should not repeatedly send sms/email for same incident.Some checks must be there in order to prevent this.


REQUIREMENTS!

1- Make sure you have WVDIALCONF utility before executing this script. you can install wvidalconf in UBUNTU by following command

sudo apt-get install wvdial

2- for Email part, I used sendEmail which will use GMAIL account to send email.To install it use

TO Install sendEmail Tool …

mkdir /temp
cd /temp
wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
tar zxvf sendEmail-v1.56.tar.gz
cd sendEmail-v1.56/

ADD SUPPORTING LIBRARY

for ubuntu

apt-get -y install libio-socket-ssl-perl libnet-ssleay-perl perl
for centos
yum -y install perl perl-Crypt-SSLeay perl-IO-Socket-SSL

SOLUTION!

the SCRIPT

Schedule (cron) the following to run after every hour or as required.

 

Use following script.

#!/bin/bash
# Linux BASH script to check MODEM Status (example wavecom or teltnokia serial modem or any other using wvdialconf)
# If Modem not responding, then send SMS/EMail alert to admin (trigger one time for each status changed)
# Useful for remote admins, who want to be informed when modem have any responding problem.
# Created: 9th-JUNE-2016
# Syed Jahanzaib
# aacable at hotmail dot com / https://aacable.wordpress.com
# set -x

# If you have kannel configured, then STOP KANNEL SERVICE first, so that it should not conflict with the wvdialconf results / locked pid / syed.jahanzaib
# If you dont have kannel, remote these entries
echo "Stopping Kannel Service to test modem results and sleep for 10 seconds so that kannel service can be shutdown gracefully..."
service kannel stop > /dev/null 2>&1
sleep 10
killall -9 bearerbox > /dev/null 2>&1
echo "Kannel Service Stopped . Processing further ..."

# You can name your modem, MAKE SURE THAT YOU ***DONOT*** ADD SPACES IN IT
MODEM1="SERIAL_MODEM"

# Text that will be checked in wvdialconf output file, if wvdialconf does not detects any modem, it usually add the following line
# Sorry, no modem was detected! Is it in use by another program?
# So we will simply use the grep to catch word **no modem**. You may use other technique that may work for you. Chill / zaib
TEXT_TO_CATCH="no modem"

# Hostname and other Variables
HOSTNAME=`hostname`
COMPANY="zaib (Pvt) Ltd."
FOOTER="Powered By Syed.Jahanzaib"
DATE=`date`

# Temporary file holder for MODEM status
MODEM1_STATUS_TMP_HOLDER="/tmp/modemstatus.txt"
MODEM_QUERY_RESULT="/tmp/modem_query_result.txt"
# Create temp file if not already present, usually for 1st time execution
touch $MODEM1_STATUS_TMP_HOLDER
touch $MODEM_QUERY_RESULT

# SMS RELATED and KANNEL INFO
# KANNEL SMS Gateway Info, will not be useful when modem is down, however it will be ok when moem will respond,
# for down status, we have to use GMAIL to send email
KANNELURL="127.0.0.1:13013"
KANNELID="kannel"
KANNELPASS="KANNELPASS"
CELL1="03333021909"

# GMAIL Section
GMAILID="YOURGMAILID@gmail.com"
GMAILPASS="YOURGMAILPASS"
ADMINMAIL1="TO@hotmail.com"

# sms/email message temporary holder
MSGDOWNHOLDER="/tmp/$MODEM1_down.msg"
MSGUPHOLDER="/tmp/$MODEM1_up.msg"

## SMS/EMAIL Messages for DOWN
MSG_DOWN="ALERT: SMS $MODEM1 not responding @ $DATE. Try to restart it by power off/on.

$COMPANY
$FOOTER"

## SME/EMAIL Messages for UP
MSG_UP="INFO: SMS $MODEM1 is now responding @ $DATE OK.

$COMPANY
$FOOTER"

# RUN WVDIALCONF utility to output modem query result in $MODEM_QUERY_RESULT file
wvdialconf > $MODEM_QUERY_RESULT

# Run the script
echo "Checking $MODEM1 Current Status @$DATE..."
for MODEM in $MODEM1
do

# Match $TEXT_TO_CATCH in the query result
STATUS=`cat $MODEM_QUERY_RESULT | grep "$TEXT_TO_CATCH"`
if [ -n "$STATUS" ]; then

# Print Result for information purposes
echo "ALERT: text ** $TEXT_TO_CATCH ** found in $MODEM_QUERY_RESULT"
echo "$MODEM seems to be DOWN ..."

# IF modem found DOWN, and email/SMS not already sent, then send it one time to $CELL1/x
if [ $(grep -c "$MODEM" "$MODEM1_STATUS_TMP_HOLDER") -eq 0 ]; then
echo "$MODEM is down at $(date) .. SENDING DOWN SMS/EMAIL for current incident / for one time only ..."

# Update down message to be sent via sms/email
echo "$MSG_DOWN" > $MSGDOWNHOLDER

# Update Modem down status in a file so that sms/email should not be sent again and again
echo "$MODEM1" > $MODEM1_STATUS_TMP_HOLDER

#cat $MSGDOWNHOLDER

# Sending DOWN SMS via KANNEL
echo "Sending SMS , if modem is down, sms will not be sent ..."
cat $MSGDOWNHOLDER | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-

######## EMAIL SECTION ##############
# Make sure you install sendEMAIL tool and test it properly before using email section.
#SEND EMAIL Alert As well using sendEMAIL tool using GMAIL ADDRESS.
# If you want to send email , use below ...
echo "Sending SEMAIL ALERT to $ADMINMAIL1  ..."
/temp/sendEmail-v1.56/sendEmail -u "ALERT: $MODEM1 not responding @ $DATE" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$MSGDOWNHOLDER -o message-content-type=text
fi

# Else if $TEXT_TO_CATCH does not found in $MODEM_QUERY_RESULT, then consider MODEM is UP
else
echo "INFO: Word ** $TEXT_TO_CATCH ** was NOT found in $MODEM_QUERY_RESULT"
echo "$MODEM seems to be responding OK!"

# If modem found UP and last status was DOWN, then send UP msg one time
if [ $(grep -c "$MODEM" "$MODEM1_STATUS_TMP_HOLDER") -eq 1 ]; then
echo "$MODEM is now responding at $(date)... SENDING UP SMS for current incident one time only ..."
echo "$MSG_UP" > $MSGUPHOLDER
#cat $MSGUPHOLDER

# START KANEL SERVICE SO THAT UP INFO SMM CAN BE SENT to ADMIN
echo "Starting Kannel Services so that UP sms can be sent via kannel (with 5 seconds delay) ..."
service kannel start > /dev/null 2>&1
sleep 5

# Sending UP SMS via KANNEL
echo "Sending UP SMS , if modem is down, sms will not be sent ..."
cat $MSGUPHOLDER | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-

############## EMAIL SECTION ##############
# Make sure you install sendEMAIL tool and test it properly before using email section.
#SEND EMAIL Alert As well using sendEMAIL tool using GMAIL ADDRESS.
# If you want to send email , use below ...

echo "Sending SEMAIL UP/OK to $ADMINMAIL1 & $ADMINMAIL2 ..."
/temp/sendEmail-v1.56/sendEmail -u "INFO: $MODEM1 responding now OK @ $DATE" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$MSGDOWNHOLDER -o message-content-type=text

# Update Modem UP status in a file so that sms/email should not be sent again and again
sed -i "/$MODEM1/d" "$MODEM1_STATUS_TMP_HOLDER"
fi
fi
done

# START KANEL SERVICE IN END, because we stopped it in the beginning
echo "Starting Kannel Services that we stopped in beginning of this script ..."
service kannel start > /dev/null 2>&1

# Script Ends Here ...
# z@iB

RESULT!

1- DOWN result

Screenshot_2016-06-09-11-24-08

 

1 Comment »

  1. Love your work mate! If you are interested in doing some remote work please ping me!

    Kind regards,

    Mark Vella-West | Chief Technical Officer

    * Decipher Technology | Empower Life™*

    WEB: http://www.reciphergroup.com | EMAIL: mark @reciphergroup.com | 24/7 HELPDESK: +613 8395 1766

    *COMPANY INFO:* *https://youtu.be/Qa_QOpfqQpQ *

    *VPN ROUTERS:* https://youtu.be/x8hjMBW_KdM

    *UNBLOCK USA NETFLIX: GetFlix *

    On 9 June 2016 at 17:31, Syed Jahanzaib Personal Blog to Share Knowledge ! wrote:

    > Syed Jahanzaib / Pinochio~:) posted: ” SCENARIO! Serial modem is > attached to Linux (Ubuntu) system for sending receiving sms via locally > installed KANNEL as gateway. Rarely sometimes modem stops responding to sms > requests and it require physical power off/on. if system i” >

    Comment by RECiPHER Group Pty. Ltd. — June 9, 2016 @ 12:52 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 )

Google+ photo

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

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: