#!/usr/bin/perl ###### # ipfw2mrtg: feed ipfw ip counters to mrtg by giving 4 arguments: # - rule number for Incomming traffic # - the occurence for that rule (1 if it is only one time used) # - rule number for Outgoing traffic # - the occurence for that rule (1 if it is only one time used) # Author: Jimmy Scott # Nick: Sick` # Mail: jimmy *at* inet-solutions.be # Address: Jimmy Scott # Goordijk 66 # 2930 BRASSCHAAT # BELGIUM # source: http://pub.devbox.be ### # # Copyright (C) 2003 Jimmy Scott # # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # 3. The names of the authors may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF # THE POSSIBILITY OF SUCH DAMAGE. # ###### use strict; my ($ruleI,$ruleO,$occuI,$occuO,$comm,@ipactlist,$ipfw,$ipfw_record); $ruleI=shift @ARGV || die "no rule (I)\n"; $occuI=shift @ARGV || die "no occurrence (I)\n"; $ruleO=shift @ARGV || die "no rule (O)\n"; $occuO=shift @ARGV || die "no occurrence (O)\n"; $comm=join " ", @ARGV; $ipfw="/sbin/ipfw"; @ipactlist=`$ipfw -a l || echo FAILURE`; if ( $ipactlist[0] =~ /FAILURE/ ) { die "failed to run 'ipfw -a l'\n"; } my ($found,$found1,$found2,$rule_now,$rule_prev,$occu_now,$ipfw_byteI,$ipfw_byteO,$ipfw_byte); $occu_now=0; SEARCH: foreach $ipfw_record (@ipactlist) { chomp $ipfw_record; $rule_prev=$rule_now; if ( $ipfw_record =~ /^0*(\d+)\s+\d+\s+(\d+)\s+.+/ ) { $rule_now=$1; $ipfw_byte=$2; } else { die "invalid ruleset detected\n"; } if (($rule_now != $ruleI) && ($rule_now != $ruleO)) { $occu_now=0; next SEARCH; } elsif ($rule_now == $rule_prev) { $occu_now++; } else { $occu_now=1; } if (($occu_now == $occuI) && ($rule_now == $ruleI)) { $found1=1; $ipfw_byteI=$ipfw_byte; } if (($occu_now == $occuO) && ($rule_now == $ruleO)) { $found2=1; $ipfw_byteO=$ipfw_byte; } if ( $found1 && $found2 ) { $found=1; last SEARCH; } } if ( $found ) { print "$ipfw_byteI\n"; print "$ipfw_byteO\n"; print "0\n"; print "$comm\n"; } else { die "Input rule, occurrence not found\n" unless defined $ipfw_byteI; die "Output rule, occurrence not found\n" unless defined $ipfw_byteO; die "Error in ruleset\n"; }