import torrus 1.0.9
[freeside.git] / torrus / perllib / Torrus / DevDiscover / NetBotz.pm
1 #  Copyright (C) 2009 Stanislav Sinyagin
2 #
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.
7 #
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.
12 #
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., 675 Mass Ave, Cambridge, MA 02139, USA.
16
17 # $Id: NetBotz.pm,v 1.1 2010-12-27 00:03:47 ivan Exp $
18
19 # NetBotz modular sensors
20
21 package Torrus::DevDiscover::NetBotz;
22
23 use strict;
24 use Torrus::Log;
25
26
27 $Torrus::DevDiscover::registry{'NetBotz'} = {
28     'sequence'     => 500,
29     'checkdevtype' => \&checkdevtype,
30     'discover'     => \&discover,
31     'buildConfig'  => \&buildConfig
32     };
33
34
35 our %oiddef =
36     (
37      'netBotzV2Products'     => '1.3.6.1.4.1.5528.100.20',
38      );
39
40
41 our %sensor_types =
42     ('temp'   => {
43         'oid' => '1.3.6.1.4.1.5528.100.4.1.1.1',
44         'template' => 'NetBotz::netbotz-temp-sensor',
45         'max' => 'NetBotz::temp-max',
46         },
47      'humi'   => {
48          'oid' => '1.3.6.1.4.1.5528.100.4.1.2.1',
49          'template' => 'NetBotz::netbotz-humi-sensor',
50          'max' => 'NetBotz::humi-max',
51          },
52      'dew'    => {
53          'oid' => '1.3.6.1.4.1.5528.100.4.1.3.1',
54          'template' => 'NetBotz::netbotz-dew-sensor',
55          'max' => 'NetBotz::dew-max',
56          },
57      'audio'  => {
58          'oid' => '1.3.6.1.4.1.5528.100.4.1.4.1',
59          'template' => 'NetBotz::netbotz-audio-sensor'
60          },
61      'air' => {
62          'oid' => '1.3.6.1.4.1.5528.100.4.1.5.1',
63          'template' => 'NetBotz::netbotz-air-sensor'
64          },
65      'door' => {
66          'oid' => '1.3.6.1.4.1.5528.100.4.2.2.1',
67          'template' => 'NetBotz::netbotz-door-sensor'
68          },
69      );
70      
71      
72
73 sub checkdevtype
74 {
75     my $dd = shift;
76     my $devdetails = shift;
77
78     if( not $dd->oidBaseMatch
79         ( 'netBotzV2Products',
80           $devdetails->snmpVar( $dd->oiddef('sysObjectID') ) ) )
81     {
82         return 0;
83     }
84     
85     $devdetails->setCap('interfaceIndexingPersistent');
86
87     return 1;
88 }
89
90
91 sub discover
92 {
93     my $dd = shift;
94     my $devdetails = shift;
95
96     my $data = $devdetails->data();
97     my $session = $dd->session();
98
99     foreach my $stype (sort keys %sensor_types)
100     {
101         my $oid = $sensor_types{$stype}{'oid'};
102         
103         my $sensorTable = $session->get_table( -baseoid => $oid );
104         
105         if( defined( $sensorTable ) )
106         {
107             $devdetails->storeSnmpVars( $sensorTable );
108
109             # store the sensor names to guarantee uniqueness
110             my %sensorNames;
111             
112             foreach my $INDEX ($devdetails->getSnmpIndices($oid . '.1'))
113             {
114                 my $label = $devdetails->snmpVar( $oid . '.4.' . $INDEX );
115                 
116                 if( $sensorNames{$label} )
117                 {
118                     Warn('Duplicate sensor names: ' . $label);
119                     $sensorNames{$label}++;
120                 }
121                 else
122                 {
123                     $sensorNames{$label} = 1;
124                 }
125                 
126                 if( $sensorNames{$label} > 1 )
127                 {
128                     $label .= sprintf(' %d', $sensorNames{$label});
129                 }
130                 
131                 my $leafName = $label;
132                 $leafName =~ s/\W/_/g;
133
134                 my $param = {
135                     'netbotz-sensor-index' => $INDEX,
136                     'node-display-name' => $label,
137                     'graph-title' => $label,
138                     'precedence' => sprintf('%d', 1000 - $INDEX)
139                 };
140
141                 if( defined( $sensor_types{$stype}{'max'} ) )
142                 {
143                     my $max =
144                         $devdetails->param($sensor_types{$stype}{'max'});
145                     
146                     if( defined($max) and $max > 0 )
147                     {
148                         $param->{'upper-limit'} = $max;
149                     }
150                 }
151                 
152
153                 $data->{'NetBotz'}{$INDEX} = {
154                     'param'    => $param,
155                     'leafName' => $leafName,
156                     'template' => $sensor_types{$stype}{'template'}};
157             }
158         }        
159     }
160     
161     if( not defined($data->{'param'}{'comment'}) or
162         length($data->{'param'}{'comment'}) == 0 )
163     {
164         $data->{'param'}{'comment'} = 'NetBotz environment sensors';
165     }
166
167     return 1;
168 }
169
170
171 sub buildConfig
172 {
173     my $devdetails = shift;
174     my $cb = shift;
175     my $devNode = shift;
176
177     my $data = $devdetails->data();
178
179     foreach my $INDEX ( sort {$a<=>$b} keys %{$data->{'NetBotz'}} )
180     {
181         my $ref = $data->{'NetBotz'}{$INDEX};
182         
183         $cb->addLeaf( $devNode, $ref->{'leafName'}, $ref->{'param'},
184                       [$ref->{'template'}] );
185     }
186 }
187
188
189
190 1;
191
192
193 # Local Variables:
194 # mode: perl
195 # indent-tabs-mode: nil
196 # perl-indent-level: 4
197 # End: