#!/usr/bin/perl -wT #***************************************************************************** #* * #* AUTHOR : Chris Pile - cpile@snoogans.co.uk #* * #* DESCRIPTION : Simple script to calculate/display DNS queries per second. #* Intended for use with BIND 9 #* * #***************************************************************************** #* * #* $Source: /usr/local/cvsroot/perl/rndcstats.pl,v $ #* $State: Exp $ #* Tag $Name: $ #* * #***************************************************************************** #* * #* $Log: rndcstats.pl,v $ #* Revision 1.3 2001/05/23 13:34:10 chris #* Added a "chown $current_statsfile" to make things easier for first time users. #* Also indicates that you need to run "./rndcstats.pl start" the first time you run the script. #* The perldoc DESCRIPTION now mentions that this script is for use with BIND 9. #* #* Revision 1.2 2001/05/23 11:32:16 chris #* Fixed a few bugs: #* nxdomain and failure values were mixed up #* converted the totals column to use decimal points #* updated examples #* if the CURRENT file couldn't be opened, it would report the START file couldn't be opened, corrected. #* #* Revision 1.1.1.1 2001/05/23 11:13:01 chris #* Initial import into CVS. #* #***************************************************************************** use strict; use File::Copy; #### # Clean path whenever you use taint checks (Make %ENV safer) $ENV{'PATH'} = "/usr/local/sbin/"; # set up variables my $dir = "/var/log"; my $start_statsfile = "$dir/named.stats.start"; my $current_statsfile = "$dir/named.stats"; my $rndc = "/usr/local/sbin/rndc"; my $nameduserid = "6464"; my @startlines; my @currentlines; #### # if user entered "stats.pl start" (create start file) if(@ARGV && $ARGV[0] eq "start") { open(STATS, ">$current_statsfile") or die "Could not open $current_statsfile\n"; close(STATS); chown($nameduserid, $nameduserid, $current_statsfile); system("$rndc", "stats"); copy($current_statsfile, $start_statsfile); chown($nameduserid, $nameduserid, $start_statsfile); # wait for 1 second to avoid divide by zero errors sleep 1; } #### # read in start stats file open(START, "<$start_statsfile") or die "Could not open $start_statsfile\n\nHave you tried running \"$0 start\"\n"; while() { chomp $_; push(@startlines, $_); } close(START); # erase contents of current stats file, then run rndc stats again. open(STATS, ">$current_statsfile") or die "Could not open $current_statsfile\n"; close(STATS); system("$rndc", "stats"); # read in the current stats file open(CURRENT, "<$current_statsfile") or die "Could not open $current_statsfile\n"; while() { chomp $_; push(@currentlines, $_); } close(CURRENT); #### # work out all the stats values (will convert into hashes at some point) my ($starttimestamp, $startsuccess, $startreferral, $startnxrrset, $startnxdomain, $startfailure) = &getvals(@startlines); my ($currenttimestamp, $currentsuccess, $currentreferral, $currentnxrrset, $currentnxdomain, $currentfailure) = &getvals(@currentlines); my $totaltimestamp = $currenttimestamp - $starttimestamp; my $totalsuccess = $currentsuccess - $startsuccess; my $persec_success = $totalsuccess / $totaltimestamp; my $totalreferral = $currentreferral - $startreferral; my $persec_referral = $totalreferral / $totaltimestamp; my $totalnxrrset = $currentnxrrset - $startnxrrset; my $persec_nxrrset = $totalnxrrset / $totaltimestamp; my $totalnxdomain = $currentnxdomain - $startnxdomain; my $persec_nxdomain = $totalnxdomain / $totaltimestamp; my $totalfailure = $currentfailure - $startfailure; my $persec_failure = $totalfailure / $totaltimestamp; my $persec_total = ($totalsuccess + $totalreferral + $totalnxrrset + $totalnxdomain + $totalfailure) / $totaltimestamp; my $starttime = localtime($starttimestamp); #### # display the stats format STDOUT_TOP = STATS BREAKDOWN OF "named.stats" stats since: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $starttime start current total per second ------------------------------------------------------------------------------ . format STDOUT = time @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<< $starttimestamp, $currenttimestamp, $totaltimestamp success @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<< @######.### $startsuccess, $currentsuccess, $totalsuccess, $persec_success referral @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<< @######.### $startreferral, $currentreferral, $totalreferral, $persec_referral nxrrset @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<< @######.### $startnxrrset, $currentnxrrset, $totalnxrrset, $persec_nxrrset nxdomain @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<< @######.### $startnxdomain, $currentnxdomain, $totalnxdomain, $persec_nxdomain failure @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<< @######.### $startfailure, $currentfailure, $totalfailure, $persec_failure ------------------------------------------------------------------------------ total per sec @######.### $persec_total . write; #### ## example stats dump: #+++ Statistics Dump +++ (989830934) #success 25460 #referral 6 #nxrrset 8138 #nxdomain 119129 #recursion 113449 #failure 19781 #--- Statistics Dump --- (989830934) ## # get the number values sub getvals { my @lines = @_; my $timestamp = pop(@lines); $timestamp =~ s/--- Statistics Dump --- \(//; $timestamp =~ s/\)//; my $failure = pop(@lines); $failure =~ s/failure //; my $recursion = pop(@lines); $recursion =~ s/recursion //; my $nxdomain = pop(@lines); $nxdomain =~ s/nxdomain //; my $nxrrset = pop(@lines); $nxrrset =~ s/nxrrset //; my $referral = pop(@lines); $referral =~ s/referral //; my $success = pop(@lines); $success =~ s/success //; return($timestamp, $success, $referral, $nxrrset, $nxdomain, $failure); } __END__ =head1 NAME rndcstats.pl - Uses the B command and file to calculate number of queries per second. =head1 SYNOPSIS B BstartE> =head1 DESCRIPTION Intended for use with BIND 9 Quick and nasty at the mo. The following parameter is required when you first run the script: =over 4 =item B Indicates that this is the first time you have run the script and creates a base stats file which subsequent calls of the script will use. Once you have used B B once, further calls should just use B. Use B B if you want to start the count from zero again. =back You need an entry like: options { statistics-file "/var/log/named.stats"; } in your named.conf file Also you may have to edit the B<$ENV{'PATH'}>, B<$dir>, B<$start_statsfile>, B<$current_statsfile>, B<$rndc> and B<$nameduserid> variables at the top of this script. =head1 EXAMPLE Here is an example: $ ./rndcstats.pl start rndc: stats command successful rndc: stats command successful STATS BREAKDOWN OF "named.stats" stats since: Wed May 23 12:19:57 2001 start current total per second ------------------------------------------------------------------------------ time 990616797 990616798 1 success 39270 39270 0 0.000 referral 6 6 0 0.000 nxrrset 8210 8210 0 0.000 nxdomain 119315 119315 0 0.000 failure 19841 19841 0 0.000 ------------------------------------------------------------------------------ total per sec 0.000 Here is another example: $ ./rndcstats.pl rndc: stats command successful STATS BREAKDOWN OF "named.stats" stats since: Mon May 21 13:57:42 2001 start current total per second ------------------------------------------------------------------------------ time 990449862 990617034 167172 success 283045487 327777535 44732048 267.581 referral 39017 40036 1019 0.006 nxrrset 2377514 2747167 369653 2.211 nxdomain 21310712 24890755 3580043 21.415 failure 4299085 4834178 535093 3.201 ------------------------------------------------------------------------------ total per sec 294.414 =head1 BUGS There is possibly a bug in the way BIND 9 calculates the rndc stats. Also this code is a bit messy and long winded, could simplify =head1 SEE ALSO L, L, L, L http://groups.google.com/groups?hl=en&lr=&safe=off&ic=1&th=68fc7c7a51b6fff8,6&seekm=9e0k0e%2435u%40pub3.rc.vix.com#p =head1 AUTHOR Chris Pile http://www.snoogans.co.uk/ $Id: rndcstats.pl,v 1.3 2001/05/23 13:34:10 chris Exp $ =cut