aboutsummaryrefslogtreecommitdiffstats
path: root/extras/tools/lldp/lolwhat.pl
blob: e9f15e101c5bb76e2a2f7a6935693f38c83a1cc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
#! /usr/bin/perl
# 
# What? WHAAAAT? What's going on?
#
# Simple to to figure out what those tools in the network department have
# actually done. Uses SNMP to gather information about your network, using
# multiple techniques to father information about "next hop".
#
# Usage: ./lolwhat.pl <ip> <community>
#
# This will connect to <ip> and poll it for SNMP-data, then add that to an
# asset database. 
use POSIX;
use Time::HiRes;
use strict;
use warnings;
use Data::Dumper;

use lib '/opt/gondul/include';
use FixedSNMP;
use nms;
use nms::snmp;

# If we are given one switch on the command line, add that and then exit.
my $cmdline_community = shift;
my @ips = @ARGV;

# Actual SNMP results - keyed by IP
my %snmpresults = ();

# LLDP peers detected from remotes.
# Keyed by chassis ID - as seen remotely.
my %lldppeers = ();

my %lldpmap = ();
my %lldpnamemap = ();

my %lldpresolver = ();
my %ipmap = ();
my %peermap = ();
my %claimedips = ();

# tracking for log indentation
my $mylogindent = "";

mylog("Polling initial systems");

logindent(1);
foreach my $target (@ips) {
	probe_sys($target, $cmdline_community);
	pad_snmp_results();
}
logindent(-1);


peerdetect();
pad_snmp_results();

# List of chassis IDs checked.
# Note that we deliberately don't add the chassis id's from systems prboed
# directly from the command line. Experience has shown us that
# lldpLocChassisId can be unreliable, so we don't trust it, even if it does
# mean we might end up polling the same system multiple times.
my @chassis_ids_checked = ();
while (scalar keys %lldppeers > scalar @chassis_ids_checked) {
	mylog("Probed auto-detected peers: " . scalar @chassis_ids_checked . " Total detected: " . scalar keys %lldppeers );
	logindent(1);
	OUTER: for my $id (keys %lldppeers) {
	       for my $id2 (@chassis_ids_checked) {
		       if ($id2 eq $id) {
			       next OUTER;
		       }
	       }
	       probe_sys($lldppeers{$id}{ip}, $cmdline_community);
	       push @chassis_ids_checked,$id;
	}
	pad_snmp_results();
	peerdetect();
	logindent(-1);
}
mylog("Probing complete. Trying to make road in the velling");

pad_snmp_results();
deduplicate();
pad_snmp_results();

populate_lldpmap();
populate_ipmap();
populate_peermap();

my %result = ( snmpresults => \%snmpresults, ipmap => \%ipmap, peermap => \%peermap, lldpmap => \%lldpmap, lldppeers => \%lldppeers);

mylog("Done. Outputting JSON.");
print JSON::XS::encode_json(\%result);
exit;

