3.0: updated flush_accounting_rules() to 3.0

This commit is contained in:
Jordi Sanfeliu 2012-12-14 12:06:28 +01:00
parent af5a63479f
commit c5aa5f4198
1 changed files with 58 additions and 1 deletions

View File

@ -23,7 +23,7 @@ package Monitorix;
use strict;
use warnings;
use Exporter 'import';
our @EXPORT = qw(logger trim get_nvidia_data);
our @EXPORT = qw(logger trim get_nvidia_data flush_accounting_rules);
sub logger {
my ($msg) = @_;
@ -158,4 +158,61 @@ sub get_nvidia_data {
return join(" ", $mem, $cpu, $temp);
}
# flushes out any Monitorix iptables/ipfw rules
sub flush_accounting_rules {
my ($config, $debug) = @_;
if($config->{os} eq "Linux") {
my $num = 0;
logger("Flushing out iptables rules.") if $debug;
if(open(IN, "iptables -nxvL INPUT --line-numbers |")) {
my @rules;
my @names;
while(<IN>) {
my ($rule, undef, undef, $name) = split(' ', $_);
if($name =~ /monitorix_IN/ || /monitorix_nginx_IN/) {
push(@rules, $rule);
push(@names, $name);
}
}
close(IN);
@rules = reverse(@rules);
foreach(@rules) {
system("iptables -D INPUT $_");
$num++;
}
foreach(@names) {
system("iptables -X $_");
}
}
if(open(IN, "iptables -nxvL OUTPUT --line-numbers |")) {
my @rules;
my @names;
while(<IN>) {
my ($rule, undef, undef, $name) = split(' ', $_);
if($name =~ /monitorix_OUT/ || /monitorix_nginx_OUT/) {
push(@rules, $rule);
push(@names, $name);
}
}
close(IN);
@rules = reverse(@rules);
foreach(@rules) {
system("iptables -D OUTPUT $_");
$num++;
}
foreach(@names) {
system("iptables -X $_");
}
}
logger("$num iptables rules have been flushed.") if $debug;
}
if(grep {$_ eq $config->{os}} ("FreeBSD", "OpenBSD", "NetBSD")) {
logger("Flushing out ipfw rules.") if $debug;
system("ipfw delete $config->{port}->{rule} 2>/dev/null");
system("ipfw delete $config->{nginx}->{rule} 2>/dev/null");
}
}
1;