]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/cfgroot/net-traffic-admin.pl
Change default NTP servers.
[people/pmueller/ipfire-2.x.git] / config / cfgroot / net-traffic-admin.pl
1 #!/usr/bin/perl
2 #
3 # This file is a library file for the Net-Traffic Addon.
4 #
5 # Copyright (C) 2006 Achim Weber <dotzball@users.sourceforge.net>
6 #
7 # $Id: net-traffic-admin.pl,v 1.13 2006/12/10 13:46:00 dotzball Exp $
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 package NETTRAFF;
24
25 use strict;
26 use LWP::UserAgent;
27
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
31
32 $|=1; # line buffering
33
34 my $updateUrl = "http://blockouttraffic.de/version/Net-Traffic.latest";
35 my $latestVersionFile = "${General::swroot}/net-traffic/latestVersion";
36
37 %NETTRAFF::settings;
38
39
40 $NETTRAFF::settingsfile = "${General::swroot}/net-traffic/settings";
41 $NETTRAFF::versionfile = "${General::swroot}/net-traffic/version";
42 $NETTRAFF::logfile = "/var/log/net-traffic.log";
43 $NETTRAFF::colorOk = '#00FF00';
44 $NETTRAFF::colorWarn = '#FFFF00';
45 $NETTRAFF::colorMax = '#FF0000';
46
47
48 #~ $NETTRAFF::settingsCGI = '/cgi-bin/fwrulesadm.cgi';
49 #~ $NETTRAFF::configCGI = '/cgi-bin/fwrules.cgi';
50 #~ $NETTRAFF::advConfCGI = '/cgi-bin/fwadvconf.cgi';
51
52
53 @NETTRAFF::longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
54 $Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
55 $Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
56 $Lang::tr{'december'} );
57
58 @NETTRAFF::months = ( 0,1,2,3,4,5,6,7,8,9,10,11 );
59
60 @NETTRAFF::years=("2001","2002","2003","2004","2005","2006","2007","2008","2009");
61
62 #workaround to suppress a warning when a variable is used only once
63 my @dummy = ( $General::version );
64 undef (@dummy);
65
66
67 # Init Settings
68 $NETTRAFF::settings{'MONTHLY_VOLUME_ON'} = 'off';
69 $NETTRAFF::settings{'MONTHLY_VOLUME'} = '1';
70 $NETTRAFF::settings{'STARTDAY'} = '1';
71 $NETTRAFF::settings{'WARN_ON'} = 'off';
72 $NETTRAFF::settings{'WARN'} = '80';
73 $NETTRAFF::settings{'CALC_INTERVAL'} = '60';
74 $NETTRAFF::settings{'SHOW_AT_HOME'} = 'on';
75 $NETTRAFF::settings{'SEND_EMAIL_ON'} = 'off';
76 $NETTRAFF::settings{'EMAIL_TO'} = '';
77 $NETTRAFF::settings{'EMAIL_FROM'} = '';
78 $NETTRAFF::settings{'EMAIL_USR'} = '';
79 $NETTRAFF::settings{'EMAIL_PW'} = '';
80 $NETTRAFF::settings{'EMAIL_SERVER'} = '';
81 $NETTRAFF::settings{'VERSION_CHECK_ON'} = 'off';
82
83 &NETTRAFF::readSettings();
84
85
86 sub readSettings
87 {
88 &General::readhash($NETTRAFF::settingsfile, \%NETTRAFF::settings);
89 }
90
91
92 sub showNetTrafficVersion
93 {
94 my %versionSettings = ();
95
96 &General::readhash($NETTRAFF::versionfile, \%versionSettings);
97
98 print <<END;
99 <a href="http://$versionSettings{'URL'}" target="_blank">
100 <b>Net-Traffic $versionSettings{'VERSION_INSTALLED'}
101 -
102 END
103 print "Build $versionSettings{'BUILD_INSTALLED'}";
104
105 if ($versionSettings{'IS_TESTVERSION'} == 1) {
106 print " - Testversion $versionSettings{'TESTVERSION'}";
107 }
108 print "</b></a><br /><br />\n";
109
110 # check for new version
111 &checkForNewVersion();
112 }
113
114 sub traffPercentbar
115 {
116 my $percent = $_[0];
117 my $fg = '#a0a0a0';
118 my $bg = '#e2e2e2';
119
120 if ($percent =~ m/^(\d+)%$/ )
121 {
122 print <<END;
123 <table width='100%' border='1' cellspacing='0' cellpadding='0' style='border-width:1px;border-style:solid;border-color:$fg;width:100%;height:10px;'>
124 <tr>
125 END
126
127 if ($percent eq "100%" || $1 > 100)
128 {
129 $fg = $NETTRAFF::colorMax;
130 print "<td width='100%' bgcolor='$fg' style='background-color:$fg;border-style:solid;border-width:1px;border-color:$bg'>"
131 }
132 elsif ($percent eq "0%")
133 {
134 print "<td width='100%' bgcolor='$bg' style='background-color:$bg;border-style:solid;border-width:1px;border-color:$bg'>"
135 }
136 else
137 {
138 if($NETTRAFF::settings{'WARN_ON'} eq 'on'
139 && $1 >= $NETTRAFF::settings{'WARN'})
140 {
141 $fg = $NETTRAFF::colorWarn;
142 }
143
144 print "<td width='$percent' bgcolor='$fg' style='background-color:$fg;border-style:solid;border-width:1px;border-color:$bg'></td><td width='" . (100-$1) . "%' bgcolor='$bg' style='background-color:$bg;border-style:solid;border-width:1px;border-color:$bg'>"
145 }
146 print <<END;
147 <img src='/images/null.gif' width='1' height='1' alt='' />
148 </td>
149 </tr>
150 </table>
151 END
152
153 }
154 }
155
156
157 sub checkForNewVersion
158 {
159 if ($NETTRAFF::settings{'VERSION_CHECK_ON'} ne 'on')
160 {
161 return;
162 }
163
164 # download latest version
165 &downloadLatestVersionInfo();
166
167 if(-e $latestVersionFile)
168 {
169 my %versionSettings = ();
170 &General::readhash($NETTRAFF::versionfile, \%versionSettings);
171
172 my %latestVersion = ();
173 &General::readhash($latestVersionFile, \%latestVersion);
174
175 if( $versionSettings{'VERSION_INSTALLED'} lt $latestVersion{'VERSION_AVAILABLE'}
176 || ( $versionSettings{'VERSION_INSTALLED'} le $latestVersion{'VERSION_AVAILABLE'}
177 && $versionSettings{'BUILD_INSTALLED'} lt $latestVersion{'BUILD_AVAILABLE'} ) )
178 {
179 &Header::openbox('100%', 'left', $Lang::tr{'info'});
180 print <<END;
181 <table width="100%">
182 <tr>
183 <td>
184 $Lang::tr{'net traffic newversion'}
185 <a href="$latestVersion{'URL_UPDATE'}" target="_blank">
186 <b>$latestVersion{'URL_UPDATE'}</b>
187 </a>
188 </td>
189 </tr>
190 <tr>
191 <td>
192 <b>v$latestVersion{'VERSION_AVAILABLE'} - Build $latestVersion{'BUILD_AVAILABLE'}
193 </table>
194 END
195
196 &Header::closebox();
197 }
198 }
199 }
200
201 sub downloadLatestVersionInfo
202 {
203 # only check if we are online
204 if (! -e '/var/ipfire/red/active')
205 {
206 return;
207 }
208
209 # download latest version file if it is not existing or outdated (i.e. 5 days old)
210 if((! -e $latestVersionFile) || (int(-M $latestVersionFile) > 5))
211 {
212 my %versionSettings = ();
213 &General::readhash($NETTRAFF::versionfile, \%versionSettings);
214
215 my $ua = LWP::UserAgent->new;
216 $ua->timeout(120);
217 $ua->agent("Mozilla/4.0 (compatible; IPFire $General::version; $versionSettings{'VERSION_INSTALLED'})");
218 my $content = $ua->get($updateUrl);
219
220 if ( $content->is_success )
221 {
222 #~ open(FILE, ">$latestVersionFile") or die "Could not write file: $latestVersionFile";
223 #~ flock (FILE, 2);
224 #~ print FILE "$content->content\n";
225 #~ close(FILE);
226
227 my %latestVersion = ();
228
229 # latest versions, format is: MOD_VERSION="1.3.0"
230 $content->content =~ /MOD_VERSION="(.+?)"/;
231 $latestVersion{'VERSION_AVAILABLE'} = $1;
232
233 # latest build, format is: MOD_BUILD="0"
234 $content->content =~ /MOD_BUILD="(.+?)"/;
235 $latestVersion{'BUILD_AVAILABLE'} = $1;
236
237 # URL format is: MOD_URL="http://blockouttraffic.de/nt_index.php"
238 $content->content =~ /MOD_URL="(.+?)"/;
239 $latestVersion{'URL_UPDATE'} = $1;
240
241 &General::writehash($latestVersionFile, \%latestVersion);
242 }
243 }
244 }
245
246
247 sub getFormatedDate
248 {
249 my $time = shift;
250 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time);
251
252 return sprintf("%04d-%02d-%02d, %02d:%02d", 1900+$year, $mon+1, $mday, $hour, $min);;
253
254 }
255 # always return 1;
256 1;
257 # EOF