sub compare_targets_depth {
	my ($t1, $t2)  = @_;
	my $res = 0;
	my $matches = 0;
	while (my ($port, $data) = each %{$snmpresults{$t1}{interfaces}}) {
		my $one = $data->{ifPhysAddress};
		my $two = $snmpresults{$t2}{interfaces}{$port}{ifPhysAddress};
		if (!defined($one) and !defined($two)) {
			next;
		}
		if (!defined($one) or !defined($two)) {
			$res++;
		}  elsif ($one ne $two) {
			$res++;
		} else {
			if ($one ne "" and $two ne "") {
				if ($one ne "00:00:00:00:00:00") {
					$matches++;
				}
			}
		}
	}
	if ($matches > 10 and $res == 0) {
		mylog("$matches interfaces share MAC address. Calling it OK.");
		return $res;
	} else {
		mylog("$res mismatched interfaces versus $matches matched. Not enough for confidence.");
		$res++;
	}
	return $res;
}
sub compare_targets {
	my ($t1, $t2)  = @_;
	my $res = 0;
	my $fuckperl = 0;
	my $bad = "";
	$bad = "$t2 not found on $t1";
	foreach my $t1ip (@{$snmpresults{$t1}{ips}}) {
		if ($fuckperl == 0) {
			if ($t1ip eq $t2) {
				$res = 1;
				$fuckperl = 1;
			}
		}
	}
	$fuckperl = 0;
	if ($res == 0) {
		$bad = "$t1 not found on $t2";
	}
	foreach my $t2ip (@{$snmpresults{$t2}{ips}}) {
		if ($fuckperl == 0) {
			if ($t2ip eq $t1) {
				$res = $res + 1;
				$fuckperl = 1;
			}
		}
	}
	if ($res == 1) {
		mylog("... So there are two systems that look 50% similar. $bad (But not the other way around)");
	}
	if ($res != 2) {
		mylog("Lacking confidence. Doing in-depth comparison instead");
		logindent(1);
		$res = compare_targets_depth($t1, $t2);
		if ($res == 0) {
			$res = 2;
			mylog("Gained confidence. Injecting missing IPs to both canidates.");
			logindent(1);
			inject_ips($t1, $t2);
			logindent(-1);
		}
		logindent(-1);
	}
	return $res;
}
# Get raw SNMP data for an ip/community.
sub get_snmp_data {
	my ($ip, $community) = @_;
	my %ret = ();
	mylog("Polling $ip");
	eval {
		my $session = nms::snmp::snmp_open_session($ip, $community);
		$ret{'sysName'} = $session->get('sysName.0');
		$ret{'sysDescr'} = $session->get('sysDescr.0');
		$ret{'lldpLocChassisId'} = $session->get('lldpLocChassisId.0');
		$ret{'lldpRemManAddrTable'} = $session->gettable("lldpRemManAddrTable");
		$ret{'lldpRemTable'} = $session->gettable("lldpRemTable");
		$ret{'ipNetToMediaTable'} = $session->gettable('ipNetToMediaTable');
		$ret{'ifTable'} = $session->gettable('ifTable');
		$ret{'ifXTable'} = $session->gettable('ifXTable');
		$ret{'ipAddressTable'} = $session->gettable('ipAddressTable');
		$ret{'ipAddrTable'} = $session->gettable('ipAddrTable');
		#print Dumper(\%ret);
	};
	if ($@) {
		my $tmp = "$@";
		chomp($tmp);
		mylog($tmp);
		return undef;
	}
	if (!defined($ret{sysName})) {
		return undef;
	}
	return \%ret;
}

# Compare a new LLDP with old ones.
sub compare
{
	my %in = %{$_[0]};
	if (!defined($lldppeers{$in{id}})) {
		return 1;
	}
	my %old = %{$lldppeers{$in{id}}};
	if ($old{name} eq $in{name}) {
		return 0;
	} else {
		mylog("\t\tXXX: Fu... chassis ID collision from remote ID $in{id}.");
		mylog("\t\t\tXXX: Old sysname/ip: $old{name} / $old{ip}");
		mylog("\t\t\tXXX: New sysname/ip: $in{name} / $in{ip}");
		return -1;
	}
}

sub peerdetect
{
	mylog("Detecting new candidates for probes");
	  
	logindent(1);
	while (my ($target, $value) = each %snmpresults) {
		while (my ($port, $data) = each %{$value->{interfaces}}) {
			my %d = ( ip => $data->{lldpRemManAddr}, id => $data->{lldpRemChassisId}, name => $data->{lldpRemSysName} );
			if (!defined($d{ip})) {
				next;
			}
			if (!defined($d{id})) {
				mylog("wtf is up here? Man addr but no chassis id? I HATE NETWORK EQUIPMENT");
				next;
			}
			if (!defined($d{name})) {
				mylog("I. Hate. Network. Equipment. We got IP, chassis id but no sysname.");
				next;
			}
			if (compare(\%d) > 0) {
				%{$lldppeers{$d{id}}} = %d;
				mylog("Adding peer $d{name} / $d{ip} / $d{id}");
			}
		}
	}
	logindent(-1);
}

