refactor set_canonical_ip
* check SNMP connection on new ip is good, else abort * use new ->renumber method on device * set hostname on new ip * improved logical path for using sysname and ospf root * stub support for device_identity
This commit is contained in:
@@ -42,61 +42,47 @@ subroutines.
|
|||||||
Returns: C<$device>
|
Returns: C<$device>
|
||||||
|
|
||||||
Given a Device database object, and a working SNMP connection, check whether
|
Given a Device database object, and a working SNMP connection, check whether
|
||||||
the database object's IP is the best choice for that device. If not, return
|
the database object's IP is the best choice for that device. If not, update
|
||||||
a new Device database object with the canonical IP.
|
the IP and hostname in the device object for the canonical IP.
|
||||||
|
|
||||||
The Device database object can be a fresh L<DBIx::Class::Row> object which is
|
|
||||||
not yet stored to the database.
|
|
||||||
|
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
sub set_canonical_ip {
|
sub set_canonical_ip {
|
||||||
my ($device, $snmp) = @_;
|
my ($device, $snmp) = @_;
|
||||||
|
|
||||||
my $oldip = $device->ip;
|
my $new_ip = undef;
|
||||||
my $newip = $snmp->root_ip;
|
my $old_ip = $device->ip;
|
||||||
|
my $ospf_ip = $snmp->root_ip;
|
||||||
|
my $revname = ipv4_from_hostname($snmp->name);
|
||||||
|
|
||||||
if (defined $newip) {
|
if (setting('device_identity')) {
|
||||||
if ($oldip ne $newip) {
|
}
|
||||||
debug sprintf ' [%s] device - changing root IP to alt IP %s',
|
elsif ((not $new_ip) and setting('reverse_sysname') and $revname) {
|
||||||
$oldip, $newip;
|
$new_ip = $revname;
|
||||||
|
}
|
||||||
|
elsif ((not $new_ip) and $ospf_ip) {
|
||||||
|
$new_ip = $ospf_ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
return unless $new_ip and ($new_ip ne $old_ip);
|
||||||
|
|
||||||
|
if (not $snmp->snmp_connect_ip( $new_ip )) {
|
||||||
|
# should be warning or error?
|
||||||
|
debug sprintf ' [%s] device - cannot change IP to %s - SNMP connect failed',
|
||||||
|
$old_ip, $device->ip;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
schema('netdisco')->txn_do(sub {
|
schema('netdisco')->txn_do(sub {
|
||||||
if ($device->in_storage) {
|
$device->renumber($new_ip) or return;
|
||||||
# remove old device and aliases
|
|
||||||
my $copy = schema('netdisco')->resultset('Device')
|
|
||||||
->find({ip => $oldip});
|
|
||||||
|
|
||||||
schema('netdisco')->resultset('Device')
|
my $hostname = hostname_from_ip($device->ip);
|
||||||
->search({ ip => $device->ip })->delete({keep_nodes => 1});
|
$device->update({dns => $hostname});
|
||||||
debug sprintf ' [%s] device - deleted self', $oldip;
|
|
||||||
|
|
||||||
$device = schema('netdisco')->resultset('Device')
|
debug sprintf ' [%s] device - changed IP to %s (%s)',
|
||||||
->create({ $copy->get_columns, ip => $newip });
|
$old_ip, $device->ip, ($device->dns || '');
|
||||||
|
|
||||||
# make nodes follow device
|
|
||||||
schema('netdisco')->resultset('Node')
|
|
||||||
->search({switch => $oldip})
|
|
||||||
->update({switch => $newip});
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$device->set_column(ip => $newip);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else {
|
|
||||||
my $revname = ipv4_from_hostname($snmp->name);
|
|
||||||
if (setting('reverse_sysname') and $revname) {
|
|
||||||
debug sprintf ' [%s] device - changing root IP to revname %s',
|
|
||||||
$oldip, $revname;
|
|
||||||
$device->ip($revname);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# either root_ip is changed or unchanged, but it exists
|
|
||||||
return $device;
|
|
||||||
}
|
|
||||||
|
|
||||||
=head2 store_device( $device, $snmp )
|
=head2 store_device( $device, $snmp )
|
||||||
|
|
||||||
|
|||||||
@@ -61,8 +61,8 @@ sub discover {
|
|||||||
return job_error("discover failed: could not SNMP connect to $host");
|
return job_error("discover failed: could not SNMP connect to $host");
|
||||||
}
|
}
|
||||||
|
|
||||||
$device = set_canonical_ip($device, $snmp);
|
|
||||||
store_device($device, $snmp);
|
store_device($device, $snmp);
|
||||||
|
set_canonical_ip($device, $snmp); # must come after store_device
|
||||||
store_interfaces($device, $snmp);
|
store_interfaces($device, $snmp);
|
||||||
store_wireless($device, $snmp);
|
store_wireless($device, $snmp);
|
||||||
store_vlans($device, $snmp);
|
store_vlans($device, $snmp);
|
||||||
|
|||||||
Reference in New Issue
Block a user