Changeset 74


Ignore:
Timestamp:
Nov 8, 2010, 10:50:46 AM (13 years ago)
Author:
g7moreau
Message:
  • Add cleandb command to delete duplicate entry when time date are different
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/klask

    r73 r74  
    5454   searchdb   => \&cmd_searchdb,
    5555   removedb   => \&cmd_removedb,
     56   cleandb   => \&cmd_cleandb,
    5657   search     => \&cmd_search,
    5758   enable     => \&cmd_enable,
     
    859860   mkdir "$dirdb", 0755 unless -d "$dirdb";
    860861   YAML::Syck::DumpFile("$KLASK_DB_FILE", $computerdb);
     862   return;
     863   }
     864
     865sub cmd_cleandb {
     866   my @options = @_;
     867
     868   my $days_to_clean = 15;
     869   my $verbose;
     870   my $database_has_changed;
     871
     872   my $ret = GetOptionsFromArray(\@options,
     873      'day|d=i'   => \$days_to_clean,
     874      'verbose|v' => \$verbose,
     875      );
     876
     877   my @vlan_name = get_list_network();
     878
     879   my $computerdb = {};
     880      $computerdb = YAML::Syck::LoadFile("$KLASK_DB_FILE") if -e "$KLASK_DB_FILE";
     881   my $timestamp = time;
     882
     883   my $timestamp_barrier = 3600 * 24 * $days_to_clean;
     884
     885   ALL_VLAN:
     886   for my $vlan (@vlan_name) {
     887
     888      my @ip_list   = get_list_ip($vlan);
     889      my %mactimedb = ();
     890     
     891      LOOP_ON_IP_ADDRESS:
     892      for my $ip (@ip_list) {
     893
     894         next LOOP_ON_IP_ADDRESS if
     895            not exists $computerdb->{$ip};
     896           
     897            #&& $computerdb->{$ip}{timestamp} > $timestamp_barrier;
     898         my $ip_timestamp = $computerdb->{$ip}{timestamp};
     899         my $ip_mac       = $computerdb->{$ip}{mac_address};
     900         
     901         $mactimedb{$ip_mac} ||= [ $ip, $ip_timestamp ];
     902         
     903         if ( $mactimedb{$ip_mac}->[1] - $ip_timestamp > $timestamp_barrier ) {
     904            print "remove ip $ip\n" if $verbose;
     905            delete $computerdb->{$ip};
     906            $database_has_changed++;
     907            }
     908
     909         if ( $ip_timestamp - $mactimedb{$ip_mac}->[1] > $timestamp_barrier ) {
     910            print "remove ip ".$mactimedb{$ip_mac}->[0]."\n" if $verbose;
     911            delete $computerdb->{$mactimedb{$ip_mac}->[0]};
     912            $database_has_changed++;
     913            }
     914
     915         if ( $ip_timestamp > $mactimedb{$ip_mac}->[1]) {
     916            $mactimedb{$ip_mac} = [ $ip, $ip_timestamp ];
     917            }
     918         }
     919      }
     920
     921   if ( $database_has_changed ) {
     922      my $dirdb = $KLASK_DB_FILE;
     923         $dirdb =~ s{ / [^/]* $}{}xms;
     924      mkdir "$dirdb", 0755 unless -d "$dirdb";
     925      YAML::Syck::DumpFile("$KLASK_DB_FILE", $computerdb);
     926      }
    861927   return;
    862928   }
Note: See TracChangeset for help on using the changeset viewer.