Listing 1 NNTP-client
#!/usr/bin/perl
#
#
# Newsserver-client-test
#
# Ralf van Dooren <r.vdooren@snow.nl>
#
use Socket;
use RRDs;
use Net::NNTP;
use Time::HiRes ("gettimeofday");
require "/usr/local/newstest/newslib.pl";
## Invariables
$max_articles = 2;
$check_local_time = 10*60; # seconds
$check_remote_time = 30*60; # seconds
$reference_server = "reference-server.isp2.tld";
$testgroup = "alt.test";
$TEXT= "/usr/local/newstest/garbage-file";
sub post_articles
{
$posted_ok=0;
for (my $i = 0 ; $i < $max_articles ; $i++ )
{
push(@messageid,"<insomnia-". gettimeofday . "-test-" . $i . \
"\@" . $server.">");
}
$devnull=shift(@messageid);
foreach my $msgid (@messageid)
{
#print $msgid;
&newslib::open_connection("$server");
@header=("Newsgroups: ".$testgroup."\n", \
"From: valid\@emailaddres.tld\n", \
"Subject: Heartbeat test: ignore \n", \
"Message-ID: ".$msgid."\n");
#body has to be of variable length, so prevent Cleanfeed to interfere
srand( time() ^ ($$ + ($$ << 69)) );
@body="";
my $t1=0;
my $t2=int(rand(300));
open TEXT or die "Can't find textfile: $! \n";
while (<TEXT>) {
if ($t1 eq $t2) { next };
if (rand() > 0.99)
{
@body=(@body,$_);
$t1++;
}
}
close TEXT;
$newslib::c->post(@header,"\n",@body);
my $nntpcode=$newslib::c->code;
#print $nntpcode; print "\n";
if ($nntpcode eq "240") { $posted_ok++ };
&newslib::close_connection("$server");
}
}
sub check_articles_local
# code 430: article doesn't exist on server
# code 220: article exists on server
{
$local_exist=0;
foreach my $msgid (@messageid)
{
my $devnull=$newslib::c->article("$msgid");
my $nntpcode=$newslib::c->code;
if ($nntpcode eq "220") { $local_exist++ };
#print $nntpcode; print "\n";
}
}
sub check_articles_remote
{
$remote_exist=0;
foreach my $msgid (@messageid)
{
my $devnull=$newslib::c->article("$msgid");
my $nntpcode=$newslib::c->code;
if ($nntpcode eq "220") { $remote_exist++ };
#print $nntpcode; print "\n";
}
}
sub cancel_messages
{
my $t=0;
&newslib::open_connection("$server");
foreach my $msgid (@messageid)
{
$cmsgid="<insomnia-". gettimeofday . "-cancel-" . $t . "\@" . \
$server.">";
@header=("Newsgroups: ".$testgroup."\n", \
"From: valid\@emailaddress.tld\n", \
"Subject: cmsg cancel ". $msgid."\n","Control: cancel \
".$msgid."\n","Message-ID: ".$cmsgid."\n","Summary: own \
cancel by original author\n");
@body=("This cancels the testmessage ".$msgid."\n","\n","If you \
have anything to say, please reply to the email address in \
the From-field\n");
$newslib::c->post(@header,"\n",@body);
my $nntpcode=$newslib::c->code;
#print $nntpcode; print "\n";
$t++;
}
&newslib::close_connection("$server");
}
sub write_results
{
$per_posted_ok=(($posted_ok/$max_articles)*100);
$per_local_exist=(($local_exist/$max_articles)*100);
$per_remote_exist=(($remote_exist/$max_articles)*100);
RRDs::update ("/usr/local/newstest/client.$host.rrd" , \
"$timestamp:$per_posted_ok:$per_local_exist:$per_remote_exist");
my $ERR=RRDs::error;
die "ERROR while updating client.$host.rrd: $ERR\n" if $ERR;
}
## Main
foreach $host (sort keys %newslib::hosts)
{
@messageid="";
#print $host; print "\n";
$timestamp=time;
foreach $server (@{$newslib::hosts{$host}{'servers'}})
{
&post_articles;
sleep $check_local_time;
&newslib::open_connection("$server");
&check_articles_local;
&newslib::close_connection("$server");
sleep $check_remote_time-$check_local_time; # (localtime is already
# 'sleep'-ed)
&newslib::open_connection("$reference_server");
&check_articles_remote;
&write_results;
&newslib::close_connection("$reference_server");
&cancel_messages;
}
}
|