tidy up last commit and fix some corner cases
This commit is contained in:
@@ -103,7 +103,6 @@ unless ($action) {
|
|||||||
use App::Netdisco::Util::SNMP ();
|
use App::Netdisco::Util::SNMP ();
|
||||||
use App::Netdisco::Util::Device
|
use App::Netdisco::Util::Device
|
||||||
qw/get_device delete_device renumber_device/;
|
qw/get_device delete_device renumber_device/;
|
||||||
use App::Netdisco::Util::DNS 'hostname_from_ip';
|
|
||||||
|
|
||||||
with 'App::Netdisco::Daemon::Worker::Poller::Device';
|
with 'App::Netdisco::Daemon::Worker::Poller::Device';
|
||||||
with 'App::Netdisco::Daemon::Worker::Poller::Arpnip';
|
with 'App::Netdisco::Daemon::Worker::Poller::Arpnip';
|
||||||
@@ -171,8 +170,6 @@ unless ($action) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
renumber_device($device, $new_dev->ip);
|
renumber_device($device, $new_dev->ip);
|
||||||
my $hostname = hostname_from_ip($device->ip);
|
|
||||||
$device->update({dns => $hostname});
|
|
||||||
return ('done', sprintf 'Renumbered device %s to %s (%s).',
|
return ('done', sprintf 'Renumbered device %s to %s (%s).',
|
||||||
$device->ip, $new_dev->ip, ($hostname || ''));
|
$device->ip, $new_dev->ip, ($hostname || ''));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,21 +50,29 @@ the IP and hostname in the device object for the canonical IP.
|
|||||||
sub set_canonical_ip {
|
sub set_canonical_ip {
|
||||||
my ($device, $snmp) = @_;
|
my ($device, $snmp) = @_;
|
||||||
|
|
||||||
my $new_ip = undef;
|
|
||||||
my $old_ip = $device->ip;
|
my $old_ip = $device->ip;
|
||||||
|
my $new_ip = $old_ip;
|
||||||
my $ospf_ip = $snmp->root_ip;
|
my $ospf_ip = $snmp->root_ip;
|
||||||
my $revname = ipv4_from_hostname($snmp->name);
|
my $revname = ipv4_from_hostname($snmp->name);
|
||||||
|
|
||||||
if (setting('device_identity')) {
|
if ($ospf_ip) {
|
||||||
}
|
|
||||||
elsif ((not $new_ip) and setting('reverse_sysname') and $revname) {
|
|
||||||
$new_ip = $revname;
|
|
||||||
}
|
|
||||||
elsif ((not $new_ip) and $ospf_ip) {
|
|
||||||
$new_ip = $ospf_ip;
|
$new_ip = $ospf_ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
return unless $new_ip and ($new_ip ne $old_ip);
|
if (setting('reverse_sysname') and $revname) {
|
||||||
|
$new_ip = $revname;
|
||||||
|
}
|
||||||
|
|
||||||
|
# check if user has renumbered to an alias
|
||||||
|
if ($new_ip ne $old_ip
|
||||||
|
and $device->device_ips->count({alias => $old_ip})) {
|
||||||
|
$new_ip = $old_ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (setting('device_identity')) {
|
||||||
|
}
|
||||||
|
|
||||||
|
return if $new_ip eq $old_ip;
|
||||||
|
|
||||||
if (not $snmp->snmp_connect_ip( $new_ip )) {
|
if (not $snmp->snmp_connect_ip( $new_ip )) {
|
||||||
# should be warning or error?
|
# should be warning or error?
|
||||||
@@ -101,8 +109,6 @@ sub store_device {
|
|||||||
my $interfaces = $snmp->interfaces;
|
my $interfaces = $snmp->interfaces;
|
||||||
my $ip_netmask = $snmp->ip_netmask;
|
my $ip_netmask = $snmp->ip_netmask;
|
||||||
|
|
||||||
my $hostname = hostname_from_ip($device->ip);
|
|
||||||
$device->dns($hostname) if $hostname;
|
|
||||||
my $localnet = NetAddr::IP::Lite->new('127.0.0.0/8');
|
my $localnet = NetAddr::IP::Lite->new('127.0.0.0/8');
|
||||||
|
|
||||||
# build device aliases suitable for DBIC
|
# build device aliases suitable for DBIC
|
||||||
@@ -143,7 +149,7 @@ sub store_device {
|
|||||||
my $vtpdomains = $snmp->vtp_d_name;
|
my $vtpdomains = $snmp->vtp_d_name;
|
||||||
my $vtpdomain;
|
my $vtpdomain;
|
||||||
if (defined $vtpdomains and scalar values %$vtpdomains) {
|
if (defined $vtpdomains and scalar values %$vtpdomains) {
|
||||||
$device->vtp_domain( (values %$vtpdomains)[-1] );
|
$device->set_column( vtp_domain => (values %$vtpdomains)[-1] );
|
||||||
}
|
}
|
||||||
|
|
||||||
my @properties = qw/
|
my @properties = qw/
|
||||||
@@ -156,14 +162,14 @@ sub store_device {
|
|||||||
/;
|
/;
|
||||||
|
|
||||||
foreach my $property (@properties) {
|
foreach my $property (@properties) {
|
||||||
$device->$property( $snmp->$property );
|
$device->set_column( $property => $snmp->$property );
|
||||||
}
|
}
|
||||||
|
|
||||||
$device->model( Encode::decode('UTF-8', $snmp->model) );
|
$device->set_column( model => Encode::decode('UTF-8', $snmp->model) );
|
||||||
$device->serial( Encode::decode('UTF-8', $snmp->serial) );
|
$device->set_column( serial => Encode::decode('UTF-8', $snmp->serial) );
|
||||||
|
|
||||||
$device->snmp_class( $snmp->class );
|
$device->set_column( snmp_class => $snmp->class );
|
||||||
$device->last_discover(\'now()');
|
$device->set_column( last_discover => \'now()' );
|
||||||
|
|
||||||
schema('netdisco')->txn_do(sub {
|
schema('netdisco')->txn_do(sub {
|
||||||
my $gone = $device->device_ips->delete;
|
my $gone = $device->device_ips->delete;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package App::Netdisco::Util::Device;
|
|||||||
use Dancer qw/:syntax :script/;
|
use Dancer qw/:syntax :script/;
|
||||||
use Dancer::Plugin::DBIC 'schema';
|
use Dancer::Plugin::DBIC 'schema';
|
||||||
use App::Netdisco::Util::Permission 'check_acl';
|
use App::Netdisco::Util::Permission 'check_acl';
|
||||||
|
use App::Netdisco::Util::DNS 'hostname_from_ip';
|
||||||
|
|
||||||
use base 'Exporter';
|
use base 'Exporter';
|
||||||
our @EXPORT = ();
|
our @EXPORT = ();
|
||||||
@@ -115,13 +116,17 @@ sub renumber_device {
|
|||||||
|
|
||||||
my $happy = 0;
|
my $happy = 0;
|
||||||
schema('netdisco')->txn_do(sub {
|
schema('netdisco')->txn_do(sub {
|
||||||
|
$device->renumber($new_ip) or return;
|
||||||
|
|
||||||
schema('netdisco')->resultset('UserLog')->create({
|
schema('netdisco')->resultset('UserLog')->create({
|
||||||
username => session('logged_in_user'),
|
username => session('logged_in_user'),
|
||||||
userip => scalar eval {request->remote_address},
|
userip => scalar eval {request->remote_address},
|
||||||
event => "Renumbered device from $ip to $new_ip",
|
event => "Renumbered device from $ip to $new_ip",
|
||||||
});
|
});
|
||||||
|
|
||||||
$device->renumber($new_ip);
|
my $hostname = hostname_from_ip($device->ip);
|
||||||
|
$device->update({dns => $hostname});
|
||||||
|
|
||||||
$happy = 1;
|
$happy = 1;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user