Info/Layer3/F5OS.pm untested

filter: Statementstmt 0.0% 0 / 180 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 70 MC/DCmcdc 0.0% 0 / 38 Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 400 CC 28 SCAR 34.5
CC
28
Cov
0%
_f5_entities
132.0
model
20.0
_f5_decode_string_indexes
20.0
CRAP 31.6
1 package SNMP::Info::Layer3::F5OS;
2  
3 use strict;
4 use warnings;
5 use Exporter;
6 use SNMP::Info::Layer3;
7  
8 @SNMP::Info::Layer3::F5OS::ISA =
9 qw/SNMP::Info::Layer3 SNMP::Info::LLDP Exporter/;
10 @SNMP::Info::Layer3::F5OS::EXPORT_OK = qw//;
11  
12 our ($VERSION, %GLOBALS, %FUNCS, %MIBS, %MUNGE);
13  
14 $VERSION = '3.985003';
15  
16  
17 # ------------------------------------------------------------------------
18 # MIB definitions
19 # ------------------------------------------------------------------------
20 #
21 # F5OS exposes only a subset of the networking information normally
22 # expected from a Layer3 device.
23 #
24 # In particular, the tested F5OS SNMP agents do not implement:
25 #
26 # - IP-MIB address tables
27 # - ipNetToMediaTable / ipNetToPhysicalTable
28 # - IEEE8023-LAG-MIB aggregation membership
29 # - standard VLAN-to-interface membership tables
30 #
31 # F5-OS-TENANT-MIB::tenantVlansTable does expose VLAN IDs assigned to
32 # tenants, but it does not provide the interface/LAG membership required
33 # by SNMP::Info's normal VLAN model. It is therefore intentionally not
34 # used here.
35 #
36 # F5-PLATFORM-STATS-MIB is used to construct ENTITY-MIB compatible
37 # inventory information for the chassis, power supplies and disks.
38 #
39  
40 %MIBS = (
41 %SNMP::Info::Layer3::MIBS,
42 %SNMP::Info::LLDP::MIBS,
43  
44 'F5-OS-SYSTEM-MIB' => 'serialNumber',
45 'F5-OS-LLDP-MIB' => 'lldpSystemId',
46 'F5-PLATFORM-STATS-MIB' => 'model',
47 );
48  
49  
50 %GLOBALS = (
51 %SNMP::Info::Layer3::GLOBALS,
52  
53 # Chassis serial number reported by F5OS.
54 'f5_serial' => 'serialNumber',
55  
56 # F5OS does not expose the local chassis identifier through the
57 # standard LLDP-MIB objects expected by SNMP::Info. Use the
58 # vendor-specific F5-OS-LLDP-MIB::lldpSystemId instead.
59 'serial1' => 'lldpSystemId',
60 );
61  
62  
63 %FUNCS = (
64 %SNMP::Info::Layer3::FUNCS,
65  
66 # F5-OS-LLDP-MIB::lldpNeighborsTable
67 'f5_lldp_local_if' => 'lldpLocalInterface',
68 'f5_lldp_rem_port' => 'lldpNeighborPortId',
69 'f5_lldp_rem_chassis' => 'lldpNeighborChassisId',
70 'f5_lldp_rem_pdesc' => 'lldpNeighborPortDesc',
71 'f5_lldp_rem_name' => 'lldpNeighborSysName',
72 'f5_lldp_rem_desc' => 'lldpNeighborSysDesc',
73 'f5_lldp_rem_cap' => 'lldpNeighborSysCap',
74 'f5_lldp_rem_ip' => 'lldpNeighborMgmtAddr',
75 'f5_lldp_rem_model' => 'lldpNeighborF5ProductModel',
76  
77 # F5-PLATFORM-STATS-MIB::componentInfoTable
78 'f5_component_serial' => 'serialNo',
79 'f5_component_model' => 'model',
80  
81 # F5-PLATFORM-STATS-MIB::psuStatsTable
82 'f5_psu_name' => 'psuName',
83 'f5_psu_serial' => 'psuSerialNo',
84 'f5_psu_part' => 'psuPartNo',
85  
86 # F5-PLATFORM-STATS-MIB::diskInfoTable
87 'f5_disk_name' => 'diskName',
88 'f5_disk_model' => 'diskModel',
89 'f5_disk_vendor' => 'diskVendor',
90 'f5_disk_version' => 'diskVersion',
91 'f5_disk_serial' => 'diskSerialNo',
92 'f5_disk_size' => 'diskSize',
93 'f5_disk_type' => 'diskType',
94 );
95  
96  
97 %MUNGE = (
98 %SNMP::Info::Layer3::MUNGE,
99 );
100  
101  
102 # ------------------------------------------------------------------------
103 # Device information
104 # ------------------------------------------------------------------------
105  
106 sub vendor {
107 return 'f5';
108 }
109  
110  
111 sub os {
112 return 'f5os';
113 }
114  
115  
116 sub model {
117 my $f5 = shift;
118  
119 # componentInfoTable uses a DisplayString as its index. The platform
120 # entry represents the chassis itself and contains the F5OS hardware
121 # model (for example "r2600" or "r12900-DS").
122 #
123 # Do not simply return the first populated model from the table:
124 # additional component types may gain model information in future
125 # F5OS releases.
126 my $models = $f5->f5_component_model() || {};
127  
128 foreach my $iid (keys %$models) {
129 my ($name) = _f5_decode_string_indexes($iid);
130  
131 next unless defined $name && $name eq 'platform';
132  
133 my $model = $models->{$iid};
134 return $model if defined $model && $model ne '';
135 }
136  
137 # Fall back to the model derived from sysObjectID if the platform
138 # component is unavailable.
139 my $id = $f5->id();
140 my $model = &SNMP::translateObj($id);
141  
142 return defined $model ? $model : $id;
143 }
144  
145  
146 sub os_ver {
147 my $f5 = shift;
148  
149 my $version = $f5->f5os_version();
150  
151 return unless defined $version;
152  
153 # sysDescr may contain additional text after the semantic version.
154 # Netdisco only needs the F5OS version itself.
155 if ($version =~ /^(\d+\.\d+\.\d+)/) {
156 return $1;
157 }
158  
159 return $version;
160 }
161  
162  
163 sub f5os_version {
164 my $f5 = shift;
165  
166 my $descr = $f5->description();
167  
168 return unless defined $descr;
169  
170 # F5OS exposes the appliance services version through sysDescr rather
171 # than through a dedicated scalar in the tested SNMP implementation.
172 if ($descr =~ /Appliance services version\s+([^\s]+)/i) {
173 return $1;
174 }
175  
176 return;
177 }
178  
179  
180 sub serial {
181 my $f5 = shift;
182  
183 # Use F5-OS-SYSTEM-MIB::serialNumber as the authoritative hardware
184 # serial number. The separate serial1() value is sourced from
185 # F5-OS-LLDP-MIB::lldpSystemId and is used by SNMP::Info as the local
186 # LLDP chassis identifier.
187 return $f5->f5_serial();
188 }
189  
190  
191 # ------------------------------------------------------------------------
192 # Interfaces
193 # ------------------------------------------------------------------------
194  
195 sub interfaces {
196 my $f5 = shift;
197 my $partial = shift;
198  
199 my $i_index = $f5->i_index($partial) || {};
200 my $i_name = $f5->i_name($partial) || {};
201  
202 my %if;
203  
204 foreach my $iid (keys %$i_index) {
205 my $index = $i_index->{$iid};
206 next unless defined $index;
207  
208 my $name = $i_name->{$iid};
209  
210 # F5OS provides useful interface names through ifName, including
211 # physical port names and logical LAG names. Prefer these over the
212 # numeric ifIndex value.
213 if (defined $name && $name ne '') {
214 $if{$iid} = $name;
215 }
216 else {
217 $if{$iid} = $index;
218 }
219 }
220  
221 return \%if;
222 }
223  
224  
225 # ------------------------------------------------------------------------
226 # LLDP
227 # ------------------------------------------------------------------------
228  
229 sub hasLLDP {
230 my $f5 = shift;
231  
232 # F5OS provides LLDP neighbor information through its vendor-specific
233 # F5-OS-LLDP-MIB rather than through the standard LLDP MIB tables used
234 # by SNMP::Info.
235 my $neighbors = $f5->f5_lldp_rem_chassis() || {};
236  
237 return 1 if scalar keys %$neighbors;
238 return;
239 }
240  
241  
242 sub lldp_if {
243 my $f5 = shift;
244 my $partial = shift;
245  
246 my $local_if = $f5->f5_lldp_local_if($partial) || {};
247 my $i_name = $f5->i_name() || {};
248  
249 # F5-OS-LLDP-MIB identifies the local interface by its interface name.
250 # SNMP::Info expects an ifIndex, so map the F5OS interface name back
251 # to the corresponding IF-MIB index.
252 my %name_to_iid = reverse %$i_name;
253 my %lldp_if;
254  
255 foreach my $key (keys %$local_if) {
256 my $name = $local_if->{$key};
257 next unless defined $name;
258  
259 my $ifindex = $name_to_iid{$name};
260 next unless defined $ifindex;
261  
262 $lldp_if{$key} = $ifindex;
263 }
264  
265 return \%lldp_if;
266 }
267  
268  
269 sub lldp_port {
270 my $f5 = shift;
271 my $partial = shift;
272  
273 return $f5->f5_lldp_rem_port($partial) || {};
274 }
275  
276  
277 sub lldp_id {
278 my $f5 = shift;
279 my $partial = shift;
280  
281 return $f5->f5_lldp_rem_chassis($partial) || {};
282 }
283  
284  
285 sub lldp_ip {
286 my $f5 = shift;
287 my $partial = shift;
288  
289 return $f5->f5_lldp_rem_ip($partial) || {};
290 }
291  
292 sub lldp_addrs {
293 my $f5 = shift;
294 my $partial = shift;
295  
296 return $f5->_lldp_addrs_from_families($partial);
297 }
298  
299  
300 sub lldp_platform {
301 my $f5 = shift;
302 my $partial = shift;
303  
304 my $desc = $f5->f5_lldp_rem_desc($partial) || {};
305 my %platform;
306  
307 foreach my $iid (keys %$desc) {
308 next unless defined $desc->{$iid};
309  
310 # Keep the value within the size expected by Netdisco.
311 $platform{$iid} = substr($desc->{$iid}, 0, 255);
312 }
313  
314 return \%platform;
315 }
316  
317  
318 # ------------------------------------------------------------------------
319 # ENTITY inventory helpers
320 # ------------------------------------------------------------------------
321  
322 sub _f5_decode_string_indexes {
323 my $iid = shift;
324  
325 return unless defined $iid;
326  
327 # Several F5 platform tables use one or more variable-length strings
328 # as their SNMP index. Net-SNMP exposes such indexes as:
329 #
330 # <length>.<char>.<char>...
331 #
332 # For example:
333 #
334 # 8.112.108.97.116.102.111.114.109
335 #
336 # decodes to:
337 #
338 # platform
339 #
340 # Some tables contain multiple consecutive string indexes, therefore
341 # decode all strings present in the instance identifier.
342 my @oid = split /\./, $iid;
343 my @strings;
344  
345 while (@oid) {
346 my $len = shift @oid;
347  
348 return unless defined $len && $len =~ /^\d+$/;
349 return if @oid < $len;
350  
351 my @chars = splice @oid, 0, $len;
352 push @strings, join '', map { chr($_) } @chars;
353 }
354  
355 return @strings;
356 }
357  
358  
359 sub _f5_entities {
360 my $f5 = shift;
361  
362 # F5OS does not expose the platform inventory through ENTITY-MIB.
363 # Build the SNMP::Info entity view from F5-PLATFORM-STATS-MIB instead.
364 #
365 # Only inventory information explicitly supplied by F5OS is mapped.
366 # Operational health is intentionally not inferred from measurements
367 # such as PSU voltage/current, temperatures or fan RPM values.
368  
369 my $component_serial = $f5->f5_component_serial() || {};
370 my $component_model = $f5->f5_component_model() || {};
371  
372 my $psu_name = $f5->f5_psu_name() || {};
373 my $psu_serial = $f5->f5_psu_serial() || {};
374 my $psu_part = $f5->f5_psu_part() || {};
375  
376 my $disk_name = $f5->f5_disk_name() || {};
377 my $disk_model = $f5->f5_disk_model() || {};
378 my $disk_vendor = $f5->f5_disk_vendor() || {};
379 my $disk_version = $f5->f5_disk_version() || {};
380 my $disk_serial = $f5->f5_disk_serial() || {};
381 my $disk_size = $f5->f5_disk_size() || {};
382 my $disk_type = $f5->f5_disk_type() || {};
383  
384 my %entities;
385  
386  
387 # ------------------------------------------------------------------
388 # Chassis
389 # ------------------------------------------------------------------
390 #
391 # componentInfoTable contains a component named "platform". Treat
392 # this entry as the root chassis entity.
393 #
394  
395 foreach my $iid (sort keys %$component_serial) {
396 my ($name) = _f5_decode_string_indexes($iid);
397 next unless defined $name;
398 next unless $name eq 'platform';
399  
400 $entities{1} = {
401 class => 'chassis',
402 name => $name,
403 model => $component_model->{$iid},
404 serial => $component_serial->{$iid},
405 vendor => 'F5',
406 parent => 0,
407 pos => -1,
408 };
409  
410 last;
411 }
412  
413  
414 # ------------------------------------------------------------------
415 # Other platform components
416 # ------------------------------------------------------------------
417 #
418 # componentInfoTable also contains components such as the LCD module.
419 # PSU entries are deliberately skipped here because psuStatsTable
420 # contains richer PSU inventory data and would otherwise result in
421 # duplicate entities.
422 #
423  
424 my $component_index = 1001;
425  
426 foreach my $iid (sort keys %$component_serial) {
427 my ($name) = _f5_decode_string_indexes($iid);
428  
429 next unless defined $name;
430 next if $name eq 'platform';
431 next if $name =~ /^psu-\d+$/;
432  
433 $entities{$component_index++} = {
434 class => 'module',
435 name => $name,
436 model => $component_model->{$iid},
437 serial => $component_serial->{$iid},
438 parent => 1,
439 };
440 }
441  
442  
443 # ------------------------------------------------------------------
444 # Power supplies
445 # ------------------------------------------------------------------
446 #
447 # psuStatsTable provides PSU name, serial number and part number.
448 # The part number is exposed as the entity model.
449 #
450 # The table also contains electrical and thermal measurements, but
451 # F5OS does not expose a reliable operational status. Do not derive
452 # e_status from those measurements.
453 #
454  
455 my $psu_index = 2001;
456  
457 foreach my $iid (
458 sort {
459 ($psu_name->{$a} || '') cmp ($psu_name->{$b} || '')
460 } keys %$psu_name
461 )
462 {
463 my $name = $psu_name->{$iid};
464 next unless defined $name;
465  
466 $entities{$psu_index++} = {
467 class => 'powerSupply',
468 name => $name,
469 model => $psu_part->{$iid},
470 serial => $psu_serial->{$iid},
471 parent => 1,
472 };
473 }
474  
475  
476 # ------------------------------------------------------------------
477 # Disks
478 # ------------------------------------------------------------------
479 #
480 # diskInfoTable contains useful inventory information for each local
481 # disk. SNMP::Info has no dedicated disk entity class, so expose the
482 # disks as modules below the chassis.
483 #
484  
485 my $disk_index = 3001;
486  
487 foreach my $iid (
488 sort {
489 ($disk_name->{$a} || '') cmp ($disk_name->{$b} || '')
490 } keys %$disk_name
491 )
492 {
493 my $name = $disk_name->{$iid};
494 next unless defined $name;
495  
496 my @descr;
497  
498 push @descr, $disk_type->{$iid}
499 if defined $disk_type->{$iid} && length $disk_type->{$iid};
500  
501 push @descr, $disk_size->{$iid}
502 if defined $disk_size->{$iid} && length $disk_size->{$iid};
503  
504 my %entity = (
505 class => 'module',
506 name => $name,
507 model => $disk_model->{$iid},
508 serial => $disk_serial->{$iid},
509 vendor => $disk_vendor->{$iid},
510 fwver => $disk_version->{$iid},
511 parent => 1,
512 );
513  
514 # Only add a description when F5-OS provides usable disk metadata.
515 # This avoids exposing an empty entity description when neither disk
516 # type nor disk size is available.
517 $entity{descr} = join(', ', @descr) if @descr;
518  
519 $entities{$disk_index++} = \%entity;
520  
521 }
522  
523  
524 # Assign stable relative positions to all entities directly contained
525 # in the chassis. The synthetic entity indexes themselves are grouped
526 # by component type:
527 #
528 # 1 chassis
529 # 1001+ generic platform components
530 # 2001+ power supplies
531 # 3001+ disks
532 #
533 # These indexes do not originate from ENTITY-MIB; they provide a
534 # deterministic entity hierarchy for Netdisco.
535 my $pos = 0;
536  
537 foreach my $iid (
538 sort { $a <=> $b }
539 grep { $_ != 1 } keys %entities
540 )
541 {
542 $entities{$iid}->{pos} = ++$pos;
543 }
544  
545 return \%entities;
546 }
547  
548  
549 sub _f5_entity_field {
550 my ($f5, $field) = @_;
551  
552 my $entities = $f5->_f5_entities();
553 my %result;
554  
555 foreach my $iid (keys %$entities) {
556 my $value = $entities->{$iid}{$field};
557 next unless defined $value;
558  
559 $result{$iid} = $value;
560 }
561  
562 return \%result;
563 }
564  
565  
566 # ------------------------------------------------------------------------
567 # ENTITY-MIB compatible SNMP::Info methods
568 # ------------------------------------------------------------------------
569  
570 sub e_index {
571 my $f5 = shift;
572  
573 my $entities = $f5->_f5_entities();
574  
575 return { map { $_ => $_ } keys %$entities };
576 }
577  
578  
579 sub e_class {
580 return shift->_f5_entity_field('class');
581 }
582  
583  
584 sub e_name {
585 return shift->_f5_entity_field('name');
586 }
587  
588  
589 sub e_descr {
590 return shift->_f5_entity_field('descr');
591 }
592  
593  
594 sub e_model {
595 return shift->_f5_entity_field('model');
596 }
597  
598  
599 sub e_serial {
600 return shift->_f5_entity_field('serial');
601 }
602  
603  
604 sub e_vendor {
605 return shift->_f5_entity_field('vendor');
606 }
607  
608  
609 sub e_fwver {
610 return shift->_f5_entity_field('fwver');
611 }
612  
613  
614 sub e_parent {
615 return shift->_f5_entity_field('parent');
616 }
617  
618  
619 sub e_pos {
620 return shift->_f5_entity_field('pos');
621 }
622  
623  
624 1;
625  
626  
627 =head1 NAME
628  
629 SNMP::Info::Layer3::F5OS - SNMP Interface to F5OS network devices.
630  
631 =head1 AUTHOR
632  
633 Daniel Tuecks (dt@dtconnect.de)
634  
635 =head1 SYNOPSIS
636  
637 # Let SNMP::Info determine the correct subclass for you.
638 my $f5os = new SNMP::Info(
639 AutoSpecify => 1,
640 Debug => 1,
641 DestHost => 'mydevice',
642 Community => 'public',
643 Version => 2
644 )
645 or die "Can't connect to DestHost.\n";
646  
647 my $class = $f5os->class();
648 print "SNMP::Info determined this device to fall under subclass : $class\n";
649  
650 =head1 DESCRIPTION
651  
652 Abstraction subclass for F5 devices running F5OS.
653  
654 This class provides F5OS-specific system information and builds an
655 ENTITY-MIB compatible hardware inventory from the F5OS platform statistics
656 MIBs.
657  
658 =head2 Inherited Classes
659  
660 =over
661  
662 =item SNMP::Info::Layer3
663  
664 =back
665  
666 =head2 Required MIBs
667  
668 =over
669  
670 =item F<F5-OS-SYSTEM-MIB>
671  
672 =item F<F5-OS-LLDP-MIB>
673  
674 =item F<F5-PLATFORM-STATS-MIB>
675  
676 =item Inherited Classes' MIBs
677  
678 See L<SNMP::Info::Layer3/"Required MIBs"> for its own MIB requirements.
679  
680 =back
681  
682 =head1 GLOBALS
683  
684 These are methods that return scalar values from SNMP.
685  
686 =over
687  
688 =item $f5os->vendor()
689  
690 Returns C<f5>.
691  
692 =item $f5os->os()
693  
694 Returns C<f5os>.
695  
696 =item $f5os->os_ver()
697  
698 Returns the F5OS software version.
699  
700 =item $f5os->model()
701  
702 Returns the platform model reported by the F5 platform component
703 information.
704  
705 =item $f5os->serial()
706  
707 Returns the system serial number reported by F<F5-OS-SYSTEM-MIB>.
708  
709 =item $f5os->f5os_version()
710  
711 Extracts and returns the F5OS software version from C<sysDescr>.
712  
713 =back
714  
715 =head2 Globals imported from SNMP::Info::Layer3
716  
717 See documentation in L<SNMP::Info::Layer3/"GLOBALS"> for details.
718  
719 =head1 TABLE METHODS
720  
721 These are methods that return tables of information in the form of a
722 reference to a hash.
723  
724 =head2 Overrides
725  
726 =over
727  
728 =item $f5os->interfaces()
729  
730 Returns a mapping between C<ifIndex> and interface names.
731  
732 F5OS interface names are taken from C<ifName>. If no interface name is
733 available, the C<ifIndex> is used as a fallback.
734  
735 =item $f5os->hasLLDP()
736  
737 Returns true when LLDP neighbor information is available from the
738 F5OS LLDP MIB.
739  
740 =item $f5os->lldp_if()
741  
742 Returns a mapping between LLDP remote entries and the local C<ifIndex>.
743  
744 F5OS reports the local interface as an interface name. This method maps
745 the interface name to the corresponding C<ifIndex>.
746  
747 =item $f5os->lldp_id()
748  
749 Returns the remote LLDP chassis identifiers.
750  
751 =item $f5os->lldp_ip()
752  
753 Returns the remote LLDP management addresses.
754  
755 =item $f5os->lldp_addrs()
756  
757 Returns reference to hash of array references: the address C<lldp_ip()>
758 returns for each neighbor.
759  
760 =item $f5os->lldp_port()
761  
762 Returns the remote LLDP port identifiers.
763  
764 =item $f5os->lldp_platform()
765  
766 Returns the remote LLDP system descriptions as platform information.
767  
768 =back
769  
770 =head2 Entity Information
771  
772 The F5OS platform statistics MIBs expose hardware inventory information
773 using several platform-specific tables rather than ENTITY-MIB. The
774 following methods normalize this information into the standard SNMP::Info
775 entity interface.
776  
777 =over
778  
779 =item $f5os->e_index()
780  
781 Returns the generated entity indexes.
782  
783 =item $f5os->e_class()
784  
785 Returns the entity classes.
786  
787 =item $f5os->e_name()
788  
789 Returns the entity names.
790  
791 =item $f5os->e_descr()
792  
793 Returns the entity descriptions where descriptive information is available.
794  
795 =item $f5os->e_model()
796  
797 Returns the entity model or part information.
798  
799 =item $f5os->e_serial()
800  
801 Returns the entity serial numbers.
802  
803 =item $f5os->e_vendor()
804  
805 Returns the entity vendors where available.
806  
807 =item $f5os->e_fwver()
808  
809 Returns firmware versions for entities where available.
810  
811 =item $f5os->e_parent()
812  
813 Returns the generated entity containment hierarchy.
814  
815 =item $f5os->e_pos()
816  
817 Returns the generated relative positions of entities.
818  
819 =back
820  
821 =head2 Table Methods imported from SNMP::Info::Layer3
822  
823 See documentation in L<SNMP::Info::Layer3/"TABLE METHODS"> for details.
824  
825 =cut
826  
827 # vim: expandtab tabstop=4 shiftwidth=4