import torrus 1.0.9
[freeside.git] / torrus / perllib / Torrus / DevDiscover / Jacarta.pm
1 #  Copyright (C) 2010 Roman Hochuli
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: Jacarta.pm,v 1.1 2010-12-27 00:03:55 ivan Exp $
18
19 # Sensor-MIBs of Jacarta iMeter-Products
20
21
22 package Torrus::DevDiscover::Jacarta;
23
24 use strict;
25 use Torrus::Log;
26 use Switch;
27 use Data::Dumper;
28
29
30 $Torrus::DevDiscover::registry{'Jacarta'} = {
31     'sequence'     => 500,
32     'checkdevtype' => \&checkdevtype,
33     'discover'     => \&discover,
34     'buildConfig'  => \&buildConfig
35     };
36
37
38 our %oiddef =
39     (
40      'jacarta'             => '1.3.6.1.4.1.19011',
41      'sensorEntry'         => '1.3.6.1.4.1.19011.2.3.1.1',
42      'sensorIndex'         => '1.3.6.1.4.1.19011.2.3.1.1.1',
43      'sensorDescription'   => '1.3.6.1.4.1.19011.2.3.1.1.2',
44      'sensorType'          => '1.3.6.1.4.1.19011.2.3.1.1.3',
45      'sensorValue'         => '1.3.6.1.4.1.19011.2.3.1.1.4',
46      'sensorUnit'          => '1.3.6.1.4.1.19011.2.3.1.1.5',
47      );
48
49
50 our %sensor_types =
51     (
52      2 => {
53          'template' => 'Jacarta::imeter-humi-sensor',
54          'max' => 'NetBotz::humi-max',
55      },
56      3 => {
57          'template' => 'Jacarta::imeter-temp-sensor',
58          'max' => 'NetBotz::dew-max',
59      },
60      5 => {
61          'template' => 'Jacarta::imeter-amps-sensor',
62          'max' => 'NetBotz::dew-max',
63      },     
64      
65      );
66
67
68
69 sub checkdevtype
70 {
71     my $dd = shift;
72     my $devdetails = shift;
73
74     if( not $dd->oidBaseMatch
75         ( 'jacarta',
76           $devdetails->snmpVar( $dd->oiddef('sysObjectID') ) ) )
77     {
78         return 0;
79     }
80     
81     $devdetails->setCap('interfaceIndexingPersistent');
82
83     return 1;
84 }
85
86
87 sub discover
88 {
89     my $dd = shift;
90     my $devdetails = shift;
91
92     my $data = $devdetails->data();
93     my $session = $dd->session();
94
95     $data->{'Jacarta'} = {};
96     
97     my $sensorTable =
98         $session->get_table( -baseoid => $oiddef{'sensorEntry'} );
99
100     if( not defined( $sensorTable ) )
101     {
102         return 1;
103     }
104     
105     $devdetails->storeSnmpVars( $sensorTable );
106         
107     # store the sensor names to guarantee uniqueness
108     my %sensorNames;
109     
110     foreach my $INDEX
111         ($devdetails->getSnmpIndices( $oiddef{'sensorIndex'} ))
112     {
113         my $sensorType =
114             $devdetails->snmpVar( $oiddef{'sensorType'} . '.' .
115                                   $INDEX);
116         my $sensorName =
117             $devdetails->snmpVar( $oiddef{'sensorDescription'} . '.' .
118                                   $INDEX);
119         
120         if( not defined( $sensor_types{$sensorType} ) )
121         {
122             Error('Sensor ' . $INDEX . ' of unknown type: ' . $sensorType);
123             next;
124         }
125         
126         if( $sensorNames{$sensorName} )
127         {
128             Warn('Duplicate sensor names: ' . $sensorName);
129             $sensorNames{$sensorName}++;
130         }
131         else
132         {
133             $sensorNames{$sensorName} = 1;
134         }
135         
136         if( $sensorNames{$sensorName} > 1 )
137         {
138             $sensorName .= sprintf(' %d', $INDEX);
139         }
140         
141         my $leafName = $sensorName;
142         $leafName =~ s/\W/_/g;
143         
144         my $param = {
145             'imeter-sensor-index' => $INDEX,
146             'node-display-name' => $sensorName,
147             'graph-title' => $sensorName,
148             'precedence' => sprintf('%d', 1000 - $INDEX)
149             };
150
151         
152         if( defined( $sensor_types{$sensorType}{'max'} ) )
153         {
154             my $max =
155                 $devdetails->param($sensor_types{$sensorType}{'max'});
156             
157             if( defined($max) and $max > 0 )
158             {
159                 $param->{'upper-limit'} = $max;
160             }
161         }
162                 
163         $data->{'Jacarta'}{$INDEX} = {
164             'param'    => $param,
165             'leafName' => $leafName,
166             'template' => $sensor_types{$sensorType}{'template'}};
167         
168         Debug('Found Sensor ' . $INDEX . ' of type ' . $sensorType .
169               ', named ' . $sensorName );
170     }
171     
172     return 1;
173 }
174
175
176 sub buildConfig
177 {
178     my $devdetails = shift;
179     my $cb = shift;
180     my $devNode = shift;
181     
182     my $data = $devdetails->data();
183     
184     my $param = {
185         'node-display-name' => 'Sensors',
186         'comment' => 'All sensors connected via this iMeter Master',
187     };
188     
189     my $sensorTree =
190         $cb->addSubtree( $devNode, 'Sensors', $param );
191
192     foreach my $INDEX ( sort {$a<=>$b} keys %{$data->{'Jacarta'}} )
193     {
194         my $ref = $data->{'Jacarta'}{$INDEX};
195         
196         $cb->addLeaf( $sensorTree, $ref->{'leafName'}, $ref->{'param'},
197                       [$ref->{'template'}] );
198     }
199 }
200
201
202
203 1;
204
205
206 # Local Variables:
207 # mode: perl
208 # indent-tabs-mode: nil
209 # perl-indent-level: 4
210 # End: