Feb 19
- Change language to:
Below is a short little script that i wrote with some help of some FreeBSD Perl developers (Thanks mat and siks).
The script should be runned with root permissions, sudo could be used for that:
For example with the follwing information in /usr/local/etc/sudoers (or where your configfile lives)
User_Alias MRTG = mrtg
MRTG NOPASSWD: /sbin/pfctl -si
The script will run /sbin/pfctl -si which will give you statistical output which can be used for processing by mrtg after running pf.pl. Do note that the current markup might not fit on the script. Please click on the Plain text button to see the original script in a new window, which will fit.
PERL:
-
#!/usr/bin/perl -w
-
-
# $Id: pf-to-mrtg.pl 831 2008-09-07 13:45:27Z remko $
-
#
-
# (c) 2005 Remko Lodder <remko@elvandar.org>
-
# (c) 2008 Remko Lodder <remko@FreeBSD.org>
-
# Based upon ipf.pl by Ronald Florence <ron@18james.com>
-
# last modified remko@elvandar.org, 05 Sept 2009
-
# Made the script working again.
-
-
# Example output from pfctl -si
-
#Interface Stats for fxp0 IPv4 IPv6
-
# Bytes In 156041 0
-
# Bytes Out 1895602 0
-
# Packets In
-
# Passed 1504 0
-
# Blocked 256 0
-
# Packets Out
-
# Passed 1756 0
-
# Blocked 0 0
-
#
-
#State Table Total Rate
-
# current entries 33
-
# searches 8865 10.7/s
-
# inserts 204 0.2/s
-
# removals 171 0.2/s
-
#Counters
-
# match 460 0.6/s
-
# bad-offset 0 0.0/s
-
# fragment 0 0.0/s
-
# short 0 0.0/s
-
# normalize 0 0.0/s
-
# memory 0 0.0/s
-
-
use strict;
-
use Getopt::Std;
-
-
# prototype variables.
-
my($inp,$outp) = 0;
-
-
my ($out,$in,$uptime,$firewall);
-
my(@pfctl);
-
my(%option);
-
-
# define some variables upfront
-
my $scriptname = __FILE__;
-
my $version = "2.0";
-
-
# Index our options
-
getopts("bBfimMnpsS", \%option);
-
-
my $pass_flag = 1 if $option{p};
-
my $block_flag = 1 if $option{b};
-
my $bytes_flag = 1 if $option{B};
-
my $search_flag = 1 if $option{S};
-
my $inres_flag = 1 if $option{i};
-
my $frag_flag = 1 if $option{f};
-
my $short_flag = 1 if $option{n};
-
my $memory_flag = 1 if $option{M};
-
-
@pfctl=`/sbin/pfctl -si`;
-
-
foreach my $packetfilter (@pfctl) {
-
if ($pass_flag || $block_flag) {
-
if ($packetfilter =~ /Packets In/) { $in = 1; $out = 0; }
-
if ($packetfilter =~ /Packets Out/) { $in = 0; $out = 1; }
-
if ($pass_flag) {
-
}
-
if ($block_flag) {
-
}
-
}
-
-
elsif ($bytes_flag) {
-
}
-
elsif ($state_flag) {
-
$outp = 0;
-
}
-
elsif ($search_flag) {
-
}
-
elsif ($inres_flag) {
-
}
-
elsif ($match_flag) {
-
}
-
elsif ($frag_flag) {
-
}
-
elsif ($short_flag) {
-
}
-
elsif ($memory_flag) {
-
}
-
else {
-
\t-b\tIndex blocked packets incoming and outgoing.
-
\t-B\tIndex bytes incoming and outgoing
-
\t-f\tIndex bad-offsets and fragments
-
\t-i\tIndex inserts and removals
-
\t-m\tIndex matched packets current values and usage rate
-
\t-M\tIndex memory usage current values and usage rate
-
\t-n\tIndex short packets and normalized packets
-
\t-p\tIndex passed packets incoming and outgoing.
-
\t-s\tIndex states other value will be zero (0)
-
\t-S\tIndex searches current searches and search rate
-
Version: $version
-
Originally written by Remko Lodder <remko\@FreeBSD.org>.\n");
-
}
-
}
-
-
print "$inp\n",
-
"$outp\n";
-
-
$_ = `/usr/bin/uptime`;
-
-
print "$uptime\n",
-
-
__END__
-
-
=head1 NAME
-
-
pf-to-mrtg.pl - generates statistics for use with MRTG
-
-
=head1 SYNOPSIS
-
-
Two example configuration options for MRTG:
-
-
Target[connections]: `/path/to/pf-to-mrtg.pl -p`
-
Options[connections]: growright, perhour
-
MaxBytes[connections]: 50000
-
Title[connections]: Firewall Connections
-
PageTop[connections]: <h3>Firewall Connections</h3>
-
YLegend[connections]: packets/hr
-
ShortLegend[connections]: pkts/h
-
Legend1[connections]: Incoming Connections
-
Legend2[connections]: Outgoing Connections
-
-
Options[state]: growright, gauge, integer
-
MaxBytes[state]: 2048
-
Title[state]: State Table
-
PageTop[state]: <h3>NAT & IP state tables</h3>
-
YLegend[state]: states
-
ShortLegend[state]: states
-
Legend1[state]: NAT states
-
Legend2[state]: IP states
-
LegendI[state]: nat:
-
LegendO[state]: ip:
-
-
Other available targets are `pf-to-mrtg.pl -b`
-
`pf-to-mrtg.pl -B`
-
`pf-to-mrtg.pl -f`
-
`pf-to-mrtg.pl -i`
-
`pf-to-mrtg.pl -m`
-
`pf-to-mrtg.pl -M`
-
`pf-to-mrtg.pl -n`
-
`pf-to-mrtg.pl -p`
-
`pf-to-mrtg.pl -s`
-
`pf-to-mrtg.pl -S`
-
-
See the instructions in the help to see what they represent.
-
-
=head1 DESCRIPTION
-
-
The purpose of this script is to be able to create statistics
-
from various PF counters. By using this script you will be
-
able to see what your firewall does, how it behaves, and see
-
trends. Note: Due to MRTG's nature you will get spikes after
-
rebooting the machine.
-
=head1 LICENSE
-
This module is licensed under the BSD license.
-
=head1 AUTHOR
-
Remko Lodder <remko@FreeBSD.org> with additional help from
-
Mattieu Arnold <mat@FreeBSD.org> and Pasi Hirvonen.
-
Originally based on work from Ron James (before this was entirely rewritten).
-
=cut

