Info/Layer7/QNAP.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 232 CC 22 SCAR 22.5
CC
22
Cov
0%
os
20.0
_qnap_value
12.0
_qnap_current_value
12.0
CRAP 9.5
1 # SNMP::Info::Layer7::QNAP
2 #
3 # Copyright (c) 2026 df-an and the SNMP::Info Developers
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::Layer7::QNAP;
31  
32 use strict;
33 use warnings;
34 use Exporter;
35 use SNMP::Info::Entity;
36 use SNMP::Info::Layer7;
37  
38 @SNMP::Info::Layer7::QNAP::ISA =
39 qw/SNMP::Info::Layer7 SNMP::Info::Entity Exporter/;
40 @SNMP::Info::Layer7::QNAP::EXPORT_OK = qw//;
41  
42 our ($VERSION, %GLOBALS, %MIBS, %FUNCS, %MUNGE);
43  
44 $VERSION = '3.985003';
45  
46 %MIBS = (
47 %SNMP::Info::Layer7::MIBS,
48 %SNMP::Info::Entity::MIBS,
49 );
50  
51 # Numeric OIDs allow inventory to work without installing QNAP's MIB files.
52 # QTS-MIB uses enterprise 55062 with separate QTS (.1) and QuTS hero (.2)
53 # subtrees. Older devices use NAS-MIB (enterprise 24681), which exposes the
54 # model but not dedicated serial number or firmware scalars.
55 %GLOBALS = (
56 %SNMP::Info::Layer7::GLOBALS,
57 %SNMP::Info::Entity::GLOBALS,
58 'qnap_qts_hostname' => '.1.3.6.1.4.1.55062.1.12.4.0',
59 'qnap_qts_model' => '.1.3.6.1.4.1.55062.1.12.3.0',
60 'qnap_qts_serial' => '.1.3.6.1.4.1.55062.1.12.5.0',
61 'qnap_qts_firmware' => '.1.3.6.1.4.1.55062.1.12.6.0',
62 'qnap_quts_hostname' => '.1.3.6.1.4.1.55062.2.12.4.0',
63 'qnap_quts_model' => '.1.3.6.1.4.1.55062.2.12.3.0',
64 'qnap_quts_serial' => '.1.3.6.1.4.1.55062.2.12.5.0',
65 'qnap_quts_firmware' => '.1.3.6.1.4.1.55062.2.12.6.0',
66 'qnap_legacy_model' => '.1.3.6.1.4.1.24681.1.2.12.0',
67 );
68  
69 %FUNCS = (
70 %SNMP::Info::Layer7::FUNCS,
71 %SNMP::Info::Entity::FUNCS,
72 );
73  
74 %MUNGE = (
75 %SNMP::Info::Layer7::MUNGE,
76 %SNMP::Info::Entity::MUNGE,
77 );
78  
79 sub _qnap_value {
80 my $value = shift;
81  
82 return unless defined $value;
83 $value =~ s/\x00+$//;
84 $value =~ s/^\s+|\s+$//g;
85 return length($value) ? $value : undef;
86 }
87  
88 sub _qnap_sysobjectid {
89 my $qnap = shift;
90  
91 my $id = _qnap_value($qnap->id());
92 return '' unless defined $id;
93  
94 $id =~ s/^\.//;
95 return $id;
96 }
97  
98 sub _qnap_current_value {
99 my ($qnap, $qts_method, $quts_method) = @_;
100  
101 my $id = _qnap_sysobjectid($qnap);
102 my @methods = $id =~ /^1\.3\.6\.1\.4\.1\.55062\.2(?:\.|$)/
103 ? ($quts_method, $qts_method)
104 : ($qts_method, $quts_method);
105  
106 for my $method (@methods) {
107 my $value = _qnap_value($qnap->$method());
108 return $value if defined $value;
109 }
110  
111 return;
112 }
113  
114 sub _qnap_description_version {
115 my $qnap = shift;
116  
117 # QTS example: Linux TS-X41 5.2.5.3145
118 # QuTS hero example: Linux TS-X73A h5.2.10.3577
119 # Keep this deliberately strict so an unrelated Linux kernel version is
120 # not accepted after an arbitrary description.
121 my $description = $qnap->description() || '';
122 return $1
123 if $description
124 =~ /^Linux\s+
125 (?:(?:TS|TVS|TBS|HS|SS|IS|TES|ES|GM|TL|TR|QGD|QVP)-\S+|QBoat\S*)
126 \s+([hH]?(?:\d+\.){2,3}\d+(?:[-+][[:alnum:]._-]+)?)(?:\s|$)/x;
127  
128 return;
129 }
130  
131 sub _qnap_primary_firmware_version {
132 my $qnap = shift;
133  
134 my $version = _qnap_current_value(
135 $qnap, 'qnap_qts_firmware', 'qnap_quts_firmware'
136 );
137 return $version if defined $version;
138  
139 $version = _qnap_description_version($qnap);
140 return $version if defined $version;
141  
142 return;
143 }
144  
145 sub _qnap_entity_firmware_version {
146 my $qnap = shift;
147  
148 my $e_parent = $qnap->e_parent() || {};
149 my $e_class = $qnap->e_class() || {};
150  
151 for my $iid (sort keys %$e_parent) {
152 my $parent = $e_parent->{$iid};
153 my $class = $e_class->{$iid} || '';
154 next unless (defined $parent && $parent eq '0')
155 || $class eq 'chassis';
156  
157 # Read the original ENTITY-MIB leaf directly. Calling e_swver() here
158 # would recurse into the QNAP override below.
159 my $versions = $qnap->entPhysicalSoftwareRev($iid) || {};
160 my $version = _qnap_value($versions->{$iid});
161 return $version if defined $version;
162 }
163  
164 return;
165 }
166  
167 sub _qnap_firmware_version {
168 my $qnap = shift;
169  
170 my $version = _qnap_primary_firmware_version($qnap);
171 return $version if defined $version;
172  
173 return _qnap_entity_firmware_version($qnap);
174 }
175  
176 sub e_swver {
177 my ($qnap, $partial) = @_;
178  
179 my $versions = $qnap->entPhysicalSoftwareRev($partial) || {};
180 my $version = _qnap_primary_firmware_version($qnap);
181 return $versions unless defined $version;
182  
183 my $e_parent = $qnap->e_parent() || {};
184 my $e_class = $qnap->e_class() || {};
185 my %result = %$versions;
186  
187 # Netdisco's module inventory prefers the chassis e_swver value over the
188 # device os_ver value. Some QNAP agents publish only major.minor.patch in
189 # entPhysicalSoftwareRev, while QTS-MIB/sysDescr contains the complete
190 # vendor firmware (including build and the QuTS hero "h" prefix). Keep
191 # ENTITY-MIB support for legacy inventory, but make its chassis value agree
192 # with os_ver so every Netdisco view receives the same complete version.
193 for my $iid (keys %result) {
194 my $parent = $e_parent->{$iid};
195 my $class = $e_class->{$iid} || '';
196 next unless (defined $parent && $parent eq '0')
197 || $class eq 'chassis';
198  
199 $result{$iid} = $version;
200 }
201  
202 return \%result;
203 }
204  
205 sub vendor {
206 return 'qnap';
207 }
208  
209 sub name {
210 my $qnap = shift;
211  
212 my $name = _qnap_current_value(
213 $qnap, 'qnap_qts_hostname', 'qnap_quts_hostname'
214 );
215 return $name if defined $name;
216  
217 return _qnap_value($qnap->SUPER::name());
218 }
219  
220 sub model {
221 my $qnap = shift;
222  
223 my $model = _qnap_current_value(
224 $qnap, 'qnap_qts_model', 'qnap_quts_model'
225 );
226 return $model if defined $model;
227  
228 $model = _qnap_value($qnap->qnap_legacy_model());
229 return $model if defined $model;
230  
231 return $qnap->SUPER::model();
232 }
233  
234 sub serial {
235 my $qnap = shift;
236  
237 my $serial = _qnap_current_value(
238 $qnap, 'qnap_qts_serial', 'qnap_quts_serial'
239 );
240 return $serial if defined $serial;
241  
242 return _qnap_value($qnap->entity_derived_serial());
243 }
244  
245 sub os {
246 my $qnap = shift;
247  
248 my $id = _qnap_sysobjectid($qnap);
249 return 'quts hero'
250 if $id =~ /^1\.3\.6\.1\.4\.1\.55062\.2(?:\.|$)/;
251 return 'qts'
252 if $id =~ /^1\.3\.6\.1\.4\.1\.55062\.1(?:\.|$)/;
253  
254 # Legacy agents and some firmware versions expose only a generic QNAP
255 # sysObjectID. QuTS hero firmware versions are prefixed with "h".
256 my $version = _qnap_firmware_version($qnap);
257 return 'quts hero' if defined $version && $version =~ /^h\d/i;
258  
259 return 'qts';
260 }
261  
262 sub os_ver {
263 my $qnap = shift;
264  
265 my $version = _qnap_firmware_version($qnap);
266 return $version if defined $version;
267  
268 return;
269 }
270  
271 1;
272  
273 __END__