🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.nntp
Article #36010

Unsupported release: pathinfo

#36010
From: Brad Knowles
Date: Thu, 18 Mar 1999 00:00
543 lines
15522 bytes
Folks,

    A long time ago, in a newsgroup far far away, I promised that I would
release a tool that I had written to parse "Path:" headers and to
calculate various statistics on them, etc....

    Well, I recently got another private request for it, so I figured I'd
post it here (it's around 13KB, and actually the very first Perl program I
ever wrote from scratch).


    Anyway, here goes (please don't laugh too hard ;-):

#!/usr/bin/perl

# pathinfo -- Scan news headers, record and print various bits of information
#
#   Author: Brad Knowles <blk@skynet.be>
#     Date: Tue Dec  1 11:15:48 MET 1998
#
# Inspired by a conversation with Keni Lorber
# Additional ideas from Mark Filteau
#
#============================================================
# Copyright (c) 1998 by Belgacom Skynet, all rights reserved.
#
# This program is provided as-is, with absolutely no
# warranty or suitability for any purspose implied.
#
# This program is not even guaranteed to run or compile, and
# you use it entirely at your own risk!
#
# No support will be provided, although comments and
# suggestions are appreciated, and attempts will be made to
# fold in improvements and make them available under the
# same terms as the rest of the program.
#
# This program is provided as freely available and
# distributable Perl source code only, and you are free
# to make whatever modifications you like, so long as you
# contribute them back to me and do not make the modified
# versions available to other persons without express prior
# consent.
#============================================================


############################################################
############################################################
###                                                      ###
###     DEFAULT SETTINGS FOR IMPORTANT VARIABLES         ###
###                                                      ###
############################################################
############################################################

$default_min_report||=30;
#
# $min_report is the minimum number of times a particular news site
# has to be seen before it can potentially show up on the "Top XXX"
# reports.  We do this because we do things like calculate mean and
# standard deviation on how many news peers it is away from us, and
# statistics like this are not meaningful if the sample size is not
# large enough.  Typically, at least thirty samples are required
# before the data is likely to be statistically significant.
# 

$default_top||=20;
#
# $top is the maximum number of sites we'll display in our various 
# reports.  If set to twenty, then we'll display the "Top 20" and
# "Bottom 20" in each category.
#

$default_headers||="/usr/local/news/log/path.log";
#
# $headers is assumed to be a file that contains all the "Path:"
# headers that this site has seen in the previous reporting
# period (usually a day).  If this file exists, then we use it
# to save a *BOATLOAD* of I/O that would be required to read in
# all the various news articles, parse their headers, and pull
# out the "Path:" headers ourselves.
#
#
# If you're using INN 2.x, you can create this file by specifying
# the following line in your "etc/newsfeeds" file (typically
# already present, merely commented out):
#
# path!\
#   :*,!junk,!control,!control.*\
#   :Tc,WH\
#   :/usr/local/news/bin/pathgrep path.log
#
# The "pathgrep" script looks something like this:
#
# #!/bin/sh
# # =()<. @<_PATH_SHELLVARS>@> ()=
# . /usr/local/news/lib/innshellvars
# PATH_LOG="$MOST_LOGS/$1"
# $EGREP '^Path:' >> "$PATH_LOG"
#

$default_dir||="/var/news/spool/articles/";
#
# $dir is scanned for news articles to be parsed, if the file
# in $headers does not exist.  If $dir is a file instead, it is
# assumed to contain a list of directories/files to be scanned,
# as opposed to actually being read and parsed in and of itself.
#

$default_file||="/usr/local/news/etc/search.hosts";
#
# $file is assumed to contain a list of hosts for which we are
# looking for information on.  This could be a list of your
# local news peers, so you can see if any of them are getting
# screwed up and you're getting news from them that is not
# coming in directly to you.  Or, it could be a list of Freenix
# Top 1000 hosts that you're interested in checking out, so you
# can see which ones are "close" to you and which ones are not,
# and therefore which ones you might be interested in setting up
# a peering arrangement.
#

############################################################
############################################################
###                                                      ###
### NO MODIFICATIONS BELOW THIS LINE SHOULD BE NECESSARY ###
###                                                      ###
############################################################
############################################################

use Getopt::Std;
use Sys::Hostname;
use POSIX;
use BSD::Resource;

getopts('DVS:d:f:h:m:s:t:');

if ($#ARGV != -1)
{
    usage();
    exit 1;
}

if (defined $opt_D)
{
    $validate_dns=$opt_D;
    $validate=$opt_D;
}

if (defined $opt_V)
{
    $validate=$opt_V;
}

$min_report||=$default_min_report;
if (defined $opt_m)
{
    $min_report=$opt_m;
    if ($min_report < 0)
    {
        $min_report=$default_min_report;
    }
}

$top||=$default_top;
if (defined $opt_t)
{
    $top=$opt_t;
    if ($top < 0)
    {
        $top=$default_top;
    }
}

$headers||=$default_headers;
if (defined $opt_h)
{
    $headers=$opt_h;
}

$dir||=$default_dir;
if (defined $opt_d)
{
    $dir=$opt_d;
}

$file||=$default_file;
if (defined $opt_f)
{
    $file=$opt_f;
}

$host=hostname();
$now=POSIX::strftime("%a %b %d %T %Z %Y", localtime());

if (defined $opt_s)
{
    $skip=$opt_s;
}

if (defined $opt_S)
{
    $sleep=$opt_S;
    if ($sleep < 0)
    {
        $sleep=1;
    }
}

setpriority (PRIO_PROCESS, $$self, PRIO_MAX);

if (-f $file)
{
    my $input = "< " . $file;
    open (INPUT, $input);
    while (<INPUT>)
    {
        tr/A-Z/a-z/;
        chop;
        @hosts=split;
        for my $i (0 .. $#hosts)
        {
            my $j = $hosts[$i];
            next if (defined $hosts{$j}{name});
            $hosts{$j}{name}=$j;
        }
    }
    close INPUT;
}

if (-f $headers)
{
    snarf($headers);
}
elsif (-f $dir)
{
    my $input = "< " . $input;
    open (INPUT, $input);
    while (<INPUT>)
    {
        chop;
        my $myfile = $_;
        next if ((! -d $myfile) && (! -f $myfile));
        recurse($myfile);
    }
    close INPUT;
}
elsif (-d $dir)
{
    recurse($dir);
}
else
{
    print "ERROR: $headers doesn't exist and $dir is not readable, not a
directory, or not a normal file.";
    usage();
    exit 2;
}

foreach $site (keys %path)
{
    if ($path{$site}{count} != 0)
    {
        $path{$site}{mean}=$path{$site}{distance} / $path{$site}{count};
    }
    else
    {
        $path{$site}{mean}=0
    }
}

print_data($top,$min_report,"Mean",sort bymean keys %path);
print_data($top,$min_report,"Reverse Mean",reverse sort bymean keys %path);
print_data($top,$min_report,"Min",sort bymin keys %path);
print_data($top,$min_report,"Reverse Min",reverse sort bymin keys %path);
print_data($top,$min_report,"Max",sort bymax keys %path);
print_data($top,$min_report,"Reverse Max",reverse sort bymax keys %path);
print_data($top,$min_report,"Count",sort bycount keys %path);
print_data($top,$min_report,"Reverse Count",reverse sort bycount keys %path);
print_data($top,$min_report,"Total Distance",sort bydist keys %path);
print_data($top,$min_report,"Reverse Total Distance",reverse sort bydist
keys %path);

sub usage
{
    my @prog=split(/\//, $0);
    my $prog=$prog[$#prog];
    print "Usage: $prog [-DV] [-S <S>] [-d <p>] [-f <f>] [-h <h>] [-m <m>]
[-s <s>] [-t <t>]\n";
    print "\n";
    print "Where:\n";
    print " <S> = # of seconds to sleep between processing each file in <p>\n";
    print " <p> = /path/to/news/spool/or/subdirectory/to/be/processed\n";
    print "       or file containing list of directories or files\n";
    print " <f> = File containing names of sites to search for\n";
    print " <h> = File containing archive of headers to be processed\n";
    print " <m> = Minimum # of references required for site to show up on
reports\n";
    print " <s> = Site names to skip (substring)\n";
    print " <t> = Show only the top # of sites\n";
    print "\n";
    print "Use '-V' if you want a fast but simple validation scheme\n";
    print "Use '-D' if you want to validate w/ DNS only those that pass
the simpler test\n";
    print "\n";
    print "Notes:\n";
    print "-D implies -V, but no harm is done if you use -DV\n";
    print "-h is preferred over -d.  If both are specified and exist,\n";
    print "the argument to -d will get silently ignored.\n";
}

sub recurse
{
    my $dir = shift;

    if (-f $dir)
    {
        snarf($dir);
    }
    elsif (-d $dir)
    {
        opendir DIR, $dir or return;
        @allfiles = readdir DIR;
        closedir(DIR);
        for $i (0 .. $#allfiles)
        {
            my $name = $allfiles[$i];
            next if $name =~ /\.\.?/;
            my $fullname=$dir . "/" . $name;
            if (-f $fullname)
            {
                snarf($fullname);
            }
            elsif (-d $fullname)
            {
                recurse($fullname);
            }
            if (defined $sleep)
            {
                sleep $sleep;
            }
        }
    }
    else
    {
        print "WARNING: $dir is not readable, not a directory, or not a
normal file.";
    }
    return;
}

sub snarf
{
    my $input = shift;
    
    $input = "< " . $input;
    open (INPUT, $input);
    while (<INPUT>)
    {
        # next if !((/^Path: /io) || (/^From: /io) || (/^Reply-to: /io));
        next if !(/^Path: /io);
        if (/^Path: /io)
        {
            s/^Path: //;
            tr/A-Z/a-z/;
            chop;
            @route = split (/!/);
            for $i (0 .. $#route)
            {
                $site=$route[$i];

                next if ((defined $file) && (!defined $hosts{$site}{name}));
                    # Grab only those sites we're told to look for

                next if ((defined $skip) && ($site =~ $skip));
                    # Skip all of our own servers

                next if ((defined $validate) && ($site =~ /^[0-9\.]*$/));
                    # Skip if numeric IP

                if (defined $validate)
                {
                    my @tld = split(/\./, $site);
                    my $j = $#tld;

                    next if ($j < 1);
                        # Require at least one dot

                    my $k = $tld[$j];
                    my $l = length $k;

                    next if ($l > 3 || $l < 2);
                    # Pretty lame check to see if the TLD is legit
                }

                if (defined $validate_dns) # Actually look up in DNS
                {
                    if (!defined $addr{$site})
                    {
                        $addr{$site} = gethostbyname $site;
                    }
                    next if ($addr{$site} =~ /^$/);
                }

                if (!defined $path{$site})
                {
                    $path{$site}{name}=$site;
                }
                $path{$site}{count}++;
                $path{$site}{distance}+=$i;
                $path{$site}{distsq}+=($i * $i);
                if ($path{$site}{min} > $i || $path{$site}{min} == 0)
                {
                    $path{$site}{min}=$i;
                }
                if ($path{$site}{max} < $i)
                {
                    $path{$site}{max}=$i;
                }
            }
        }
    }
    close INPUT;
}

sub bymean
{
    $path{$a}{mean} <=> $path{$b}{mean}
}

sub bymin
{
    $path{$a}{min} <=> $path{$b}{min}
}

sub bymax
{
    $path{$a}{max} <=> $path{$b}{max}
}

sub bycount
{
    $path{$a}{count} <=> $path{$b}{count}
}

sub bydist
{
    $path{$a}{distance} <=> $path{$b}{distance}
}

sub print_data
{

my ($top, $min_report, @path, $label, $top_label, $site, $current, $sd);
($top, $min_report, $label, @path) = @_;

format HEADER1 =
@|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$top_label

Site: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  Date: @>>>>>>>>>>>>>>>>>>>>>>>>>>>
$host, $now
..
format SPOOL =
Spool: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$dir
..
format HEADERS =
Header file: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$headers
..
format FILE =
Searching for sites in: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$file
..
format HEADER2 =

Site                               Dist / Count =  Mean  (s.d.)     Min     Max
-------------------------------------------------------------------------------
..
format BODY =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @###### @###### @###.## @###.## @###### @######
$path{$site}{name}, $path{$site}{distance}, $path{$site}{count},
$path{$site}{mean}, $sd, $path{$site}{min}, $path{$site}{max}
..
format TRAILER=

..

    $~ = "HEADER1";
    $top_label = "Top $top Sites Sorted by $label";
    write;
    if ((defined $headers) && (-f $headers))
    {
        $~ = "HEADERS";
        write;
    }
    elsif ((defined $dir) && (-d $dir))
    {
        $~ = "SPOOL";
        write;
    }
    if ((defined $file) && (-f $file))
    {
        $~ = "FILE";
        write;
    }
    $~ = "HEADER2";
    write;

    $current = 0;

    $~ = "BODY";
    foreach $site (@path)
    {
        if ($path{$site}{count} >= $min_report)
        {
            if ($path{$site}{count} > 1)
            {
                my $var=$path{$site}{distsq} / $path{$site}{count} - ($md
* $md);
                $sd=sqrt($var);
            }
            else
            {
                $sd=0;
            }
            write;
            $current++;
        }
        last if $current > $top;
    }
    $~ = "TRAILER";
    write;
}

-- 
Brad Knowles <brad@shub-internet.org> <http://www.shub-internet.org/brad/>
    <http://wwwkeys.pgp.net:11371/pks/lookup?op=get&search=0xE38CCEF1>

Are you looking for a news feed from a site in the Freenix Top 200?
If so, contact me via private e-mail for details.

Technical Details
Message-ID: <brad-1803991537280001@brad.techos.skynet.be>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mail

Show Full Headers
Complete Article Headers
Message-ID: <brad-1803991537280001@brad.techos.skynet.be>
Subject: Unsupported release: pathinfo
Date: Thu, 18 Mar 1999 00:00:00 +0000
From: brad@shub-internet.org (Brad Knowles)
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mail
X-Deja-AN: 456119003
X-Complaints-To: abuse@skynet.be
X-Trace: news1.skynet.be 921767886 22174 195.238.1.121 (18 Mar 1999 14:38:06 GMT)
Organization: Shub-Internet
X-URL: http://www.shub-internet.org/
NNTP-Posting-Date: 18 Mar 1999 14:38:06 GMT
Newsgroups: news.software.nntp