# Deduplicate entries of SNMP (in case we spotted the same thing twice) and
# detect collisions of sysnames and sort it out.
sub deduplicate
{
	my %remmap = ();
	my %syscollisions = ();
	my %locmap = ();
	my %okips = ();
	mylog("Building inventory of decent results/ips");
	logindent(1);
	while (my ($target, $value) = each %snmpresults) {
		if (defined($value->{fakeSnmp}) and $value->{fakeSnmp} eq "yes") {
			next;
		}
		if (defined($value->{sysName}) and defined($value->{ips})) {
			mylog("Ok: $target");
			foreach my $ip (@{$value->{ips}}) {
				$okips{$ip} = 1;
			}
		}
	}
	logindent(-1);
	mylog("Checking for empty SNMP results that are covered by other results");
	logindent(1);
	my @removals = ();
	while (my ($target, $value) = each %snmpresults) {
		if (defined($value->{fakeSnmp}) and $value->{fakeSnmp} eq "yes") {
			if (defined($okips{$target})) {
				push @removals,$target;
			}
		}
	}
	mylog("Removing " . join(", ", @removals));
	foreach my $removal (@removals) {
		delete $snmpresults{$removal};
	}
	logindent(-1);

	mylog("Checking for duplicated/corrupt chassis ids");
	logindent(1);
	mylog("Building sysname -> chassis id table where possible");
	logindent(1);
	while (my ($target, $value) = each %snmpresults) {
		while (my ($port, $data) = each %{$value->{interfaces}}) {
			if (!defined($data->{lldpRemSysName})) {
				next;
			}
			my $name = $data->{lldpRemSysName};
			my $id = $data->{lldpRemChassisId};
			if (defined($remmap{$name}) and $remmap{$name} ne $id) {
				mylog("sysName collision. This is only an issue if there is _also_ chassis id collision.");
				push @{$syscollisions{$name}}, $id;
			} else {
				$remmap{$name} = $id;
			}
		}
	}
	logindent(-1);
	mylog("Building local chassis id map");
	logindent(1);
	while (my ($target, $value) = each %snmpresults) {
		my $locchassis = $value->{lldpLocChassisId};
		if (!defined($locchassis)) {
			next;
		}
		push @{$locmap{$locchassis}}, $target;
	}
	logindent(-1);
	mylog("Checking for chassis id's with duplicate systems");
	logindent(1);
	my %fixlist = ();
	my %tested = ();
	while (my ($id, $value) = each %locmap) {
		if (@$value > 1) {
			mylog("Duplicate or collision: chassis id ($id) : " . join(", ", @$value));
			logindent(1);
			my @removed = ();
			foreach my $test (@$value) {
				foreach my $isremoved1 (@removed) {
					if ($isremoved1 eq $test) {
						next;
					}
				}
				foreach my $test2 (@$value) {
					if ($test2 eq $test) {
						next;
					}
					foreach my $isremoved2 (@removed) {
						if ($isremoved2 eq $test2) {
							next;
						}
					}
					if (defined($tested{$test}{$test2}) or defined($tested{$test2}{$test})) {
						next;
					} elsif (compare_targets($test, $test2) != 2) {
						mylog("Collision between $test and $test2. Adding to fixlist.");
						mylog("a: " . $snmpresults{$test}{sysName} . " b: ". $snmpresults{$test2}{sysName});
						$tested{$test}{$test2} = 1;
						$tested{$test2}{$test} = 1;
						$fixlist{$test} = 1;
						$fixlist{$test2} = 1;
					} else {
						$tested{$test}{$test2} = 1;
						$tested{$test2}{$test} = 1;
						push @removed, $test2;
					}
				}
			}
			foreach my $old (@removed) {
				delete $snmpresults{$old};
			}
			if (@removed > 0) {
				mylog("Removed duplicates: " . join(", ", @removed));
			}
			logindent(-1);
		}
	}
	logindent(-1);
	mylog("Applying fixes for ". (keys %fixlist) . " collisions");
	logindent(1);
	while (my ($ip, $value) = each %fixlist) {
		my $sysname = $snmpresults{$ip};
		if (defined($syscollisions{$sysname})) {
			mylog("Can't really fix $ip because there's also a sysname collision for that sysname");
			#$snmpresults{$ip}{lldpLocChassisId} = undef;
		} elsif (defined($remmap{$sysname})) {
			if ($remmap{$sysname} eq $snmpresults{$ip}{lldpLocChassisId}) {
				mylog("Couldn't fix $ip / $sysname. The extrapolated chassis id seen from remote is the same as the colliding one.... ");
				$snmpresults{$ip}{lldpLocChassisId} = undef;
			} else {
				mylog("Switching chassis ID from colliding $snmpresults{$ip}{lldpLocChassisId} to what remotes think matches: $remmap{$sysname}");
				$snmpresults{$ip}{lldpLocChassisId} = $remmap{$sysname};
			}
		} else {
			mylog("No alternate chassis IDs seen for $ip. Unsetting broken chassis ID anyway.");
			$snmpresults{$ip}{lldpLocChassisId} = undef;
		}
	}
	logindent(-1);
	logindent(-1);
}

