« blib/lib/App/Netdisco/Util/SNMPText.pm
↑ Summary
blib/lib/App/Netdisco/SSHCollector/Platform/PaloAlto.pm »
| 1 | package App::Netdisco::Worker::Plugin::Discover::VLANs; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use Dancer::Plugin::DBIC 'schema'; | ||
| 5 | |||
| 6 | use App::Netdisco::Worker::Plugin; | ||
| 7 | use App::Netdisco::Transport::SNMP (); | ||
| 8 | |||
| 9 | use aliased 'App::Netdisco::Worker::Status'; | ||
| 10 | use List::MoreUtils 'uniq'; | ||
| 11 | use App::Netdisco::Util::SNMPText 'decode_snmp_text'; | ||
| 12 | use App::Netdisco::Util::SNMP qw/walk_mark incomplete_walks_for incomplete_walk_message | ||
| 13 | incomplete_walk_level/; | ||
| 14 | |||
| 15 | # a walk that was cut short must not look like an emptied table, so the stored | ||
| 16 | # rows stand and the hook payload shows them rather than the partial walk | ||
| 17 | sub _keep_stored_device_vlans { | ||
| 18 | my ($device, @incomplete) = @_; | ||
| 19 | vars->{'hook_data'}->{'vlans'} = [ $device->vlans->search(undef, { | ||
| 20 | columns => [qw/vlan description/], order_by => 'vlan', | ||
| 21 | result_class => 'DBIx::Class::ResultClass::HashRefInflator', | ||
| 22 | })->all ]; | ||
| 23 | my $message = incomplete_walk_message($device, 'device_port_vlan and device_vlan', @incomplete); | ||
| 24 | warning $message; | ||
| 25 | return $message; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub _store_device_vlans { | ||
| 29 | my ($device, $v_name, $v_index, $p_seen) = @_; | ||
| 30 | my (%d_seen, @devicevlans); | ||
| 31 | |||
| 32 | # sorted, and the first name for a VLAN number wins, so every run agrees | ||
| 33 | foreach my $entry (sort keys %$v_name) { | ||
| 34 | my $vlan = $v_index->{$entry}; | ||
| 35 | next unless $vlan; | ||
| 36 | next if $d_seen{$vlan}++; | ||
| 37 | |||
| 38 | push @devicevlans, { | ||
| 39 | ip => $device->ip, | ||
| 40 | vlan => $vlan, | ||
| 41 | description => decode_snmp_text($v_name->{$entry}, $device, "vlan $vlan name"), | ||
| 42 | last_discover => \'LOCALTIMESTAMP', | ||
| 43 | }; | ||
| 44 | } | ||
| 45 | |||
| 46 | # also add unnamed vlans to the device | ||
| 47 | foreach my $vlan (sort { $a <=> $b } keys %$p_seen) { | ||
| 48 | next unless $vlan; | ||
| 49 | next if $d_seen{$vlan}; | ||
| 50 | push @devicevlans, { | ||
| 51 | ip => $device->ip, | ||
| 52 | vlan => $vlan, | ||
| 53 | description => (sprintf "VLAN %d", $vlan), | ||
| 54 | last_discover => \'LOCALTIMESTAMP', | ||
| 55 | }; | ||
| 56 | } | ||
| 57 | |||
| 58 | # support for Hooks | ||
| 59 | vars->{'hook_data'}->{'vlans'} = [ map {{ | ||
| 60 | vlan => $_->{vlan}, description => $_->{description}, | ||
| 61 | last_discover => $_->{last_discover}, | ||
| 62 | }} @devicevlans ]; | ||
| 63 | |||
| 64 | schema('netdisco')->txn_do(sub { | ||
| 65 | my $rs = schema('netdisco')->resultset('DeviceVlan'); | ||
| 66 | $rs->upsert(\@devicevlans, { touch => ['last_discover'] }); | ||
| 67 | my $gone = $rs->remove_unseen({ ip => $device->ip }, | ||
| 68 | [ map {{ vlan => $_->{vlan} }} @devicevlans ]); | ||
| 69 | debug sprintf ' [%s] vlans - removed %d device VLANs', | ||
| 70 | $device->ip, $gone; | ||
| 71 | |||
| 72 | debug sprintf ' [%s] vlans - wrote %d device VLANs', | ||
| 73 | $device->ip, scalar @devicevlans; | ||
| 74 | }); | ||
| 75 | return; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub _port_vlan_rows { | ||
| 79 | my ($device, $walked, $device_ports) = @_; | ||
| 80 | my ($i_vlan, $i_vlan_type, $interfaces, $i_vlan_membership, | ||
| 81 | $i_vlan_membership_untagged) = @{$walked}{qw/i_vlan i_vlan_type | ||
| 82 | interfaces i_vlan_membership i_vlan_membership_untagged/}; | ||
| 83 | |||
| 84 | my (%p_seen, %row_seen, @portvlans); | ||
| 85 | my @active_ports = uniq (keys %$i_vlan_membership_untagged, keys %$i_vlan_membership); | ||
| 86 | |||
| 87 | # sorted, and the first entry for a port/vlan/native key wins: two interface | ||
| 88 | # entries can carry one port name, and an upsert refuses a duplicate key | ||
| 89 | foreach my $entry (sort @active_ports) { | ||
| 90 | my $port = $interfaces->{$entry} or next; | ||
| 91 | |||
| 92 | if (!defined $device_ports->{$port}) { | ||
| 93 | debug sprintf ' [%s] vlans - local port %s already skipped, ignoring', | ||
| 94 | $device->ip, $port; | ||
| 95 | next; | ||
| 96 | } | ||
| 97 | |||
| 98 | my %this_port_vlans = (); | ||
| 99 | my $type = $i_vlan_type->{$entry}; | ||
| 100 | |||
| 101 | foreach my $set ([ $i_vlan_membership_untagged->{$entry}, 0 ], | ||
| 102 | [ $i_vlan_membership->{$entry}, 1 ]) { | ||
| 103 | foreach my $vlan (@{ $set->[0] || [] }) { | ||
| 104 | next unless $vlan; | ||
| 105 | next if $this_port_vlans{$vlan}; | ||
| 106 | my $native = ((defined $i_vlan->{$entry}) | ||
| 107 | and ($vlan eq $i_vlan->{$entry})) ? 't' : 'f'; | ||
| 108 | ++$this_port_vlans{$vlan}; | ||
| 109 | ++$p_seen{$vlan}; | ||
| 110 | next if $row_seen{join "\0", $port, $vlan, $native}++; | ||
| 111 | |||
| 112 | push @portvlans, { | ||
| 113 | ip => $device->ip, | ||
| 114 | port => $port, | ||
| 115 | vlan => $vlan, | ||
| 116 | native => $native, | ||
| 117 | egress_tag => ($set->[1] && $native eq 'f' ? 't' : 'f'), | ||
| 118 | vlantype => $type, | ||
| 119 | last_discover => \'LOCALTIMESTAMP', | ||
| 120 | }; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | } | ||
| 124 | return (\@portvlans, \%p_seen); | ||
| 125 | } | ||
| 126 | |||
| 127 | sub _store_port_vlans { | ||
| 128 | my ($device, $portvlans) = @_; | ||
| 129 | |||
| 130 | schema('netdisco')->txn_do(sub { | ||
| 131 | my $rs = schema('netdisco')->resultset('DevicePortVlan'); | ||
| 132 | $rs->upsert($portvlans, { touch => ['last_discover'] }); | ||
| 133 | my $gone = $rs->remove_unseen({ ip => $device->ip }, | ||
| 134 | [ map {{ port => $_->{port}, vlan => $_->{vlan}, native => $_->{native} }} @$portvlans ]); | ||
| 135 | debug sprintf ' [%s] vlans - removed %d port VLANs', | ||
| 136 | $device->ip, $gone; | ||
| 137 | |||
| 138 | debug sprintf ' [%s] vlans - wrote %d port VLANs', | ||
| 139 | $device->ip, scalar @$portvlans; | ||
| 140 | }); | ||
| 141 | return; | ||
| 142 | } | ||
| 143 | |||
| 144 | register_worker({ phase => 'main', driver => 'snmp' }, sub { | ||
| 145 | my ($job, $workerconf) = @_; | ||
| 146 | |||
| 147 | my $device = $job->device; | ||
| 148 | return unless $device->in_storage; | ||
| 149 | my $snmp = App::Netdisco::Transport::SNMP->reader_for($device) | ||
| 150 | or return Status->defer("discover failed: could not SNMP connect to $device"); | ||
| 151 | |||
| 152 | my $walk_mark = walk_mark($snmp); | ||
| 153 | my $v_name = $snmp->v_name; | ||
| 154 | my $v_index = $snmp->v_index; | ||
| 155 | |||
| 156 | # every port VLAN table is read before the guard, which looks at the walks | ||
| 157 | # read so far. scalar, because SNMP::Info returns an empty list for a table | ||
| 158 | # the device lacks, which in list context shifts every later key and value | ||
| 159 | my %walked = map {($_ => scalar $snmp->$_)} qw/i_vlan i_vlan_type interfaces | ||
| 160 | i_vlan_membership i_vlan_membership_untagged/; | ||
| 161 | |||
| 162 | # cache the device ports to save hitting the database for many single rows | ||
| 163 | my $device_ports = vars->{'device_ports'} | ||
| 164 | || { map {($_->port => $_)} $device->ports->all }; | ||
| 165 | |||
| 166 | my @incomplete = incomplete_walks_for($device, $snmp, $walk_mark, qw/ | ||
| 167 | v_name v_index i_vlan i_vlan_type i_vlan_membership | ||
| 168 | i_vlan_membership_untagged interfaces/); | ||
| 169 | my $level = incomplete_walk_level(@incomplete); | ||
| 170 | my $status = sprintf ' [%s] vlans - discovered for ports and device', | ||
| 171 | $device->ip; | ||
| 172 | |||
| 173 | if (scalar @incomplete) { | ||
| 174 | $status = _keep_stored_device_vlans($device, @incomplete); | ||
| 175 | } | ||
| 176 | else { | ||
| 177 | my ($portvlans, $p_seen) = _port_vlan_rows($device, \%walked, $device_ports); | ||
| 178 | _store_port_vlans($device, $portvlans); | ||
| 179 | _store_device_vlans($device, $v_name, $v_index, $p_seen); | ||
| 180 | } | ||
| 181 | |||
| 182 | return Status->$level($status); | ||
| 183 | }); | ||
| 184 | |||
| 185 | true; |