mirror of
https://github.com/vincentmli/bpfire.git
synced 2026-04-28 03:33:25 +02:00
Net-Traffic aktualisiert
git-svn-id: http://svn.ipfire.org/svn/ipfire/trunk@595 ea5c0bd1-69bd-2848-81d8-4f18e57aeed8
This commit is contained in:
367
config/cfgroot/monitorTraffic.pl
Normal file
367
config/cfgroot/monitorTraffic.pl
Normal file
@@ -0,0 +1,367 @@
|
||||
#!/usr/bin/perl
|
||||
#
|
||||
# This code is distributed under the terms of the GPL
|
||||
#
|
||||
# (c) Achim Weber 2006
|
||||
#
|
||||
# $Id: monitorTraffic.pl,v 1.14 2006/12/15 14:43:57 dotzball Exp $
|
||||
#
|
||||
#
|
||||
|
||||
use strict;
|
||||
|
||||
# enable only the following on debugging purpose
|
||||
use warnings;
|
||||
|
||||
require '/var/ipfire/general-functions.pl';
|
||||
require "${General::swroot}/lang.pl";
|
||||
require "${General::swroot}/net-traffic/net-traffic-lib.pl";
|
||||
require "${General::swroot}/net-traffic/net-traffic-admin.pl";
|
||||
|
||||
my @dummy = (${Traffic::red_in},${Traffic::red_out});
|
||||
undef(@dummy);
|
||||
|
||||
# Debug level:
|
||||
# 0 - send email (if enabled), no print
|
||||
# 1 - send email (if enabled), print
|
||||
# 2 - only print
|
||||
my $debugLevel = 0;
|
||||
# Debug
|
||||
|
||||
my %log = ();
|
||||
$log{'CALC_VOLUME_TOTAL'} = 0;
|
||||
$log{'CALC_VOLUME_IN'} = 0;
|
||||
$log{'CALC_VOLUME_OUT'} = 0;
|
||||
$log{'CALC_WEEK_TOTAL'} = 0;
|
||||
$log{'CALC_WEEK_IN'} = 0;
|
||||
$log{'CALC_WEEK_OUT'} = 0;
|
||||
$log{'CALC_LAST_RUN'} = 0;
|
||||
$log{'CALC_PERCENT'} = 0;
|
||||
$log{'WARNMAIL_SEND'} = 'no';
|
||||
|
||||
# current time == endtime
|
||||
my $currentTime = time;
|
||||
|
||||
# on force we don't load the log data
|
||||
unless(defined($ARGV[0]) && $ARGV[0] eq '--force') {
|
||||
&General::readhash($NETTRAFF::logfile, \%log);
|
||||
}
|
||||
|
||||
|
||||
# Only send email?
|
||||
if(defined($ARGV[0]) && ($ARGV[0] eq '--testEmail' || $ARGV[0] eq '--warnEmail'))
|
||||
{
|
||||
print "Send testmail\n" if($debugLevel > 0);
|
||||
# send (test|warn) email
|
||||
my $return = &sendEmail($ARGV[0]);
|
||||
print "$return\n";
|
||||
exit 0;
|
||||
}
|
||||
|
||||
|
||||
# should we recalculate?
|
||||
# calc seconds for one interval
|
||||
my $intervalTime = $NETTRAFF::settings{'CALC_INTERVAL'} * 60;
|
||||
# next time, we have to calculate
|
||||
my $nextRunTime = $log{'CALC_LAST_RUN'} + $intervalTime;
|
||||
|
||||
if ($debugLevel > 0)
|
||||
{
|
||||
my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($log{'CALC_LAST_RUN'});
|
||||
my $lastRun = sprintf("%04d-%02d-%02d, %02d:%02d", 1900+$year, $mon+1, $mday, $hour, $min);
|
||||
print "last run: $lastRun\n";
|
||||
|
||||
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($nextRunTime);
|
||||
my $nextRun = sprintf("%04d-%02d-%02d, %02d:%02d", 1900+$year, $mon+1, $mday, $hour, $min);
|
||||
print "next run: $nextRun\n";
|
||||
|
||||
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($currentTime);
|
||||
my $current = sprintf("%04d-%02d-%02d, %02d:%02d", 1900+$year, $mon+1, $mday, $hour, $min);
|
||||
print "current time: $current\n";
|
||||
}
|
||||
|
||||
# use a little time buffer in case the last run started some seconds earlier
|
||||
if($currentTime < ($nextRunTime - 60) )
|
||||
{
|
||||
# nothing to do
|
||||
if ($debugLevel > 0)
|
||||
{
|
||||
my $infoMsg = "Net-Traffic: nothing to do, do next calculation later.";
|
||||
print "$infoMsg\n";
|
||||
&General::log($infoMsg);
|
||||
}
|
||||
exit 0;
|
||||
}
|
||||
elsif($debugLevel > 0)
|
||||
{
|
||||
my $infoMsg = "Net-Traffic: Calc traffic now.";
|
||||
print "$infoMsg\n";
|
||||
&General::log($infoMsg);
|
||||
}
|
||||
|
||||
####
|
||||
# Calculate Traffic
|
||||
#
|
||||
|
||||
$log{'CALC_LAST_RUN'} = $currentTime;
|
||||
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($currentTime);
|
||||
|
||||
|
||||
#####################
|
||||
# this month traffic
|
||||
###
|
||||
my $startDay = '1';
|
||||
my $startMonth = $mon + 1;
|
||||
my $startYear = $year + 1900;
|
||||
|
||||
if($NETTRAFF::settings{'MONTHLY_VOLUME_ON'} eq 'on')
|
||||
{
|
||||
$startDay = $NETTRAFF::settings{'STARTDAY'};
|
||||
}
|
||||
|
||||
# this periode started last month
|
||||
if ($mday < $startDay)
|
||||
{
|
||||
# when current month is january we start in last year december
|
||||
if ($startMonth == 1) {
|
||||
$startYear--;
|
||||
$startMonth = 12;
|
||||
}
|
||||
else
|
||||
{
|
||||
$startMonth--;
|
||||
}
|
||||
}
|
||||
$startMonth = $startMonth < 10 ? $startMonth = "0".$startMonth : $startMonth;
|
||||
$startDay = $startDay < 10 ? $startDay = "0".$startDay : $startDay;
|
||||
|
||||
my $start = "$startYear$startMonth$startDay";
|
||||
|
||||
my %month = &getTrafficData($start, $currentTime);
|
||||
|
||||
$log{'CALC_VOLUME_TOTAL'} = $month{'TOTAL'};
|
||||
$log{'CALC_VOLUME_IN'} = $month{'IN'};
|
||||
$log{'CALC_VOLUME_OUT'} = $month{'OUT'};
|
||||
#####################
|
||||
|
||||
|
||||
#####################
|
||||
# this week traffic
|
||||
###
|
||||
$startMonth = $mon;
|
||||
$startYear = $year + 1900;
|
||||
$startDay = $mday-($wday >0 ? $wday-1 : 6);
|
||||
# borrowed from ipacsum
|
||||
my @mofg = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
|
||||
|
||||
if ($startDay < 1) {
|
||||
$startMonth--;
|
||||
if ($startMonth < 0) {
|
||||
$startMonth += 12;
|
||||
$startYear--;
|
||||
}
|
||||
$startDay += $mofg[$startMonth];
|
||||
}
|
||||
|
||||
# $mon starts at 0 but we have to start at 1
|
||||
$startMonth++;
|
||||
|
||||
$startMonth = $startMonth < 10 ? $startMonth = "0".$startMonth : $startMonth;
|
||||
$startDay = $startDay < 10 ? $startDay = "0".$startDay : $startDay;
|
||||
|
||||
$start = "$startYear$startMonth$startDay";
|
||||
|
||||
my %week = &getTrafficData($start, $currentTime);
|
||||
$log{'CALC_WEEK_TOTAL'} = $week{'TOTAL'};
|
||||
$log{'CALC_WEEK_IN'} = $week{'IN'};
|
||||
$log{'CALC_WEEK_OUT'} = $week{'OUT'};
|
||||
####################
|
||||
|
||||
|
||||
|
||||
my $infoMsg = "Reached: $log{'CALC_VOLUME_TOTAL'} MB\n";
|
||||
$infoMsg .= "start: $start";
|
||||
print "$infoMsg\n" if ($debugLevel > 0);
|
||||
|
||||
|
||||
# monthly traffic volume?
|
||||
if ($NETTRAFF::settings{'MONTHLY_VOLUME_ON'} eq 'on')
|
||||
{
|
||||
$log{'CALC_PERCENT'} = sprintf("%d", ($log{'CALC_VOLUME_TOTAL'} / $NETTRAFF::settings{'MONTHLY_VOLUME'} * 100));
|
||||
|
||||
my $infoMsg = "Used (\%): $log{'CALC_PERCENT'} \% - Max.: $NETTRAFF::settings{'MONTHLY_VOLUME'} MB";
|
||||
print "$infoMsg\n" if ($debugLevel > 0);
|
||||
|
||||
|
||||
if($NETTRAFF::settings{'WARN_ON'} eq 'on'
|
||||
&& $log{'CALC_PERCENT'} >= $NETTRAFF::settings{'WARN'})
|
||||
{
|
||||
# warnlevel is reached
|
||||
if ($debugLevel > 0)
|
||||
{
|
||||
my $warnMsg = "Net-Traffic warning: $infoMsg";
|
||||
print "$warnMsg\n";
|
||||
&General::log($warnMsg);
|
||||
}
|
||||
|
||||
if($debugLevel < 2)
|
||||
{
|
||||
if($NETTRAFF::settings{'SEND_EMAIL_ON'} eq 'on'
|
||||
&& $log{'WARNMAIL_SEND'} ne 'yes')
|
||||
{
|
||||
# send warn email
|
||||
my $return = &sendEmail('--warnEmail');
|
||||
|
||||
if($return =~ /Email was sent successfully!/)
|
||||
{
|
||||
$log{'WARNMAIL_SEND'} = 'yes';
|
||||
}
|
||||
else {
|
||||
$log{'WARNMAIL_SEND'} = 'no';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
# warnlevel not reached, reset warnmail send
|
||||
$log{'WARNMAIL_SEND'} = 'no';
|
||||
}
|
||||
}
|
||||
|
||||
&General::writehash($NETTRAFF::logfile, \%log);
|
||||
|
||||
exit 0;
|
||||
|
||||
|
||||
sub getTrafficData
|
||||
{
|
||||
my $p_start = shift;
|
||||
my $p_currentTime = shift;
|
||||
|
||||
if($debugLevel > 0)
|
||||
{
|
||||
print "----------------------\n";
|
||||
print "start: $p_start\n";
|
||||
print "current time: $p_currentTime\n";
|
||||
}
|
||||
|
||||
#my $displayMode = "exactTimeframe";
|
||||
my $displayMode = "exactEnd";
|
||||
|
||||
my %allDaysBytes = ();
|
||||
my @allDays = &Traffic::calcTraffic(\%allDaysBytes, $p_start, $p_currentTime, $displayMode);
|
||||
|
||||
my %traff = ();
|
||||
$traff{'IN'} = 0;
|
||||
$traff{'OUT'} = 0;
|
||||
$traff{'TOTAL'} = 0;
|
||||
|
||||
foreach my $day (@allDays)
|
||||
{
|
||||
if($debugLevel > 0)
|
||||
{
|
||||
print "day: $day\n";
|
||||
print "in: $allDaysBytes{$day}{${Traffic::red_in}}\n";
|
||||
print "out: $allDaysBytes{$day}{${Traffic::red_out}}\n";
|
||||
}
|
||||
|
||||
$traff{'IN'} += $allDaysBytes{$day}{${Traffic::red_in}};
|
||||
$traff{'OUT'} += $allDaysBytes{$day}{${Traffic::red_out}};
|
||||
}
|
||||
|
||||
$traff{'TOTAL'} = $traff{'IN'} + $traff{'OUT'};
|
||||
|
||||
# formating
|
||||
$traff{'TOTAL'} = sprintf("%.2f", ($traff{'TOTAL'}/1048576));
|
||||
$traff{'IN'} = sprintf("%.2f", ($traff{'IN'}/1048576));
|
||||
$traff{'OUT'} = sprintf("%.2f", ($traff{'OUT'}/1048576));
|
||||
|
||||
if($debugLevel > 0)
|
||||
{
|
||||
print "IN: $traff{'IN'}\n";
|
||||
print "OUT: $traff{'OUT'}\n";
|
||||
print "TOTAL: $traff{'TOTAL'}\n";
|
||||
print "----------------------\n";
|
||||
}
|
||||
|
||||
return %traff;
|
||||
}
|
||||
|
||||
|
||||
sub sendEmail
|
||||
{
|
||||
my $mailtyp = shift;
|
||||
|
||||
my $template = "";
|
||||
|
||||
my %ipfireSettings = ();
|
||||
&General::readhash("${General::swroot}/main/settings", \%ipfireSettings);
|
||||
my $host = "$ipfireSettings{'HOSTNAME'}.$ipfireSettings{'DOMAINNAME'}";
|
||||
|
||||
my $subject = "[Net-Traffic] $host: ";
|
||||
|
||||
if($mailtyp eq '--warnEmail')
|
||||
{
|
||||
$subject .= $Lang::tr{'subject warn'};
|
||||
$template = "warn";
|
||||
}
|
||||
else
|
||||
{
|
||||
$subject .= $Lang::tr{'subject test'};
|
||||
$template = "test";
|
||||
}
|
||||
|
||||
if(-e "${General::swroot}/net-traffic/templates/$template.${Lang::language}")
|
||||
{
|
||||
$template .= ".${Lang::language}";
|
||||
}
|
||||
else
|
||||
{
|
||||
$template .= ".en";
|
||||
}
|
||||
|
||||
# read template
|
||||
open(FILE, "${General::swroot}/net-traffic/templates/$template");
|
||||
my @temp = <FILE>;
|
||||
close(FILE);
|
||||
|
||||
my $date_current = &NETTRAFF::getFormatedDate($currentTime);
|
||||
my $date_lastrun = &NETTRAFF::getFormatedDate($log{'CALC_LAST_RUN'});
|
||||
|
||||
my $message = "";
|
||||
foreach my $line (@temp)
|
||||
{
|
||||
chomp($line);
|
||||
$line =~ s/__HOSTNAME__/$host/;
|
||||
$line =~ s/__CALC_VOLUME_TOTAL__/$log{'CALC_VOLUME_TOTAL'}/;
|
||||
$line =~ s/__CALC_PERCENT__/$log{'CALC_PERCENT'}/;
|
||||
$line =~ s/__MONTHLY_VOLUME__/$NETTRAFF::settings{'MONTHLY_VOLUME'}/;
|
||||
$line =~ s/__STARTDAY__/$NETTRAFF::settings{'STARTDAY'}/;
|
||||
$line =~ s/__CURRENT_DATE__/$date_current/;
|
||||
$line =~ s/__LAST_RUN__/$date_lastrun/;
|
||||
|
||||
$message .= "$line\n";
|
||||
}
|
||||
|
||||
|
||||
my $cmd = "/usr/local/bin/sendEmail_nettraffic -f $NETTRAFF::settings{'EMAIL_FROM'} ";
|
||||
$cmd .= " -t $NETTRAFF::settings{'EMAIL_TO'} ";
|
||||
$cmd .= " -u \"$subject\" ";
|
||||
$cmd .= " -m \"$message\" ";
|
||||
$cmd .= " -s $NETTRAFF::settings{'EMAIL_SERVER'} ";
|
||||
|
||||
if($NETTRAFF::settings{'EMAIL_USR'} ne '') {
|
||||
$cmd .= " -xu $NETTRAFF::settings{'EMAIL_USR'} ";
|
||||
}
|
||||
if($NETTRAFF::settings{'EMAIL_PW'} ne '') {
|
||||
$cmd .= " -xp $NETTRAFF::settings{'EMAIL_PW'} ";
|
||||
}
|
||||
|
||||
my $return = `$cmd`;
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
|
||||
257
config/cfgroot/net-traffic-admin.pl
Normal file
257
config/cfgroot/net-traffic-admin.pl
Normal file
@@ -0,0 +1,257 @@
|
||||
#!/usr/bin/perl
|
||||
#
|
||||
# This file is a library file for the Net-Traffic Addon.
|
||||
#
|
||||
# Copyright (C) 2006 Achim Weber <dotzball@users.sourceforge.net>
|
||||
#
|
||||
# $Id: net-traffic-admin.pl,v 1.13 2006/12/10 13:46:00 dotzball Exp $
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
package NETTRAFF;
|
||||
|
||||
use strict;
|
||||
use LWP::UserAgent;
|
||||
|
||||
require '/var/ipfire/general-functions.pl';
|
||||
require "${General::swroot}/lang.pl";
|
||||
require "${General::swroot}/header.pl";
|
||||
|
||||
$|=1; # line buffering
|
||||
|
||||
my $updateUrl = "http://blockouttraffic.de/version/Net-Traffic.latest";
|
||||
my $latestVersionFile = "${General::swroot}/net-traffic/latestVersion";
|
||||
|
||||
%NETTRAFF::settings;
|
||||
|
||||
|
||||
$NETTRAFF::settingsfile = "${General::swroot}/net-traffic/settings";
|
||||
$NETTRAFF::versionfile = "${General::swroot}/net-traffic/version";
|
||||
$NETTRAFF::logfile = "/var/log/net-traffic.log";
|
||||
$NETTRAFF::colorOk = '#00FF00';
|
||||
$NETTRAFF::colorWarn = '#FFFF00';
|
||||
$NETTRAFF::colorMax = '#FF0000';
|
||||
|
||||
|
||||
#~ $NETTRAFF::settingsCGI = '/cgi-bin/fwrulesadm.cgi';
|
||||
#~ $NETTRAFF::configCGI = '/cgi-bin/fwrules.cgi';
|
||||
#~ $NETTRAFF::advConfCGI = '/cgi-bin/fwadvconf.cgi';
|
||||
|
||||
|
||||
@NETTRAFF::longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
|
||||
$Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
|
||||
$Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
|
||||
$Lang::tr{'december'} );
|
||||
|
||||
@NETTRAFF::months = ( 0,1,2,3,4,5,6,7,8,9,10,11 );
|
||||
|
||||
@NETTRAFF::years=("2001","2002","2003","2004","2005","2006","2007","2008","2009");
|
||||
|
||||
#workaround to suppress a warning when a variable is used only once
|
||||
my @dummy = ( $General::version );
|
||||
undef (@dummy);
|
||||
|
||||
|
||||
# Init Settings
|
||||
$NETTRAFF::settings{'MONTHLY_VOLUME_ON'} = 'off';
|
||||
$NETTRAFF::settings{'MONTHLY_VOLUME'} = '1';
|
||||
$NETTRAFF::settings{'STARTDAY'} = '1';
|
||||
$NETTRAFF::settings{'WARN_ON'} = 'off';
|
||||
$NETTRAFF::settings{'WARN'} = '80';
|
||||
$NETTRAFF::settings{'CALC_INTERVAL'} = '60';
|
||||
$NETTRAFF::settings{'SHOW_AT_HOME'} = 'on';
|
||||
$NETTRAFF::settings{'SEND_EMAIL_ON'} = 'off';
|
||||
$NETTRAFF::settings{'EMAIL_TO'} = '';
|
||||
$NETTRAFF::settings{'EMAIL_FROM'} = '';
|
||||
$NETTRAFF::settings{'EMAIL_USR'} = '';
|
||||
$NETTRAFF::settings{'EMAIL_PW'} = '';
|
||||
$NETTRAFF::settings{'EMAIL_SERVER'} = '';
|
||||
$NETTRAFF::settings{'VERSION_CHECK_ON'} = 'off';
|
||||
|
||||
&NETTRAFF::readSettings();
|
||||
|
||||
|
||||
sub readSettings
|
||||
{
|
||||
&General::readhash($NETTRAFF::settingsfile, \%NETTRAFF::settings);
|
||||
}
|
||||
|
||||
|
||||
sub showNetTrafficVersion
|
||||
{
|
||||
my %versionSettings = ();
|
||||
|
||||
&General::readhash($NETTRAFF::versionfile, \%versionSettings);
|
||||
|
||||
print <<END;
|
||||
<a href="http://$versionSettings{'URL'}" target="_blank">
|
||||
<b>Net-Traffic $versionSettings{'VERSION_INSTALLED'}
|
||||
-
|
||||
END
|
||||
print "Build $versionSettings{'BUILD_INSTALLED'}";
|
||||
|
||||
if ($versionSettings{'IS_TESTVERSION'} == 1) {
|
||||
print " - Testversion $versionSettings{'TESTVERSION'}";
|
||||
}
|
||||
print "</b></a><br /><br />\n";
|
||||
|
||||
# check for new version
|
||||
&checkForNewVersion();
|
||||
}
|
||||
|
||||
sub traffPercentbar
|
||||
{
|
||||
my $percent = $_[0];
|
||||
my $fg = '#a0a0a0';
|
||||
my $bg = '#e2e2e2';
|
||||
|
||||
if ($percent =~ m/^(\d+)%$/ )
|
||||
{
|
||||
print <<END;
|
||||
<table width='100%' border='1' cellspacing='0' cellpadding='0' style='border-width:1px;border-style:solid;border-color:$fg;width:100%;height:10px;'>
|
||||
<tr>
|
||||
END
|
||||
|
||||
if ($percent eq "100%" || $1 > 100)
|
||||
{
|
||||
$fg = $NETTRAFF::colorMax;
|
||||
print "<td width='100%' bgcolor='$fg' style='background-color:$fg;border-style:solid;border-width:1px;border-color:$bg'>"
|
||||
}
|
||||
elsif ($percent eq "0%")
|
||||
{
|
||||
print "<td width='100%' bgcolor='$bg' style='background-color:$bg;border-style:solid;border-width:1px;border-color:$bg'>"
|
||||
}
|
||||
else
|
||||
{
|
||||
if($NETTRAFF::settings{'WARN_ON'} eq 'on'
|
||||
&& $1 >= $NETTRAFF::settings{'WARN'})
|
||||
{
|
||||
$fg = $NETTRAFF::colorWarn;
|
||||
}
|
||||
|
||||
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'>"
|
||||
}
|
||||
print <<END;
|
||||
<img src='/images/null.gif' width='1' height='1' alt='' />
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
END
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
sub checkForNewVersion
|
||||
{
|
||||
if ($NETTRAFF::settings{'VERSION_CHECK_ON'} ne 'on')
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
# download latest version
|
||||
&downloadLatestVersionInfo();
|
||||
|
||||
if(-e $latestVersionFile)
|
||||
{
|
||||
my %versionSettings = ();
|
||||
&General::readhash($NETTRAFF::versionfile, \%versionSettings);
|
||||
|
||||
my %latestVersion = ();
|
||||
&General::readhash($latestVersionFile, \%latestVersion);
|
||||
|
||||
if( $versionSettings{'VERSION_INSTALLED'} lt $latestVersion{'VERSION_AVAILABLE'}
|
||||
|| ( $versionSettings{'VERSION_INSTALLED'} le $latestVersion{'VERSION_AVAILABLE'}
|
||||
&& $versionSettings{'BUILD_INSTALLED'} lt $latestVersion{'BUILD_AVAILABLE'} ) )
|
||||
{
|
||||
&Header::openbox('100%', 'left', $Lang::tr{'info'});
|
||||
print <<END;
|
||||
<table width="100%">
|
||||
<tr>
|
||||
<td>
|
||||
$Lang::tr{'net traffic newversion'}
|
||||
<a href="$latestVersion{'URL_UPDATE'}" target="_blank">
|
||||
<b>$latestVersion{'URL_UPDATE'}</b>
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>v$latestVersion{'VERSION_AVAILABLE'} - Build $latestVersion{'BUILD_AVAILABLE'}
|
||||
</table>
|
||||
END
|
||||
|
||||
&Header::closebox();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sub downloadLatestVersionInfo
|
||||
{
|
||||
# only check if we are online
|
||||
if (! -e '/var/ipfire/red/active')
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
# download latest version file if it is not existing or outdated (i.e. 5 days old)
|
||||
if((! -e $latestVersionFile) || (int(-M $latestVersionFile) > 5))
|
||||
{
|
||||
my %versionSettings = ();
|
||||
&General::readhash($NETTRAFF::versionfile, \%versionSettings);
|
||||
|
||||
my $ua = LWP::UserAgent->new;
|
||||
$ua->timeout(120);
|
||||
$ua->agent("Mozilla/4.0 (compatible; IPFire $General::version; $versionSettings{'VERSION_INSTALLED'})");
|
||||
my $content = $ua->get($updateUrl);
|
||||
|
||||
if ( $content->is_success )
|
||||
{
|
||||
#~ open(FILE, ">$latestVersionFile") or die "Could not write file: $latestVersionFile";
|
||||
#~ flock (FILE, 2);
|
||||
#~ print FILE "$content->content\n";
|
||||
#~ close(FILE);
|
||||
|
||||
my %latestVersion = ();
|
||||
|
||||
# latest versions, format is: MOD_VERSION="1.3.0"
|
||||
$content->content =~ /MOD_VERSION="(.+?)"/;
|
||||
$latestVersion{'VERSION_AVAILABLE'} = $1;
|
||||
|
||||
# latest build, format is: MOD_BUILD="0"
|
||||
$content->content =~ /MOD_BUILD="(.+?)"/;
|
||||
$latestVersion{'BUILD_AVAILABLE'} = $1;
|
||||
|
||||
# URL format is: MOD_URL="http://blockouttraffic.de/nt_index.php"
|
||||
$content->content =~ /MOD_URL="(.+?)"/;
|
||||
$latestVersion{'URL_UPDATE'} = $1;
|
||||
|
||||
&General::writehash($latestVersionFile, \%latestVersion);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
sub getFormatedDate
|
||||
{
|
||||
my $time = shift;
|
||||
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time);
|
||||
|
||||
return sprintf("%04d-%02d-%02d, %02d:%02d", 1900+$year, $mon+1, $mday, $hour, $min);;
|
||||
|
||||
}
|
||||
# always return 1;
|
||||
1;
|
||||
# EOF
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/perl
|
||||
#
|
||||
# $Id: net-traffic-lib.pl,v 1.4 2005/03/17 11:43:55 dotzball Exp $
|
||||
# $Id: net-traffic-lib.pl,v 1.10 2007/01/09 19:00:35 dotzball Exp $
|
||||
#
|
||||
# Summarize all IP accounting files from start to end time
|
||||
#
|
||||
@@ -40,29 +40,39 @@ use POSIX qw(strftime);
|
||||
use Time::Local;
|
||||
use Socket;
|
||||
use IO::Handle;
|
||||
#use warnings;
|
||||
#use strict;
|
||||
|
||||
$|=1; # line buffering
|
||||
|
||||
@moff = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 );
|
||||
my @moff = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 );
|
||||
|
||||
# =()<$datdelim="@<DATDELIM>@";>()=
|
||||
$datdelim="#-#-#-#-#";
|
||||
my $datdelim="#-#-#-#-#";
|
||||
# =()<$prefix="@<prefix>@";>()=
|
||||
$prefix="/usr";
|
||||
my $prefix="/usr";
|
||||
# =()<$exec_prefix="@<exec_prefix>@";>()=
|
||||
$exec_prefix="${prefix}";
|
||||
my $exec_prefix="${prefix}";
|
||||
# =()<$INSTALLPATH="@<INSTALLPATH>@";>()=
|
||||
$INSTALLPATH="${exec_prefix}/sbin";
|
||||
$datdir="/var/log/ip-acct";
|
||||
my $INSTALLPATH="${exec_prefix}/sbin";
|
||||
my $datdir="/var/log/ip-acct";
|
||||
|
||||
$me=$0;
|
||||
my $me=$0;
|
||||
$me =~ s|^.*/([^/]+)$|$1|;
|
||||
$now = time;
|
||||
$fetchipac="$INSTALLPATH/fetchipac";
|
||||
$rule_regex = ".*"; # match rules with this regex only
|
||||
my $now = time;
|
||||
my $fetchipac="$INSTALLPATH/fetchipac";
|
||||
my $rule_regex = ".*"; # match rules with this regex only
|
||||
my $machine_name;
|
||||
my $fetchipac_options;
|
||||
my ($newest_timestamp_before_starttime, $oldest_timestamp_after_endtime);
|
||||
my (%rule_firstfile, %rule_lastfile);
|
||||
my $count;
|
||||
my @timestamps;
|
||||
my $rulenumber;
|
||||
my ($starttime, $endtime);
|
||||
|
||||
## Net-Traffic variables ##
|
||||
my %allDays = ();
|
||||
my %allDays;
|
||||
my $allDaysBytes;
|
||||
my $tzoffset = 0;
|
||||
my $displayMode = "daily";
|
||||
@@ -82,7 +92,9 @@ sub calcTraffic{
|
||||
$starttime = shift;
|
||||
$endtime = shift;
|
||||
$displayMode = shift;
|
||||
|
||||
|
||||
# init
|
||||
%allDays = ();
|
||||
$starttime =~ /^(\d\d\d\d)(\d\d)/;
|
||||
$curYear = $1;
|
||||
$curMonth = $2;
|
||||
@@ -92,8 +104,13 @@ sub calcTraffic{
|
||||
$tzoffset = time-timegm(localtime());
|
||||
$machine_name = undef;
|
||||
|
||||
$starttime = makeunixtime($starttime);
|
||||
$endtime = makeunixtime($endtime);
|
||||
if($displayMode ne "exactTimeframe")
|
||||
{
|
||||
$starttime = makeunixtime($starttime);
|
||||
if($displayMode ne 'exactEnd') {
|
||||
$endtime = makeunixtime($endtime);
|
||||
}
|
||||
}
|
||||
$endtime -= 1;
|
||||
|
||||
# options that we need to pass to fetchipac if we call it.
|
||||
@@ -101,9 +118,11 @@ sub calcTraffic{
|
||||
|
||||
$endtime = $now if ($endtime > $now);
|
||||
$starttime = 0 if ($starttime < 0);
|
||||
$mystarttime = &makemydailytime($starttime);
|
||||
$myendtime = &makemydailytime($endtime);
|
||||
%rule_firstfile = %rule_lastfile = ( );
|
||||
#~ $mystarttime = &makemydailytime($starttime);
|
||||
#~ $myendtime = &makemydailytime($endtime);
|
||||
%rule_firstfile = ( );
|
||||
%rule_lastfile = ( );
|
||||
@timestamps = ();
|
||||
|
||||
# find out which timestamps we need to read.
|
||||
# remember newest timestamp before starttime so we know when data for
|
||||
@@ -141,9 +160,9 @@ sub calcTraffic{
|
||||
}
|
||||
close DATA;
|
||||
|
||||
push(@timestamps, $oldest_timestamp_after_endtime)
|
||||
push(@timestamps, $oldest_timestamp_after_endtime)
|
||||
if ($oldest_timestamp_after_endtime);
|
||||
unshift(@timestamps, $newest_timestamp_before_starttime)
|
||||
unshift(@timestamps, $newest_timestamp_before_starttime)
|
||||
if ($newest_timestamp_before_starttime);
|
||||
|
||||
$rulenumber = 0;
|
||||
@@ -151,7 +170,7 @@ sub calcTraffic{
|
||||
# read all data we need and put the data into memory.
|
||||
&read_data;
|
||||
|
||||
@days_sorted = sort keys %allDays;
|
||||
my @days_sorted = sort keys %allDays;
|
||||
return @days_sorted;
|
||||
}
|
||||
##########################
|
||||
@@ -233,7 +252,7 @@ sub read_data {
|
||||
$curMonth = 1;
|
||||
$curYear++;
|
||||
}
|
||||
my $newMonth = $curYear;
|
||||
my $newMonth = $curYear;
|
||||
$newMonth .= $curMonth < 10 ? "0".$curMonth."01" : $curMonth."01";
|
||||
$newMonth .= "01";
|
||||
$curDay = &makeunixtime($newMonth);
|
||||
@@ -242,7 +261,7 @@ sub read_data {
|
||||
|
||||
if ($timestamp < $starttime) {
|
||||
# this record is too old, we dont need the data.
|
||||
# However, the timestamp gives us a clue on the
|
||||
# However, the timestamp gives us a clue on the
|
||||
# time period the next item covers.
|
||||
$do_collect = 0;
|
||||
}
|
||||
@@ -256,7 +275,7 @@ sub read_data {
|
||||
# first one, split the data (if we know for how
|
||||
# long this data is valid, and if $laststamp is not
|
||||
# equal to $starttime in which case the split is
|
||||
# redundant). If we don't have a clue about the
|
||||
# redundant). If we don't have a clue about the
|
||||
# last file time before our first file was created,
|
||||
# we do not know how much of the file data is in our
|
||||
# time frame. we assume everything belongs to us.
|
||||
@@ -265,7 +284,7 @@ sub read_data {
|
||||
if ($laststamp && $laststamp != $newest_timestamp_before_starttime) {
|
||||
my $newdata = &split_data($data,
|
||||
$laststamp, $timestamp, $starttime);
|
||||
$glb_data_before = $data;
|
||||
#~ $glb_data_before = $data;
|
||||
$data = $newdata;
|
||||
$laststamp = $starttime;
|
||||
}
|
||||
@@ -279,8 +298,9 @@ sub read_data {
|
||||
if ($after_time == 0) {
|
||||
$after_time = 1;
|
||||
if ($laststamp) {
|
||||
$glb_data_after =
|
||||
&split_data($data,$laststamp,$timestamp,$endtime);
|
||||
#~ $glb_data_after =
|
||||
#~ &split_data($data,$laststamp,$timestamp,$endtime);
|
||||
&split_data($data,$laststamp,$timestamp,$endtime);
|
||||
} else {
|
||||
$do_collect = 0;
|
||||
}
|
||||
@@ -334,8 +354,8 @@ sub split_data {
|
||||
# $fac1 now says us how much weight the first result has.
|
||||
# initialize the set we will return.
|
||||
my @ret = ( );
|
||||
|
||||
foreach $set (@$data) {
|
||||
|
||||
foreach my $set (@$data) {
|
||||
my ($rule, $bytes, $pkts) = @$set;
|
||||
$$set[1] = int($bytes * $fac1 + 0.5);
|
||||
$$set[2] = int($pkts * $fac1 + 0.5);
|
||||
@@ -347,7 +367,7 @@ sub split_data {
|
||||
# put data from one file into global data structures
|
||||
# must be called in correct sorted file name order to set rules_lastfile
|
||||
# and rules_firstfile (which are currently useless)
|
||||
# arguments:
|
||||
# arguments:
|
||||
# $1=index number of file; $2 = reference to array with data from file
|
||||
sub collect_data {
|
||||
my($filedata, $ifile, $i, $day);
|
||||
@@ -368,7 +388,7 @@ sub collect_data {
|
||||
my $rule = $$set[0];
|
||||
my $bytes = $$set[1];
|
||||
my $pkts = $$set[2];
|
||||
|
||||
|
||||
$_ = $rule;
|
||||
/^(.*) \(.*$/;
|
||||
$_ = $1;
|
||||
@@ -387,8 +407,8 @@ sub init_filter_id {
|
||||
my $newDay = &makemydailytime($s);
|
||||
$newDay =~ /^\d\d\d\d-(\d\d)-\d\d$/;
|
||||
|
||||
return 1 if ($1 > $curMonth && $displayMode ne "daily_multi");
|
||||
|
||||
return 1 if ($1 > $curMonth && $displayMode ne "daily_multi");
|
||||
|
||||
$allDaysBytes->{$s}{'Day'} = $newDay;
|
||||
}
|
||||
else {
|
||||
@@ -408,7 +428,7 @@ sub init_filter_id {
|
||||
|
||||
# read data record from filehandle $1
|
||||
# number of records is $2
|
||||
# Return value: reference to array a of length n;
|
||||
# Return value: reference to array a of length n;
|
||||
# n is the number of rules
|
||||
# each field in a is an array aa with 3 fields
|
||||
# the fields in arrays aa are: [0]=name of rule; [1]=byte count;
|
||||
@@ -452,7 +472,7 @@ sub read_data_record {
|
||||
# use time zone offset $tzoffset (input=wall clock time, output=UTC)
|
||||
sub makeunixtime {
|
||||
my($y, $m, $d, $h, $i, $e);
|
||||
$s = shift;
|
||||
my $s = shift;
|
||||
|
||||
$h=0; $i=0; $e=0;
|
||||
if ($s =~ /^(\d\d\d\d)(\d\d)(\d\d)/) {
|
||||
|
||||
@@ -56,3 +56,6 @@ HOME=/
|
||||
|
||||
# Reset Dialup Statistics
|
||||
bootrun 0 0 1 * * /usr/local/bin/dialctrl.pl reset
|
||||
|
||||
# run traffic monitoring every 5 minutes
|
||||
*/5 * * * * /usr/local/bin/monitorTraff > /dev/null
|
||||
|
||||
10
config/nettraffic/templates/test.de
Normal file
10
config/nettraffic/templates/test.de
Normal file
@@ -0,0 +1,10 @@
|
||||
Dies ist eine Testemail von Net-Traffic auf Ihrem IPCop __HOSTNAME__.
|
||||
|
||||
Verbrauchtes Monatsvolumen: __CALC_VOLUME_TOTAL__ MB (__CALC_PERCENT__ %)
|
||||
Maximales Volumen pro Abrechnungsperiode: __MONTHLY_VOLUME__ MB
|
||||
|
||||
Erste Tag der Abrechnungsperiode ist: __STARTDAY__
|
||||
|
||||
Aktuelles Datum: __CURRENT_DATE__
|
||||
Letzte Traffic Berechnung: __LAST_RUN__
|
||||
|
||||
10
config/nettraffic/templates/test.en
Normal file
10
config/nettraffic/templates/test.en
Normal file
@@ -0,0 +1,10 @@
|
||||
This is a testemail from Net-Traffic on your IPCop __HOSTNAME__.
|
||||
|
||||
This months volume: __CALC_VOLUME_TOTAL__ MB (__CALC_PERCENT__ %)
|
||||
Maximum volume per period: __MONTHLY_VOLUME__ MB
|
||||
|
||||
Startday of periode is: __STARTDAY__
|
||||
|
||||
Current date is: __CURRENT_DATE__
|
||||
Last traffic calculation: __LAST_RUN__
|
||||
|
||||
11
config/nettraffic/templates/test.pl
Normal file
11
config/nettraffic/templates/test.pl
Normal file
@@ -0,0 +1,11 @@
|
||||
To jest email testowy z Net-Traffic na Twoim IPCop __HOSTNAME__.
|
||||
|
||||
Transfer w tym miesi¹cu: __CALC_VOLUME_TOTAL__ MB (__CALC_PERCENT__ %)
|
||||
Maksymalny transfer w okresie: __MONTHLY_VOLUME__ MB
|
||||
|
||||
Pocz¹tkowy dzieñ okresu: __STARTDAY__
|
||||
|
||||
Bie¿¹ca data: __CURRENT_DATE__
|
||||
Ostatnie obliczenia ruchu: __LAST_RUN__
|
||||
|
||||
|
||||
9
config/nettraffic/templates/warn.de
Normal file
9
config/nettraffic/templates/warn.de
Normal file
@@ -0,0 +1,9 @@
|
||||
Der Traffic auf Ihrem IPCop __HOSTNAME__ hat das gew<65>hlte Warnlevel erreicht!
|
||||
|
||||
Verbrauchtes Monatsvolumen: __CALC_VOLUME_TOTAL__ MB (__CALC_PERCENT__ %)
|
||||
Maximales Volumen pro Abrechnungsperiode: __MONTHLY_VOLUME__ MB
|
||||
|
||||
Erste Tag der Abrechnungsperiode ist: __STARTDAY__
|
||||
|
||||
Aktuelles Datum: __CURRENT_DATE__
|
||||
|
||||
9
config/nettraffic/templates/warn.en
Normal file
9
config/nettraffic/templates/warn.en
Normal file
@@ -0,0 +1,9 @@
|
||||
The traffic on your IPCop __HOSTNAME__ has reached the selected warnlevel!
|
||||
|
||||
This months volume: __CALC_VOLUME_TOTAL__ MB (__CALC_PERCENT__ %)
|
||||
Maximum volume per period: __MONTHLY_VOLUME__ MB
|
||||
|
||||
Startday of periode is: __STARTDAY__
|
||||
|
||||
Current date is: __CURRENT_DATE__
|
||||
|
||||
10
config/nettraffic/templates/warn.pl
Normal file
10
config/nettraffic/templates/warn.pl
Normal file
@@ -0,0 +1,10 @@
|
||||
Transfer na Twoim IPCop __HOSTNAME__ osi<EFBFBD>gn<EFBFBD><EFBFBD> ustalony poziom ostrze<EFBFBD>e<EFBFBD>!
|
||||
|
||||
Transfer w tym miesi<EFBFBD>cu: __CALC_VOLUME_TOTAL__ MB (__CALC_PERCENT__ %)
|
||||
Maksymalny transfer w okresie: __MONTHLY_VOLUME__ MB
|
||||
|
||||
Pocz<EFBFBD>tkowy dzie<EFBFBD> okresu: __STARTDAY__
|
||||
|
||||
Bie<EFBFBD><EFBFBD>ca data: __CURRENT_DATE__
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ usr/local/bin/openvpnctrl
|
||||
usr/local/bin/qosctrl
|
||||
usr/local/bin/rebuildhosts
|
||||
usr/local/bin/redctrl
|
||||
usr/local/bin/monitorTraff
|
||||
#usr/local/bin/restartapplejuice
|
||||
usr/local/bin/restartsnort
|
||||
usr/local/bin/restartssh
|
||||
|
||||
Reference in New Issue
Block a user