Info/Layer3/TeltonikaRutOS.pm untested

filter: Statementstmt 0.0% 0 / 120 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 46 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 260 CC 17 SCAR 24.4
CC
17
Cov
0%
bp_index
30.0
_teltonika_vlan_ids
20.0
_teltonika_vlan_ports
20.0
CRAP 11.5
1 # SNMP::Info::Layer3::TeltonikaRutOS - SNMP interface to Teltonika RutOS devices
2 #
3 # Copyright (c) 2026 by SNMP::Info Developers.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7 #
8 # * Redistributions of source code must retain the above copyright notice,
9 # this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of the University of California, Santa Cruz nor the
14 # names of its contributors may be used to endorse or promote products
15 # derived from this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
28  
29 package SNMP::Info::Layer3::TeltonikaRutOS;
30  
31 use strict;
32 use warnings;
33 use Exporter;
34 use SNMP::Info::Layer3::NetSNMP;
35  
36 @SNMP::Info::Layer3::TeltonikaRutOS::ISA
37 = qw/SNMP::Info::Layer3::NetSNMP Exporter/;
38 @SNMP::Info::Layer3::TeltonikaRutOS::EXPORT_OK = qw//;
39  
40 our ($VERSION, %GLOBALS, %MIBS, %FUNCS, %MUNGE);
41  
42 $VERSION = '3.985003';
43  
44 # Current RutOS devices share this OID layout across router and gateway lines.
45 # The released 2013 TELTONIKA-MIB_RUT9XX was written for RUT9xx devices and
46 # assigns .1.1.0, .1.3.0, and .1.6.0 to modemImei, modemManufacturer, and imsi.
47 # Current RutOS returns serial, product code, and firmware at those same OIDs,
48 # so use numeric leaves to avoid the legacy MIB's misleading names.
49 %MIBS = (
50 %SNMP::Info::Layer3::NetSNMP::MIBS,
51 );
52  
53 %GLOBALS = (
54 %SNMP::Info::Layer3::NetSNMP::GLOBALS,
55 'serial1' => '.1.3.6.1.4.1.48690.1.1.0',
56 'teltonika_model' => '.1.3.6.1.4.1.48690.1.3.0',
57 'teltonika_firmware' => '.1.3.6.1.4.1.48690.1.6.0',
58 );
59  
60 %FUNCS = (
61 %SNMP::Info::Layer3::NetSNMP::FUNCS,
62 # TELTONIKA-MIB portTable pName on TSW switches. That MIB shares its
63 # module name with the RUT9xx one in netdisco-mibs, so read it by OID.
64 'teltonika_port_name' => '.1.3.6.1.4.1.48690.10.2.1.2',
65 # TELTONIKA-MIB pVlanTable on TSW switches: pVlanName, pVlanVID,
66 # pVlanPortsUntag, pVlanPortsTag. The port lists are strings of
67 # pName tokens, "port1:u port2:u", or "None".
68 'teltonika_vlan_name' => '.1.3.6.1.4.1.48690.8.2.1.2',
69 'teltonika_vlan_id' => '.1.3.6.1.4.1.48690.8.2.1.3',
70 'teltonika_vlan_untagged' => '.1.3.6.1.4.1.48690.8.2.1.4',
71 'teltonika_vlan_tagged' => '.1.3.6.1.4.1.48690.8.2.1.5',
72 );
73 %MUNGE = (%SNMP::Info::Layer3::NetSNMP::MUNGE);
74  
75 sub vendor { return 'teltonika'; }
76  
77 sub os { return 'rutos'; }
78  
79 sub model {
80 my $dev = shift;
81 my $model = $dev->teltonika_model();
82 return $model if defined $model and $model =~ /\S/;
83 return $dev->SUPER::model();
84 }
85  
86 sub os_ver {
87 my $dev = shift;
88 my $version = $dev->teltonika_firmware();
89 return $version if defined $version and $version =~ /\S/;
90 return $dev->SUPER::os_ver();
91 }
92  
93 sub serial {
94 my $dev = shift;
95 my $serial = $dev->serial1();
96 return $serial if defined $serial and $serial =~ /\S/;
97 return $dev->SUPER::serial();
98 }
99  
100 # Interfaces keyed by ifDescr, leaving out a name two interfaces share.
101 sub _teltonika_ifindex_by_name {
102 my $dev = shift;
103 my $i_descr = $dev->i_description() || {};
104  
105 my %ifindexes_by_name;
106 foreach my $ifindex ( keys %$i_descr ) {
107 my $name = $i_descr->{$ifindex};
108 next unless defined $name;
109 push @{ $ifindexes_by_name{$name} }, $ifindex;
110 }
111 return {
112 map { $_ => $ifindexes_by_name{$_}[0] }
113 grep { @{ $ifindexes_by_name{$_} } == 1 } keys %ifindexes_by_name
114 };
115 }
116  
117 # TSW switches serve dot1dTpFdbPort but no dot1dBasePortIfIndex. Their port
118 # table rows are numbered as the bridge numbers its ports, so join each row
119 # to the interface whose ifDescr carries the same name.
120 sub bp_index {
121 my $dev = shift;
122 my $standard = $dev->SUPER::bp_index();
123 return $standard if ref {} eq ref $standard and scalar keys %$standard;
124  
125 my $port_names = $dev->teltonika_port_name() || {};
126 my $ifindex_by_name = $dev->_teltonika_ifindex_by_name();
127  
128 my %rows_named;
129 foreach my $name ( values %$port_names ) {
130 next unless defined $name;
131 $rows_named{$name}++;
132 }
133  
134 my %bp_index;
135 foreach my $bridge_port ( keys %$port_names ) {
136 my $name = $port_names->{$bridge_port};
137 next unless defined $name and $rows_named{$name} == 1;
138 my $ifindex = $ifindex_by_name->{$name};
139 next unless defined $ifindex;
140 $bp_index{$bridge_port} = $ifindex;
141 }
142 return \%bp_index;
143 }
144  
145 # TSW switches serve no Q-BRIDGE-MIB VLAN tables, only pVlanTable, so the
146 # VLAN methods read it first and fall back to Q-BRIDGE when it is empty.
147 # RUTX routers serve a pVlanTable of their own under the same OIDs, with
148 # the port list where TSW has the VLAN ID, so a table whose third column
149 # is not all VLAN IDs is not read.
150 sub _teltonika_vlan_ids {
151 my $dev = shift;
152 my $vlan_ids = $dev->teltonika_vlan_id() || {};
153 return unless scalar keys %$vlan_ids;
154 foreach my $vlan ( values %$vlan_ids ) {
155 return unless defined $vlan and $vlan =~ /^\d+$/;
156 return unless $vlan >= 1 and $vlan <= 4094;
157 }
158 return $vlan_ids;
159 }
160  
161 sub v_index {
162 my $dev = shift;
163 return $dev->_teltonika_vlan_ids() || $dev->SUPER::v_index(@_);
164 }
165  
166 sub v_name {
167 my $dev = shift;
168 return $dev->SUPER::v_name(@_) unless $dev->_teltonika_vlan_ids();
169 return $dev->teltonika_vlan_name() || {};
170 }
171  
172 sub i_vlan {
173 my $dev = shift;
174 my $untagged = $dev->_teltonika_untagged()
175 or return $dev->SUPER::i_vlan(@_);
176  
177 my %i_vlan;
178 foreach my $ifindex ( keys %$untagged ) {
179 my @vlans = @{ $untagged->{$ifindex} };
180 $i_vlan{$ifindex} = $vlans[0] if @vlans == 1;
181 }
182 return \%i_vlan;
183 }
184  
185 sub i_vlan_membership {
186 my $dev = shift;
187 my $untagged = $dev->_teltonika_untagged()
188 or return $dev->SUPER::i_vlan_membership(@_);
189 my $tagged = $dev->_teltonika_vlan_ports( 'teltonika_vlan_tagged', 't' );
190  
191 my %members;
192 foreach my $ports ( $untagged, $tagged ) {
193 foreach my $ifindex ( keys %$ports ) {
194 $members{$ifindex}{$_} = 1 for @{ $ports->{$ifindex} };
195 }
196 }
197 return {
198 map { $_ => [ sort { $a <=> $b } keys %{ $members{$_} } ] }
199 keys %members
200 };
201 }
202  
203 sub i_vlan_membership_untagged {
204 my $dev = shift;
205 return $dev->_teltonika_untagged()
206 || $dev->SUPER::i_vlan_membership_untagged(@_);
207 }
208  
209 sub _teltonika_untagged {
210 my $dev = shift;
211 return $dev->_teltonika_vlan_ports( 'teltonika_vlan_untagged', 'u' );
212 }
213  
214 # ifIndex => [VLAN IDs] from one pVlanTable port list column, keeping the
215 # tokens that carry that column's suffix and name exactly one interface.
216 # Undef when the device serves no TSW pVlanTable rows.
217 sub _teltonika_vlan_ports {
218 my ( $dev, $column, $suffix ) = @_;
219 my $vlan_ids = $dev->_teltonika_vlan_ids() or return;
220  
221 my $port_lists = $dev->$column() || {};
222 my $ifindex_by_name = $dev->_teltonika_ifindex_by_name();
223  
224 my %vlans_by_ifindex;
225 foreach my $row ( keys %$port_lists ) {
226 my $vlan = $vlan_ids->{$row};
227 next unless defined $vlan and defined $port_lists->{$row};
228 foreach my $token ( split ' ', $port_lists->{$row} ) {
229 next unless $token =~ /^(\S+):\Q$suffix\E$/;
230 my $ifindex = $ifindex_by_name->{$1};
231 push @{ $vlans_by_ifindex{$ifindex} }, $vlan if defined $ifindex;
232 }
233 }
234 foreach my $vlans ( values %vlans_by_ifindex ) {
235 @$vlans = sort { $a <=> $b } @$vlans;
236 }
237 return \%vlans_by_ifindex;
238 }
239  
240 1;
241 __END__