summaryrefslogtreecommitdiff
path: root/bin/update-pinger-cache
blob: 6c434d973cd7c3df76765db41fa00012978f4890 (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
#!/usr/bin/perl -w

#   update-pinger-cache: (c) 2002, 2004 Peter Palfrader <peter@palfrader.org>
#   $Id$

=pod

=head1 NAME

update-pinger-cache		Fetch new reliability stats

=over

=head1 SYNOPSIS

=item B<update-pinger-cache>

=back

=head1 DESCRIPTION

FIXME

=back

=head1 BUGS

Please report them to the author.

=head1 AUTHOR

Peter  Palfrader, E<lt>peter@palfrader.orgE<gt>

=cut

use strict;
use English;
use LWP::UserAgent;
use IO::File;
use Date::Parse;
use Carp;

$ENV{'PATH'} = '/bin:/usr/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

my $CONFIG = {
	#allpingers => 'allpingers/allpingers.txt',
	#outdir => 'meta/',
	useragent_timeout => 120,
	timeout => 130,
	old => 24 * 60 * 60,
};

$CONFIG->{'allpingers'} = shift @ARGV;
$CONFIG->{'outdir'} = shift @ARGV;
die ("Usage: $PROGRAM_NAME <allpingersfile> <outdir>\n") if (!defined $CONFIG->{'outdir'} || scalar @ARGV > 0);

my $user_agent =
	LWP::UserAgent->new(env_proxy => 1,
	                    keep_alive => 1,
	                    timeout => $CONFIG->{'useragent_timeout'} );

sub store_content($$$$) {
	my ($content_or_error, $site_taint, $type_taint, $content) = @_;
	my ($site) = $site_taint =~ /^([a-zA-Z0-9]+)$/;
	my ($type) = $type_taint =~ /^([a-zA-Z0-9]+)$/;
	return undef unless defined $site;
	return undef unless defined $type;

	my $write = $content_or_error eq 'content' ? 'cache' :
	            $content_or_error eq 'error'   ? 'error' :
	            die ("Unepxected value of content_or_error: $content_or_error");
	my $unlink = $content_or_error eq 'content' ? 'error' :
	             $content_or_error eq 'error'   ? 'cache' :
	             die ("Unepxected value of content_or_error: $content_or_error");

	open(F, '>',$CONFIG->{'outdir'}.'/'.$write.'.'.$site.'.'.$type.'.txt') or die ("Could not open outdir: $!\n");
	print(F $content) or die ("Could not write to outfile: $!");
	close(F) or die ("Could not close outfile: $!");
	unlink($CONFIG->{'outdir'}.'/'.$unlink.'.'.$site.'.'.$type.'.txt') if -e $CONFIG->{'outdir'}.'/cache.'.$site.'.'.$type.'.txt';
	return 1;
};

my @CHILDREN;

sub get_stats($$$) {
	my ($site, $type, $value) = @_;
	return undef unless defined $value;

	my $uri = URI->new($value);
	if ($user_agent->is_protocol_supported( $uri )) {
		my $pid = fork();
		die "Can't fork: $!" unless defined $pid;
		unless ($pid) {
			eval{
				local $SIG{ALRM} = sub { die "alarm\n" };
				alarm $CONFIG->{'timeout'};
				my $response = $user_agent->get($value);
				if ($response->is_success) {
					my $content = $response->content;
					$content =~ s/\r\n/\n/g;
					store_content('content', $site, $type, $content);
				} else {
					store_content('error', $site, $type, "Error: ".$response->status_line());
				};
				alarm 0;
			};
			if ($EVAL_ERROR) {
				if ($EVAL_ERROR eq "alarm\n") {
					store_content('error', $site, $type, "Timeout");
				} else {
					store_content('error', $site, $type, "Error: $EVAL_ERROR");
					die unless $EVAL_ERROR eq "alarm\n";
				};
			};
			exit;
		} else {
			push @CHILDREN, $pid;
		};
	} else {
		warn ("$PROGRAM_NAME: Protocol for '$value' is not supported\n");
		store_content('error', $site, $type, "Error: Protocol for '$value' is not supported");
	};
};


# Read Pinger URLs and get stats (forked)
my %pinger_uris;
my %type_fhs;
{
	my $source;
	open (F, $CONFIG->{'allpingers'}) or die ("Could not open allpingers '$CONFIG->{'allpingers'}': $!\n");
	while(<F>) {
		s/\s*(.*)\s*/$1/;
		next if (/^\s*#/);
		$source = $1, next if (/^\[(.*)\]$/);
		if (/(.+?)\s*=\s*(.+)/) {
			$pinger_uris{$source}->{$1} = $2 if defined $source;
		};
	};
	for $source (sort keys %pinger_uris) {
		my $uris = $pinger_uris{$source};
		get_stats($source, 1, $uris->{'rlist2'} || $uris->{'rlist2_html'} || $uris->{'rlist'} || $uris->{'rlist_html'});
		get_stats($source, 2, $uris->{'mlist2'} || $uris->{'mlist2_html'} || $uris->{'mlist'} || $uris->{'mlist_html'});
	};
};

for (@CHILDREN) {
	waitpid($_, 0);
};

open(F, '>',$CONFIG->{'outdir'}.'/stamp-cache') or die ("Could not open <outdir>/stamp-cache: $!\n");
close(F);

# vim:set ts=4:
# vim:set shiftwidth=4: