File: entls

package info (click to toggle)
libsnmp-session-perl 1.14~git20221124T101957-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,104 kB
  • sloc: perl: 11,920; ansic: 25; makefile: 15
file content (269 lines) | stat: -rw-r--r-- 7,487 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/perl -w
###
### entls - list ENTITY-MIB
###
### Author:       Simon Leinen <simon@switch.ch>
### Date created: 03-May-2005

use strict;
use SNMP_util;
use SNMP_Table;

## Prototypes
sub init_mibs ();
sub collect_entity_information ($ );
sub print_physical ($$);
sub print_phys_tree ($$);
sub print_phys_tree_1 ($$@);
sub get_physical ($ );
sub decode_truth_value ($ );
sub snmp_decode_value ($@);
sub node_is_transceiver_sensor ($ );
sub router_pretty_name ($ );
sub router_pretty_name_1 ($ );
sub usage ();

my @targets = ();

my $phys_tree;

## Set to 1 if you want entPhysicalVendorType to be printed.
##
my $print_vendor_type = 0;

## Set to 1 if you want the entPhysical index to be printed.
##
my $print_ent_physical_index = 0;

## Set to 1 if you want transceiver sensors listed.
##
my $transceiver_sensors = 0;

## Directory in which to find router configurations.
## Currently, this is only used to find the "canonical" names
## of routers, using the `hostname' command from the router
## configuration file.
##
my $rancid_base = '/usr/local/rancid/backbone/configs';

my $print_relative_index = 1;
my $print_hierarchy_indentation = 1;

my $current_router;
my $current_transceiver;

while (@ARGV) {
    if ($ARGV[0] eq '-V') {
	$print_vendor_type = 1;
    } elsif ($ARGV[0] eq '-i') {
	$print_ent_physical_index = 1;
    } elsif ($ARGV[0] eq '-t') {
	$transceiver_sensors = 1;
	$print_ent_physical_index = 1;
	$print_relative_index = 0;
	$print_hierarchy_indentation = 0;
    } elsif ($ARGV[0] eq '-h') {
	usage ();
	exit (0);
    } elsif ($ARGV[0] =~ /^-/) {
	warn ("Unknown option ",$ARGV[0]);
	usage ();
	exit (1);
    } else {
	push @targets, $ARGV[0];
    }
    shift @ARGV;
}
usage (), exit 1 unless @targets;

init_mibs ();
foreach my $target (@targets) {
    $current_router = $target;
    $current_router =~ s/.*@//;
    $current_router =~ s/:.*//;
    collect_entity_information ($target);
    print_physical ($current_router, $phys_tree);
}
1;

sub collect_entity_information ($ ) {
    my ($target) = @_;
    $phys_tree = get_physical ($target);
}

sub print_physical ($$) {
    my ($current_router, $phys_tree) = @_;
    if ($transceiver_sensors) {
	print "target --default--\n\trouter\t= ",
	router_pretty_name ($current_router), "\n";
	print "\tdirectory-desc\t= \"Router %router% transceiver monitoring\"\n";
	print "\tdisplay-name\t= \"%router% - %short-desc%\"\n";
	print "\tlong-desc\t= \"<H3>%short-desc%</H3>\"\n";
    } else {
	print "Physical Entities\n\n";
    }
    print_phys_tree ($phys_tree, "");
}

sub print_phys_tree ($$) {
    print_phys_tree_1 ($_[0], $_[1], ());
}

sub print_phys_tree_1 ($$@) {
    my ($node, $prefix, @stack) = @_;
    $node->{parent_stack} = \@stack;
    printf STDOUT ("%s%s\n", $prefix, $node->tostring ())
	if !$transceiver_sensors or node_is_transceiver_sensor ($node);
    foreach my $class (sort keys %{$node->{_children}}) {
	my $class_children = $node->{_children}->{$class};
	foreach my $child_index (sort { $a <=> $b } keys %{$class_children}) {
	    print_phys_tree_1 ($class_children->{$child_index},
			       ($print_hierarchy_indentation
				? (' 'x length $prefix)
				: '')
			       .($print_relative_index
				 ? sprintf ("%d. ", $child_index)
				 : ''),
			       ($node, @stack));
	}
    }
}

sub node_is_transceiver_sensor ($ ) {
    my ($node) = @_;
    ($node->{vendorType} =~ /1\.3\.6\.1\.4\.1\.9\.12\.3\.1\.8\.(4[6789]|50)$/)
	? 1 : 0;
}
### get_if_entries TARGET
###
### Read the MIB-II interface table, and construct a hash mapping the
### interface indices to hashes containing important slots.
### Currently, only ifDescr and ifAlias are recorded.
###
sub get_if_entries ($ ) {
    my ($target) = @_;
    return snmp_rows_to_objects
      ($target, 'MIBII::Interface', 'if', qw(descr alias));
}

