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

 

November 26, 2012

Howto configure your Local SMS HTTP Gateway using KANNEL on Ubuntu

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

 

1

As per web defines: Kannel (http://www.kannel.org/) is one of the most popular open source WAP/SMS Gateway, because its very good performance to handle a huge SMS. Kannel supports many modem’s brand even generic modem, also support multi modems (modem bank).

x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x

After some testing, I have found Kannel one of the best , fastest SMS and WAP gateway among other open source sms gateways , capable of handling several hundreds of requests per second.

Following is a guide on how you can configure your own Local SMS HTTP gateway using KANNEL 

(The aim of configuring kannel on my Ubuntu box was that I required it to send sms via DMASOFTLAB RADIUS MANAGER which uses API to send sms via clickatell HTTP gateway for New Account / Expiry Warning / Password recovery via sms , verification code and many other cool functions. It comes with the clickatell HTTP gateway API which off course require you to buy sms bundle package and do require handsome amount of $$ if you have a huge number of users, Therefore to save $$ money , I  created my own gateway)

O/S = Ubuntu 10.4
Hardware = Teltonika ModemUSB/E12 UM1400 / and Dlink dwm-156 , dlink is a problematic modem

 


 Detecting MODEM

First thing you have to do is to search for working modem port , various methods are used, try to Use WVDIALCONF command to detect modem and see at which port its working (or not detected)

As showed below …

root@radius:/temp# wvdialconf

Editing `/etc/wvdial.conf'.

Scanning your serial ports for a modem.

ttyS0<*1>: ATQ0 V1 E1 -- failed with 2400 baud, next try: 9600 baud
ttyS0<*1>: ATQ0 V1 E1 -- failed with 9600 baud, next try: 115200 baud
ttyS0<*1>: ATQ0 V1 E1 -- and failed too at 115200, giving up.
Modem Port Scan<*1>: S1   S2   S3   S4   S5   S6   S7   S8
Modem Port Scan<*1>: S9   S10  S11  S12  S13  S14  S15  S16
Modem Port Scan<*1>: S17  S18  S19  S20  S21  S22  S23  S24
Modem Port Scan<*1>: S25  S26  S27  S28  S29  S30  S31
ttyUSB0<Info>: Device or resource busy
Modem Port Scan<*1>: USB0
ttyUSB1<*1>: ATQ0 V1 E1 -- failed with 2400 baud, next try: 9600 baud
ttyUSB1<*1>: ATQ0 V1 E1 -- OK
ttyUSB1<*1>: ATQ0 V1 E1 Z -- OK
ttyUSB1<*1>: ATQ0 V1 E1 S0=0 -- OK
ttyUSB1<*1>: ATQ0 V1 E1 S0=0 &C1 -- OK
ttyUSB1<*1>: ATQ0 V1 E1 S0=0 &C1 &D2 -- OK
ttyUSB1<*1>: ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0 -- OK
ttyUSB1<*1>: Modem Identifier: ATI -- MTK2
ttyUSB1<*1>: Max speed is 9600; that should be safe.
ttyUSB1<*1>: ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0 -- OK
ttyUSB2<*1>: ATQ0 V1 E1 -- failed with 2400 baud, next try: 9600 baud
ttyUSB2<*1>: ATQ0 V1 E1 -- failed with 9600 baud, next try: 9600 baud
ttyUSB2<*1>: ATQ0 V1 E1 -- and failed too at 115200, giving up.
ttyUSB3<*1>: ATQ0 V1 E1 -- failed with 2400 baud, next try: 9600 baud
ttyUSB3<*1>: ATQ0 V1 E1 -- failed with 9600 baud, next try: 9600 baud
ttyUSB3<*1>: ATQ0 V1 E1 -- and failed too at 115200, giving up.

Found a modem on /dev/ttyUSB1.
Modem configuration written to /etc/wvdial.conf.
ttyUSB1<Info>: Speed 9600; init "ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0"

As you can see the modem is detected on ttyUSB1 port.


 

INSTALL KANNEL in ubuntu

First install KANNEL using apt-get (configuring it on ubuntu is the easiest task ! that’s why i love ubuntu ,not all but in most cases 😉 )


apt-get install libxml2

apt-get install kannel

 


EDIT KANNEL CONFIGURATION FILE

Now open the kannel config file by

nano /etc/kannel/kannel.conf

Remove all the lines and paste the following config

!     MAKE     SURE     YOU     CHANGE     THE     USB     PORT      IN    THE  DEVICE     SECTION     ! Also pay attention to speed as some modems like serial may work on 115200 speed , zaib

# Syed Jahanzaib
# https://aacable.wordpress.com
# aacable [at] hotmail.com

group = core
admin-port = 13000
smsbox-port = 13001
admin-password = zaibadmin
status-password = zaibstatus
log-file = "/var/log/kannel/bearerbox.log"
log-level = 0
box-deny-ip = "*.*.*.*"
box-allow-ip = "127.0.0.1"
#Send sms only once. No retry
sms-resend-retry = 1

group = smsc
smsc = at
smsc-id = zaibgsmid
modemtype = teltonika
device = /dev/ttyACM0
# Change above port to match your port, it could ACM1 or USB0, if you dont set it correctly,  it will not work and you will get error in logs that unable to connect to device)
# Or use serial-by-id like
# device = /dev/serial/by-id/usb-D-Link_Inc_D-Link_DWM-156-if03-port0
speed = 19200
my-number = 0333302100000 # (put your sim number, not necessary)
# Following SMSC number is for zong
# sms-center = 00923040000011
sim-buffering = true
log-level = 0

# for teltonkia only, if you are using other brand you may need to get there INIT stribgs
# But what I have seen that this string worked for dlink and huawei modem as well.
group = modems
id = teltonika
name = "Teltonika E12"
detect-string = "Undefined"
enable-mms = true
init-string = "AT+CNMI=2,2,0,1,1"
# or i used this for dlink as well.
#init-string = "ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0"
speed = 115200
message-storage = "SM"

# for Huawei USB/GSM modem e155 i guess, remove hashes from below lines if u r using this model.
#group = modems
#id = huawei
#name = "Huawei"
#detect-string = "huawei"
#enable-mms = true
#init-string = "ATZ"
#init-string = "ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0"

group = smsbox
bearerbox-host = localhost
sendsms-port = 13013
global-sender = 13013
sendsms-chars = "0123456789 +-"
log-file = "/var/log/kannel/smsbox.log"
log-level = 0
access-log = "/var/log/kannel/access.log"

group = sendsms-user
# MAKE VERY SURE that you change the PASSWORD for security reasons
username = kannel
password = kannel
concatenation = true
max-messages = 1000

group = sms-service
keyword = default
get-url = "http://localhost/kannel/receivesms.php?sender=%p&text=%b"
# If you have playSMS installed to receive sms use below line instead
#get-url = "http://localhost/playsms/index.php?app=call&cat=gateway&plugin=kannel&access=geturl&t=%t&q=%q&a=%a&Q=%Q"
accept-x-kannel-headers = true
max-messages = 99
concatenation = true
catch-all = true
text = "Galaxy ROBOT by Zaib"
omit-empty = true

 


 START KANNEL CONSOLE   

Now start kannel (for test start with two terminals, one bearerbox and smsbox)

[ BEARER & SMSBOX ]

Open two terminal windows and issue below commands in each terminal box in the following order.

1. Start the Bearer Box

and leave it running as it will show you the debug logs)

bearerbox -v 0 /etc/kannel/kannel.conf

You may see the following screenshot if your config is ok

Now leave it running.

 

2. Start the SMSBOX in separate terminal

(in second terminal window and leave it running as it will show you the debug logs)


smsbox -v 0 /etc/kannel/kannel.conf

 

You may see the following screenshot if your config is OK

[If you receive error that the address is already in use, first issue the command service kannel stop]

 


 

CHECK KANNEL CONFIGURATION STATUS VIA CLI and WEB

First check kannel service status with

ps aux |grep kannel

and you should see something like this


root@radius:/temp# ps aux |grep kannel
kannel   15448  0.0  0.0   2252   308 ?        Ss   12:05   0:00 /usr/sbin/run_kannel_box --pidfile /var/run/kannel/kannel_bearerbox.pid --no-extra-args /usr/sbin/bearerbox -v 4 -- /etc/kannel/kannel.conf
kannel   15450  1.5  0.1  94936  7780 ?        Sl   12:05   0:10 /usr/sbin/bearerbox -v 4 -- /etc/kannel/kannel.conf
kannel   15465  0.0  0.0   2252   368 ?        Ss   12:05   0:00 /usr/sbin/run_kannel_box --pidfile /var/run/kannel/kannel_wapbox.pid --no-extra-args /usr/sbin/wapbox -v 4 -- /etc/kannel/kannel.conf
kannel   15468  0.0  0.0   2252   312 ?        Ss   12:05   0:00 /usr/sbin/run_kannel_box --pidfile /var/run/kannel/kannel_smsbox.pid --no-extra-args /usr/sbin/smsbox -v 4 -- /etc/kannel/kannel.conf
kannel   15470  0.0  0.0  71192  2544 ?        Sl   12:05   0:00 /usr/sbin/smsbox -v 4 -- /etc/kannel/kannel.conf
root     23357  0.0  0.0   4388   820 pts/0    S+   12:16   0:00 grep --color=auto kannel

CHECK via WEB

Now From your browse, type the following to make sure kannel is working fine 🙂

http://101.11.11.250:13000/status?password=zaibstatus

[change the ip and password to match your config]

and you may see the following screen , if your config is ok

 


 

How to Send SMS via HTTP command

Open your browser and paste the following command.

http://101.11.11.250:13013/cgi-bin/sendsms?username=kannel&password=kannel&to=03333021909&text=ZAIB+KANNEL+WORK

 

NOTE: Change the IP / ID + Password = and number to match your local configuration.

 

The result would be similar to the following . . .

 

and you shoudl receive sms on your cell as following …

kannel_works

 

 


Some Tips and reference.


Howto send SMS via GAMMU CLI

gammu sendsms TEXT 03333021909 -text "Test Msg from GAMMU  .."

Howto send USSD CODES via GAMMU CLI

For balance check (below example is for zong balacne check)

gammu --getussd *222# 


Howto send sms via CLI
using KANNEL

curl "http://localhost:13013/cgi-bin/sendsms?username=kannel&password=KANNELPASS&to=03333021909&text=Welcome+Jahanzaib+Test+Message"

Sending mesage with new line using %0A code using KANNEL

curl "http://localhost:13013/cgi-bin/sendsms?username=kannel&password=KANNELPASS&to=03333021909&text=Welcome+Jahanzaib+Test+Message%0ANew+line"

Howto send sms using FILE via CLI using KANNEL

curl "http://127.0.0.1:13013/cgi-bin/sendsms?username=kannel&password=kannelpassword&to=03333333333" -G --data-urlencode text@/temp/message.txt

Howto send sms using any output  via CLI using KANNEL

ls -l | curl "http://127.0.0.1:13013/cgi-bin/sendsms?username=kannel&password=kannelpassword&to=03333333333" -G --data-urlencode text@-

 

SEND SMS FROM MIKROTIK USING KANNEL AS GW


/tool fetch url="http://KANNEL_GW_IP:13013/cgi-bin/sendsms\?username=kannel&password=KANNELPASSWORD&to=03333021909&text=ALERT:+INTERNET+Link+DOWN+at+$date+$time+[YOURCOMPANY+Pvt+Ltd]"

 


 

Howto make KANNEL start on reboot 

edit following file

/etc/default/kannel

and make sure it looks like following

START_WAPBOX=1
START_SMSBOX=1

If you receive some permission error in logs like below image

ttyacm

then follow this guide

https://aacable.wordpress.com/2014/11/21/kannel-auto-startup-problem-with-system-error-13-permission-denied/

 


 

 

 

Regard’s
Syed Jahanzaib

%d bloggers like this: