#!/usr/bin/perl
# Version: 1.1

# This is a script to use with mrtg to count shorewall entrys in the log.
#
# Updates can be found at: http://ftp.shorewall.net/pub/shorewall/contrib/
# Made by Rickard Eriksson riceri@home.se

#
# Here you can see what i have in my mrtg config to use this script.
#
#  ######################################################################
#  #                                                                    #
#  # Rejected connections from you /var/log/messages file               #
#  #                                                                    #
#  ######################################################################
#  Target[Hits]: `perl /path/to/hits.pl -reject`;
#  Options[Hits]: gauge, noinfo, growright, transparent, nopercent, noo
#  WithPeak[Hits]: ymw
#  MaxBytes[Hits]: 10000
#  Colours[Hits]: GREEN#00eb0c,BLUE#1000ff,DARK GREEN#006600,VIOLET#ff00ff
#  YLegend[Hits]: Hits
#  ShortLegend[Hits]: Hits
#  Legend1[Hits]: Hits
#  Legend2[Hits]: Hits
#  LegendI[Hits]: Hits:
#  LegendO[Hits]: Hits:
#  Timezone[Hits]: GMT
#  Title[Hits]: Connections rejected
#  PageTop[Hits]: <H1>Connections rejected</H1>
#  ######################################################################

## Start of config

# Path to Logfile:
my $Logfile = "/var/log/messages";

## End of config

use Time::Local;

if (@ARGV < 1) {
  print "Usage: $0 [-drop][-reject][-both]\n";
  exit;
}




my $Time = time - 300;
my %Month = ("Jan" => 0, "Feb" => 1, "Mar" => 2, "Apr" => 3, "Maj" => 4, "Jun" => 5,
             "jan" => 0, "feb" => 1, "mar" => 2, "apr" => 3, "maj" => 4, "jun" => 5,
             "Jul" => 6, "Aug" => 7, "Sep" => 8, "Oct" => 9, "Nov" => 10, "Dec" => 11,
             "jul" => 6, "aug" => 7, "sep" => 8, "oct" => 9, "nov" => 10, "dec" => 11);
my $Count = 0;
open (LOG,$Logfile) or die "Can't find file: $Logfile\n";
  while (<LOG>) {
    my ($Sec,$Min,$Hour,$Mday,$Mon,$Year) = localtime(time);
    $Sec = substr $_, 13, 2;
    $Min = substr $_, 10, 2;
    $Hours = substr $_, 7, 2;
    $Mday = substr $_, 4, 2;
    $Mon = $Month{substr $_, 0, 3};
    $Year += 1900;
    my $Time2 = timelocal($Sec,$Min,$Hours,$Mday,$Mon,$Year);
    if ($Time < $Time2) {
      my @Line = split(/: /,$_);
      if (substr($Line[1],0,10) eq "Shorewall:") {
        my @Line2 = split(/:/,$Line[1]);
        if (($ARGV[0] eq "-reject") || ($ARGV[0] eq "-both")) {
          if ($Line2[2] eq "REJECT") {
            $Count++;
          }
        }
        if (($ARGV[0] eq "-drop") || ($ARGV[0] eq "-both")) {
          if ($Line2[2] eq "DROP") {
            $Count++;
          }
        }
      }
    }
  }
close LOG;

print "$Count\n0\n1\nHits\n";
print STDERR "$Count\n0\n1\nHits\n";