« blib/lib/App/Netdisco/Worker/Plugin/Macsuck/PortAccessEntity.pm
↑ Summary
blib/lib/App/Netdisco/Worker/Plugin/Macwalk.pm »
| 1 | package App::Netdisco::Worker::Plugin::Macsuck::WirelessNodes; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use App::Netdisco::Worker::Plugin; | ||
| 5 | use aliased 'App::Netdisco::Worker::Status'; | ||
| 6 | |||
| 7 | use App::Netdisco::Transport::SNMP (); | ||
| 8 | use Dancer::Plugin::DBIC 'schema'; | ||
| 9 | use App::Netdisco::Util::SNMPText 'decode_snmp_text'; | ||
| 10 | use Time::HiRes 'gettimeofday'; | ||
| 11 | |||
| 12 | register_worker({ phase => 'main', driver => 'snmp' }, sub { | ||
| 13 | my ($job, $workerconf) = @_; | ||
| 14 | |||
| 15 | my $device = $job->device; | ||
| 16 | my $snmp = App::Netdisco::Transport::SNMP->reader_for($device) | ||
| 17 | or return Status->defer("macsuck failed: could not SNMP connect to $device"); | ||
| 18 | |||
| 19 | my $now = 'to_timestamp('. (join '.', gettimeofday) .')::timestamp'; | ||
| 20 | |||
| 21 | my $cd11_txrate = $snmp->cd11_txrate; | ||
| 22 | return unless $cd11_txrate and scalar keys %$cd11_txrate; | ||
| 23 | |||
| 24 | return Status->info(sprintf ' [%s] macsuck - dot11 info available but skipped due to config', | ||
| 25 | $device->ip) unless setting('store_wireless_clients'); | ||
| 26 | |||
| 27 | my $cd11_rateset = $snmp->cd11_rateset(); | ||
| 28 | my $cd11_uptime = $snmp->cd11_uptime(); | ||
| 29 | my $cd11_sigstrength = $snmp->cd11_sigstrength(); | ||
| 30 | my $cd11_sigqual = $snmp->cd11_sigqual(); | ||
| 31 | my $cd11_mac = $snmp->cd11_mac(); | ||
| 32 | my $cd11_port = $snmp->cd11_port(); | ||
| 33 | my $cd11_rxpkt = $snmp->cd11_rxpkt(); | ||
| 34 | my $cd11_txpkt = $snmp->cd11_txpkt(); | ||
| 35 | my $cd11_rxbyte = $snmp->cd11_rxbyte(); | ||
| 36 | my $cd11_txbyte = $snmp->cd11_txbyte(); | ||
| 37 | my $cd11_ssid = $snmp->cd11_ssid(); | ||
| 38 | |||
| 39 | # A class that implements cd11_txrate but not cd11_ssid at all - Trapeze and | ||
| 40 | # NWSS2300 both do - gets undef back from AUTOLOAD rather than an error, and | ||
| 41 | # for those the old fallback is still the best available answer. Only treat a | ||
| 42 | # missing index as unattributable when the walk returned something. | ||
| 43 | my $have_ssids = (ref $cd11_ssid eq 'HASH' and scalar keys %$cd11_ssid); | ||
| 44 | my $nossid = 0; | ||
| 45 | |||
| 46 | while (my ($idx, $txrates) = each %$cd11_txrate) { | ||
| 47 | my $rates = $cd11_rateset->{$idx}; | ||
| 48 | my $mac = $cd11_mac->{$idx}; | ||
| 49 | next unless defined $mac; # avoid null entries | ||
| 50 | # there can be more rows in txrate than other tables | ||
| 51 | |||
| 52 | my $txrate = (ref $txrates and defined $txrates->[$#$txrates]) | ||
| 53 | ? int($txrates->[$#$txrates]) | ||
| 54 | : undef; | ||
| 55 | |||
| 56 | my $maxrate = (ref $rates and defined $rates->[$#$rates]) | ||
| 57 | ? int($rates->[$#$rates]) | ||
| 58 | : undef; | ||
| 59 | |||
| 60 | # the SSID table is a separate walk from the rate table and, as above, | ||
| 61 | # can have fewer rows. An index missing here is a client we cannot | ||
| 62 | # attribute to an SSID on this pass, not a client on an SSID called | ||
| 63 | # "unknown" - and since (mac, ssid) is the primary key, storing it under | ||
| 64 | # that name gives the client a second identity which nothing expires. | ||
| 65 | # Skip it and pick the client up on a pass where the walk is complete. | ||
| 66 | my $ssid = decode_snmp_text($cd11_ssid->{$idx}, $device, "client $idx ssid"); | ||
| 67 | if (! defined $ssid or ! length $ssid) { | ||
| 68 | if (! $have_ssids) { | ||
| 69 | $ssid = 'unknown'; | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | ++$nossid; | ||
| 73 | next; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | schema('netdisco')->txn_do(sub { | ||
| 78 | schema('netdisco')->resultset('NodeWireless') | ||
| 79 | ->search({ 'me.mac' => $mac, 'me.ssid' => $ssid }) | ||
| 80 | ->update_or_create({ | ||
| 81 | txrate => $txrate, | ||
| 82 | maxrate => $maxrate, | ||
| 83 | uptime => $cd11_uptime->{$idx}, | ||
| 84 | rxpkt => $cd11_rxpkt->{$idx}, | ||
| 85 | txpkt => $cd11_txpkt->{$idx}, | ||
| 86 | rxbyte => $cd11_rxbyte->{$idx}, | ||
| 87 | txbyte => $cd11_txbyte->{$idx}, | ||
| 88 | sigqual => $cd11_sigqual->{$idx}, | ||
| 89 | sigstrength => $cd11_sigstrength->{$idx}, | ||
| 90 | time_last => \$now, | ||
| 91 | }, { | ||
| 92 | order_by => [qw/mac ssid/], | ||
| 93 | for => 'update', | ||
| 94 | }); | ||
| 95 | }); | ||
| 96 | } | ||
| 97 | |||
| 98 | return Status->info(sprintf ' [%s] macsuck - processed %s wireless nodes%s', | ||
| 99 | $device->ip, (scalar keys %{ $cd11_txrate }), | ||
| 100 | ($nossid ? sprintf(' (%s skipped, no SSID reported)', $nossid) : '')); | ||
| 101 | }); | ||
| 102 | |||
| 103 | true; |