| 1 | # SNMP::Info::Layer3::Fortinet | ||
| 2 | # | ||
| 3 | # Copyright (c) 2014 Eric Miller | ||
| 4 | # All rights reserved. | ||
| 5 | # | ||
| 6 | # Redistribution and use in source and binary forms, with or without | ||
| 7 | # modification, are permitted provided that the following conditions are met: | ||
| 8 | # | ||
| 9 | # * Redistributions of source code must retain the above copyright notice, | ||
| 10 | # this list of conditions and the following disclaimer. | ||
| 11 | # * Redistributions in binary form must reproduce the above copyright | ||
| 12 | # notice, this list of conditions and the following disclaimer in the | ||
| 13 | # documentation and/or other materials provided with the distribution. | ||
| 14 | # * Neither the name of the University of California, Santa Cruz nor the | ||
| 15 | # names of its contributors may be used to endorse or promote products | ||
| 16 | # derived from this software without specific prior written permission. | ||
| 17 | # | ||
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 19 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 20 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 21 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
| 22 | # LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 23 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 24 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 25 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 26 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 27 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 28 | # POSSIBILITY OF SUCH DAMAGE. | ||
| 29 | |||
| 30 | package SNMP::Info::Layer3::Fortinet; | ||
| 31 | |||
| 32 | use strict; | ||
| 33 | use warnings; | ||
| 34 | use Exporter; | ||
| 35 | use SNMP::Info::Layer3; | ||
| 36 | use SNMP::Info::Aggregate 'agg_ports_ifstack'; | ||
| 37 | |||
| 38 | @SNMP::Info::Layer3::Fortinet::ISA = qw/ | ||
| 39 | SNMP::Info::Layer3 | ||
| 40 | SNMP::Info::Aggregate | ||
| 41 | Exporter | ||
| 42 | /; | ||
| 43 | @SNMP::Info::Layer3::Fortinet::EXPORT_OK = qw//; | ||
| 44 | |||
| 45 | our ($VERSION, %GLOBALS, %FUNCS, %MIBS, %MUNGE); | ||
| 46 | |||
| 47 | $VERSION = '3.985003'; | ||
| 48 | |||
| 49 | %MIBS = ( | ||
| 50 | %SNMP::Info::Layer3::MIBS, | ||
| 51 | %SNMP::Info::Aggregate::MIBS, | ||
| 52 | 'FORTINET-CORE-MIB' => 'fnSysSerial', | ||
| 53 | 'FORTINET-FORTIGATE-MIB' => 'fgVdMaxVdoms', | ||
| 54 | 'FORTINET-FORTIMANAGER-FORTIANALYZER-MIB' => 'fmSysVersion', | ||
| 55 | 'FORTINET-FORTIAUTHENTICATOR-MIB' => 'facSysVersion', | ||
| 56 | 'FORTINET-FORTISWITCH-MIB' => 'fsSysVersion', | ||
| 57 | ); | ||
| 58 | |||
| 59 | %GLOBALS = ( | ||
| 60 | %SNMP::Info::Layer3::GLOBALS, | ||
| 61 | |||
| 62 | # FORTINET-FORTISWITCH-MIB | ||
| 63 | 'fs_version' => 'fsSysVersion', | ||
| 64 | ); | ||
| 65 | |||
| 66 | %FUNCS = ( | ||
| 67 | %SNMP::Info::Layer3::FUNCS, | ||
| 68 | ); | ||
| 69 | |||
| 70 | %MUNGE = ( | ||
| 71 | %SNMP::Info::Layer3::MUNGE, | ||
| 72 | ); | ||
| 73 | |||
| 74 | sub vendor { | ||
| 75 | return 'fortinet'; | ||
| 76 | } | ||
| 77 | |||
| 78 | # fortios 5.4 and higher can have empty ifDescr. use ifName (but | ||
| 79 | # without the ifAlias fixup that's done in layer3::i_name()) which | ||
| 80 | # mimics fortios >5.4 | ||
| 81 | # copied from an old Layer3.pm which did not have duplicate | ||
| 82 | # description fixup | ||
| 83 | sub interfaces { | ||
| 84 | my $fortinet = shift; | ||
| 85 | my $partial = shift; | ||
| 86 | |||
| 87 | my $interfaces = $fortinet->i_index($partial); | ||
| 88 | my $descriptions = $fortinet->orig_i_name($partial); | ||
| 89 | |||
| 90 | my %interfaces = (); | ||
| 91 | foreach my $iid ( keys %$interfaces ) { | ||
| 92 | my $desc = $descriptions->{$iid}; | ||
| 93 | next unless defined $desc; | ||
| 94 | |||
| 95 | $interfaces{$iid} = $desc; | ||
| 96 | } | ||
| 97 | |||
| 98 | return \%interfaces; | ||
| 99 | } | ||
| 100 | |||
| 101 | # FortiSwitches register under 12356.106.1, not under the MIB's fsModel | ||
| 102 | # branch (12356.106.5), so their sysObjectID does not translate to a model | ||
| 103 | sub _is_fortiswitch { | ||
| 104 | my $fortinet = shift; | ||
| 105 | my $id = $fortinet->id() // ''; | ||
| 106 | |||
| 107 | return $id =~ /^\.?1\.3\.6\.1\.4\.1\.12356\.106(?:\.|$)/; | ||
| 108 | } | ||
| 109 | |||
| 110 | # fsSysVersion is "FortiSwitch-<model> v<version>,build...": | ||
| 111 | # "FortiSwitch-148F-FPOE v7.6.0,build1015,240812 (GA)" | ||
| 112 | sub _fs_version_parts { | ||
| 113 | my $fortinet = shift; | ||
| 114 | my $version = $fortinet->fs_version() // ''; | ||
| 115 | |||
| 116 | return $version =~ /^FortiSwitch-(\S+)\s+v(\d+(?:\.\d+)+)/; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub model { | ||
| 120 | my $fortinet = shift; | ||
| 121 | |||
| 122 | if ( $fortinet->_is_fortiswitch() ) { | ||
| 123 | my ($model) = $fortinet->_fs_version_parts(); | ||
| 124 | return "FS-$model" if defined $model; | ||
| 125 | } | ||
| 126 | |||
| 127 | my $id = $fortinet->id() || ''; | ||
| 128 | |||
| 129 | my $model = &SNMP::translateObj($id); | ||
| 130 | |||
| 131 | return $id unless defined $model; | ||
| 132 | |||
| 133 | $model =~ s/^f[grsw][tfw]?//i; | ||
| 134 | return $model; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub os { | ||
| 138 | return 'fortios'; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub os_ver { | ||
| 142 | my $fortinet = shift; | ||
| 143 | |||
| 144 | if ( $fortinet->_is_fortiswitch() ) { | ||
| 145 | my ( undef, $version ) = $fortinet->_fs_version_parts(); | ||
| 146 | return $version if defined $version; | ||
| 147 | } | ||
| 148 | |||
| 149 | my $ver = $fortinet->fgSysVersion() || $fortinet->fmSysVersion() || $fortinet->facSysVersion() || ''; | ||
| 150 | |||
| 151 | if ( $ver =~ /(\d+[\.\d]+)/ ) { | ||
| 152 | return $1; | ||
| 153 | } | ||
| 154 | |||
| 155 | return $ver; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub serial { | ||
| 159 | my $fortinet = shift; | ||
| 160 | |||
| 161 | # FortiSwitches serve no fnSysSerial; their serial is the base entity's | ||
| 162 | return $fortinet->fnSysSerial() || $fortinet->SUPER::serial(); | ||
| 163 | } | ||
| 164 | |||
| 165 | # FortiSwitch management address rows name other local ports and remote | ||
| 166 | # indexes than the neighbor rows they belong to, so none can be trusted. | ||
| 167 | # A chassis ID that is an address is taken from the neighbor row itself; | ||
| 168 | # every other neighbor gets 0.0.0.0, which Netdisco resolves by looking up | ||
| 169 | # its chassis ID among known devices. | ||
| 170 | sub lldp_ip { | ||
| 171 | my $fortinet = shift; | ||
| 172 | my $partial = shift; | ||
| 173 | |||
| 174 | return $fortinet->SUPER::lldp_ip($partial) | ||
| 175 | unless $fortinet->_is_fortiswitch(); | ||
| 176 | |||
| 177 | my $id_types = $fortinet->_lldp_rem( 'lldp_rem_id_type', $partial ); | ||
| 178 | my $ids = $fortinet->lldp_id($partial) || {}; | ||
| 179 | |||
| 180 | my %lldp_ip; | ||
| 181 | foreach my $key ( keys %$id_types ) { | ||
| 182 | my $id = $ids->{$key} // ''; | ||
| 183 | $lldp_ip{$key} | ||
| 184 | = ( $id_types->{$key} eq 'networkAddress' | ||
| 185 | and $id =~ /^\d{1,3}(?:\.\d{1,3}){3}$/ ) | ||
| 186 | ? $id | ||
| 187 | : '0.0.0.0'; | ||
| 188 | } | ||
| 189 | return \%lldp_ip; | ||
| 190 | } | ||
| 191 | |||
| 192 | # FortiSwitches can serve one neighbor's lldpRemTable columns under | ||
| 193 | # different time marks, so its rows share no key across columns. The local | ||
| 194 | # port and remote index identify the neighbor; where two time marks give | ||
| 195 | # the same neighbor, the later row is kept. | ||
| 196 | sub _lldp_rem { | ||
| 197 | my ( $fortinet, $method, $partial ) = @_; | ||
| 198 | |||
| 199 | my $rows = $fortinet->SUPER::_lldp_rem( $method, $partial ); | ||
| 200 | return $rows unless $fortinet->_is_fortiswitch(); | ||
| 201 | |||
| 202 | my @keys | ||
| 203 | = sort { _lldp_time_mark($a) <=> _lldp_time_mark($b) } keys %$rows; | ||
| 204 | my %rows; | ||
| 205 | foreach my $key (@keys) { | ||
| 206 | next unless $key =~ /^\d+\.(\d+(?:\.\d+)*)$/; | ||
| 207 | $rows{"0.$1"} = $rows->{$key}; | ||
| 208 | } | ||
| 209 | return \%rows; | ||
| 210 | } | ||
| 211 | |||
| 212 | sub _lldp_time_mark { | ||
| 213 | my $key = shift; | ||
| 214 | |||
| 215 | return $key =~ /^(\d+)\./ ? $1 : 0; | ||
| 216 | } | ||
| 217 | |||
| 218 | # A FortiSwitch's LLDP chassis ID, which its neighbors report, is one | ||
| 219 | # below the address of its first interface | ||
| 220 | sub mac { | ||
| 221 | my $fortinet = shift; | ||
| 222 | |||
| 223 | if ( $fortinet->_is_fortiswitch() ) { | ||
| 224 | my $type = $fortinet->lldp_loc_id_type() // ''; | ||
| 225 | my $chassis = $type =~ /mac/ ? $fortinet->lldp_loc_id() : undef; | ||
| 226 | return $chassis | ||
| 227 | if defined $chassis | ||
| 228 | and $chassis =~ /^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$/; | ||
| 229 | } | ||
| 230 | return $fortinet->SUPER::mac(); | ||
| 231 | } | ||
| 232 | |||
| 233 | sub lldp_ipv6 { | ||
| 234 | my $fortinet = shift; | ||
| 235 | my $partial = shift; | ||
| 236 | |||
| 237 | return $fortinet->SUPER::lldp_ipv6($partial) | ||
| 238 | unless $fortinet->_is_fortiswitch(); | ||
| 239 | return {}; | ||
| 240 | } | ||
| 241 | |||
| 242 | sub lldp_addrs { | ||
| 243 | my $fortinet = shift; | ||
| 244 | my $partial = shift; | ||
| 245 | |||
| 246 | return $fortinet->SUPER::lldp_addrs($partial) | ||
| 247 | unless $fortinet->_is_fortiswitch(); | ||
| 248 | return $fortinet->_lldp_addrs_from_families($partial); | ||
| 249 | } | ||
| 250 | |||
| 251 | sub agg_ports { return agg_ports_ifstack(@_) } | ||
| 252 | |||
| 253 | sub _agg_ports_walked { return qw/ifStackStatus ifType/ } | ||
| 254 | |||
| 255 | 1; | ||
| 256 | __END__ |