#!/usr/bin/perl ### Settings # Path to ipfw (can be found using: locate ipfw) my $ipfw = "/sbin/ipfw"; # Hostname for your box, can be automatically generated with the value `hostname` my $hostname = `hostname`; ### Real Program, better do not touch the code unless ( @ARGV ) { die "Usage: fwstatreader.pl "; } $inrule = "$ARGV[0]"; $outrule = "$ARGV[1]"; $method = "$ARGV[2]"; open (FWSTAT, "$ipfw show $inrule |") || die "Can't exec firewall stats\n"; while () { if ($_ =~ /^$inrule/) { $_ =~ s/\W+/ /g; @data = split /\s/, $_; if ( $method eq "bytes" ) { $countin = $data[2]; } elsif ( $method eq "packets" ) { $countin = $data[1]; } } else { print "ERROR: line should begin with $inrule, but was $_\n"; } } close FWSTAT; open (FWSTAT, "$ipfw show $outrule |") || die "Can't exec firewall stats\n"; while () { if ($_ =~ /^$outrule/) { $_ =~ s/\W+/ /g; @data = split /\s/, $_; if ( $method eq "bytes" ) { $countout = $data[2]; } elsif ( $method eq "packets" ) { $countout = $data[1]; } } else { print "ERROR: line should begin with $outrule, but was $_\n"; } } close FWSTAT; @uptime = split /\s/, `uptime`; $running = ""; $start = 0; foreach $line (@uptime) { if ($start == 1) { $running = $running." ".$line; if ($line eq "users," || $line eq "user,") { $start = 0; @uptime = split /,/, $running; my $count = @uptime; $running = $uptime[0]; for($i=1;$i<$count-1;$i++) { $uptime[$i] =~ s/^\s+//; $running = $running.", ".$uptime[$i]; } $running =~ s/^\s+//; } } elsif ($line eq "up") { $start = 1; } } print "$countin\n"; print "$countout\n"; print "$running\n"; print "$hostname\n"; 1;