Info/Entity.pm untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 132 CC 7 SCAR 22.9
CC
7
Cov
0%
entity_derived_serial
20.0
entity_derived_os_ver
12.0
e_index
6.0
CRAP 9.9
1 # SNMP::Info::Entity
2 #
3 # Copyright (c) 2008 Max Baker changes from version 0.8 and beyond.
4 #
5 # Copyright (c) 2003 Regents of the University of California
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are met:
10 #
11 # * Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 # * Neither the name of the University of California, Santa Cruz nor the
17 # names of its contributors may be used to endorse or promote products
18 # derived from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 # LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31  
32 package SNMP::Info::Entity;
33  
34 use strict;
35 use warnings;
36 use Exporter;
37 use SNMP::Info;
38  
39 @SNMP::Info::Entity::ISA = qw/SNMP::Info Exporter/;
40 @SNMP::Info::Entity::EXPORT_OK = qw//;
41  
42 our ($VERSION, %MIBS, %FUNCS, %GLOBALS, %MUNGE);
43  
44 $VERSION = '3.985003';
45  
46 %MIBS = ( 'ENTITY-MIB' => 'entPhysicalSerialNum' );
47  
48 %GLOBALS = ();
49  
50 %FUNCS = (
51 'e_alias' => 'entPhysicalAlias',
52 'e_class' => 'entPhysicalClass',
53 'e_descr' => 'entPhysicalDescr',
54 'e_fwver' => 'entPhysicalFirmwareRev',
55 'e_fru' => 'entPhysicalIsFRU',
56 'e_hwver' => 'entPhysicalHardwareRev',
57 'e_id' => 'entPhysicalAssetID',
58 'e_map' => 'entAliasMappingIdentifier',
59 'e_model' => 'entPhysicalModelName',
60 'e_name' => 'entPhysicalName',
61 'e_parent' => 'entPhysicalContainedIn',
62 'e_pos' => 'entPhysicalParentRelPos',
63 'e_serial' => 'entPhysicalSerialNum',
64 'e_swver' => 'entPhysicalSoftwareRev',
65 'e_type' => 'entPhysicalVendorType',
66 'e_vendor' => 'entPhysicalMfgName',
67 );
68  
69 %MUNGE = ( 'e_type' => \&SNMP::Info::munge_e_type, );
70  
71 # entPhysicalIndex is not-accessible. Create to facilitate emulation methods
72 # in other classes
73  
74 sub e_index {
75 my $entity = shift;
76 my $partial = shift;
77  
78 # Force use of MIB leaf to avoid inheritance issues in pseudo classes
79 my $e_descr = $entity->entPhysicalDescr($partial);
80  
81 return unless ($e_descr);
82  
83 my %e_index;
84  
85 foreach my $iid ( keys %$e_descr ) {
86 $e_index{$iid} = $iid;
87 }
88 return \%e_index;
89 }
90  
91 sub e_port {
92 my $entity = shift;
93 my $partial = shift;
94  
95 my $e_map = $entity->e_map($partial);
96  
97 my %e_port;
98  
99 foreach my $e_id ( keys %$e_map ) {
100 my $id = $e_id;
101 $id =~ s/\.0$//;
102  
103 my $iid = $e_map->{$e_id};
104 $iid =~ s/.*\.//;
105  
106 $e_port{$id} = $iid;
107 }
108  
109 return \%e_port;
110 }
111  
112 sub entity_derived_serial {
113 my $entity = shift;
114  
115 my $e_parent = $entity->e_parent() || {};
116 my $e_class = $entity->e_class() || {};
117  
118 # Sort keys to return a consistent result between runs
119 foreach my $iid ( sort keys %$e_parent ) {
120 my $parent = $e_parent->{$iid};
121 my $class = $e_class->{$iid} || '';
122 # Only consider serial numbers for entries without a parent, or
123 # if they are of type "chassis"
124 if ( $parent eq '0' or $class eq 'chassis') {
125 my $serial = $entity->e_serial($iid);
126 if ( $serial && $serial->{$iid} ) {
127 return $serial->{$iid};
128 }
129 else {
130 my $descr = $entity->e_descr($iid);
131 if ( $descr and $descr->{$iid} =~ /serial#?:\s*([a-z0-9]+)/i )
132 {
133 return $1;
134 }
135 }
136 }
137 }
138 return;
139 }
140  
141 sub entity_derived_os_ver {
142 my $entity = shift;
143  
144 my $e_parent = $entity->e_parent() || {};
145 my $e_class = $entity->e_class() || {};
146  
147 # Sort keys to return a consistent result between runs
148 foreach my $iid ( sort keys %$e_parent ) {
149 my $parent = $e_parent->{$iid};
150 my $class = $e_class->{$iid} || '';
151 # Only consider serial numbers for entries without a parent, or
152 # if they are of type "chassis"
153 if ( $parent eq '0' or $class eq 'chassis') {
154 my $os_ver = $entity->e_swver($iid);
155 if ( $os_ver && $os_ver->{$iid} ) {
156 return $os_ver->{$iid};
157 }
158 }
159 }
160 return;
161 }
162  
163 1;
164  
165 __END__