summaryrefslogtreecommitdiff
path: root/other/mixminion/bin/rrd-update
blob: 61dc7dbaae91e6a1423ac84a7e68c1ce5adf9775 (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
#!/usr/bin/perl -w

use strict;
use RRDs;
use Time::ParseDate;
use Compress::Zlib;

my $NOW = time;
my $VERBOSE = 0;
my $RRD = '/home/weasel/www/www.noreply.org/Build/other/mixminion/rrd/nodes.rrd';
my $RRD_DIR = '/home/weasel/www/www.noreply.org/Build/other/mixminion/rrd/nodes';
my $DIR_DIR = '/home/weasel/www/www.noreply.org/Build/other/mixminion/mixminion-directory';

sub check_exists($) {
	my ($rrd) = @_;
	return if (-e $rrd);
	my @params = ($rrd);
	push @params,  '-b', 'now - 1 year',
	 qw{ --step 14400
	  DS:inDirectory:GAUGE:172800:U:U
	  DS:recommended:GAUGE:172800:U:U
	  RRA:AVERAGE:0.5:1:21900
	};
	print "Creating rrd: $rrd...\n" if $VERBOSE;
	RRDs::create @params;
	my $err=RRDs::error;
	warn "ERROR while creating $rrd: $err\n" if $err;
}
sub get_last($) {
	my ($rrd) = @_;
	my $last = 0;
	if ( -e $rrd) {
		$last = RRDs::last($rrd);
		my $err = RRDs::error;
		warn "ERROR while getting last for $rrd: $err\n" if $err;
	};
	return $last;
};

check_exists($RRD);
my $last = get_last($RRD);

opendir(DIR, $RRD_DIR) || die ("Cannot opendir $RRD_DIR: $!\n");
my @rrdfiles = grep { /\.rrd$/ } readdir (DIR);
closedir(DIR);
my %last_node;
for my $rrdfile (@rrdfiles) {
	my $nodename = $rrdfile;
	$nodename =~ s/\.rrd$//;
	$last_node{$nodename} = get_last($RRD_DIR.'/'.$rrdfile);
}

my @dirfiles;
if (scalar @ARGV) {
	@dirfiles = @ARGV;
} else {
	opendir(DIR, $DIR_DIR) || die ("Cannot opendir $DIR_DIR: $!\n");
	@dirfiles = sort { ($a cmp $b) } grep { /^directory-/ } readdir (DIR);
	closedir(DIR);
};

my @updateGlobal;
my %updates;
for my $dir (@dirfiles) {
	print "Doing $dir\n" if $VERBOSE;
	open (DIRECTORY, $DIR_DIR.'/'.$dir) || die ("Cannot open $DIR_DIR/$dir: $!\n");
	my $compressed_dir = join '', <DIRECTORY>;
	close (DIRECTORY);

	my $directory = Compress::Zlib::memGunzip($compressed_dir);

	my $published = undef;
	my $recommended_servers = undef;
	my %in_directory;
	my %is_recommended;
	my $section = '' ;
	for my $line (split /\r?\n/, $directory) {
		chomp $line;
		if ($line =~ /^\[(.*)\]\s*$/) {
			$section = $1;
		};
		if ($section eq 'Directory' && $line =~ /^Published: (\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})$/) {
			my $date = $1;
			print "Found published: $date\n" if ($VERBOSE >= 3);
			$date =~ s#/#-#g;
			$published = parsedate($date);
			print "Parsed as $published (".(gmtime $published).")\n" if ($VERBOSE >= 4);
		};
		if ($section eq 'Directory' && $line =~ m#^Published: (\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2})$#) {
			my $date = $1;
			print "Found published: $date\n" if ($VERBOSE >= 3);
			$date =~ s#/#-#g;
			$published = parsedate($date);
			print "Parsed as $published (".(gmtime $published).")\n" if ($VERBOSE >= 4);
		};
		if ($section eq 'Directory' && $line =~ /^Recommended-Servers: \s*(.*?)\s*$/) {
			$recommended_servers = $1
		};
		if ($section eq 'Server' && $line =~ /Nickname: ([a-zA-Z0-9_-]+)\s*$/) {
			$in_directory{$1} = 1;
		};
	}
	close (DIRECTORY);
	my @recommended_servers = split /,?\s*\s+/, $recommended_servers;
	for my $node (@recommended_servers) {
		$is_recommended{$node} = 1;
	}

	if ($published > $last) {
		push @updateGlobal, $published.':'.(scalar keys %in_directory).':'.(scalar @recommended_servers);
		$last = $published;
		print "at  ".(gmtime $published)."  there are ".(scalar keys %in_directory).' in the directory and '.(scalar @recommended_servers)." recommended servers\n" if ($VERBOSE >= 3);
	} else {
		print "ignoring data at  ".(gmtime $published)."  because it's older than  ".(gmtime $last)."\n" if ($VERBOSE >= 3);
	};
	for my $node (keys %in_directory) {
		if (!defined $last_node{$node} || $published > $last_node{$node}) {
			push @{$updates{$node}}, $published.':1:'.( defined $is_recommended{$node} ? '1' : '0' );
			$last_node{$node} = $published;
		}
		delete $is_recommended{$node};
	}
	if (scalar keys %is_recommended) {
		my $nodes = join ', ', keys %is_recommended;
		warn ("$nodes recommended but not in directory.\n");
	}
};

if (scalar @updateGlobal != 0) {
	check_exists($RRD);
	RRDs::update( $RRD, @updateGlobal );
	my $err=RRDs::error;
	warn "ERROR while updating $RRD: $err\n" if $err;
};

for my $node (keys %updates) {
	if ($node =~ /[^a-zA-Z0-9_-]/) {
		warn ("Illegal characters in node name '$node'\n");
		next;
	};
	my $rrd = $RRD_DIR.'/'.$node.'.rrd';
	check_exists($rrd);
	RRDs::update( $rrd, @{$updates{$node}} );
	my $err=RRDs::error;
	warn "ERROR while updating $rrd: $err\n" if $err;
};