]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/updxlrator/checkup
Updated updxlrator to latest stable
[people/pmueller/ipfire-2.x.git] / config / updxlrator / checkup
1 #!/usr/bin/perl
2 #
3 # This code is distributed under the terms of the GPL
4 #
5 # (c) 2006-2008 marco.s - http://update-accelerator.advproxy.net
6 #
7 # Portions (c) 2008 by dotzball - http://www.blockouttraffic.de
8 #
9 # $Id: checkup,v 1.0 2008/07/15 00:00:00 marco.s Exp $
10 #
11
12 use strict;
13
14 use HTTP::Date;
15
16 require '/var/ipfire/updatexlrator/updxlrator-lib.pl';
17
18 my $logfile="/var/log/updatexlrator/checkup.log";
19 my $repository='/var/updatecache';
20 my %proxysettings=();
21 my %xlratorsettings=();
22 my $download=0;
23 my $updatefile='';
24 my $sourceurl='';
25 my @sources=();
26 my @updatelist=();
27 my $logging=0;
28
29
30 if (-e "$UPDXLT::swroot/updatexlrator/settings")
31 {
32 &UPDXLT::readhash("$UPDXLT::swroot/updatexlrator/settings", \%xlratorsettings);
33 if ($xlratorsettings{'FULL_AUTOSYNC'} eq 'on') { $download=1; };
34 if ($xlratorsettings{'ENABLE_LOG'} eq 'on') { $logging=1; };
35 }
36
37 if (-e "$UPDXLT::swroot/proxy/settings") { &UPDXLT::readhash("$UPDXLT::swroot/proxy/settings", \%proxysettings); }
38
39 if (-e "$UPDXLT::swroot/proxy/advanced/settings")
40 {
41 %proxysettings=();
42 &UPDXLT::readhash("$UPDXLT::swroot/proxy/advanced/settings", \%proxysettings);
43 }
44
45 foreach (<$repository/*>)
46 {
47 if (-d $_)
48 {
49 unless (/^$repository\/download$/) { push(@sources,$_); }
50 }
51 }
52
53 foreach (@sources)
54 {
55 @updatelist=<$_/*>;
56 foreach(@updatelist)
57 {
58 if (-e "$_/source.url")
59 {
60 open (FILE,"$_/source.url");
61 $sourceurl=<FILE>;
62 close FILE;
63 chomp($sourceurl);
64 $updatefile = substr($sourceurl,rindex($sourceurl,'/')+1,length($sourceurl));
65 &checksource($_);
66 }
67 }
68 }
69
70 # dotzball: check for dead downloads
71 system("$UPDXLT::apphome/bin/checkdeaddl");
72
73 # -------------------------------------------------------------------
74
75 sub writelog
76 {
77 print "$_[0]\n";
78 if ($logging)
79 {
80 open (LOGFILE,">>$logfile");
81 my @now = localtime(time);
82 printf LOGFILE "%04d-%02d-%02d %02d:%02d:%02d %s\n",$now[5]+1900,$now[4]+1,$now[3],$now[2],$now[1],$now[0],$_[0];
83 close LOGFILE;
84 }
85 }
86
87
88 # -------------------------------------------------------------------
89
90 sub checksource
91 {
92 my @http_header=();
93 my $http_result='000 n/a';
94 my $returncode=0;
95 my $localfile='';
96 my $remote_size=0;
97 my $remote_mtime=0;
98 my $login='';
99 my $url='';
100 my $cdir=$_[0];
101
102 open (FILE,"$cdir/source.url");
103 $url=<FILE>;
104 close FILE;
105 chomp($url);
106
107 $localfile = $cdir . substr($url,rindex($url,'/'),length($url));
108
109 if (($proxysettings{'UPSTREAM_PROXY'}) && ($proxysettings{'UPSTREAM_USER'}))
110 {
111 $login = "--proxy-user=\"$proxysettings{'UPSTREAM_USER'}\"";
112 if ($proxysettings{'UPSTREAM_PASSWORD'})
113 {
114 $login .= " --proxy-password=\"$proxysettings{'UPSTREAM_PASSWORD'}\"";
115 }
116 }
117
118 $ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
119 @http_header = `$UPDXLT::wget $login --user-agent="$UPDXLT::useragent" --spider -S $url 2>&1`;
120 $ENV{'http_proxy'} = '';
121
122 foreach (@http_header)
123 {
124 chomp;
125 if (/^\s*HTTP\/\d+\.\d+\s\d+\s+\w+/) { $http_result = $_; $http_result =~ s/^\s*HTTP\/\d+\.\d+\s+//; }
126 if (/^\s*Content-Length:\s/) { $remote_size = $_; $remote_size =~ s/[^0-9]//g; }
127 if (/^\s*Last-Modified:\s/) { $remote_mtime = $_; $remote_mtime =~ s/^\s*Last-Modified:\s//; $remote_mtime = HTTP::Date::str2time($remote_mtime) }
128 }
129
130 &writelog($localfile);
131 &writelog("HTTP result: $http_result");
132 &writelog("Source size: $remote_size");
133 &writelog("Cached size: " . (-s $localfile));
134 &writelog("Source time: $remote_mtime");
135 &writelog("Cached time: " . &UPDXLT::getmtime($localfile));
136
137 if ($http_result =~ /\d+\s+OK$/)
138 {
139 if (($remote_size == -s $localfile) && ($remote_mtime == &UPDXLT::getmtime($localfile)))
140 {
141 &writelog("Status: Ok");
142 &UPDXLT::setcachestatus("$cdir/status",$UPDXLT::sfOk);
143 } else {
144 &writelog("Status: Outdated");
145 &UPDXLT::setcachestatus("$cdir/status",$UPDXLT::sfOutdated);
146 if ($download)
147 {
148 &writelog("Retrieving file from source: $remote_size bytes");
149 $_ = system("$UPDXLT::wget $login --user-agent=\"$UPDXLT::useragent\" -q -O $localfile $url");
150 &writelog("Download finished with code: $_");
151 if ($_ == 0) { &UPDXLT::setcachestatus("$cdir/status",$UPDXLT::sfOk); }
152 }
153 }
154 } else {
155 $_ = $http_result;
156 s/\D+//;
157 if ($_ eq '404')
158 {
159 &writelog("Status: No source");
160 &UPDXLT::setcachestatus("$cdir/status",$UPDXLT::sfNoSource);
161 } else {
162 &writelog("Status: Error");
163 &UPDXLT::setcachestatus("$cdir/status",$UPDXLT::sfUnknown);
164 }
165 }
166
167 &UPDXLT::setcachestatus("$cdir/checkup.log",time);
168 }
169
170 # -------------------------------------------------------------------