| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package FusionInventory::Agent::SNMP::Mock; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 4715676 | use strict; | 
|  | 1 |  |  |  |  | 8 |  | 
|  | 1 |  |  |  |  | 76 |  | 
| 4 | 1 |  |  | 1 |  | 18 | use warnings; | 
|  | 1 |  |  |  |  | 7 |  | 
|  | 1 |  |  |  |  | 90 |  | 
| 5 | 1 |  |  | 1 |  | 7 | use base 'FusionInventory::Agent::SNMP'; | 
|  | 1 |  |  |  |  | 41 |  | 
|  | 1 |  |  |  |  | 431 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 1 |  |  | 1 |  | 394 | use FusionInventory::Agent::Tools; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 1110 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | sub new { | 
| 10 | 0 |  |  | 0 | 1 |  | my ($class, %params) = @_; | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 0 |  |  |  |  |  | my $self = {}; | 
| 13 | 0 |  |  |  |  |  | bless $self, $class; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | SWITCH: { | 
| 16 | 0 | 0 |  |  |  |  | if ($params{file}) { | 
|  | 0 |  |  |  |  |  |  | 
| 17 | 0 | 0 |  |  |  |  | die "non-existing file '$params{file}'\n" | 
| 18 |  |  |  |  |  |  | unless -f $params{file}; | 
| 19 | 0 | 0 |  |  |  |  | die "unreadable file '$params{file}'\n" | 
| 20 |  |  |  |  |  |  | unless -r $params{file}; | 
| 21 | 0 |  |  |  |  |  | $self->{values} = _getIndexedValues($params{file}); | 
| 22 | 0 |  |  |  |  |  | $self->{file}   = $params{file}; | 
| 23 | 0 |  |  |  |  |  | last SWITCH; | 
| 24 |  |  |  |  |  |  | } | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 0 | 0 |  |  |  |  | if ($params{hash}) { | 
| 27 | 0 |  |  |  |  |  | $self->{values} = $params{hash}; | 
| 28 | 0 |  |  |  |  |  | last SWITCH; | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 0 |  |  |  |  |  | return $self; | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | sub switch_vlan_context { | 
| 36 | 0 |  |  | 0 | 1 |  | my ($self, $vlan_id) = @_; | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 0 | 0 |  |  |  |  | $self->{oldvalues} = $self->{values} unless $self->{oldvalues}; | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 0 |  |  |  |  |  | my $file = $self->{file} . '@' . $vlan_id; | 
| 41 | 0 | 0 | 0 |  |  |  | if (-r $file && -f $file) { | 
| 42 | 0 |  |  |  |  |  | $self->{values} = _getIndexedValues($file); | 
| 43 |  |  |  |  |  |  | } else { | 
| 44 | 0 |  |  |  |  |  | delete $self->{values}; | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | sub reset_original_context { | 
| 49 | 0 |  |  | 0 | 1 |  | my ($self) = @_; | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 0 |  |  |  |  |  | $self->{values} = $self->{oldvalues}; | 
| 52 | 0 |  |  |  |  |  | delete $self->{oldvalues}; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | sub _getIndexedValues { | 
| 56 | 0 |  |  | 0 |  |  | my ($file) = @_; | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 0 |  |  |  |  |  | my $handle = getFileHandle(file => $file); | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | # check first line | 
| 61 | 0 |  |  |  |  |  | my $first_line = <$handle>; | 
| 62 | 0 |  |  |  |  |  | seek($handle, 0, 0); | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | # check first line for safety | 
| 65 | 0 | 0 |  |  |  |  | die "invalid file format\n" unless $first_line =~ /^(\S+) = .*/; | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 0 | 0 |  |  |  |  | my $values = substr($first_line, 0, 1) eq '.' ? | 
| 68 |  |  |  |  |  |  | _readNumericalOids($handle) : | 
| 69 |  |  |  |  |  |  | _readSymbolicOids($handle)  ; | 
| 70 | 0 |  |  |  |  |  | close ($handle); | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 0 |  |  |  |  |  | return $values; | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | sub _readNumericalOids { | 
| 76 | 0 |  |  | 0 |  |  | my ($handle) = @_; | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 0 |  |  |  |  |  | my ($values, $last_oid); | 
| 79 | 0 |  |  |  |  |  | while (my $line = <$handle>) { | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 0 | 0 |  |  |  |  | if ($line =~ /^ | 
| 82 |  |  |  |  |  |  | (\S+) \s | 
| 83 |  |  |  |  |  |  | = \s | 
| 84 |  |  |  |  |  |  | (?:Wrong \s Type \s \(should \s be \s [^:]+\): \s)? | 
| 85 |  |  |  |  |  |  | ([^:]+): \s | 
| 86 |  |  |  |  |  |  | (.*) | 
| 87 |  |  |  |  |  |  | /x | 
| 88 |  |  |  |  |  |  | ) { | 
| 89 | 0 |  |  |  |  |  | my ($oid, $type, $value) = ($1, $2, $3); | 
| 90 | 0 |  |  |  |  |  | $values->{$oid} = [ $type, $value ]; | 
| 91 | 0 |  |  |  |  |  | $last_oid = $oid; | 
| 92 | 0 |  |  |  |  |  | next; | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | # potential continuation | 
| 96 | 0 | 0 | 0 |  |  |  | if ($line !~ /^$/ && $line !~ /= ""$/ && $last_oid) { | 
|  |  |  | 0 |  |  |  |  | 
| 97 | 0 | 0 | 0 |  |  |  | if ($values->{$last_oid}->[0] eq 'STRING' && | 
| 98 |  |  |  |  |  |  | $values->{$last_oid}->[1] !~ /"$/ | 
| 99 |  |  |  |  |  |  | ) { | 
| 100 | 0 |  |  |  |  |  | chomp $line; | 
| 101 | 0 |  |  |  |  |  | $values->{$last_oid}->[1] .= "\n" . $line; | 
| 102 | 0 |  |  |  |  |  | next; | 
| 103 |  |  |  |  |  |  | } | 
| 104 | 0 | 0 |  |  |  |  | if ($values->{$last_oid}->[0] eq 'Hex-STRING') { | 
| 105 | 0 |  |  |  |  |  | chomp $line; | 
| 106 | 0 |  |  |  |  |  | $values->{$last_oid}->[1] .= $line; | 
| 107 | 0 |  |  |  |  |  | next; | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 0 |  |  |  |  |  | $last_oid = undef; | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 0 |  |  |  |  |  | return $values; | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | sub _readSymbolicOids { | 
| 118 | 0 |  |  | 0 |  |  | my ($handle) = @_; | 
| 119 |  |  |  |  |  |  |  | 
| 120 | 0 |  |  |  |  |  | my %prefixes = ( | 
| 121 |  |  |  |  |  |  | 'iso'                               => '.1', | 
| 122 |  |  |  |  |  |  | 'SNMPv2-MIB::sysDescr'              => '.1.3.6.1.2.1.1.1', | 
| 123 |  |  |  |  |  |  | 'SNMPv2-MIB::sysObjectID'           => '.1.3.6.1.2.1.1.2', | 
| 124 |  |  |  |  |  |  | 'SNMPv2-MIB::sysUpTime'             => '.1.3.6.1.2.1.1.3', | 
| 125 |  |  |  |  |  |  | 'SNMPv2-MIB::sysContact'            => '.1.3.6.1.2.1.1.4', | 
| 126 |  |  |  |  |  |  | 'SNMPv2-MIB::sysName'               => '.1.3.6.1.2.1.1.5', | 
| 127 |  |  |  |  |  |  | 'SNMPv2-MIB::sysLocation'           => '.1.3.6.1.2.1.1.6', | 
| 128 |  |  |  |  |  |  | 'SNMPv2-SMI::mib-2'                 => '.1.3.6.1.2.1', | 
| 129 |  |  |  |  |  |  | 'SNMPv2-SMI::enterprises'           => '.1.3.6.1.4.1', | 
| 130 |  |  |  |  |  |  | 'IF-MIB::ifIndex'                   => '.1.3.6.1.2.1.2.2.1.1', | 
| 131 |  |  |  |  |  |  | 'IF-MIB::ifDescr'                   => '.1.3.6.1.2.1.2.2.1.2', | 
| 132 |  |  |  |  |  |  | 'IF-MIB::ifType'                    => '.1.3.6.1.2.1.2.2.1.3', | 
| 133 |  |  |  |  |  |  | 'IF-MIB::ifMtu'                     => '.1.3.6.1.2.1.2.2.1.4', | 
| 134 |  |  |  |  |  |  | 'IF-MIB::ifSpeed'                   => '.1.3.6.1.2.1.2.2.1.5', | 
| 135 |  |  |  |  |  |  | 'IF-MIB::ifPhysAddress'             => '.1.3.6.1.2.1.2.2.1.6', | 
| 136 |  |  |  |  |  |  | 'IF-MIB::ifLastChange'              => '.1.3.6.1.2.1.2.2.1.9', | 
| 137 |  |  |  |  |  |  | 'IF-MIB::ifInOctets'                => '.1.3.6.1.2.1.2.2.1.10', | 
| 138 |  |  |  |  |  |  | 'IF-MIB::ifInErrors'                => '.1.3.6.1.2.1.2.2.1.14', | 
| 139 |  |  |  |  |  |  | 'IF-MIB::ifOutOctets'               => '.1.3.6.1.2.1.2.2.1.16', | 
| 140 |  |  |  |  |  |  | 'IF-MIB::ifOutErrors'               => '.1.3.6.1.2.1.2.2.1.20', | 
| 141 |  |  |  |  |  |  | 'IF-MIB::ifName'                    => '.1.3.6.1.2.1.31.1.1.1.1', | 
| 142 |  |  |  |  |  |  | 'HOST-RESOURCES-MIB::hrDeviceDescr' => '.1.3.6.1.2.1.25.3.2.1.3', | 
| 143 |  |  |  |  |  |  | ); | 
| 144 |  |  |  |  |  |  |  | 
| 145 | 0 |  |  |  |  |  | my ($values, $last_oid); | 
| 146 | 0 |  |  |  |  |  | while (my $line = <$handle>) { | 
| 147 |  |  |  |  |  |  |  | 
| 148 | 0 | 0 |  |  |  |  | if ($line =~ /^ | 
| 149 |  |  |  |  |  |  | ([^.]+) \. ([\d.]+) \s | 
| 150 |  |  |  |  |  |  | = \s | 
| 151 |  |  |  |  |  |  | (?:Wrong \s Type \s \(should \s be \s [^:]+\): \s)? | 
| 152 |  |  |  |  |  |  | ([^:]+): \s | 
| 153 |  |  |  |  |  |  | (.*) | 
| 154 |  |  |  |  |  |  | /x | 
| 155 |  |  |  |  |  |  | ) { | 
| 156 | 0 |  |  |  |  |  | my ($mib, $suffix, $type, $value) = ($1, $2, $3, $4); | 
| 157 |  |  |  |  |  |  |  | 
| 158 | 0 | 0 |  |  |  |  | if ($prefixes{$mib}) { | 
| 159 | 0 |  |  |  |  |  | my $oid = $prefixes{$mib} . '.' . $suffix; | 
| 160 | 0 |  |  |  |  |  | $values->{$oid} = [ $type, $value ]; | 
| 161 | 0 |  |  |  |  |  | $last_oid = $oid; | 
| 162 |  |  |  |  |  |  | } else { | 
| 163 |  |  |  |  |  |  | # irrelevant OID | 
| 164 | 0 |  |  |  |  |  | $last_oid = undef; | 
| 165 |  |  |  |  |  |  | } | 
| 166 |  |  |  |  |  |  |  | 
| 167 | 0 |  |  |  |  |  | next; | 
| 168 |  |  |  |  |  |  | } | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | # potential continuation | 
| 171 | 0 | 0 | 0 |  |  |  | if ($line !~ /^$/ && $line !~ /= ""$/ && $last_oid) { | 
|  |  |  | 0 |  |  |  |  | 
| 172 | 0 | 0 | 0 |  |  |  | if ($values->{$last_oid}->[0] eq 'STRING' && | 
| 173 |  |  |  |  |  |  | $values->{$last_oid}->[1] !~ /"$/ | 
| 174 |  |  |  |  |  |  | ) { | 
| 175 | 0 |  |  |  |  |  | chomp $line; | 
| 176 | 0 |  |  |  |  |  | $values->{$last_oid}->[1] .= "\n" . $line; | 
| 177 |  |  |  |  |  |  | next | 
| 178 | 0 |  |  |  |  |  | } | 
| 179 | 0 | 0 | 0 |  |  |  | if ($values->{$last_oid}->[0] eq 'Hex-STRING' && | 
| 180 |  |  |  |  |  |  | $line =~ /^([A-F0-9]{2})( [A-F0-9]{2})?/ | 
| 181 |  |  |  |  |  |  | ) { | 
| 182 | 0 |  |  |  |  |  | chomp $line; | 
| 183 | 0 |  |  |  |  |  | $values->{$last_oid}->[1] .= $line; | 
| 184 |  |  |  |  |  |  | next | 
| 185 | 0 |  |  |  |  |  | } | 
| 186 |  |  |  |  |  |  | } | 
| 187 |  |  |  |  |  |  |  | 
| 188 | 0 |  |  |  |  |  | $last_oid = undef; | 
| 189 |  |  |  |  |  |  | } | 
| 190 |  |  |  |  |  |  |  | 
| 191 | 0 |  |  |  |  |  | return $values; | 
| 192 |  |  |  |  |  |  | } | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | sub get { | 
| 195 | 0 |  |  | 0 | 1 |  | my ($self, $oid) = @_; | 
| 196 |  |  |  |  |  |  |  | 
| 197 | 0 | 0 |  |  |  |  | return unless $oid; | 
| 198 | 0 | 0 |  |  |  |  | return unless $self->{values}->{$oid}; | 
| 199 |  |  |  |  |  |  |  | 
| 200 | 0 |  |  |  |  |  | return _getSanitizedValue( | 
| 201 |  |  |  |  |  |  | $self->{values}->{$oid}->[0], | 
| 202 |  |  |  |  |  |  | $self->{values}->{$oid}->[1], | 
| 203 |  |  |  |  |  |  | ); | 
| 204 |  |  |  |  |  |  | } | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | sub walk { | 
| 207 | 0 |  |  | 0 | 1 |  | my ($self, $oid) = @_; | 
| 208 |  |  |  |  |  |  |  | 
| 209 | 0 | 0 |  |  |  |  | return unless $oid; | 
| 210 |  |  |  |  |  |  |  | 
| 211 | 0 |  |  |  |  |  | my $values; | 
| 212 | 0 |  |  |  |  |  | foreach my $key (keys %{$self->{values}}) { | 
|  | 0 |  |  |  |  |  |  | 
| 213 | 0 | 0 |  |  |  |  | next unless $key =~ /^$oid\.(.+)/; | 
| 214 | 0 |  |  |  |  |  | $values->{$1} = _getSanitizedValue( | 
| 215 |  |  |  |  |  |  | $self->{values}->{$key}->[0], | 
| 216 |  |  |  |  |  |  | $self->{values}->{$key}->[1] | 
| 217 |  |  |  |  |  |  | ); | 
| 218 |  |  |  |  |  |  | } | 
| 219 |  |  |  |  |  |  |  | 
| 220 | 0 |  |  |  |  |  | return $values; | 
| 221 |  |  |  |  |  |  | } | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | sub _getSanitizedValue { | 
| 224 | 0 |  |  | 0 |  |  | my ($format, $value) = @_; | 
| 225 |  |  |  |  |  |  |  | 
| 226 | 0 | 0 |  |  |  |  | if ($format eq 'Hex-STRING') { | 
|  |  | 0 |  |  |  |  |  | 
| 227 | 0 |  |  |  |  |  | $value =~ s/\s//g; | 
| 228 | 0 |  |  |  |  |  | $value = "0x".$value; | 
| 229 |  |  |  |  |  |  | } elsif ($format eq 'STRING') { | 
| 230 | 0 |  |  |  |  |  | $value =~ s/^(? | 
| 231 | 0 |  |  |  |  |  | $value =~ s/(? | 
| 232 |  |  |  |  |  |  | } | 
| 233 |  |  |  |  |  |  |  | 
| 234 | 0 |  |  |  |  |  | return $value; | 
| 235 |  |  |  |  |  |  | } | 
| 236 |  |  |  |  |  |  |  | 
| 237 |  |  |  |  |  |  | 1; | 
| 238 |  |  |  |  |  |  | __END__ |