Simple Ping Monitoring with OpenWRT and aspsms
15 Mar, 2016
I use this to get informed by SMS, when VirtHost or Monitoring goes down.
ssh -lroot openwrt-box
/etc/config/bin/aspsms.sh
------
#!/bin/sh
# send aspsms messages
PATH=/usr/bin:/usr/sbin:/bin:/sbin:/etc/config/bin
export PATH
UserKey='AABBCC112233'
Password='secret9'
FROM=fw-owrt
MOBILE="$1"
SMS="$2"
SMS="`echo $SMS | tr ' ' '_'`"
echo $SMS
curl -k "https://webservice.aspsms.com/aspsmsx2.asmx/SendSimpleTextSMS?UserKey=$UserKey&Password=$Password&Recipients=$MOBILE&Originator=$FROM&MessageText=$SMS"
------
/etc/config/bin/checkhost.sh
------
#!/bin/sh
# check some hosts and send ping
PATH=/usr/bin:/usr/sbin:/bin:/sbin:/etc/config/bin
export PATH
HOSTS='kvm1:10.2.1.30 kvm2:10.2.1.31 fw-a-u008-01:10.2.100.1 nagios1:10.2.1.24 uplink_to_google:8.8.8.8'
MOBILES='+41794221100 +41782112120'
for HOST in $HOSTS
do
IP=`echo $HOST | cut -d: -f2`
NAME=`echo $HOST | cut -d: -f1`
ping -c1 -w1 $IP >/dev/null 2>&1 || ping -c1 -w1 $IP >/dev/null 2>&1
if [ $? -ne 0 ]
then
test -f /tmp/$NAME.fail
if [ $? -ne 0 ]
then
touch /tmp/$NAME.fail
for MOBILE in $MOBILES
do
aspsms.sh $MOBILE "$NAME is down"
done
fi
else
test -f /tmp/$NAME.fail
if [ $? -eq 0 ]
then
rm -f /tmp/$NAME.fail
for MOBILE in $MOBILES
do
aspsms.sh $MOBILE "$NAME is online"
done
fi
fi
done
------
insert cron job
crontab -e
------
* * * * * /etc/config/bin/checkhost.sh
------
put things together:
opkg update
opkg install curl
chmod 700 /etc/config/bin/*
/etc/init.d/cron enable
/etc/init.d/cron stop
/etc/init.d/cron start