2013-01-08 16:22:05 +00:00
#
# Monitorix - A lightweight system monitoring tool.
#
2017-01-19 17:27:17 +00:00
# Copyright (C) 2005-2017 by Jordi Sanfeliu <jordi@fibranet.cat>
2013-01-08 16:22:05 +00:00
#
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
package traffacct ;
use strict ;
use warnings ;
use Monitorix ;
use RRDs ;
2013-01-08 17:47:34 +00:00
use MIME::Lite ;
2013-01-09 10:15:51 +00:00
use LWP::UserAgent ;
2013-05-08 19:10:45 +01:00
use Socket ;
2013-01-08 16:22:05 +00:00
use Exporter 'import' ;
our @ EXPORT = qw( traffacct_init traffacct_update traffacct_cgi traffacct_getcounters traffacct_sendreports ) ;
sub traffacct_init {
my $ myself = ( caller ( 0 ) ) [ 3 ] ;
my ( $ package , $ config , $ debug ) = @ _ ;
my $ rrd = $ config - > { base_lib } . $ package . ".rrd" ;
my $ traffacct = $ config - > { traffacct } ;
my $ info ;
my @ ds ;
2013-11-04 09:51:48 +00:00
my @ rra ;
2013-01-08 16:22:05 +00:00
my @ tmp ;
my $ n ;
2013-11-04 09:51:48 +00:00
my @ average ;
my @ min ;
my @ max ;
my @ last ;
2014-06-26 10:50:17 +01:00
my $ table = $ config - > { ip_default_table } ;
2013-01-08 16:22:05 +00:00
if ( ! grep { $ _ eq $ config - > { os } } ( "Linux" ) ) {
logger ( "$myself is not supported yet by your operating system ($config->{os}." ) ;
return ;
}
if ( - e $ rrd ) {
$ info = RRDs:: info ( $ rrd ) ;
for my $ key ( keys %$ info ) {
if ( index ( $ key , 'ds[' ) == 0 ) {
if ( index ( $ key , '.type' ) != - 1 ) {
push ( @ ds , substr ( $ key , 3 , index ( $ key , ']' ) - 3 ) ) ;
}
}
2013-11-04 09:51:48 +00:00
if ( index ( $ key , 'rra[' ) == 0 ) {
if ( index ( $ key , '.rows' ) != - 1 ) {
push ( @ rra , substr ( $ key , 4 , index ( $ key , ']' ) - 4 ) ) ;
}
}
2013-01-08 16:22:05 +00:00
}
if ( scalar ( @ ds ) / 2 != $ traffacct - > { max } ) {
2013-11-04 09:51:48 +00:00
logger ( "$myself: Detected size mismatch between 'max = $traffacct->{max}' and $rrd (" . scalar ( @ ds ) / 2 . "). Resizing it accordingly. All historical data will be lost. Backup file created." ) ;
rename ( $ rrd , "$rrd.bak" ) ;
}
2013-11-04 15:51:19 +00:00
if ( scalar ( @ rra ) < 12 + ( 4 * $ config - > { max_historic_years } ) ) {
2013-11-04 09:51:48 +00:00
logger ( "$myself: Detected size mismatch between 'max_historic_years' (" . $ config - > { max_historic_years } . ") and $rrd (" . ( ( scalar ( @ rra ) - 12 ) / 4 ) . "). Resizing it accordingly. All historical data will be lost. Backup file created." ) ;
2013-01-08 16:22:05 +00:00
rename ( $ rrd , "$rrd.bak" ) ;
}
}
if ( ! ( - e $ rrd ) ) {
logger ( "Creating '$rrd' file." ) ;
2013-11-04 09:51:48 +00:00
for ( $ n = 1 ; $ n <= $ config - > { max_historic_years } ; $ n + + ) {
push ( @ average , "RRA:AVERAGE:0.5:1440:" . ( 365 * $ n ) ) ;
push ( @ min , "RRA:MIN:0.5:1440:" . ( 365 * $ n ) ) ;
push ( @ max , "RRA:MAX:0.5:1440:" . ( 365 * $ n ) ) ;
push ( @ last , "RRA:LAST:0.5:1440:" . ( 365 * $ n ) ) ;
}
2013-01-08 16:22:05 +00:00
for ( $ n = 0 ; $ n < $ traffacct - > { max } ; $ n + + ) {
push ( @ tmp , "DS:traffacct" . $ n . "_in:GAUGE:120:0:U" ) ;
push ( @ tmp , "DS:traffacct" . $ n . "_out:GAUGE:120:0:U" ) ;
}
eval {
RRDs:: create ( $ rrd ,
"--step=60" ,
@ tmp ,
"RRA:AVERAGE:0.5:1:1440" ,
"RRA:AVERAGE:0.5:30:336" ,
"RRA:AVERAGE:0.5:60:744" ,
2013-11-04 09:51:48 +00:00
@ average ,
2013-01-08 16:22:05 +00:00
"RRA:MIN:0.5:1:1440" ,
"RRA:MIN:0.5:30:336" ,
"RRA:MIN:0.5:60:744" ,
2013-11-04 09:51:48 +00:00
@ min ,
2013-01-08 16:22:05 +00:00
"RRA:MAX:0.5:1:1440" ,
"RRA:MAX:0.5:30:336" ,
"RRA:MAX:0.5:60:744" ,
2013-11-04 09:51:48 +00:00
@ max ,
2013-01-08 16:22:05 +00:00
"RRA:LAST:0.5:1:1440" ,
"RRA:LAST:0.5:30:336" ,
"RRA:LAST:0.5:60:744" ,
2013-11-04 09:51:48 +00:00
@ last ,
2013-01-08 16:22:05 +00:00
) ;
} ;
my $ err = RRDs:: error ;
if ( $@ || $ err ) {
logger ( "$@" ) unless ! $@ ;
if ( $ err ) {
logger ( "ERROR: while creating $rrd: $err" ) ;
if ( $ err eq "RRDs::error" ) {
logger ( "... is the RRDtool Perl package installed?" ) ;
}
}
return ;
}
}
if ( $ config - > { os } eq "Linux" ) {
if ( ! $ config - > { net } - > { gateway } ) {
2013-11-04 10:50:28 +00:00
logger ( "$myself: ERROR: You must assign a valid ethernet interface in 'net->gateway'" ) ;
2013-01-08 16:22:05 +00:00
return ;
}
# set the iptables rules for each defined host/network
my @ tal = split ( ',' , $ traffacct - > { list } ) ;
for ( $ n = 0 ; $ n < $ traffacct - > { max } ; $ n + + ) {
my $ name = trim ( $ tal [ $ n ] ) ;
if ( $ name ) {
2013-05-08 19:10:45 +01:00
my $ ip ;
if ( $ traffacct - > { desc } - > { $ n } ) {
$ ip = trim ( ( split ( ',' , $ traffacct - > { desc } - > { $ n } ) ) [ 0 ] ) ;
}
2013-01-08 16:22:05 +00:00
if ( ! $ ip ) {
if ( ! gethostbyname ( $ name ) ) {
logger ( "WARNING: Unable to resolve '" . $ name . "'. Check your DNS." ) ;
}
$ ip = inet_ntoa ( ( gethostbyname ( $ name ) ) [ 4 ] ) ;
$ ip = $ ip . "/32" ;
}
2014-06-26 10:50:17 +01:00
open ( IN , "iptables -t $table -nxvL monitorix_daily_$name 2>/dev/null |" ) ;
2013-01-08 16:22:05 +00:00
my @ data = <IN> ;
close ( IN ) ;
if ( ! scalar ( @ data ) ) {
2014-06-26 10:50:17 +01:00
system ( "iptables -t $table -N monitorix_daily_$name" ) ;
system ( "iptables -t $table -I FORWARD -j monitorix_daily_$name" ) ;
system ( "iptables -t $table -A monitorix_daily_$name -s $ip -d 0/0 -o $config->{net}->{gateway}" ) ;
system ( "iptables -t $table -A monitorix_daily_$name -s 0/0 -d $ip -i $config->{net}->{gateway}" ) ;
2013-01-08 16:22:05 +00:00
}
}
}
}
# Since 3.0.0 PC_LAN values were renamed to TRAFFACCT.
for ( $ n = 0 ; $ n < $ traffacct - > { max } ; $ n + + ) {
RRDs:: tune ( $ rrd ,
"--data-source-rename=pc" . $ n . "_in:traffacct" . $ n . "_in" ,
"--data-source-rename=pc" . $ n . "_out:traffacct" . $ n . "_out" ,
) ;
}
$ config - > { traffacct_hist_in } = ( ) ;
$ config - > { traffacct_hist_out } = ( ) ;
push ( @ { $ config - > { func_update } } , $ package ) ;
logger ( "$myself: Ok" ) if $ debug ;
}
sub traffacct_update {
my $ myself = ( caller ( 0 ) ) [ 3 ] ;
my ( $ package , $ config , $ debug ) = @ _ ;
my $ rrd = $ config - > { base_lib } . $ package . ".rrd" ;
my $ traffacct = $ config - > { traffacct } ;
2014-06-26 10:50:17 +01:00
my $ table = $ config - > { ip_default_table } ;
2013-01-08 16:22:05 +00:00
my @ in ;
my @ out ;
my $ n ;
my $ rrdata = "N" ;
my @ tal = split ( ',' , $ traffacct - > { list } ) ;
for ( $ n = 0 ; $ n < $ traffacct - > { max } ; $ n + + ) {
my $ name = trim ( $ tal [ $ n ] ) ;
if ( $ name ) {
2013-05-08 19:10:45 +01:00
my $ ip ;
if ( $ traffacct - > { desc } - > { $ n } ) {
$ ip = trim ( ( split ( ',' , $ traffacct - > { desc } - > { $ n } ) ) [ 0 ] ) ;
}
2013-01-08 16:22:05 +00:00
if ( ! $ ip ) {
if ( ! gethostbyname ( $ name ) ) {
logger ( "WARNING: Unable to resolve '" . $ name . "'. Check your DNS." ) ;
}
$ ip = inet_ntoa ( ( gethostbyname ( $ name ) ) [ 4 ] ) ;
}
$ ip =~ s/\/\d+// ;
2014-06-26 10:50:17 +01:00
open ( IN , "iptables -t $table -nxvL monitorix_daily_$name |" ) ;
2013-01-08 16:22:05 +00:00
$ in [ $ n ] = 0 unless $ in [ $ n ] ;
$ out [ $ n ] = 0 unless $ out [ $ n ] ;
while ( <IN> ) {
my ( undef , $ bytes , undef , undef , undef , undef , $ source ) = split ( ' ' , $ _ ) ;
if ( $ source ) {
if ( $ source =~ /0.0.0.0/ ) {
$ in [ $ n ] = $ bytes - ( $ config - > { traffacct_hist_in } [ $ n ] || 0 ) ;
$ in [ $ n ] = 0 unless $ in [ $ n ] != $ bytes ;
$ config - > { traffacct_hist_in } [ $ n ] = $ bytes ;
$ in [ $ n ] /= 60 ;
}
if ( $ source eq $ ip ) {
$ out [ $ n ] = $ bytes - ( $ config - > { traffacct_hist_out } [ $ n ] || 0 ) ;
$ out [ $ n ] = 0 unless $ out [ $ n ] != $ bytes ;
$ config - > { traffacct_hist_out } [ $ n ] = $ bytes ;
$ out [ $ n ] /= 60 ;
}
}
}
close ( IN ) ;
}
}
for ( $ n = 0 ; $ n < $ traffacct - > { max } ; $ n + + ) {
my $ i = $ in [ $ n ] || 0 ;
my $ o = $ out [ $ n ] || 0 ;
$ rrdata . = ":$i:$o" ;
}
RRDs:: update ( $ rrd , $ rrdata ) ;
logger ( "$myself: $rrdata" ) if $ debug ;
my $ err = RRDs:: error ;
logger ( "ERROR: while updating $rrd: $err" ) if $ err ;
}
sub traffacct_getcounters {
my $ myself = ( caller ( 0 ) ) [ 3 ] ;
my ( $ config , $ debug ) = @ _ ;