Let’s face it; as a network administrator, your most critical job is keeping your network up. Although operating systems and hardware are making this a much easier task, nothing is infallible. No matter how well your network was designed, no matter how many 9s are in your OS’s uptime promise, your network is going to come crashing down. When that happens, you must react and react immediately. Of course, without the knowledge of a downed network, you cannot possibly react.
In this Daily Feature, I’m going to show you a simple way to set up a free and reliable network alarm system that will sound off if your network connection fails. We are going to try to make this system as flexible and simple as possible.
Using Linux, bash, and cron
Seeing as how my favorite OS is Linux, I thought I’d start there. It only took me about 10 minutes to come up with this simple alarm system using crontab and a simple bash script.
My idea was basically to write a script that would ping an IP address (or URL) and react accordingly. The primary goal here is to loop a sound if the ping fails. For this to work, you will have to have a working Linux installation, complete with network connection and sound.
The first step is to create the bash script that will serve as our watch guard. The script (called netwatch) looks like this:
! ping -c 1 -U www.yahoo.com; do
And that’s it!
URLs and sounds
Note that both the URLs and the sounds in the above file are user-configurable.
Basically, this file runs a single ping on a URL; if the ping is successful, the script ends. If the ping is unsuccessful, the script runs the command (we’ll substitute an actual .wav file found in RedHat 7.1):
until the script is killed with
killall –9 netwatch
After you create the file, there are two more steps to take before it will actually work. The first step is to change the permissions of the file so that it is executable with the command
chmod u+x netwatch
chmod 700 netwatch
The next step is to create a crontab entry that will run the netwatch at certain intervals. We create our crontab file like so:
which will open the crontab editor. (It’s actually the vi editor focused on editing a special file.) Within the crontab editor, you will hit the I key and then enter the following (using my home directory as an example):
*/10 * * * * /home/jlwallen/netwatch
which will run the netwatch script every 10 minutes. Once you’ve entered the above line, hit the [Esc] key and then [Shift]colon (:). Then, type wq and press [Enter]. As soon as you’ve exited the crontab application, the crontab will go into effect and the netwatch script will begin monitoring the status of your network. Should the ping command not work, you will be greeted with a continual sound until the killall command is run.
One issue with this monitor is that it depends on the uptime of the Linux box’s connection to the network it is testing. It also does not take into account all routes on your network. Of course, you could make this much more complex by setting up your routing table so that the script actually tests all possible routes. Say, for instance, you want to have the monitor test both an external and an internal network. For this, you can add two ping lines to the script: the first to an external URL (or IP address) and the second to an internal address.
Let’s say you want to monitor your internetwork as well as your external connection. You could alter your netwatch script to look like:
! ping -c 1 -U external_address
! ping -c 1 -U internal_address; do
Now the alarm will sound anytime either the internal or external connection fails.
The best thing about this system (and Linux in general) is that it allows you a world of creativity to make the system fit your needs. Use your imagination with this type of scripting, and you’ll see that Linux can do things most OSs can’t.