# Filter raw SNMP data over to something more legible.
# This is the place to add all post-processed results so all parts of the
# tool can use them.
sub parse_snmp
{
	my $snmp = $_[0];
	my $targetip = $_[1];
	my %result = ();
	my %lol = ();
	if (!defined($snmp)) {
		mylog("XXX: No snmp data. Probably disconnect?");
		return;
	}
	my $sysname = $snmp->{sysName};
	mylog("Post-processing SNMP. Sysname: $sysname");
	logindent(1);
	$result{sysName} = $sysname;
	$result{sysDescr} = $snmp->{sysDescr};
	my $chassis_id =  nms::convert_mac($snmp->{lldpLocChassisId});
	if (defined($chassis_id)) {
		if (defined($result{lldpLocChassisId})) {
			if ($result{lldpLocChassisId} eq $chassis_id) {
				mylog("Chassis id matched remote...");
			} else {
				mylog("Bad chassis id.... Fudge. Ignoring direct result.");
			}
		} else {
			$result{lldpLocChassisId} = $chassis_id;
			mylog("Chassis id: $chassis_id");
		}
	} else {
		mylog("XXX: No lldpLocChassisId found. Bummer. Enable LLDP?");
	}
	@{$result{ips}} = ();
	@{$result{peers}} = ();
	mylog("Parsing lldp neighbors");
	logindent(1);
	while (my ($key, $value) = each %{$snmp->{lldpRemTable}}) {
		my $idx = $value->{lldpRemLocalPortNum};
		my $rem_chassis_id = nms::convert_mac($value->{'lldpRemChassisId'});
		my $remname = $value->{lldpRemSysName};
		mylog("Spotted $rem_chassis_id / $remname");
		foreach my $key2 (keys %$value) {
			$lol{$idx}{$key2} = $value->{$key2};
		}
		$lol{$idx}{key} = $key;
		$lol{$idx}{'lldpRemChassisId'} = $rem_chassis_id;
		my %caps = ();
		nms::convert_lldp_caps($value->{'lldpRemSysCapEnabled'}, \%caps);
		$lol{$idx}{'lldpRemSysCapEnabled'} = \%caps;
		my %caps2 = ();
		nms::convert_lldp_caps($value->{'lldpRemSysCapSupported'}, \%caps2);
		$lol{$idx}{'lldpRemSysCapSupported'} = \%caps2;
	}
	logindent(-1);
	mylog("Parsing lldp neighbors management interfaces");
	logindent(1);
	while (my ($key, $value) = each %{$snmp->{lldpRemManAddrTable}}) {
		my $old = 0;
		my $idx = $value->{lldpRemLocalPortNum};
		my $remname = $lol{$idx}{lldpRemSysName};
		if (defined($lol{$idx}{lldpRemManAddr})) {
			$old = $lol{$idx}{lldpRemManAddrSubtype};
		}
		my $addr = $value->{'lldpRemManAddr'};
		my $addrtype = $value->{'lldpRemManAddrSubtype'};
		foreach my $key2 (keys %$value) {
			if ($key2 eq 'lldpRemManAddr') {
				next;
			}
			$lol{$idx}{$key2} = $value->{$key2};
		}
		my $remip;
		if ($addrtype == 1) {
			$remip = nms::convert_ipv4($addr);
		} elsif ($addrtype == 2) {
			$remip = nms::convert_ipv6($addr);
		} else {
			next;
		}
		if ($old != 0) {
			if ($old != $addrtype) {
				mylog("Two management IPs discovered on port. v4 vs v6.");
				mylog("\t$lol{$idx}{lldpRemManAddr} vs $remip");
			}
			if ($old > $addrtype) {
				$lol{$idx}{lldpRemManAddr} = $remip;
			}
		} else {
			$lol{$idx}{lldpRemManAddr} = $remip;
		}
		push @{$result{peers}}, $remip;
		push @{$lol{$idx}{peers}}, $remip;
	}
	logindent(-1);
	mylog("Parsing local interfaces");
	logindent(1);
	while (my ($key, $value) = each %{$snmp->{ifTable}}) {
		$value->{ifPhysAddress} = nms::convert_mac($value->{ifPhysAddress});
		foreach my $key2 (keys %$value) {
			$lol{$value->{ifIndex}}{$key2} = $value->{$key2};
		}
	}
	logindent(-1);
	mylog("Parsing ARP table");
	logindent(1);
	while (my ($key, $value) = each %{$snmp->{ipNetToMediaTable}}) {
		my $mac = nms::convert_mac($value->{ipNetToMediaPhysAddress});
		my $idx = $value->{ipNetToMediaIfIndex};
		$value->{ipNetToMediaPhysAddress} = $mac;
		push @{$lol{$idx}{ARP}}, $value;
		if ($lol{$idx}{ifPhysAddress} eq $mac) {
			push @{$lol{$idx}{ips}}, $value->{ipNetToMediaNetAddress};
			push @{$result{ips}}, $value->{ipNetToMediaNetAddress};
			mylog("Found my self: " . $value->{ipNetToMediaNetAddress});
		} else {
			push @{$lol{$idx}{peers}}, $value->{ipNetToMediaNetAddress};
			push @{$result{peers}}, $value->{ipNetToMediaNetAddress};
		}
	}
	while (my ($key, $value) = each %{$snmp->{ifXTable}}) {
		foreach my $key2 (keys %$value) {
			$lol{$key}{$key2} = $value->{$key2};
		}
	}
	logindent(-1);
	mylog("Parsing ipAddressTable");
	logindent(1);
	while (my ($key, $value) = each %{$snmp->{ipAddressTable}}) {
		my $addr = $value->{ipAddressAddr};
		my $addrtype = $value->{ipAddressAddrType};
		if ($addrtype == 1) {
			$addr = nms::convert_ipv4($addr);
		} elsif ($addrtype == 2) {
			$addr = nms::convert_ipv6($addr);
		} else {
			next;
		}
		mylog("Local IP added: $addr");
		push @{$result{ips}}, $addr;
	}
	logindent(-1);
	mylog("Parsing ipAddrTable");
	logindent(1);
	while (my ($key, $value) = each %{$snmp->{ipAddrTable}}) {
		push @{$result{ips}}, $value->{ipAdEntAddr};
		mylog("Adding local ipv4 ip $value->{ipAdEntAddr}");
	}
	logindent(-1);
	@{$result{peers}} = sort @{$result{peers}};
	@{$result{ips}} = sort @{$result{ips}};
	$result{interfaces} = \%lol;
	mylog("Ensuring some sanity: Checking if the $targetip is among claimed IPs for the SNMP results");
	logindent(1);
	my $sanitytest = 0;
	foreach my $ip (@{$result{ips}}) {
		if ($ip eq $targetip) {
			mylog("Phew, it is...");
			$sanitytest = 1;
		}
	}
	if ($sanitytest == 0) {
		mylog("Didn't find myself. Hoping deduplication will take care of it?");
		$result{badSelf} = 1;
	}
	logindent(-1);
	mylog("Registering known ips for " . ($sysname || "?" ));
	foreach my $ip (@{$result{ips}}) {
		$claimedips{$ip} = $sysname || "?";
	}
	logindent(-1);
	
	return \%result;
}
sub inject_ips {
	my ($t1, $t2) = @_;
	push @{$snmpresults{$t1}{ips}}, $t1;
	push @{$snmpresults{$t1}{ips}}, $t2;
	push @{$snmpresults{$t2}{ips}}, $t1;
	push @{$snmpresults{$t2}{ips}}, $t2;
}
sub logindent {
	my $change = $_[0];
	if ($change > 0) {
		$mylogindent = $mylogindent . "\t";
	} else {
		chop $mylogindent;
	}
}
sub mylog {
	my $msg = shift;
	my $time = POSIX::ctime(time);
	$time =~ s/\n.*$//;
	printf STDERR "[%s] %s%s\n", $time, $mylogindent, $msg;
}
# Porbe a single system.
sub probe_sys
{
	my ($target, $community) = @_;
	if (defined($snmpresults{$target}) and defined($snmpresults{$target}{'probed'})) {
		mylog("Already probed $target. Skipping.");
		return;
	}
	if (defined($claimedips{$target})) {
		mylog("IP claimed by $claimedips{$target}. Skipping.");
		return;
	}
	my $snmp = get_snmp_data($target, $community);
	$snmpresults{$target}{'probed'} = 1;
	if (!defined($snmp)) {
		return;
	}
	my $parsed = parse_snmp($snmp,$target);
	if (!defined($parsed)) {
		return;
	}
	$snmpresults{$target} = $parsed;
	$snmpresults{$target}{'probed'} = 1;
}
sub populate_lldpmap
{
	mylog("Populate LLDP map");
	while (my ($ip, $value) = each %snmpresults) {
		my $sysname = $value->{sysName};
		my $id = $value->{lldpLocChassisId};
		while (my ($if, $data) = each %{$value->{interfaces}}) {
			if (!defined($data->{lldpRemSysName})) {
				next;
			} else {
				if (defined($id)) {
					$lldpmap{$id}{$data->{lldpRemChassisId}} = 1;
				}
				$lldpnamemap{$sysname}{$data->{lldpRemSysName}} = 1;
			}
		}
	}
}

