summaryrefslogtreecommitdiff
path: root/bin/load-update
blob: 070cc3577e64f035b60e0d3e25709c22a5a84fc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/perl -w

use strict;
use RRDs;
use English;
use Time::ParseDate;

my $ECHOLOT_BASE = shift @ARGV;
my $RRDPATH = shift @ARGV;
die ("Usage: $PROGRAM_NAME <echolot base> <rrd dir>\n") if (!defined $ECHOLOT_BASE || !defined $RRDPATH || scalar @ARGV > 0);


my @MONTH = qw{Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec};
my $BASEDIR = $ECHOLOT_BASE.'/results/thesaurus/';  # we depend on that trailing /
my $INDEX = $BASEDIR.'index.txt';
my $NOW = time;
my $VERBOSE = 0;

chdir $RRDPATH or die ("Cannot chdir $RRDPATH: $!\n");

sub check_exists($) {
	my ($name) = @_;
	my $rrd = "$name.rrd";
	return if (-e $rrd);
	my @params = ($rrd);
	push @params, '-b', 'now - 5 months',
	  qw{ --step 86400
	      DS:total:GAUGE:172800:U:U
	      DS:typeI:GAUGE:172800:U:U
	      DS:typeII:GAUGE:172800:U:U
	      RRA:AVERAGE:0.5:1:3650
	};
	RRDs::create @params;
	my $err=RRDs::error;
	warn "ERROR while creating $rrd: $err\n" if $err;
}

sub check_exists_PoolSize($) {
	my ($name) = @_;
	my $rrd = "$name.pool.rrd";
	return if (-e $rrd);
	my @params = ($rrd);
	push @params, '-b', 'now - 5 months',
	  qw{ --step 86400
	      DS:poolSize:GAUGE:172800:0:U
	      RRA:AVERAGE:0.5:1:3650
	      RRA:MIN:0.5:1:3650
	      RRA:MAX:0.5:1:3650
	};
	RRDs::create @params;
	my $err=RRDs::error;
	warn "ERROR while creating $rrd: $err\n" if $err;
}

my %REMAILERS;
open(INDEX, $INDEX) or die ("Cannot open $INDEX: $!\n");
while (<INDEX>) {
	my($nick, $id) = $_ =~ /^([a-z0-9]+)  \s+ ([0-9]+) \s/x;
	next unless defined $nick;
	$REMAILERS{$id} = $nick;
};
close(INDEX);

my %RELIABLE;
my %MIXMASTER;
for my $id (keys %REMAILERS) {
	my $nick = $REMAILERS{$id};
	unless (open(F, $BASEDIR.$id.'.stats')) {
		warn("Cannot open $id.stats: $!\n") if ($VERBOSE >= 1);
		next;
	};
	local $/;
	undef $/;
	my $stats = <F>;
	close F;

	if ($stats =~ /^Number of messages per day:/m) {
		$MIXMASTER{$id} = $stats;
	} elsif ($stats =~ /^\s*Messages Received Last Week:\s+([0-9]+)/m) {
		my $total = $1;
		my ($generated) = $stats =~ /^\s*Generated: (.*)/m;
		if (defined $generated) {
			my $gen = parsedate($generated);
			unless (defined $generated) {
				print "Cannot parse generated '$generated' for $id\n";
			};
			$generated = $gen;
			#print scalar gmtime $generated,"\n";
		};
		unless (defined $generated) { # no else as if block may set generated to undef.
			print "Don't have a generated date for $id.\n";
			my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
			    $atime,$mtime,$ctime,$blksize,$blocks)
			   = stat($BASEDIR.$id.'.stats');
			unless (defined $mtime) {
				print "Don't even have an mtime for the file\n";
				next;
			};
			$generated = $mtime;
		}
		$RELIABLE{$id} = { total => $total, generated => $generated};
	} elsif ($stats =~ /^Remailer-Type: Reliable v/m) {
		# Reliable without stats enabled
	} else {
		print "Don't know what $id is.\n";
	}
}


# do reliable
# find out last week:
my $last_week = $NOW - ($NOW % (3600*24*7)) - 3600*24*7;
for my $id (keys %RELIABLE) {
	my $remailer = $RELIABLE{$id};
	check_exists($REMAILERS{$id});
	my $last = RRDs::last($REMAILERS{$id}.'.rrd');
	my $err=RRDs::error;
	warn "ERROR while updating:  $err\n" if $err;
	my $time = $remailer->{'generated'} - ($remailer->{'generated'} % (3600*24));
	next if ($time <= $last);
	RRDs::update( $REMAILERS{$id}.'.rrd' , $time.':'.int($remailer->{'total'}/7).':U:U' );
	$err=RRDs::error;
	warn "ERROR while updating:  $err\n" if $err;
}

	# Number of messages per day:
	# 16 Aug: 14691  Intermediate:9568  Mail:4637  Postings:  24
	# 17 Aug:  717  Intermediate: 579  Mail: 104  Postings:   0

for my $id (keys %MIXMASTER) {
	my @updates;
	my @updatesPoolSize;
	check_exists($REMAILERS{$id});

	my $last = 0;
	if ( -e $REMAILERS{$id}.'.rrd') {
		$last = RRDs::last($REMAILERS{$id}.'.rrd');
		my $err=RRDs::error;
		warn "ERROR while updating:  $err\n" if $err;
	};
	my $lastPoolSize = 0;
	if (-e $REMAILERS{$id}.'.pool.rrd') {
		$lastPoolSize = RRDs::last($REMAILERS{$id}.'.pool.rrd');
		my $err=RRDs::error;
		warn "ERROR while updating:  $err\n" if $err;
	};

	# start 4 months ago:
	for (my $time = $NOW - ($NOW % (3600*24)) - 3600*24*4*30; $time < $NOW; $time += 3600*24) {
		my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime($time);
		my $match = sprintf("%02d %s:", $mday, $MONTH[$mon]);

		my ($type1, $type2, $total, $pool);
		if ($MIXMASTER{$id} =~ /^$match\s*([0-9]+)(?:.*Pool\s*size:\s*([0-9]+))?/m) {
			$type2 = $total = $1;
			$type1 = 'U';
			$pool = defined $2 ? $2 : undef;
		} elsif ($MIXMASTER{$id} =~ /^$match\s+Mix:\s*([0-9]+)\s* PGP:\s*([0-9]+)(?:.*Pool\s*size:\s*([0-9]+))?/m) {
			$type1 = $2;
			$type2 = $1;
			$total = $1 + $2;
			$pool = defined $3 ? $3 : undef;
		} else {
			next;
		};

		if ($time > $last) {
			push @updates, $time.':'.$total.':'.$type1.':'.$type2;
		};
		if (defined $pool && $time > $lastPoolSize) {
			push @updatesPoolSize, $time.':'.$pool;
		};
	}
	if (scalar @updates != 0) {
		check_exists($REMAILERS{$id});
		RRDs::update( $REMAILERS{$id}.'.rrd', @updates );
		my $err=RRDs::error;
		warn "ERROR while updating:  $err\n" if $err;
	};
	if (scalar @updatesPoolSize != 0) {
		check_exists_PoolSize($REMAILERS{$id});
		RRDs::update( $REMAILERS{$id}.'.pool.rrd', @updatesPoolSize );
		my $err=RRDs::error;
		warn "ERROR while updating:  $err\n" if $err;
	};
}