diff options
author | Kristian Lyngstol <kly@kly.no> | 2016-06-04 19:36:37 +0200 |
---|---|---|
committer | Kristian Lyngstol <kly@kly.no> | 2016-06-04 19:36:37 +0200 |
commit | 546f83cc05d41aed091e8895e42f75f0295ff87d (patch) | |
tree | 66b6e651c1284b8199d7cfa8a866c383fcd2bb47 /extras/tools/lldp/lldpdiscover.pl | |
parent | 24a5282cb63c8aab2531ae3e54c07e2c5bbebea7 (diff) |
tools: redo discovery in the form of lolwhat
what?
lol...
just figure it out for me.
Ok, so the tool isn't entirely done, but it departs from the simple
lldpdiscver.pl method that was very limited when things weren't working as
they should.
The long-term idea of lolwhat is that it should be able to do all sorts of
discovery. Right now it just takes a list of IPs and figures out how things
are connected by SNMP. It uses LLDP info from SNMP, but it does not
_currently_ traverse new hosts based on what it finds.
Diffstat (limited to 'extras/tools/lldp/lldpdiscover.pl')
-rwxr-xr-x | extras/tools/lldp/lldpdiscover.pl | 64 |
1 files changed, 63 insertions, 1 deletions
diff --git a/extras/tools/lldp/lldpdiscover.pl b/extras/tools/lldp/lldpdiscover.pl index d6f2992..e5ac46f 100755 --- a/extras/tools/lldp/lldpdiscover.pl +++ b/extras/tools/lldp/lldpdiscover.pl @@ -36,6 +36,7 @@ use nms::snmp; # Actual assets detected, indexed by chassis ID my %assets; +my %arp; # Tracking arrays. Continue scanning until they are of the same length. my @chassis_ids_checked; @@ -77,7 +78,40 @@ if (defined($cmdline_ip) && defined($cmdline_community)) { push @chassis_ids_checked,$id; } } - print JSON::XS::encode_json(\%assets); + while (my ($mac, $entry) = each %arp) { + if (!defined($entry->{'neighbors'})) { + delete $arp{$mac}; + next; + } + my $sysn = $entry->{sysName}; + for my $aentry (@{$entry->{'neighbors'}}) { + if (defined($entry->{chassis_id}) and $aentry->{origin} eq $entry->{chassis_id}) { + next; + } + if ($aentry->{ipNetToMediaType} eq "static") { + next; + } + $arp{$mac}{ips}{$aentry->{ipNetToMediaNetAddress}} = 1; + if (!defined($arp{$mac}{sysName})) { + $arp{$mac}{sysName}= $aentry->{ipNetToMediaNetAddress}; + $arp{$mac}{namefromip} = 1; + } + $arp{$mac}{nsystems}{$aentry->{origin_sysname}} = 1; + } + } + my %map = (); + while (my ($mac, $entry) = each %arp) { + while (my ($sys, $ientry) = each %{$entry->{nsystems}}) { + $map{$entry->{sysName}}{$sys} = 1; + $map{$sys}{$entry->{sysName}} = 1; + } + } + + my %major = (); + $major{assets} = \%assets; + $major{arp} = \%arp; + $major{map} = \%map; + print JSON::XS::encode_json(\%major); # Creates corrupt output, hooray. # print JSON::XS->new->pretty(1)->encode(\%assets); exit; @@ -297,9 +331,37 @@ sub add_switch { $assets{$chassis_id}{'ip'} = $addr; $assets{$chassis_id}{'snmp'} = $snmp; $assets{$chassis_id}{'snmp_parsed'} = parse_snmp($snmp); + populate_arp($chassis_id); return; } +sub populate_arp { + my ($id) = @_; + while (my ($key, $value) = each %{$assets{$id}{snmp_parsed}}) { + my $mac = $value->{'ifPhysAddress'}; + if (!defined($mac) || $mac eq "") { + next; + } + mylog("mac: $mac - $assets{$id}{sysName}"); + $arp{$mac}{chassis_id} = $id; + $arp{$mac}{port} = $key; + $arp{$mac}{sysName} = $assets{$id}{sysName}; + if (!defined($value->{ARP})) { + next; + } + for my $entry (@{$value->{ARP}}) { + my $nmac = $entry->{ipNetToMediaPhysAddress}; + if (!defined($arp{$nmac}{neighbors})) { + @{$arp{$nmac}{neighbors}} = (); + } + $entry->{'origin'} = $id; + $entry->{'origin_sysname'} = $assets{$id}{sysName}; + $entry->{'origin_mgmt'} = $assets{$id}{ip}; + push @{$arp{$nmac}{neighbors}}, $entry; + } + } +} + sub get_lldp_chassis_id { my ($session) = @_; my $response; |