bury all dead whitespace, better off to just do it in one command. i wonder why ss...
[torrus-plus.git] / src / lib / Torrus / DevDiscover / DevDetails.pm
blobbd585d8d807c3f20ae819b0740d640d2bf388322
1 # Copyright (C) 2002-2011 Stanislav Sinyagin
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17 # Stanislav Sinyagin <ssinyagin@yahoo.com>
20 #### Torrus::DevDiscover::DevDetails: the information container for a device
21 ####
23 package Torrus::DevDiscover::DevDetails;
24 use strict;
25 use warnings;
27 use Torrus::RPN;
28 use Torrus::Log;
30 our $VERSION = 1.0;
32 sub new
34 my $self = {};
35 my $class = shift;
36 bless $self, $class;
38 $self->{'params'} = {};
39 $self->{'snmpvars'} = {}; # SNMP results stored here
40 $self->{'devtype'} = {}; # Device types
41 $self->{'caps'} = {}; # Device capabilities
42 $self->{'data'} = {}; # Discovery data
44 return $self;
48 sub setParams
50 my $self = shift;
51 my $params = shift;
53 while( my ($param, $value) = each %{$params} )
55 $self->{'params'}->{$param} = $value;
57 return;
61 sub setParam
63 my $self = shift;
64 my $param = shift;
65 my $value = shift;
67 $self->{'params'}->{$param} = $value;
68 return;
72 sub param
74 my $self = shift;
75 my $name = shift;
76 return $self->{'params'}->{$name};
80 # The following 3 methods get around undefined parameters and
81 # make "use warnings" happy
83 sub paramEnabled
85 my $self = shift;
86 my $name = shift;
87 my $val = $self->param($name);
88 return (defined($val) and ($val eq 'yes'));
91 sub paramDisabled
93 my $self = shift;
94 my $name = shift;
95 my $val = $self->param($name);
96 return (not defined($val) or ($val ne 'yes'));
99 sub paramString
101 my $self = shift;
102 my $name = shift;
103 my $val = $self->param($name);
104 return (defined($val) ? $val:'');
109 # store the query results for later use
110 # WARNING: this method is deprecated. Use $dd->walkSnmpTable() instead.
112 sub storeSnmpVars
114 my $self = shift;
115 my $vars = shift;
117 while( my( $oid, $value ) = each %{$vars} )
119 if( $oid !~ /^\d[0-9.]+\d$/o )
121 Error('Invalid OID syntax: from ' .
122 $self->paramString('snmp-host') .
123 ': \'' . $oid . '\'');
125 else
127 $self->{'snmpvars'}{$oid} = $value;
129 while( $oid ne '' )
131 $oid =~ s/\d+$//o;
132 $oid =~ s/\.$//o;
133 if( not exists( $self->{'snmpvars'}{$oid} ) )
135 $self->{'snmpvars'}{$oid} = undef;
141 # Clean the cache of sorted OIDs
142 $self->{'sortedoids'} = undef;
143 return;
147 # check if the stored query results have such OID prefi
148 # WARNING: this method is deprecated. Use $dd->checkSnmpTable() instead.
150 sub hasOID
152 my $self = shift;
153 my $oid = shift;
155 my $found = 0;
156 if( exists( $self->{'snmpvars'}{$oid} ) )
158 $found = 1;
160 return $found;
164 # get the value of stored SNMP variable
165 # WARNING: this method is deprecated.
167 sub snmpVar
169 my $self = shift;
170 my $oid = shift;
171 return $self->{'snmpvars'}{$oid};
175 # get the list of table indices for the specified prefix
176 # WARNING: this method is deprecated. Use $dd->walkSnmpTable() instead.
178 sub getSnmpIndices
180 my $self = shift;
181 my $prefix = shift;
183 # Remember the sorted OIDs, as sorting is quite expensive for large
184 # arrays.
186 if( not defined( $self->{'sortedoids'} ) )
188 $self->{'sortedoids'} = [];
189 push( @{$self->{'sortedoids'}},
190 Net::SNMP::oid_lex_sort( keys %{$self->{'snmpvars'}} ) );
193 my @ret;
194 my $prefixLen = length( $prefix ) + 1;
195 my $matched = 0;
197 for my $oid ( @{$self->{'sortedoids'}} )
199 if( defined($self->{'snmpvars'}{$oid} ) )
201 if( Net::SNMP::oid_base_match( $prefix, $oid ) )
203 # Extract the index from OID
204 my $index = substr( $oid, $prefixLen );
205 push( @ret, $index );
206 $matched = 1;
208 elsif( $matched )
210 last;
214 return @ret;
219 # device type is the registered discovery module name
221 sub setDevType
223 my $self = shift;
224 my $type = shift;
225 $self->{'devtype'}{$type} = 1;
226 return;
229 sub isDevType
231 my $self = shift;
232 my $type = shift;
233 return $self->{'devtype'}{$type};
236 sub getDevTypes
238 my $self = shift;
239 return keys %{$self->{'devtype'}};
243 # device capabilities. Each discovery module may define its own set of
244 # capabilities and use them for information exchange between checkdevtype(),
245 # discover(), and buildConfig() of its own and dependant modules
247 sub setCap
249 my $self = shift;
250 my $cap = shift;
251 Debug('Device capability: ' . $cap);
252 $self->{'caps'}{$cap} = 1;
253 return;
256 sub hasCap
258 my $self = shift;
259 my $cap = shift;
260 return $self->{'caps'}{$cap};
263 sub clearCap
265 my $self = shift;
266 my $cap = shift;
267 Debug('Clearing device capability: ' . $cap);
268 if( exists( $self->{'caps'}{$cap} ) )
270 delete $self->{'caps'}{$cap};
272 return;
277 sub data
279 my $self = shift;
280 return $self->{'data'};
284 sub screenSpecialChars
286 my $self = shift;
287 my $txt = shift;
289 $txt =~ s/:/{COLON}/gm;
290 $txt =~ s/;/{SEMICOL}/gm;
291 $txt =~ s/%/{PERCENT}/gm;
293 return $txt;
297 sub applySelectors
299 my $self = shift;
301 my $selList = $self->param('selectors');
302 return if not defined( $selList );
304 my $reg = \%Torrus::DevDiscover::selectorsRegistry;
306 for my $sel ( split('\s*,\s*', $selList) )
308 my $type = $self->param( $sel . '-selector-type' );
309 if( not defined( $type ) )
311 Error('Parameter ' . $sel . '-selector-type must be defined ' .
312 'for ' . $self->param('snmp-host'));
314 elsif( not exists( $reg->{$type} ) )
316 Error('Unknown selector type: ' . $type .
317 ' for ' . $self->param('snmp-host'));
319 else
321 Debug('Initializing selector: ' . $sel);
323 my $treg = $reg->{$type};
324 my @objects = &{$treg->{'getObjects'}}( $self, $type );
326 for my $object ( @objects )
328 Debug('Checking object: ' .
329 &{$treg->{'getObjectName'}}( $self, $object, $type ));
331 my $expr = $self->param( $sel . '-selector-expr' );
332 $expr = '1' if ($expr eq '');
334 my $callback = sub
336 my $attr = shift;
337 my $checkval = $self->param( $sel . '-' . $attr );
339 Debug('Checking attribute: ' . $attr .
340 ' and value: ' . $checkval);
341 my $ret = &{$treg->{'checkAttribute'}}( $self,
342 $object, $type,
343 $attr, $checkval );
344 Debug(sprintf('Returned value: %d', $ret));
345 return $ret;
348 my $rpn = Torrus::RPN->new();
349 my $result = $rpn->run( $expr, $callback );
350 Debug('Selector result: ' . $result);
351 if( $result )
353 my $actions = $self->param( $sel . '-selector-actions' );
354 for my $action ( split('\s*,\s*', $actions) )
356 my $arg =
357 $self->param( $sel . '-' . $action . '-arg' );
358 $arg = 1 if not defined( $arg );
360 Debug('Applying action: ' . $action .
361 ' with argument: ' . $arg);
362 &{$treg->{'applyAction'}}( $self, $object, $type,
363 $action, $arg );
370 return;
376 # Local Variables:
377 # mode: perl
378 # indent-tabs-mode: nil
379 # perl-indent-level: 4
380 # End: