]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - config/updxlrator/checkup
Started core37.
[people/pmueller/ipfire-2.x.git] / config / updxlrator / checkup
CommitLineData
46c01c09
MT
1#!/usr/bin/perl
2#
3# This code is distributed under the terms of the GPL
4#
0a638396 5# (c) 2006-2008 marco.s - http://update-accelerator.advproxy.net
46c01c09 6#
4f4092e3
CS
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 $
46c01c09
MT
10#
11
12use strict;
13
46c01c09
MT
14use HTTP::Date;
15
4f4092e3
CS
16require '/var/ipfire/updatexlrator/updxlrator-lib.pl';
17
46c01c09 18my $logfile="/var/log/updatexlrator/checkup.log";
0a638396 19my $repository='/var/updatecache';
0a638396 20my %proxysettings=();
46c01c09
MT
21my %xlratorsettings=();
22my $download=0;
23my $updatefile='';
24my $sourceurl='';
0a638396 25my @sources=();
46c01c09 26my @updatelist=();
0a638396 27my $logging=0;
46c01c09 28
46c01c09 29
4f4092e3 30if (-e "$UPDXLT::swroot/updatexlrator/settings")
46c01c09 31{
4f4092e3 32 &UPDXLT::readhash("$UPDXLT::swroot/updatexlrator/settings", \%xlratorsettings);
46c01c09 33 if ($xlratorsettings{'FULL_AUTOSYNC'} eq 'on') { $download=1; };
0a638396 34 if ($xlratorsettings{'ENABLE_LOG'} eq 'on') { $logging=1; };
46c01c09
MT
35}
36
4f4092e3 37if (-e "$UPDXLT::swroot/proxy/settings") { &UPDXLT::readhash("$UPDXLT::swroot/proxy/settings", \%proxysettings); }
46c01c09 38
4f4092e3 39if (-e "$UPDXLT::swroot/proxy/advanced/settings")
0a638396
CS
40{
41 %proxysettings=();
4f4092e3 42 &UPDXLT::readhash("$UPDXLT::swroot/proxy/advanced/settings", \%proxysettings);
0a638396 43}
46c01c09 44
0a638396
CS
45foreach (<$repository/*>)
46{
47 if (-d $_)
48 {
49 unless (/^$repository\/download$/) { push(@sources,$_); }
50 }
51}
46c01c09 52
0a638396
CS
53foreach (@sources)
54{
55 @updatelist=<$_/*>;
56 foreach(@updatelist)
57 {
58 if (-e "$_/source.url")
59 {
60 open (FILE,"$_/source.url");
61 $sourceurl=<FILE>;
46c01c09 62 close FILE;
0a638396
CS
63 chomp($sourceurl);
64 $updatefile = substr($sourceurl,rindex($sourceurl,'/')+1,length($sourceurl));
65 &checksource($_);
46c01c09
MT
66 }
67 }
68}
69
4f4092e3
CS
70# dotzball: check for dead downloads
71system("$UPDXLT::apphome/bin/checkdeaddl");
46c01c09
MT
72
73# -------------------------------------------------------------------
74
0a638396 75sub writelog
46c01c09 76{
0a638396
CS
77 print "$_[0]\n";
78 if ($logging)
46c01c09 79 {
0a638396
CS
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;
46c01c09 84 }
46c01c09
MT
85}
86
46c01c09 87
0a638396 88# -------------------------------------------------------------------
46c01c09 89
0a638396
CS
90sub 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 }
46c01c09 117
0a638396 118 $ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
4f4092e3 119 @http_header = `$UPDXLT::wget $login --user-agent="$UPDXLT::useragent" --spider -S $url 2>&1`;
0a638396 120 $ENV{'http_proxy'} = '';
46c01c09 121
0a638396 122 foreach (@http_header)
46c01c09 123 {
0a638396
CS
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) }
46c01c09
MT
128 }
129
0a638396
CS
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");
4f4092e3 135 &writelog("Cached time: " . &UPDXLT::getmtime($localfile));
0a638396
CS
136
137 if ($http_result =~ /\d+\s+OK$/)
46c01c09 138 {
4f4092e3 139 if (($remote_size == -s $localfile) && ($remote_mtime == &UPDXLT::getmtime($localfile)))
46c01c09 140 {
0a638396 141 &writelog("Status: Ok");
4f4092e3 142 &UPDXLT::setcachestatus("$cdir/status",$UPDXLT::sfOk);
0a638396
CS
143 } else {
144 &writelog("Status: Outdated");
4f4092e3 145 &UPDXLT::setcachestatus("$cdir/status",$UPDXLT::sfOutdated);
0a638396 146 if ($download)
46c01c09 147 {
0a638396 148 &writelog("Retrieving file from source: $remote_size bytes");
4f4092e3 149 $_ = system("$UPDXLT::wget $login --user-agent=\"$UPDXLT::useragent\" -q -O $localfile $url");
0a638396 150 &writelog("Download finished with code: $_");
4f4092e3 151 if ($_ == 0) { &UPDXLT::setcachestatus("$cdir/status",$UPDXLT::sfOk); }
46c01c09
MT
152 }
153 }
0a638396
CS
154 } else {
155 $_ = $http_result;
156 s/\D+//;
157 if ($_ eq '404')
158 {
159 &writelog("Status: No source");
4f4092e3 160 &UPDXLT::setcachestatus("$cdir/status",$UPDXLT::sfNoSource);
0a638396
CS
161 } else {
162 &writelog("Status: Error");
4f4092e3 163 &UPDXLT::setcachestatus("$cdir/status",$UPDXLT::sfUnknown);
0a638396 164 }
46c01c09 165 }
4f4092e3
CS
166
167 &UPDXLT::setcachestatus("$cdir/checkup.log",time);
46c01c09
MT
168}
169
170# -------------------------------------------------------------------