I use this script to monitor if the laptop running it, has lost its network link.
That may mean that either the switch has broken down, the cable has been disconnected or there is a power failure.
As power failures are quite frequent at mine, when one is detected (NOCARRIER), the script automatically sends a text message from the computer monitoring the link (running the script) to my mobile phone.
First of all, you need to have a mobile phone attached to the computer though.
After you attach the phone using the usb cable, run:
root@laptop:~# dmesg
You will find the last lines are similar to these ones:
[ 593.894790] usb 2-1.1: new high-speed USB device number 3 using ehci_hcd
[ 594.100371] cdc_acm 2-1.1:1.1: ttyACM0: USB ACM device
[ 594.101194] cdc_acm 2-1.1:1.3: ttyACM1: USB ACM device
[ 594.101738] usbcore: registered new interface driver cdc_acm
[ 594.101744] cdc_acm: USB Abstract Control Model driver for USB modems and ISDN adapters
[ 594.122794] NET: Registered protocol family 35
[ 594.138859] usbcore: registered new interface driver cdc_phonet
[ 594.179092] usbcore: registered new interface driver cdc_ether
[ 594.190537] usbcore: registered new interface driver rndis_host
[ 594.199804] usbcore: registered new interface driver rndis_wlan
We are interested on the device name, which happens to be ttyACM0 ttyACM1
2 new devices in /dev can be found with this name
root@laptop:~# ls -l /dev/ttyACM*
crw-rw---- 1 root dialout 166, 0 Sep 5 18:03 /dev/ttyACM0
crw-rw---- 1 root dialout 166, 1 Sep 5 18:03 /dev/ttyACM1
Then you need to install gammu, to be able to send texts from your computer using the phone. (apt-get install gammu || yum install gammu)
In order for gammu to work, you need to create the file /etc/gammurc with a content similar to the following example, where the most important part is the port (port = /dev/ttyACM0)
You can check what port your phone is using by using a USB cable and typing dmesg as we just did.
cat /etc/gammurc
[gammu]
port = /dev/ttyACM0
connection = at19200
startinfo = no
name=Nokia 5800
synchronizetime = no
use_locking = no
;gammuloc = locfile
;gammucoding = utf8
;logfile = gammulog
;logformat = textall
;model=w200i
You can try sending a message from the mobile phone, using this command:
root@laptop:~# echo "This is a test using gammu" | gammu --sendsms TEXT PHONENUMBER
Where PHONENUMBER must be changed accordingly
It's not necessary to select any profile (PC Suite, mass storage…) on the phone, when you attach it to the computer using the usb cable.
Here's the script to monitor the network card link status
It can be easily tweaked to monitor if there is a ping reply, instead of "NOCARRIER".
Note that DHCP computers may loose its IP when there is a network link failure, that's why I monitor "NOCARRIER".
cat /usr/local/bin/powerFailure.sh
#!/bin/bash
IPToPing="192.168.6.1"
PHONE1="0034654854xxx"
PHONE2="0034603425xxx"
#Max number of checks
maxAttempts="2"
#Time to wait between checks
waitTime="3"
tmpFile="/tmp/pingDate"
attempt="0"
while [ $attempt -lt $maxAttempts ]; do
#NOCARRIER="`ping -c 2 $IPToPing|grep "packet loss"|awk -F% '{print $1}'|awk '{print $NF}'`"
NOCARRIER="`ip a|grep eth0|grep NO-CARRIER`"
NOCARRIER=$?
if [ $NOCARRIER -eq "0" ]; then
success=0
let attempt=attempt+1
sleep $waitTime
else
success=1
attempt=$maxAttempts
if [ -f $tmpFile ]; then
rm -f $tmpFile
fi
fi
done
if [ $success -eq "0" ]; then
#If it's the first check
if [ ! -f $tmpFile ]; then
echo "`date +%s`" > $tmpFile
echo "Power failure detected"|logger
echo "Power failure detected - $(date +%Y%m%d-%H:%M:%S) 1/2"|gammu --sendsms TEXT $PHONE1
# echo "Power failure detected - $(date +%Y%m%d-%H:%M:%S) 1/2"|gammu --sendsms TEXT $PHONE2
else
secondsPassed="0"
let secondsPassed="`date +%s` - `cat $tmpFile`"
echo $secondsPassed
#Do the actions when this specific time has passed
if [ $secondsPassed -gt "60" ]; then
echo "Power failure detected"|logger
echo "Power failure detected - $(date +%Y%m%d-%H:%M:%S) - Shutting down server... 2/2"|gammu --sendsms TEXT $PHONE2
rm -f $tmpFile
shutdown -h now
fi
fi
fi
exit 0
Remember to give execution permissions to the file after creating it:
chmod a+x /usr/local/bin/powerFailure.sh
This is how the script is launched from cron:
cat /etc/cron.d/powerFailure
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
*/2 * * * * root /bin/bash /usr/local/bin/powerFailure.sh
Restart cron:
/etc/init.d/cron restart