diff --git a/bin/netdisco-sshcollector b/bin/netdisco-sshcollector index 8792d4c6..ccf12357 100755 --- a/bin/netdisco-sshcollector +++ b/bin/netdisco-sshcollector @@ -40,8 +40,7 @@ BEGIN { } use App::Netdisco; -use App::Netdisco::Core::Arpnip 'store_arp'; -use App::Netdisco::Util::Node 'check_mac'; +use App::Netdisco::Util::Node qw/check_mac store_arp/; use App::Netdisco::Util::FastResolver 'hostnames_resolve_async'; use Dancer ':script'; diff --git a/lib/App/Netdisco/Util/Node.pm b/lib/App/Netdisco/Util/Node.pm index d098dda0..ca6d4eed 100644 --- a/lib/App/Netdisco/Util/Node.pm +++ b/lib/App/Netdisco/Util/Node.pm @@ -11,6 +11,7 @@ our @EXPORT = (); our @EXPORT_OK = qw/ check_mac is_nbtstatable + store_arp /; our %EXPORT_TAGS = (all => \@EXPORT_OK); @@ -141,4 +142,54 @@ sub is_nbtstatable { return 1; } +=head2 store_arp( \%host, $now? ) + +Stores a new entry to the C table with the given MAC, IP (v4 or v6) +and DNS host name. Host details are provided in a Hash ref: + + { + ip => '192.0.2.1', + node => '00:11:22:33:44:55', + dns => 'myhost.example.com', + } + +The C entry is optional. The update will mark old entries for this IP as +no longer C. + +Optionally a literal string can be passed in the second argument for the +C timestamp, otherwise the current timestamp (C) is used. + +=cut + +sub store_arp { + my ($hash_ref, $now) = @_; + $now ||= 'now()'; + my $ip = $hash_ref->{'ip'}; + my $mac = NetAddr::MAC->new($hash_ref->{'node'}); + my $name = $hash_ref->{'dns'}; + + return if !defined $mac or $mac->errstr; + + schema('netdisco')->txn_do(sub { + my $current = schema('netdisco')->resultset('NodeIp') + ->search( + { ip => $ip, -bool => 'active'}, + { columns => [qw/mac ip/] })->update({active => \'false'}); + + schema('netdisco')->resultset('NodeIp') + ->update_or_create( + { + mac => $mac->as_ieee, + ip => $ip, + dns => $name, + active => \'true', + time_last => \$now, + }, + { + key => 'primary', + for => 'update', + }); + }); +} + 1; diff --git a/lib/App/Netdisco/Worker/Plugin/Arpnip/Nodes.pm b/lib/App/Netdisco/Worker/Plugin/Arpnip/Nodes.pm index 4c5e82a4..a9620376 100644 --- a/lib/App/Netdisco/Worker/Plugin/Arpnip/Nodes.pm +++ b/lib/App/Netdisco/Worker/Plugin/Arpnip/Nodes.pm @@ -5,11 +5,10 @@ use App::Netdisco::Worker::Plugin; use aliased 'App::Netdisco::Worker::Status'; use App::Netdisco::Transport::SNMP (); -use App::Netdisco::Util::Node 'check_mac'; +use App::Netdisco::Util::Node qw/check_mac store_arp/; use App::Netdisco::Util::FastResolver 'hostnames_resolve_async'; use Dancer::Plugin::DBIC 'schema'; use Time::HiRes 'gettimeofday'; -use NetAddr::MAC (); register_worker({ phase => 'main', driver => 'snmp' }, sub { my ($job, $workerconf) = @_; @@ -64,54 +63,4 @@ sub get_arps { return $resolved_ips; } -=head2 store_arp( \%host, $now? ) - -Stores a new entry to the C table with the given MAC, IP (v4 or v6) -and DNS host name. Host details are provided in a Hash ref: - - { - ip => '192.0.2.1', - node => '00:11:22:33:44:55', - dns => 'myhost.example.com', - } - -The C entry is optional. The update will mark old entries for this IP as -no longer C. - -Optionally a literal string can be passed in the second argument for the -C timestamp, otherwise the current timestamp (C) is used. - -=cut - -sub store_arp { - my ($hash_ref, $now) = @_; - $now ||= 'now()'; - my $ip = $hash_ref->{'ip'}; - my $mac = NetAddr::MAC->new($hash_ref->{'node'}); - my $name = $hash_ref->{'dns'}; - - return if !defined $mac or $mac->errstr; - - schema('netdisco')->txn_do(sub { - my $current = schema('netdisco')->resultset('NodeIp') - ->search( - { ip => $ip, -bool => 'active'}, - { columns => [qw/mac ip/] })->update({active => \'false'}); - - schema('netdisco')->resultset('NodeIp') - ->update_or_create( - { - mac => $mac->as_ieee, - ip => $ip, - dns => $name, - active => \'true', - time_last => \$now, - }, - { - key => 'primary', - for => 'update', - }); - }); -} - true;