[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
pflabels to html
Hi
I have just started to write a little perl script which convert the
output from pfctl -s labels to html with some real basic bars.
Have this been done before? So I don't have to "reinvent the wheel".
I have attach my script below, I run with "sudo pfctl -s labels | perl
pflabels.pl > pflabels.html".
Feel free to come with any sugestions and don't kill me for the ugly
code. :)
BR -Andreas
--
( _ )
~O o~__ "I'm a dreaming man, yes that's my problem.
(._.) |\ I cant tell when i'm not being real." - Neil Young
____|_|_|______________________________________________________________
home:andreas.burmester@chello.se|work:andreas.burmester@era.ericsson.se
use strict;
use CGI qw(:standard escapeHTML);
print start_html("pflabels.pl"), p();
my @arr = <STDIN>;
print
pflabel_table(\@arr,{bgcolor=>"green",date=>"2002-06-01",width=>"300",font=>"+1"});
print end_html();
# <html_table> = pflabel_table(<arrayref_to_pfctl_-s_labels_stdout>,
<hashref_to_settings>)
#
# settings:
# font=>font used in table (default -1)
# bgcolor=>bar color (default red)
# width=>table width (default 150)
# date=>date in table caption (default today =)
#
# history:
# 2002-06-01 - andreas.burmester@chello.se - Initial version
sub pflabel_table {
my ($input_ref, $setup_ref) = @_;
my ($label_ref, $highest) = &read_input($input_ref);
my %label = %$label_ref;
my %setup = (bgcolor=>"red",date=>"today",width=>"150",font=>"-1"); #
default settings
my @row;
if($setup_ref) { %setup = (%setup, %$setup_ref) }; # overwrite default
settings
foreach (sort keys %label) {
my $width = ($label{$_} / $highest) * ($setup{"width"}-50);
$width =~ s/\..*//; # make float to int, real ugly
push(@row,th(font({-size=>$setup{"font"}},$_)) .
td(table({-width=>$setup{"width"}}, # create table within table for
the bars
Tr(
td({-width=>50,-align=>"center"},font({-size=>$setup{"font"}},$label{$_})), # no of packets
td({-width=>$width,-bgcolor=>$setup{"bgcolor"}},font({-color=>$setup{"bgcolor"}},".")), # real bar
td({-width=>(($setup{"width"}-50)-$width)}) # fill-out bar
)
) ) );
}
return table({-border=>1,-cellpadding=>2},
caption(strong(font({-size=>$setup{"font"}},"Incoming Packet
Spread @ " . $setup{"date"}))),
Tr(\@row)), p();
}
sub read_input {
my ($array_ref) = @_;
my %label;
my $highest = 0;
foreach (@$array_ref) {
my @line = split(/ /, $_);
$label{$line[0]} += $line[2]; # change $line[2] to get something else
insted of packets
if($line[2]>$highest) {
$highest = $line[2];
}
}
return (\%label, $highest);
}