0

So I want to keep an eye on my network connectivity so I can make a report for my ISP. (seems like the modem goes out for about 30-60 seconds about once a day)

So I copied this handy little script

http://www.cyberciti.biz/tips/simple-linux-and-unix-system-monitoring-with-ping-command-and-scripts.html

And an running it with

watch -n 15 ./ping-test.sh

I have been testing it by pulling the plug on my modem.

The first time watch runs the script after the network goes out it runs fine (sends some email) and then goes defunct. Once networking is restored the script "finishes" and then watch runs it again.

So I'm getting mail for when the network goes down, but not how long it was down for.

If I run the script straight from the command line it runs (emails) and then exits.

Any ideas why? How can I get the script to keep running once the network is down?

Slashterix
  • 612
  • 1
  • 5
  • 19

4 Answers4

1

You can do this in realtime by using "ip monitor". This will output any events such as the ip address being removed or added the link state changing from operational to disconnected, or a route changing in some way.

ip monitor all >> /some/log.log &

You must open in append mode. If you want to leave it to run outside of the terminal you can just disown the job with "disown %1"

If you need timestamps too, theres a couple of ways. An easy way is to use inotifywait to check if the file gets an update, then append your date to it. Itd be easy to do the above and this into a shell script and perhaps disown that instead so you get

ip monitor all >> /some/log.log &
while inotifywait -qq -e modify /some/log.log; do 
  sleep 0.2;
  date >>/root/mon.log; 
done

Enjoy

Matthew Ife
  • 22,927
  • 2
  • 54
  • 71
  • Cool tool. But I think it only monitors local connectivity. My modem (on the other side of a router) is going down. So the system I am testing from still has an active link to the LAN, just not the internet. – Slashterix Nov 19 '11 at 01:17
1

Your solution with 'watch' command is not good. I would try to solve this with something like this:

#!/bin/bash
HOSTS="foo.bar foo2.bar"
COUNT=4
while :
do
    for myHost in $HOSTS
    do
      count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
      if [ $count -eq 0 ]; then
        # 100% failed 
        echo "Host : $myHost is down (ping failed) at $(date)"
      fi
      done
sleep 15 # here is your refresh rate
done

and then run it as normal script: ./script_name.sh

terminate with CTRL+C.

readyblue
  • 119
  • 1
  • 7
1

ping will hang if it cannot reach its target and that maybe why the script goes defunct because the ping is still waiting for a reply. Try adding the -w deadline or -W timeout option to the ping.

ZaSter
  • 329
  • 4
  • 11
  • Sorry for taking so long to mark this correct. This was the trick I needed. I set -W 2 and now it works like a champ! – Slashterix Jun 19 '12 at 03:47
0

It goes defunct because it is already finished but depends on a parent process - watch command which is still alive.

So I'm getting mail for when the network goes down, but not how long it was down for.

That script is already print the date when the remote system is down:

echo "Host : $myHost is down (ping failed) at $(date)" | mail -s "$SUBJECT" $EMAILID

How can I get the script to keep running once the network is down?

It is pinging with -c 1 so it will stop after sending 1 ECHO_REQUEST packet.

Put it in cron to run every minutes:

*/1 * * * * /path/to/your/script
quanta
  • 50,327
  • 19
  • 152
  • 213
  • When the ping is succeeding, watch will keep re-running the script every 15s. I don't want to use cron as 1 minute is not a small enough window of time. I think the modem is dropping off for 30-60 seconds at a time. I want to get an email every 15s until the link is restored, so I can tell if it was down for 30s or 2 minutes. – Slashterix Nov 19 '11 at 01:14
  • How about this `while true; do /path/to/your/script; sleep 15; done`? – quanta Nov 19 '11 at 16:14