TEXT   62
bind-stats.pl
Guest on 23rd September 2022 12:48:58 AM


  1. #!/usr/bin/perl
  2.  
  3. # bind-stats.pl - a script to return bind-related statistical information
  4. #                 Author: Matt Groener, gwynnebaer@hotmail.com
  5.  
  6. # Use built-in option syntax
  7. use Getopt::Std;
  8.  
  9. # use $opt_d to override default named.stats dir location
  10. getopt('d');
  11.  
  12. $STATFILE = $opt_d ? "$opt_d/named.stats" : '/var/named/named.stats';
  13. $MEMFILE  = $opt_d ? "$opt_d/named.memstats" : '/var/named/named.memstats';
  14. $cmd_ndc  = '/usr/sbin/ndc -q stats > /dev/null 2>&1';
  15.  
  16. # Generate stats now (this could be turned off and run via cron as well)
  17. unlink($STATFILE,$MEMFILE);
  18. qx($cmd_ndc);
  19. $status = $?;
  20. die "Failed command: $cmd_ndc: EXIT_CODE: $status" if $status;
  21.  
  22. # Die unless we can locate the stats file
  23. if (!open(STATS,$STATFILE)) {
  24.         die "Failed to open $STATFILE: $!\n";
  25. }
  26.  
  27. # Parse the stats file
  28. while (<STATS>) {
  29.         next if /^[\-\+]/;
  30.         chomp();
  31.         if (/Legend/) { $start_legend++; next; }
  32.         if (/Global/) { $start_legend--; $start_global++; next; }
  33.         if ($start_legend) {
  34.                 push(@legend,split());
  35.         } elsif ($start_global) {
  36.                 @global = split();
  37.                 for (0..$#legend) { $hash{lc($legend[$_])} = $global[$_]; }
  38.                 last;
  39.         } else {
  40.                 @data = split();
  41.                 next if $data[1] =~ /^\d+$/;
  42.                 # break up the data and build hash of data
  43.                 /time since/i && do { $hash{lc($data[3])} = $data[0]; next; };
  44.                 /^\d+\s+.*\s+quer/i && do { $hash{lc($data[1])} = $data[0]; next; };
  45.         }
  46. }
  47. close (STATS);
  48.  
  49. # print out stats or usage
  50. if (@ARGV) {
  51.         foreach $argv (@ARGV) {
  52.                 push(@output,$hash{lc($argv)}) if defined $hash{lc($argv)};
  53.         }
  54.         print "@output";
  55. } else {
  56.         print "Usage: $0 [-d statsdir] args\n\n       where args is one of:\n       ";
  57.         foreach $argv (sort keys %hash) {
  58.                 print $argv;
  59.                 $incr++;
  60.                 if ($incr == 13) {
  61.                         print "\n       ";
  62.                         $incr = 0;
  63.                 } else {
  64.                         print " ";
  65.                 }
  66.         }
  67.         print "\n\n";
  68. }

Raw Paste

Login or Register to edit or fork this paste. It's free.