We introduced a ping monitoring program in this article. In this article, we will start documenting our script by going over the main routine:
use Time::Local; use Mail::Sendmail; use Net::Ping::External qw(ping); $pmwrap=0; while (6 ne 9){ # Loop forever... I don't mind. system("cp new.dnsf dnsfc"); # Each pass through the servers, copy dnsf again to catch changes open (DC,"dnsfc"); $previousline="does not exist"; while(<DC>){ open (PM,">> mon.txt"); chomp; if(/^##M/){ ($previousip,$previoushost)=split /\t/,$previousline; print PM $previoushost." : "; if($pmwrap eq 6){ #just to make the output pretty by printing a newline every seven servers. printtime(); $pmwrap=-1; } $pmwrap++; ckserv($previoushost,$previousip); } $previousline=$_; close PM; } close DC; } |
We have tested this program on both Windows and GNU/Linux platforms. Note that the cp command above needs to be changed to copy for Windows. For a discussion of the modules used, see this article. $pmwrap is just a way to track how many hosts have been listed. Every seven hosts, the script will print a newline and a timestamp. The main body, between the while (6 ne 9) just runs forever. Sigh… Agatha’s favorite programming joke, which she has used before in other articles. The cp new.dnsf command allows you to edit the hosts file and save it. Here is what the new.dnsf file looks like:
# 07-MAY-2003 Agatha Codrust Initial entries 10.50.100.1 srv-1 ##M 10.50.100.72 mondo ##M 10.50.100.59 srv-33 ##M 10.50.100.52 srv-3 10.50.100.53 srv-34 10.50.100.54 ares 10.50.100.50 srv-44 ##M 10.50.100.67 srv-48 ##M 10.50.100.66 srv-49 ##M |
(If you haven’t noticed, Agatha is quite a srv-44 fan. Season one is out on DVD here) The hosts file could be used as an actual hosts file, because the format is done with valid comment lines. In the past, we have used the hosts file to generate DNS zones as well, so dealing with monitoring and DNS can be done at the same time. The script opens dnsfc for reading and increments through the hosts. All logging is appended to the log file mon.txt. The chomp command gets rid of the newline so we can reprint the hostname. The /^##M/ determines if we should monitor the host. A ##M on the line after the host means we monitor that host. We then use the split command on /t (tab) to split out the previous lines ip and hostname. We will look at the printtime() ckserv() functions in future articles. Stay tuned.
Don’t rely on this script for production servers unless you know exactly what you are doing, and are sure that this script fits your needs. Do feel free to encorporate bits of the script as you need, or the whole script if you desire. Credit NetAdminToools, though, if you feel like it. 🙂 Please read our terms of use.
There are five parts to this article:
Introduction
Main Routine
Check/Log Routines
Adding Perl Mods
pf and rf routines