1: #!/usr/bin/perl -w
   2: #
   3: ## nstat - print a little news statistic
   4: #
   5: ## Lukas Ertl, 1999
   6: #
   7: ##
   8: 
   9: $newsdir = "/var/spool/news";
  10: 
  11: $group = $ARGV[0];
  12: 
  13: $now = localtime;
  14: 
  15: print <<EOF;
  16: nstat - print a little news statistic
  17: -------------------------------------
  18: $now
  19: (Remember: quantity != quality)
  20: EOF
  21: 
  22: unless ($group) {
  23:     print "Which newsgroup? ";
  24:     chomp ($group = <STDIN>);
  25: }
  26: 
  27: print "Some statistics about the newsgroup $group\n";
  28: 
  29: @group = split(/\./, $group);
  30: 
  31: foreach $subdir (@group) {
  32:     $newsdir = $newsdir . "/" . $subdir;
  33: }
  34: 
  35: opendir(NEWS, $newsdir) or die $!;
  36: 
  37: @posts = readdir(NEWS) or die $!;
  38: 
  39: foreach $file (@posts) {
  40:     $num_of_posts++;
  41:     if ($file =~ /^\d+$/) {
  42: 		$file = $newsdir . "/" . $file;
  43: 		open(TEMP, $file) or die $!;
  44: 		while (<TEMP>){
  45: 		    if (/^From: (.+)$/) {
  46: 				if ($1 =~ /^"(.+)" <(.+\@.+)>$/) {
  47: 				    $poster = "$1 <$2>";
  48: 				    $posters{$poster}++;
  49: 				} elsif ($1 =~ /(.+\@.+) \((.+)\)/) {
  50: 				    $poster = "$2 <$1>";
  51: 				    $posters{$poster}++;
  52: 				} else {
  53: 				    $posters{$1}++;
  54: 				}
  55: 		    }
  56: 		    if (/^(X-Newsreader:|X-Mailer:|User-Agent:|X-Newsposter:|X-Http-User-Agent:) (.+)$/) {
  57: 				$readers{$2}++;
  58: 		    }
  59: 		    next;
  60: 		}
  61:     }
  62: }
  63: 
  64: print "\n\nNumber of postings: $num_of_posts";
  65: 
  66: print "\n\nCount posters, order alphabetically:\n";
  67: print "------------------------------------\n\n";
  68: foreach $key (sort keys %posters) {
  69:     print "$key: $posters{$key}\n";
  70:     push @{$posters_by_posts{$posters{$key}}}, $key;
  71: }
  72: 
  73: print "\n\nCount posters, order by postings:\n";
  74: print "---------------------------------\n\n";
  75: foreach $num (sort {$b <=> $a} keys %posters_by_posts) {
  76:     print "$num: \n";
  77:     foreach $poster (sort @{$posters_by_posts{$num}}) {
  78: 		print "\t$poster\n";
  79:     }
  80: }
  81: 
  82: print "\n\nCount newsreaders, order alphabetically:\n";
  83: print "----------------------------------------\n\n";
  84: foreach $key (sort { $a cmp $b } keys %readers) {
  85:     print "$key: $readers{$key}\n";
  86:     push @{$readers_by_posts{$readers{$key}}}, $key;
  87: }
  88: 
  89: print "\n\nCount newsreaders, order by postings:\n";
  90: print "-------------------------------------\n\n";
  91: foreach $count (sort { $b <=> $a } keys %readers_by_posts) {
  92:     print "$count:\n";
  93:     foreach $reader (sort @{$readers_by_posts{$count}}) {
  94: 		print "\t$reader\n";
  95:     }
  96: }
  97: 
  98: foreach $key (keys %readers) {
  99:     if ($key =~ /\bMozilla\b/) {
 100: 		if ($key =~ /X11/) {
 101: 		    $readers_by_kind{'Mozilla Linux/Unix'} += $readers{$key};
 102: 		} elsif ($key =~ /Win/) {
 103: 		    $readers_by_kind{'Mozilla Windows'} += $readers{$key};
 104: 		} elsif ($key =~ /Macintosh/) {
 105: 		    $readers_by_kind{'Mozilla Macintosh'} += $readers{$key};
 106: 		} else {
 107: 		    $readers_by_kind{$key} += $readers{$key};
 108: 		}
 109:     } elsif ($key =~ /\bForte\b/) {
 110: 		$readers_by_kind{'Forte (Free) Agent'} += $readers{$key};
 111:     } elsif ($key =~ /\bCrossPoint\b/) {
 112: 		$readers_by_kind{'CrossPoint'} += $readers{$key};
 113:     } elsif ($key =~ /\bGnus\b/) {
 114: 		$readers_by_kind{'Gnus (X)Emacs'} += $readers{$key};
 115:     } elsif ($key =~ /\bMicrosoft\b/) {
 116: 		$readers_by_kind{'Microsoft Outlook'} += $readers{$key};
 117:     } elsif ($key =~ /\bKNode\b/) {
 118: 		$readers_by_kind{'KNode'} += $readers{$key};
 119:     } elsif ($key =~ /\bNN\b/) {
 120: 		$readers_by_kind{'NN'} += $readers{$key};
 121:     } elsif ($key =~ /(\bTIN\b|\btin\b)/) {
 122: 		$readers_by_kind{'tin'} += $readers{$key};
 123:     } elsif ($key =~ /\bknews\b/) {
 124: 		$readers_by_kind{'knews'} += $readers{$key};
 125:     } elsif ($key =~ /\bslrn\b/) {
 126: 		$readers_by_kind{'slrn'} += $readers{$key};
 127:     } elsif ($key =~ /\btrn\b/) {
 128: 		$readers_by_kind{'trn'} += $readers{$key};
 129:     } elsif ($key =~ /\bKRN\b/) {
 130: 		$readers_by_kind{'KRN'} += $readers{$key};
 131:     } elsif ($key =~ /\bkexpress\b/) {
 132: 		$readers_by_kind{'kexpress'} += $readers{$key};
 133:     } elsif ($key =~ /Xnews\//) {
 134: 		$readers_by_kind{'Xnews'} += $readers{$key};
 135:     } else {
 136: 		$readers_by_kind{$key} += $readers{$key};
 137:     }
 138: }
 139: 
 140: print "\n\nCount readers, order alphabetically and by kind:\n";
 141: print "------------------------------------------------\n\n";
 142: foreach $count (sort keys %readers_by_kind) {
 143:     print "$count: $readers_by_kind{$count}\n";
 144:     push @{$readers_by_kind_posts{$readers_by_kind{$count}}}, $count;
 145: }
 146: 
 147: print "\n\nCount readers, order by postings and by kind:\n";
 148: print "---------------------------------------------\n\n";
 149: foreach $num (sort { $b <=> $a } keys %readers_by_kind_posts) {
 150:     print "$num:\n";
 151:     foreach $reader (sort @{$readers_by_kind_posts{$num}}) {
 152: 		print "\t$reader\n";
 153:     }
 154: }
  • Comment on nstat - Retrieve statistics about a newsgroup when you're using the leafnode newsserver
  • Download Code

Replies are listed 'Best First'.
RE: nstat - Retrieve statistics about a newsgroup when you're using the leafnode newsserver
by chromatic (Archbishop) on Jun 09, 2000 at 04:52 UTC
    Ouch, that long pseudo-case statement could be a little more efficient:
    foreach $key (keys %readers) { if ($key =~ /\b(\w+)\b/) { $name = $1; } else { next; } if ($name eq 'Mozilla') { # do your platform check } elsif ($name eq 'whatever') { # increment the other hash } # and so forth... }
    Just do the regex once, if your data set supports it. It looks like it does.
      Thanks for pointing that out. You (I) don't always see the easiest way at first.