sub get_physical ($ ) {
    my ($target) = @_;
    my ($phys, $root);
    $phys = snmp_rows_to_objects
	($target, 'Entity::PhysicalEntry', 'entPhysical',
	 qw(descr vendorType containedIn class parentRelPos name
	    hardwareRev firmwareRev softwareRev serialNum mfgName
	    modelName alias assetID isFRU));
    foreach my $i1 (keys %{$phys}) {
	my $e1 = $phys->{$i1};
	if ($e1->{containedIn} == 0) {
	    die "multiple roots" if defined $root;
	    $root = $e1;
	}
	foreach my $i2 (keys %{$phys}) {
	    my $e2 = $phys->{$i2};
	    next if $e2->{_visited};
	    if ($i1 == $e2->{containedIn}) {
		$e1->{_children}->{$e2->{class}}->{$e2->{parentRelPos}} = $e2;
		$e2->{parent} = $e1;
	    }
	}
    }
    return $root;
}

sub decode_truth_value ($ ) {return snmp_decode_value ($_[0], qw(1 0));}

sub snmp_decode_value ($@) {
    my ($index, @mapvec) = @_;
    return $index if $index < 1 or $index > $#mapvec+1;
    return $mapvec[$index-1];
}

sub init_mibs () {
    snmpmapOID
	(qw(
entPhysicalDescr	1.3.6.1.2.1.47.1.1.1.1.2
entPhysicalVendorType	1.3.6.1.2.1.47.1.1.1.1.3
entPhysicalContainedIn	1.3.6.1.2.1.47.1.1.1.1.4
entPhysicalClass	1.3.6.1.2.1.47.1.1.1.1.5
entPhysicalParentRelPos	1.3.6.1.2.1.47.1.1.1.1.6
entPhysicalName		1.3.6.1.2.1.47.1.1.1.1.7
entPhysicalHardwareRev	1.3.6.1.2.1.47.1.1.1.1.8
entPhysicalFirmwareRev	1.3.6.1.2.1.47.1.1.1.1.9
entPhysicalSoftwareRev	1.3.6.1.2.1.47.1.1.1.1.10
entPhysicalSerialNum	1.3.6.1.2.1.47.1.1.1.1.11
entPhysicalMfgName	1.3.6.1.2.1.47.1.1.1.1.12
entPhysicalModelName	1.3.6.1.2.1.47.1.1.1.1.13
entPhysicalAlias	1.3.6.1.2.1.47.1.1.1.1.14
entPhysicalAssetID	1.3.6.1.2.1.47.1.1.1.1.15
entPhysicalIsFRU	1.3.6.1.2.1.47.1.1.1.1.16
));
}

sub usage () {
    print "Usage: $0 -h\n";
    print "Usage: $0 [-V] [-i] [-t] TARGET...\n";
    print "  -V      print entities' vendorTypes\n";
    print "  -i      print indexes from entPhysicalTable\n";
    print "  -t      list transceiver sensors\n";
}

my %router_pretty_name = ();

sub router_pretty_name ($ ) {
    my ($router_name) = @_;
    return $router_pretty_name{$router_name}
	if exists $router_pretty_name{$router_name};
    return ($router_pretty_name{$router_name}
	    = router_pretty_name_1 ($router_name));
}

sub router_pretty_name_1 ($ ) {
    my ($router_name) = @_;
    open (CONFIG, $rancid_base.'/'.$router_name)
	or return $router_name;
    while (<CONFIG>) {
	return $1 if /^hostname (.*)$/;
    }
    close CONFIG;
}

package MIBII::Interface;
package Entity::PhysicalEntry;

my $last_transceiver;

sub tostring ($ ) {
    my ($node) = @_;
    my $result = '';
    my $parent = $node->{parent_stack}->[0];
    my $current_transceiver = $parent->{name};
    if (defined $current_transceiver
	&& (!defined $last_transceiver || $current_transceiver ne $last_transceiver)) {
	my $target = "$current_transceiver";
	$target = lc $target;
	$target =~ s/^transceiver //;
	$target =~ s/[ \/.]/_/g;
	$target =~ s/^gi/gigabitethernet/g;
	$target =~ s/^te/tengigabitethernet/g;
	print "\ntarget $target\n";
	print "\tshort-desc\t= \"",$current_transceiver,"\"\n";
	print "\ttarget-type\t= \"monitored-transceiver\"\n";
	$last_transceiver = $current_transceiver;
    }
    if ($transceiver_sensors) {
	my ($interface, $sensor_type) = split (/ /, $node->{name}, 2);
	$sensor_type =~ s/[ \/]/-/g;
	$result = "\t".lc $sensor_type;
	$result .= sprintf (" = \"%d\"", $node->{index});
    } else {
	$result .= sprintf ("%d:", $node->{index})
	    if $print_ent_physical_index;
	$result .= sprintf ("%s", $node->{name});
	$result .= " (".$node->{alias}.")" if $node->{alias};
	$result .= " (".$node->{vendorType}.")"
	    if $print_vendor_type and $node->{vendorType};
    }
    return $result;
}