import torrus 1.0.9
[freeside.git] / torrus / perllib / Torrus / DevDiscover / UcdSnmp.pm
1 #  Copyright (C) 2003  Shawn Ferry
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16
17 # $Id: UcdSnmp.pm,v 1.1 2010-12-27 00:03:47 ivan Exp $
18 # Shawn Ferry <sferry at sevenspace dot com> <lalartu at obscure dot org>
19
20 # Ucd Snmp Discovery
21
22 package Torrus::DevDiscover::UcdSnmp;
23
24 use strict;
25 use Torrus::Log;
26
27
28 $Torrus::DevDiscover::registry{'UcdSnmp'} = {
29     'sequence'     => 500,
30     'checkdevtype' => \&checkdevtype,
31     'discover'     => \&discover,
32     'buildConfig'  => \&buildConfig
33     };
34
35 our %oiddef =
36     (
37      # ucd
38      'ucd'                      => '1.3.6.1.4.1.2021',
39      'net_snmp'                 => '1.3.6.1.4.1.8072',
40
41      # We assume that if we have Avail we also have Total
42      'ucd_memAvailSwap'         => '1.3.6.1.4.1.2021.4.4.0',
43      'ucd_memAvailReal'         => '1.3.6.1.4.1.2021.4.6.0',
44
45      # If we have in we assume out
46      'ucd_ssSwapIn'             => '1.3.6.1.4.1.2021.11.3.0',
47
48      # If we have User we assume System and Idle
49      'ucd_ssCpuRawUser'         => '1.3.6.1.4.1.2021.11.50.0',
50      'ucd_ssCpuRawNice'         => '1.3.6.1.4.1.2021.11.51.0',
51      'ucd_ssCpuRawWait'         => '1.3.6.1.4.1.2021.11.54.0',
52      'ucd_ssCpuRawKernel'       => '1.3.6.1.4.1.2021.11.55.0',
53      'ucd_ssCpuRawInterrupts'   => '1.3.6.1.4.1.2021.11.56.0',
54      'ucd_ssCpuRawSoftIRQ'      => '1.3.6.1.4.1.2021.11.61.0',
55
56      # if we have Sent we assume Received
57      'ucd_ssIORawSent'          => '1.3.6.1.4.1.2021.11.57.0',
58
59      'ucd_ssRawInterrupts'      => '1.3.6.1.4.1.2021.11.59.0',
60      'ucd_ssRawContexts'        => '1.3.6.1.4.1.2021.11.60.0',
61
62      'ucd_laTable'              => '1.3.6.1.4.1.2021.10'
63      );
64
65 sub checkdevtype
66 {
67     my $dd = shift;
68     my $devdetails = shift;
69
70     my $sysObjectID = $devdetails->snmpVar( $dd->oiddef('sysObjectID') );
71     
72     if( not $dd->oidBaseMatch( 'ucd', $sysObjectID )
73         and
74         not $dd->oidBaseMatch( 'net_snmp', $sysObjectID ) )
75     {
76         return 0;
77     }
78
79     return 1;
80 }
81
82
83 sub discover
84 {
85     my $dd = shift;
86     my $devdetails = shift;
87
88     my $session = $dd->session();
89     my $data = $devdetails->data();
90
91     my @checkOids = (
92                      'ucd_memAvailSwap',
93                      'ucd_memAvailReal',
94                      'ucd_ssSwapIn',
95                      'ucd_ssCpuRawUser',
96                      'ucd_ssCpuRawWait',
97                      'ucd_ssCpuRawKernel',
98                      'ucd_ssCpuRawInterrupts',
99                      'ucd_ssCpuRawNice',
100                      'ucd_ssCpuRawSoftIRQ',
101                      'ucd_ssIORawSent',
102                      'ucd_ssRawInterrupts',
103                      );
104
105
106     my $result = $dd->retrieveSnmpOIDs( @checkOids );
107     if( defined( $result ) )
108     {
109         foreach my $oid ( @checkOids )
110         {
111             if( defined($result->{$oid}) and length($result->{$oid}) > 0 )
112             {
113                 $devdetails->setCap($oid);
114             }
115         }
116     }
117
118     if( $dd->checkSnmpTable('ucd_laTable') )
119     {
120         $devdetails->setCap('ucd_laTable');
121     }
122
123     return 1;
124 }
125
126
127 sub buildConfig
128 {
129     my $devdetails = shift;
130     my $cb = shift;
131     my $devNode = shift;
132
133     my $data = $devdetails->data();
134
135     # Hostresources MIB is optional in net-snmp. We try and use the same
136     # subtree name for UCD and Hostresources statistics.
137     
138     my $subtreeName =
139         $devdetails->param('RFC2790_HOST_RESOURCES::sysperf-subtree-name');
140     if( not defined( $subtreeName ) )
141     {
142         $subtreeName = 'System_Performance';
143         $devdetails->setParam
144             ('RFC2790_HOST_RESOURCES::sysperf-subtree-name', $subtreeName);
145     }
146
147     my @templates;
148     if( $devdetails->hasCap('ucd_ssIORawSent') )
149     {
150         push( @templates, 'UcdSnmp::ucdsnmp-blockio' );
151     }
152
153     if( $devdetails->hasCap('ucd_ssRawInterrupts') )
154     {
155         push( @templates,  'UcdSnmp::ucdsnmp-raw-interrupts' );
156     }
157
158     if( $devdetails->hasCap('ucd_laTable') )
159     {
160         push( @templates, 'UcdSnmp::ucdsnmp-load-average' );
161     }
162
163     if( $devdetails->hasCap('ucd_memAvailSwap') )
164     {
165         push( @templates, 'UcdSnmp::ucdsnmp-memory-swap' );
166     }
167
168     if( $devdetails->hasCap('ucd_memAvailReal') )
169     {
170         push( @templates, 'UcdSnmp::ucdsnmp-memory-real' );
171     }
172
173     my $cpuMultiParam;
174     my @cpuMultiTemplates;
175
176     if( $devdetails->hasCap('ucd_ssCpuRawUser') )
177     {
178         $cpuMultiParam = {
179             'graph-lower-limit' => '0',
180             'rrd-hwpredict'     => 'disabled',
181             'vertical-label'    => 'Cpu Usage',
182             'comment'           => 'Cpu Idle, Sys, User',
183             'ds-names'          => 'idle,sys,user',
184             'ds-type'           => 'rrd-multigraph'
185             };
186
187         push( @templates,
188               'UcdSnmp::ucdsnmp-cpu-user',
189               'UcdSnmp::ucdsnmp-cpu-system',
190               'UcdSnmp::ucdsnmp-cpu-idle' );
191
192         push( @cpuMultiTemplates,
193               'UcdSnmp::ucdsnmp-cpu-user-multi',
194               'UcdSnmp::ucdsnmp-cpu-system-multi',
195               'UcdSnmp::ucdsnmp-cpu-idle-multi' );
196
197         if( $devdetails->hasCap('ucd_ssCpuRawWait') )
198         {
199             push( @templates, 'UcdSnmp::ucdsnmp-cpu-wait' );
200             push( @cpuMultiTemplates, 'UcdSnmp::ucdsnmp-cpu-wait-multi' );
201
202             $cpuMultiParam->{'comment'}  .= ', Wait';
203             $cpuMultiParam->{'ds-names'} .= ',wait';
204         }
205
206         if( $devdetails->hasCap('ucd_ssCpuRawKernel') )
207         {
208             push( @templates, 'UcdSnmp::ucdsnmp-cpu-kernel' );
209             push( @cpuMultiTemplates, 'UcdSnmp::ucdsnmp-cpu-kernel-multi' );
210
211             $cpuMultiParam->{'comment'}  .= ', Kernel';
212             $cpuMultiParam->{'ds-names'} .= ',kernel';
213         }
214
215         if( $devdetails->hasCap('ucd_ssCpuRawNice') )
216         {
217             push( @templates, 'UcdSnmp::ucdsnmp-cpu-nice' );
218             push( @cpuMultiTemplates, 'UcdSnmp::ucdsnmp-cpu-nice-multi' );
219
220             $cpuMultiParam->{'comment'}  .= ', Nice';
221             $cpuMultiParam->{'ds-names'} .= ',nice';
222         }
223
224         if( $devdetails->hasCap('ucd_ssCpuRawInterrupts') )
225         {
226             push( @templates, 'UcdSnmp::ucdsnmp-cpu-interrupts' );
227             push( @cpuMultiTemplates,
228                   'UcdSnmp::ucdsnmp-cpu-interrupts-multi' );
229
230             $cpuMultiParam->{'comment'}  .= ', Interrupts';
231             $cpuMultiParam->{'ds-names'} .= ',int';
232         }
233
234         if( $devdetails->hasCap('ucd_ssCpuRawSoftIRQ') )
235         {
236             push( @templates, 'UcdSnmp::ucdsnmp-cpu-softirq' );
237             push( @cpuMultiTemplates,
238                   'UcdSnmp::ucdsnmp-cpu-softirq-multi' );
239
240             $cpuMultiParam->{'comment'}  .= ', SoftIRQs';
241             $cpuMultiParam->{'ds-names'} .= ',softirq';
242         }
243
244         $cpuMultiParam->{'comment'} =~ s/\,\s+(\w+)$/ and $1/;
245     }
246
247     my $perfNode = $cb->addSubtree( $devNode, $subtreeName,
248                                     undef, \@templates);
249
250     if( $cpuMultiParam )
251     {
252         $cb->addLeaf( $perfNode, 'Cpu_Stats',
253                       $cpuMultiParam, \@cpuMultiTemplates );
254     }
255 }
256
257
258 1;
259
260
261 # Local Variables:
262 # mode: perl
263 # indent-tabs-mode: nil
264 # perl-indent-level: 4
265 # End: