Article View: news.software.nntp
Article #3322Reading the history file backwards
From: Erland Sommarskog
Date: Sun, 05 Feb 1995 15:52
Date: Sun, 05 Feb 1995 15:52
172 lines
6725 bytes
6725 bytes
A while ago I asked for input on how to read the history file back-
wards in an effective way.
I recevied three responses. First I got this perl snippet:
open(HISTORY, "/local/news/history");
seek(HISTORY, -8192, 2);
<HISTORY>; # sync to a newline
while (<HISTORY>) {
print $_;
# next unless "$_ contains a line that meets your condition";
}
But this won't read the file backwards, you say. Right, but it does
solve my problemm and embarrassinly easy. (I have to admit never
having played with (f)seek before, after all I'm a VMS person who
have been forced to Unix by the vile market forces.) 8192, is just
too little, but I started to back 200.000 lines, and if that is not
enough I proceed back 100.000 lines at a time.
I also recieved two letters which contained complete utilities to
read the files backwards, but as they arrived later I didn't use
them this time. One of the mail also contained a discussion on
SunOS's "tail -r" which indeed reverses a file. However, the same
mail argued that "tail -r" was a no-no, because it isn't portable
and the BUGS section on the man page leaves you confused. (We have
SunOS, and I think I read the man page on "tail", but either I was
very tired, or simply didn't understand.)
My sincere thanks to you who replied.
In return here is the program I wrote. This script reads the history
file and output statistics to the file given as argument.
950201 #arts Avg <0 0<� �<1 1day 2d 3d 4d 5d 6d 7d 8d 9d 10 11 12 13
1242 3811 2.54 0 1 83 1846 1109 264 237 199 18 5 3 0 0 0 44 2
1342 3708 2.61 0 0 56 2113 945 166 104 126 54 36 2 1 0 0 95 10
1442 3903 2.59 0 0 86 2505 671 107 158 51 212 21 10 0 0 1 75 6
1542 4150 2.34 0 6 99 2717 798 158 197 59 29 23 3 0 1 0 56 4
1642 4443 2.51 1 7 148 2987 618 127 271 105 23 16 6 3 1 0 123 7
(If you see an "=", the high bit is stripped. That's actually a 1/2-
character.)
The first colum is the time of day, the second is the number of articles
that have arrived since last time the output file was touched, and the
third column is the average age of these articles. From the fourth
column you get a histogramme, showing the age distribution. First those
with a negative age (system clocks not in sync). Then come the number
of articles between 0 and 0.5 days old, then 0.5 to 1 day old, 1 to 2,
2 to 3 etc. And, yes, these are actual numbers at our site!
The script is supposed to started from crontab once an hour, and you
get the heading the the first run after noon and midnight.
If you use it, you surely will want to change "enea.se" to you own site,
to filter away your own postings.
The column widths is adapted to what seems reasonable here, and to make
them fit in 80 columns.
Enjoy!
#!/usr/bin/perl
sub format_time {
local($time) = @_;
local($year, $month, $day, $hour, $min, $sec);
$time = time if (! defined $time);
($sec, $min, $hour, $day, $month, $year) = localtime($time);
return sprintf("%2.2d%2.2d%2.2d %2.2d%2.2d",
$year, ++$month, $day, $hour, $min);
}
# This routine extracts the things we want from a history line. Returns
# 0, if at end of file.
sub get_times{
local($msg_id, $times, $arrival, $expire, $posted, $line);
$line = <H>;
return (0, 0, 0) if ! $line;
($msg_id, $times) = split(" ", $line);
($arrival, $expire, $posted) = split(/\~/, $times);
($msg_id, $arrival, $posted);
}
$file = shift @ARGV;
$start_time = time - 86400 * -M $file;
# Open the file
open(H, "/usr/local/news/history") || die "Could open history file: $!\n";
# Position us somewhere near the end. -200000 is a good guess for a little
# more than an hour of traffic at our site for the moment.
seek(H, -200000, 2);
# Iterate until we are before the starting time.
while (1) {
<H>; # Sync to a new line
($msg_id, $arrival, $posted) = &get_times(); # Examine the current line
last if $arrival < $start_time; # Break if we are behind now
seek(H, -100000, 1); # Take another step back;
}
# Now iterate until we're just ahead of the starting time.
while ($arrival < $start_time) {
($msg_id, $arrival, $posted) = &get_times();
}
# Now we can start collecting the data.
$sum = $no_of_art = $neg_travs = $half_days = $#histogramme = 0;
while ($msg_id) {
# Ignore our own article. Note: this is hardcoded, so you need to change
# this
next if $msgid =~ /enea.se\>/;
# Skip cancelled or expired articles.
next if ! $posted;
# Calculate the travel time and increment.
$travel_time = ($arrival - $posted) / 86400.0;
$sum += $travel_time;
$no_of_art++;
# Put it in the right box in the histogramme
if ($travel_time < 0) {
$neg_travs++;
}
elsif ($travel_time > 0.5 && $travel_time < 1.0) {
$half_days++;
}
else {
$histogramme[int($travel_time)]++;
}
# Pick up the next line.
} continue {
($msg_id, $arrival, $posted) = &get_times();
}
close H;
$now = &format_time();
open(F, ">>$file");
printf F "%s Num %-5d", $now, $no_of_art;
if ($no_of_art > 0) {
printf F "Avg%5.2f Neg %-3dD0 %-5dD� %-5d",
$sum/$no_of_art, $neg_travs, $histogramme[0], $half_days;
foreach $i (1..3) {
printf F "D%1d %-5d", $i, $histogramme[$i];
}
foreach $i (4..$#histogramme) {
printf F "D%1d %-3d", $i, $histogramme[$i] if $histogramme[$i];
}
}
else {
$server = `/local/news/bin/ctlinnd mode`;
$server = (split(' ', $server))[1];
print F "Server mode: $server.";
}
print F "\n";
close F;
--
Erland Sommarskog, sommar@enea.se, Stockholm
"Person-person - a politically correct designation of 'mail-man'"
Wiley's Dictionary
Technical Details
Message-ID:
<3h2s81$2hs@gordon.enea.se>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-comp.lang.perl.mbox.zip!nntp.gmd.de!newsserver.jvnc.net!netnews.upenn.edu!dsinc!ub!news.kei.com!bloom-beacon.mit.edu!eru.mt.luth.se!news.kth.se!sunic!seunet!seunet!news2.swip.net!enea.se!not-for-mailShow Full Headers
Complete Article Headers
Message-ID: <3h2s81$2hs@gordon.enea.se> Subject: Reading the history file backwards Date: Sun, 05 Feb 1995 15:52:33 +0000 From: sommar@enea.se (Erland Sommarskog) Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-comp.lang.perl.mbox.zip!nntp.gmd.de!newsserver.jvnc.net!netnews.upenn.edu!dsinc!ub!news.kei.com!bloom-beacon.mit.edu!eru.mt.luth.se!news.kth.se!sunic!seunet!seunet!news2.swip.net!enea.se!not-for-mail Newsgroups: news.software.nntp,comp.lang.perl Organization: Foresta dell'estate Lines: 171 Distribution: inet NNTP-Posting-Host: gordon.enea.se