#!/usr/bin/perl -wT # # ======================================================== # DNSstats.pl version1.0 August 2004 # ---------------------------------- # Script to retreive DNS stats for graphics. # Georges Rosset, http://rosset.org # # Credit: # Hacked and modifed from Chris's rndcstats.pl # http://www.snoogans.co.uk # ======================================================== # use strict; use File::Copy; $ENV{'PATH'} = "/usr/local/sbin/"; # =========================================================================== # user defined variables # --------------------------------------------------------------------------- my $dir = "/var/log"; # location of Bind stats file my $BINDstatsfile = "$dir/named.stats"; # name of Bind stats file my $BINDid = "0"; # userID of Bind my $TMPstatsfile = "/root/tmp/DNSstats.tmp"; # temp stats file my $rndc = "/usr/local/sbin/rndc"; # location of rndc # =========================================================================== # Nothing to change below this line # =========================================================================== my %start; my %current; my %total; my %time; # -------------------------------- # copy BIND stats file to TMP file # -------------------------------- copy($BINDstatsfile, $TMPstatsfile); chown($BINDid, $BINDid, $TMPstatsfile); sleep 1; # -------------------------------- # read in TMP stats file # -------------------------------- open(TMP, "<$TMPstatsfile") or die "Could not open $TMPstatsfile\n"; while() { chomp $_; &process("start", $_); } close(TMP); # -------------------------------- # run rndc on BIND stats file # -------------------------------- open(STATS, ">$BINDstatsfile") or die "Could not open $BINDstatsfile\n"; close(STATS); system("$rndc", "stats"); # -------------------------------- # read BIND stats file # -------------------------------- open(BIND, "<$BINDstatsfile") or die "Could not open $BINDstatsfile\n"; while() { chomp $_; &process("current", $_); } close(BIND); # -------------------------------- # calculate elapsed time # -------------------------------- $time{'total'} = $time{'current'} - $time{'start'}; # -------------------------------- # process hash # -------------------------------- foreach my $zone (keys %current) { for my $type (keys %{$current{$zone}}) { if(!defined $start{$zone}{$type}) { # this zone was not hosted when this script was first run with the "start" parameter. $start{$zone}{$type} = 0; } # calculate queries since start $total{$zone}{$type} = $current{$zone}{$type} - $start{$zone}{$type}; } # calculate total queries $total{$zone}{'total'} = $total{$zone}{'success'} + $total{$zone}{'referral'} + $total{$zone}{'nxrrset'} + $total{$zone}{'nxdomain'} + $total{$zone}{'recursion'} + $total{$zone}{'failure'}; # calculate total queries per second $total{$zone}{'persec'} = ($total{$zone}{'success'} + $total{$zone}{'referral'} + $total{$zone}{'nxrrset'} + $total{$zone}{'nxdomain'} + $total{$zone}{'recursion'} + $total{$zone}{'failure'}) / $time{'total'}; } # -------------------------------- # Get number of queries per second # -------------------------------- $total{'main server'}{'persec_success'} = $total{'main server'}{'success'} / $time{'total'}; $total{'main server'}{'persec_referral'} = $total{'main server'}{'referral'} / $time{'total'}; $total{'main server'}{'persec_nxrrset'} = $total{'main server'}{'nxrrset'} / $time{'total'}; $total{'main server'}{'persec_nxdomain'} = $total{'main server'}{'nxdomain'} / $time{'total'}; $total{'main server'}{'persec_recursion'} = $total{'main server'}{'recursion'} / $time{'total'}; $total{'main server'}{'persec_failure'} = $total{'main server'}{'failure'} / $time{'total'}; my $startdate = localtime($time{'start'}); # -------------------------------- # Format statistics # -------------------------------- printf("%.3f", $total{'main server'}{'persec_success'}); print ":"; printf("%.3f", $total{'main server'}{'persec_referral'}); print ":"; printf("%.3f", $total{'main server'}{'persec_nxrrset'}); print ":"; printf("%.3f", $total{'main server'}{'persec_nxdomain'}); print ":"; printf("%.3f", $total{'main server'}{'persec_recursion'}); print ":"; printf("%.3f", $total{'main server'}{'persec_failure'}); print ":"; printf("%.3f", $total{'main server'}{'persec'}); print "\n"; exit; # -------------------------------- # get the number values # -------------------------------- sub process { my($start, $line) = @_; my($type, $val, $zone) = split(" ", $line); if(!$zone) { $zone = "main server"; } if($start eq "start") { # get time values if($line =~ / Statistics Dump /) { $time{'start'} = &gettime($line); return(); } $start{$zone}{$type} = $val; } elsif($start eq "current") { # get time values if($line =~ / Statistics Dump /) { $time{'current'} = &gettime($line); return(); } $current{$zone}{$type} = $val; } } # -------------------------------- # retreive time # -------------------------------- sub gettime { my $timestamp = $_; # strip away unwanted info $timestamp =~ s/\+\+\+ Statistics Dump \+\+\+ \(//; $timestamp =~ s/--- Statistics Dump --- \(//; $timestamp =~ s/\)//; return($timestamp); } # ===========================================================================