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
23 package Torrus
::DevDiscover
::DevDetails
;
36 $self->{'params'} = {};
37 $self->{'snmpvars'} = {}; # SNMP results stored here
38 $self->{'devtype'} = {}; # Device types
39 $self->{'caps'} = {}; # Device capabilities
40 $self->{'data'} = {}; # Discovery data
51 while( my ($param, $value) = each %{$params} )
53 $self->{'params'}->{$param} = $value;
65 $self->{'params'}->{$param} = $value;
74 return $self->{'params'}->{$name};
78 # The following 3 methods get around undefined parameters and
79 # make "use warnings" happy
85 my $val = $self->param($name);
86 return (defined($val) and ($val eq 'yes'));
93 my $val = $self->param($name);
94 return (not defined($val) or ($val ne 'yes'));
101 my $val = $self->param($name);
102 return (defined($val) ?
$val:'');
107 # store the query results for later use
114 while( my( $oid, $value ) = each %{$vars} )
116 if( $oid !~ /^\d[0-9.]+\d$/o )
118 Error
('Invalid OID syntax: from ' .
119 $self->paramString('snmp-host') .
120 ': \'' . $oid . '\'');
124 $self->{'snmpvars'}{$oid} = $value;
130 if( not exists( $self->{'snmpvars'}{$oid} ) )
132 $self->{'snmpvars'}{$oid} = undef;
138 # Clean the cache of sorted OIDs
139 $self->{'sortedoids'} = undef;
144 # check if the stored query results have such OID prefix
152 if( exists( $self->{'snmpvars'}{$oid} ) )
160 # get the value of stored SNMP variable
166 return $self->{'snmpvars'}{$oid};
170 # get the list of table indices for the specified prefix
177 # Remember the sorted OIDs, as sorting is quite expensive for large
180 if( not defined( $self->{'sortedoids'} ) )
182 $self->{'sortedoids'} = [];
183 push( @
{$self->{'sortedoids'}},
184 Net
::SNMP
::oid_lex_sort
( keys %{$self->{'snmpvars'}} ) );
188 my $prefixLen = length( $prefix ) + 1;
191 for my $oid ( @
{$self->{'sortedoids'}} )
193 if( defined($self->{'snmpvars'}{$oid} ) )
195 if( Net
::SNMP
::oid_base_match
( $prefix, $oid ) )
197 # Extract the index from OID
198 my $index = substr( $oid, $prefixLen );
199 push( @ret, $index );
213 # device type is the registered discovery module name
219 $self->{'devtype'}{$type} = 1;
227 return $self->{'devtype'}{$type};
233 return keys %{$self->{'devtype'}};
237 # device capabilities. Each discovery module may define its own set of
238 # capabilities and use them for information exchange between checkdevtype(),
239 # discover(), and buildConfig() of its own and dependant modules
245 Debug
('Device capability: ' . $cap);
246 $self->{'caps'}{$cap} = 1;
254 return $self->{'caps'}{$cap};
261 Debug
('Clearing device capability: ' . $cap);
262 if( exists( $self->{'caps'}{$cap} ) )
264 delete $self->{'caps'}{$cap};
274 return $self->{'data'};
278 sub screenSpecialChars
283 $txt =~ s/:/{COLON}/gm;
284 $txt =~ s/;/{SEMICOL}/gm;
285 $txt =~ s/%/{PERCENT}/gm;
295 my $selList = $self->param('selectors');
296 return if not defined( $selList );
298 my $reg = \
%Torrus::DevDiscover
::selectorsRegistry
;
300 for my $sel ( split('\s*,\s*', $selList) )
302 my $type = $self->param( $sel . '-selector-type' );
303 if( not defined( $type ) )
305 Error
('Parameter ' . $sel . '-selector-type must be defined ' .
306 'for ' . $self->param('snmp-host'));
308 elsif( not exists( $reg->{$type} ) )
310 Error
('Unknown selector type: ' . $type .
311 ' for ' . $self->param('snmp-host'));
315 Debug
('Initializing selector: ' . $sel);
317 my $treg = $reg->{$type};
318 my @objects = &{$treg->{'getObjects'}}( $self, $type );
320 for my $object ( @objects )
322 Debug
('Checking object: ' .
323 &{$treg->{'getObjectName'}}( $self, $object, $type ));
325 my $expr = $self->param( $sel . '-selector-expr' );
326 $expr = '1' if ($expr eq '');
331 my $checkval = $self->param( $sel . '-' . $attr );
333 Debug
('Checking attribute: ' . $attr .
334 ' and value: ' . $checkval);
335 my $ret = &{$treg->{'checkAttribute'}}( $self,
338 Debug
(sprintf('Returned value: %d', $ret));
342 my $rpn = Torrus
::RPN
->new();
343 my $result = $rpn->run( $expr, $callback );
344 Debug
('Selector result: ' . $result);
347 my $actions = $self->param( $sel . '-selector-actions' );
348 for my $action ( split('\s*,\s*', $actions) )
351 $self->param( $sel . '-' . $action . '-arg' );
352 $arg = 1 if not defined( $arg );
354 Debug
('Applying action: ' . $action .
355 ' with argument: ' . $arg);
356 &{$treg->{'applyAction'}}( $self, $object, $type,
372 # indent-tabs-mode: nil
373 # perl-indent-level: 4