aboutsummaryrefslogtreecommitdiffstats
path: root/extras/tools/lldp/draw-neighbors.pl
blob: 562e34ec1f5908432bb2a8ebbe5a83d0468eec56 (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
#!/usr/bin/perl

use strict;
use JSON;

my $in;
my ($full) = @ARGV;
while (<STDIN>) {
	$in .= $_;
}

my %assets = %{JSON::XS::decode_json($in)};
my %map = %{$assets{hood}};
my %map2 = %{$assets{extended}};
my %map3 = %{$assets{lldpmap}};

print "strict graph network {\n";
if ($full ne "lldp") {
	while (my ($key, $value) = each %map) {
		print_tree ($key,0,undef);
	}
	if ($full eq "full") {
		while (my ($key, $value) = each %map2) {
			print_tree2 ($key,0,undef);
		}
	}
} else {
	while (my ($key, $value) = each %map3) {
		print_tree3 ($key, 0, undef);
	}
}
print "}\n";

sub print_tree3
{
	my ($id,$indent,$parent,$max) = @_;
	my $name = $id;
	if (defined($map3{$id}{sysName}) and $map3{$id}{sysName} ne "") {
		$name = $map3{$id}{sysName};
	}
	if ($indent > 50) {
		die "Possible loop detected.";
	}
	print " \"$name\" -- {";
	my @n;
	while (my ($key, $value) = each %{$map3{$id}{peers}}) {
		my $peer = $key;
		if (defined($map3{$key}{sysName}) and $map3{$key}{sysName} ne "") {
			$peer = $map3{$key}{sysName};
		}
		push @n, "\"$peer\"";
	}
	print join(",",@n) . "};\n";
}
sub print_tree
{
	my ($name,$indent,$parent,$max) = @_;
	if (!defined($parent)) {
		$parent = "";
	}
	if ($indent > 50) {
		die "Possible loop detected.";
	}
	print " \"$name\" -- {";
	my @n;
	while (my ($key, $value) = each %{$map{$name}}) {
		push @n, "\"$key\"";
	}
	print join(",",@n) . "};\n";
}

sub print_tree2
{
	my ($name,$indent,$parent,$max) = @_;
	if (!defined($parent)) {
		$parent = "";
	}
	if ($indent > 50) {
		die "Possible loop detected.";
	}
	print " \"$name\" -- {";
	my @n;
	while (my ($key, $value) = each %{$map2{$name}}) {
		push @n, "\"$key\"";
	}
	print join(",",@n) . "};\n";
}