sub populate_ipmap
{
	mylog("Populating ipmap");
	my @conflicts = ();
	logindent(1);
	while (my ($ip, $value) = each %snmpresults) {
		my $sysname = $value->{sysName};
		if (defined($ipmap{$ip})) {
			mylog("Conflict for ip $ip");
		}
		$ipmap{$ip} = $ip;
		foreach my $localip (@{$value->{ips}}) {
			if (!defined($localip)) {
				next;
			} elsif (defined($ipmap{$localip}) and $ipmap{$localip} ne $ip) {
				mylog("IP conflict: $localip found multiple places ($ipmap{$localip} vs $ip)");
				push @conflicts, $localip;
			}
			$ipmap{$localip} = $ip;
		}
	}
	mylog("Removing conflicting IPs");
	foreach my $contested (@conflicts) {
		delete $ipmap{$contested};
	}
	logindent(-1);
}

sub pad_snmp_results
{
	mylog("Checking if there are peers from LLDP with no basic SNMP info");
	logindent(1);
	while (my ($id, $value) = each %lldppeers) {
		if (!defined($value->{ip}) or defined($snmpresults{$value->{ip}}{sysName})) {
			next;
		}
		mylog("Adding basic info for $value->{ip} / $value->{name} to snmp results");
		$snmpresults{$value->{ip}}{sysName} = $value->{name}; 
		$snmpresults{$value->{ip}}{lldpLocChassisId} = $value->{id};
		$snmpresults{$value->{ip}}{fakeSnmp} = "yes";
		push @{$snmpresults{$value->{ip}}{ips}}, $value->{ip};
	}
	logindent(-1);
}
sub populate_peermap
{
	mylog("Populate layer3 peermap");
	while (my ($ip, $value) = each %snmpresults) {
		my $sysname = $value->{sysName};
		foreach my $peer (@{$value->{peers}}) {
			$peermap{$ip}{$peer} = 1;
		}
	}
}