Skip to content

Commit

Permalink
Initial logging work done, log4perl is working
Browse files Browse the repository at this point in the history
  • Loading branch information
skarfacegc committed Sep 5, 2012
1 parent 010ac6a commit 99b53ac
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 46 deletions.
20 changes: 14 additions & 6 deletions FT/Configuration.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package FT::Configuration;
use strict;
use warnings;
use Log::Log4perl qw(get_logger);

use Data::Dumper;
use Carp;
use YAML;
Expand All @@ -10,16 +12,23 @@ my $oneTrueSelf;

sub setConf
{
if(!defined $oneTrueSelf)

if ( !defined $oneTrueSelf )
{
my $config_file = shift();
my $config_struct;
my $ret_struct;
my $logger;

$logger = get_logger();


$config_file = defined($config_file) ? $config_file : "./flowTrack.conf";

croak "Couldn't read " . $config_file if(!-r $config_file );
if ( !-r $config_file )
{
$logger->fatal("Couldn't read " . $config_file);
die;
}

$config_struct = YAML::LoadFile($config_file) or croak "Error parsing " . $config_file;

Expand All @@ -30,15 +39,14 @@ sub setConf
return $oneTrueSelf;
}


sub getConf
{
if(!defined($oneTrueSelf))
if ( !defined($oneTrueSelf) )
{
croak "Config not loaded.";
}

return $oneTrueSelf;
}

1;
1;
9 changes: 7 additions & 2 deletions FT/FlowCollector.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
package FT::FlowCollector;
use strict;
use warnings;
use Log::Log4perl qw(get_logger);
use Carp;

use Data::Dumper;

use Net::Flow qw(decode);
use FT::PacketHandler;
use FT::FlowTrack;
Expand Down Expand Up @@ -65,6 +68,7 @@ sub child_finish_hook
{
my $self = shift();
my $flow_data = $self->{data}{flow_data};
my $logger = get_logger();

# Load the FT object if we need to
if(!defined $FT)
Expand All @@ -73,7 +77,7 @@ sub child_finish_hook
$FT = FT::FlowTrack->new( $config->{data_dir}, 1, $config->{dbname}, $config->{internal_network} );
}

carp "Collector Cleanup";
$logger->debug("Collector Cleanup");

# carp Dumper($self->{data}{flow_data});
$FT->storeFlow($flow_data) if ( defined($flow_data) );
Expand All @@ -89,10 +93,11 @@ sub child_finish_hook
sub process_request
{
my $self = shift();
my $logger = get_logger();

my $flow_data = FT::PacketHandler::decode_packet( $self->{server}{udp_data} );

carp "Store Count: " . scalar( @{$flow_data} );
$logger->debug("Store Count: " . scalar( @{$flow_data} ));

push( @{ $self->{data}{flow_data} }, @$flow_data );

Expand Down
48 changes: 28 additions & 20 deletions FT/FlowTrack.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use v5.10;
use Carp;
use strict;
use warnings;
use Log::Log4perl qw(get_logger);

use DBI;
use Data::Dumper;
Expand All @@ -14,7 +15,6 @@ use Net::IP;
use Socket; # For inet_ntoa
use vars '$AUTOLOAD';


#
# Constructor
#
Expand Down Expand Up @@ -58,8 +58,8 @@ sub storeFlow
{
my ( $self, $flows ) = @_;
my $insert_struct;

my $insert_queue;
my $logger = get_logger();
my $batch_counter = 0;
my $total_saved = 0;
my $batch_size = 100;
Expand Down Expand Up @@ -107,7 +107,7 @@ sub storeFlow
$total_saved += $rows_saved;
}

carp "Saved: $total_saved";
$logger->debug("Saved: $total_saved");
return;
}

Expand Down Expand Up @@ -181,7 +181,8 @@ sub getIngressFlowsInTimeRange
{
my $self = shift();
my ( $start_time, $end_time ) = @_;
my $dbh = $self->_initDB();
my $logger = get_logger();
my $dbh = $self->_initDB();
my $ret_list;

my $internal_network = new Net::IP( $self->{internal_network} );
Expand All @@ -195,14 +196,14 @@ sub getIngressFlowsInTimeRange
dst_ip BETWEEN ? AND ?
};

my $sth = $dbh->prepare($sql) or croak( "failed to prepare:" . $DBI::errstr );
my $sth = $dbh->prepare($sql) or $logger->fatal( "failed to prepare:" . $DBI::errstr );

$sth->execute( $start_time, $end_time,
$internal_network->intip(),
$internal_network->last_int(),
$internal_network->intip(),
$internal_network->last_int() )
or croak( "failed executing $sql:" . $DBI::errstr );
or $logger->fatal( "failed executing $sql:" . $DBI::errstr );

while ( my $ref = $sth->fetchrow_hashref )
{
Expand Down Expand Up @@ -230,9 +231,12 @@ sub getEgressFlowsInTimeRange
{
my $self = shift();
my ( $start_time, $end_time ) = @_;
my $dbh = $self->_initDB();
my $dbh = $self->_initDB();
my $logger = get_logger();
my $ret_list;

$logger = get_logger();

my $internal_network = new Net::IP( $self->{internal_network} );

my $sql = qq{
Expand All @@ -244,14 +248,14 @@ sub getEgressFlowsInTimeRange
dst_ip NOT BETWEEN ? AND ?
};

my $sth = $dbh->prepare($sql) or croak( "failed to prepare:" . $DBI::errstr );
my $sth = $dbh->prepare($sql) or $logger->fatal( "failed to prepare:" . $DBI::errstr );

$sth->execute( $start_time, $end_time,
$internal_network->intip(),
$internal_network->last_int(),
$internal_network->intip(),
$internal_network->last_int() )
or croak( "failed executing $sql:" . $DBI::errstr );
or $logger->fatal( "failed executing $sql:" . $DBI::errstr );

while ( my $ref = $sth->fetchrow_hashref )
{
Expand All @@ -263,28 +267,28 @@ sub getEgressFlowsInTimeRange

sub purgeData
{
my $self = shift();
my $dbh = $self->_initDB();
my $self = shift();
my $dbh = $self->_initDB();
my $logger = get_logger();

my $conf = FT::Configuration::getConf();

my $watermark = time - $conf->{purge_interval};
my $watermark = time - $conf->{purge_interval};
my $rows_deleted = 0;

my $sql = qq{
DELETE FROM raw_flow WHERE fl_time < ?
};

my $sth = $dbh->prepare($sql) or croak( "failed to prepare:" . $DBI::errstr );
my $sth = $dbh->prepare($sql) or $logger->fatal( "failed to prepare:" . $DBI::errstr );

$rows_deleted = $sth->execute($watermark) or carp ("Delete failed: ". $DBI::errstr);
$rows_deleted = $sth->execute($watermark) or $logger->fatal( "Delete failed: " . $DBI::errstr );

carp "Purged: $rows_deleted" if ($rows_deleted > 0);
$logger->debug("Purged: $rows_deleted") if ( $rows_deleted > 0 );

return;
}


#
# This routine cleans up a single FlowRecord (select * from the raw_flow table)
# takes a hashref representing a single record from the raw_flow table;
Expand Down Expand Up @@ -338,6 +342,7 @@ sub processFlowRecord
sub _initDB
{
my ($self) = @_;
my $logger = get_logger();

my $db_name = $self->{dbname};

Expand All @@ -364,7 +369,8 @@ sub _initDB
else
{

croak( "_initDB failed: $dbfile" . $DBI::errstr );
$logger->fatal( "_initDB failed: $dbfile" . $DBI::errstr );
die;
}
}
}
Expand All @@ -383,6 +389,7 @@ sub _createTables
{
my ($self) = @_;
my $tables = [qw/raw_flow/];
my $logger = get_logger();

foreach my $table (@$tables)
{
Expand All @@ -393,15 +400,17 @@ sub _createTables

if ( !defined($sql) || $sql eq "" )
{
croak("Couldn't create SQL statement for $table");
$logger->fatal("Couldn't create SQL statement for $table");
die;
}

my $sth = $dbh->prepare($sql);
my $rv = $sth->execute();

if ( !defined($rv) )
{
croak($DBI::errstr);
$logger->fatal($DBI::errstr);
die;
}
}
}
Expand Down Expand Up @@ -442,7 +451,6 @@ sub _checkDirs
return;
}


#
# So we can passthrough calls to the Schema routines
#
Expand Down
2 changes: 2 additions & 0 deletions FT/FlowTrackWeb.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package FT::FlowTrackWeb;

use strict;
use warnings;
use Log::Log4perl qw(get_logger);
use Carp;

use Mojo::Base 'Mojolicious';
use Mojolicious::Static;
use Data::Dumper;
Expand Down
1 change: 1 addition & 0 deletions FT/FlowTrackWeb/Main.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package FT::FlowTrackWeb::Main;
use strict;
use warnings;
use Log::Log4perl qw(get_logger);
use Carp;

use FT::FlowTrack;
Expand Down
3 changes: 2 additions & 1 deletion FT/PacketHandler.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
# Migrated from FlowTrack.pl
package FT::PacketHandler;
use strict;
use Carp;
use warnings;
use Log::Log4perl qw(get_logger);
use Carp;
use Time::HiRes;

#
Expand Down
1 change: 1 addition & 0 deletions FT/Reporting.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package FT::Reporting;
use strict;
use warnings;
use Log::Log4perl qw(get_logger);
use RRD::Simple;

sub new
Expand Down
6 changes: 5 additions & 1 deletion FT/Schema.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
# Also has routines to provide an API to access the above.

package FT::Schema;
use v5.10;

use strict;
use warnings;
use v5.10;
use Log::Log4perl qw(get_logger);
use Carp;

use Data::Dumper;

use vars '$TABLES';
Expand Down
Loading

0 comments on commit 99b53ac

Please sign in to comment.