Monitorix/lib/traffacct.pm

728 lines
23 KiB
Perl
Raw Normal View History

#
# Monitorix - A lightweight system monitoring tool.
#
2017-01-19 17:27:17 +00:00
# Copyright (C) 2005-2017 by Jordi Sanfeliu <jordi@fibranet.cat>
#
# 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;
use MIME::Lite;
use LWP::UserAgent;
use Socket;
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;
my @rra;
my @tmp;
my $n;
my @average;
my @min;
my @max;
my @last;
my $table = $config->{ip_default_table};
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));
}
}
if(index($key, 'rra[') == 0) {
if(index($key, '.rows') != -1) {
push(@rra, substr($key, 4, index($key, ']') - 4));
}
}
}
if(scalar(@ds) / 2 != $traffacct->{max}) {
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");
}
if(scalar(@rra) < 12 + (4 * $config->{max_historic_years})) {
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.");
rename($rrd, "$rrd.bak");
}
}
if(!(-e $rrd)) {
logger("Creating '$rrd' file.");
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));
}
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",
@average,
"RRA:MIN:0.5:1:1440",
"RRA:MIN:0.5:30:336",
"RRA:MIN:0.5:60:744",
@min,
"RRA:MAX:0.5:1:1440",
"RRA:MAX:0.5:30:336",
"RRA:MAX:0.5:60:744",
@max,
"RRA:LAST:0.5:1:1440",
"RRA:LAST:0.5:30:336",
"RRA:LAST:0.5:60:744",
@last,
);
};
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'");
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) {
my $ip;
if($traffacct->{desc}->{$n}) {
$ip = trim((split(',', $traffacct->{desc}->{$n}))[0]);
}
if(!$ip) {
if(!gethostbyname($name)) {
logger("WARNING: Unable to resolve '" . $name . "'. Check your DNS.");
}
$ip = inet_ntoa((gethostbyname($name))[4]);
$ip = $ip . "/32";
}
open(IN, "iptables -t $table -nxvL monitorix_daily_$name 2>/dev/null |");
my @data = <IN>;
close(IN);
if(!scalar(@data)) {
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}");
}
}
}
}
# 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};
my $table = $config->{ip_default_table};
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) {
my $ip;
if($traffacct->{desc}->{$n}) {
$ip = trim((split(',', $traffacct->{desc}->{$n}))[0]);
}
if(!$ip) {
if(!gethostbyname($name)) {
logger("WARNING: Unable to resolve '" . $name . "'. Check your DNS.");
}
$ip = inet_ntoa((gethostbyname($name))[4]);
}
$ip =~ s/\/\d+//;
open(IN, "iptables -t $table -nxvL monitorix_daily_$name |");
$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) = @_;