########################################### # Mike Schilli, 2006 (m@perlmeister.com) ########################################### package WatchLAN; ########################################### use strict; use Apache::DBI; # share a single DB conn use Rose::DB::Object::Loader; use Log::Log4perl qw(:easy); use DateTime; my $loader = Rose::DB::Object::Loader->new( db_dsn => 'dbi:mysql:dbname=watchlan', db_username => 'root', db_password => undef, db_options => { AutoCommit => 1, RaiseError => 1 }, class_prefix => 'WatchLAN' ); $loader->make_classes(); ########################################### sub new { ########################################### my ($class) = @_; my $self = { cache => {}, flush_interval => 60, next_update => undef, }; bless $self, $class; $self->cache_flush(); return $self; } ########################################### sub event_add { ########################################### my($self, $mac, $ip)= @_; $self->{cache}->{"$mac,$ip"}++; $self->cache_flush() if time() > $self->{next_update}; } ########################################### sub cache_flush { ########################################### my ($self) = @_; for my $key ( keys %{ $self->{cache} } ){ my ($mac, $ip) = split /,/, $key; my $counter = $self->{cache}->{$key}; my $minute = DateTime->from_epoch( epoch => $self->{next_update} - $self->{flush_interval}, time_zone => "local", ); my $activity = WatchLAN::Activity->new( minute => $minute); $activity->device( { mac_address => $mac } ); $activity->ip_address( { string => $ip } ); $activity->counter($counter); $activity->save(); } $self->{cache} = {}; $self->{next_update} = time() - ( time() % $self->{flush_interval} ) + $self->{flush_interval}; } ########################################### sub device_add { ########################################### my ( $self, $name, $mac_address ) = @_; my $device = WatchLAN::Device->new( mac_address => $mac_address ); $device->load( speculative => 1 ); $device->name($name); $device->save(); } 1;