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
;
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
53 while( my ($param, $value) = each %{$params} )
55 $self->{'params'}->{$param} = $value;
67 $self->{'params'}->{$param} = $value;
76 return $self->{'params'}->{$name};
80 # The following 3 methods get around undefined parameters and
81 # make "use warnings" happy
87 my $val = $self->param($name);
88 return (defined($val) and ($val eq 'yes'));
95 my $val = $self->param($name);
96 return (not defined($val) or ($val ne 'yes'));
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.
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 . '\'');
127 $self->{'snmpvars'}{$oid} = $value;
133 if( not exists( $self->{'snmpvars'}{$oid} ) )
135 $self->{'snmpvars'}{$oid} = undef;
141 # Clean the cache of sorted OIDs
142 $self->{'sortedoids'} = undef;
147 # check if the stored query results have such OID prefi
148 # WARNING: this method is deprecated. Use $dd->checkSnmpTable() instead.
156 if( exists( $self->{'snmpvars'}{$oid} ) )
164 # get the value of stored SNMP variable
165 # WARNING: this method is deprecated.
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.
183 # Remember the sorted OIDs, as sorting is quite expensive for large
186 if( not defined( $self->{'sortedoids'} ) )
188 $self->{'sortedoids'} = [];
189 push( @
{$self->{'sortedoids'}},
190 Net
::SNMP
::oid_lex_sort
( keys %{$self->{'snmpvars'}} ) );
194 my $prefixLen = length( $prefix ) + 1;
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 );
219 # device type is the registered discovery module name
225 $self->{'devtype'}{$type} = 1;
233 return $self->{'devtype'}{$type};
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
251 Debug
('Device capability: ' . $cap);
252 $self->{'caps'}{$cap} = 1;
260 return $self->{'caps'}{$cap};
267 Debug
('Clearing device capability: ' . $cap);
268 if( exists( $self->{'caps'}{$cap} ) )
270 delete $self->{'caps'}{$cap};
280 return $self->{'data'};
284 sub screenSpecialChars
289 $txt =~ s/:/{COLON}/gm;
290 $txt =~ s/;/{SEMICOL}/gm;
291 $txt =~ s/%/{PERCENT}/gm;
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'));
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 '');
337 my $checkval = $self->param( $sel . '-' . $attr );
339 Debug
('Checking attribute: ' . $attr .
340 ' and value: ' . $checkval);
341 my $ret = &{$treg->{'checkAttribute'}}( $self,
344 Debug
(sprintf('Returned value: %d', $ret));
348 my $rpn = Torrus
::RPN
->new();
349 my $result = $rpn->run( $expr, $callback );
350 Debug
('Selector result: ' . $result);
353 my $actions = $self->param( $sel . '-selector-actions' );
354 for my $action ( split('\s*,\s*', $actions) )
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,
378 # indent-tabs-mode: nil
379 # perl-indent-level: 4