MRTG DNS availability test script

I’ve knocked up a little Perl script so that MRTG can be used to graph DNS availability. It simply does an nslookup against a specific name server and outputs either "100" or "0" depending on whether the lookup worked or not. MRTG will average these values over time and provide you with a percentage availability figure. This is what I have so far:
# DNS Availability Script for MRTG (
# Developer:
my $domain = $ARGV[0];  # Domain name to query
my $ns     = $ARGV[1];  # Name server to query
my $digs   = $ns ? "@\Q$ns\E" : "";
my $digcmd = "dig $digs \Q$domain\E a";
my $dig    = qx<$digcmd>;
my $status;
my $value=0;
for (split /\n/, $dig) {
   if (/^;;.*status:\s+(\S+)\s*,/) {
      $status = $1;
if ($status ne ‘NOERROR’) {
        # Status=bad
} else {
        # Status=ok
print "$value\n$value\n";
exit 0;

The first argument is the name you want to look up, the second argument is the IP address of the name server you want to query. The MRTG configuration looks a little like this:
Target[patas.dnsavail]: `perl /var/www/mrtg/cfg/`
Title[patas.dnsavail]: DNS Server availability
PageTop[patas.dnsavail]: <H1>DNS Availability (patas – Parkgate Campus)</H1>
MaxBytes[patas.dnsavail]: 100
ShortLegend[patas.dnsavail]: %
YLegend[patas.dnsavail]: Availability
Legend1[patas.dnsavail]: Current DNS availability
Options[patas.dnsavail]: growright,nopercent,gauge

It’s only been running for an hour as I write this, but you can see the data coming in and MRTG performing the averaging in the following screen shot.
Let me know if you find this useful – I’ve already searched google and couldn’t find anything that did this so ended up doing it myself.
This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s