per-agent configuration of batch processors, #71837
[freeside.git] / torrus / perllib / Torrus / DevDiscover / RFC2790_HOST_RESOURCES.pm
1 #  Copyright (C) 2003 Shawn Ferry, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16
17 # $Id: RFC2790_HOST_RESOURCES.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 # Stanislav Sinyagin <ssinyagin@yahoo.com>
20
21 # Standard HOST_RESOURCES_MIB discovery, which should apply to most hosts
22
23 package Torrus::DevDiscover::RFC2790_HOST_RESOURCES;
24
25 use strict;
26 use Torrus::Log;
27
28
29 $Torrus::DevDiscover::registry{'RFC2790_HOST_RESOURCES'} = {
30     'sequence'     => 100,
31     'checkdevtype' => \&checkdevtype,
32     'discover'     => \&discover,
33     'buildConfig'  => \&buildConfig
34     };
35
36
37 # define the oids that are needed to determine support,
38 # capabilities and information about the device
39 our %oiddef =
40     (
41      'hrSystemUptime'               => '1.3.6.1.2.1.25.1.1.0',
42      'hrSystemNumUsers'             => '1.3.6.1.2.1.25.1.5.0',
43      'hrSystemProcesses'            => '1.3.6.1.2.1.25.1.6.0',
44      'hrSystemMaxProcesses'         => '1.3.6.1.2.1.25.1.7.0',
45      'hrMemorySize'                 => '1.3.6.1.2.1.25.2.2.0',
46      'hrStorageTable'               => '1.3.6.1.2.1.25.2.3.1',
47      'hrStorageIndex'               => '1.3.6.1.2.1.25.2.3.1.1',
48      'hrStorageType'                => '1.3.6.1.2.1.25.2.3.1.2',
49      'hrStorageDescr'               => '1.3.6.1.2.1.25.2.3.1.3',
50      'hrStorageAllocationUnits'     => '1.3.6.1.2.1.25.2.3.1.4',
51      'hrStorageSize'                => '1.3.6.1.2.1.25.2.3.1.5',
52      'hrStorageUsed'                => '1.3.6.1.2.1.25.2.3.1.6',
53      'hrStorageAllocationFailures'  => '1.3.6.1.2.1.25.2.3.1.7'
54      );
55
56
57 our %storageDescTranslate =  ( '/' => {'subtree' => 'root' } );
58
59 # storage type names from MIB
60 my %storageTypes =
61     (
62      1  => 'Other Storage',
63      2  => 'Physical Memory (RAM)',
64      3  => 'Virtual Memory',
65      4  => 'Fixed Disk',
66      5  => 'Removable Disk',
67      6  => 'Floppy Disk',
68      7  => 'Compact Disk',
69      8  => 'RAM Disk',
70      9  => 'Flash Memory',
71      10 => 'Network File System'
72      );
73
74 our $storageGraphTop;
75 our $storageHiMark;
76
77 sub checkdevtype
78 {
79     my $dd = shift;
80     my $devdetails = shift;
81
82     my $session = $dd->session();
83     my $data = $devdetails->data();
84
85     return $dd->checkSnmpOID('hrSystemUptime');
86 }
87
88
89 sub discover
90 {
91     my $dd = shift;
92     my $devdetails = shift;
93
94     my $data = $devdetails->data();
95     my $session = $dd->session();
96
97     if( $dd->checkSnmpOID('hrSystemNumUsers') )
98     {
99         $devdetails->setCap('hrSystemNumUsers');
100     }
101
102     if( $dd->checkSnmpOID('hrSystemProcesses') )
103     {
104         $devdetails->setCap('hrSystemProcesses');
105     }
106
107     # hrStorage support
108     my $hrStorageTable = $session->get_table( -baseoid =>
109                                               $dd->oiddef('hrStorageTable') );
110     if( defined( $hrStorageTable ) )
111     {
112         $devdetails->storeSnmpVars( $hrStorageTable );
113
114         my $ref = {};
115         $data->{'hrStorage'} = $ref;
116
117         foreach my $INDEX
118             ( $devdetails->getSnmpIndices($dd->oiddef('hrStorageIndex') ) )
119         {
120             my $typeNum = $devdetails->snmpVar( $dd->oiddef('hrStorageType') .
121                                                 '.' . $INDEX );
122             $typeNum =~ s/^[0-9.]+\.(\d+)$/$1/;
123
124             my $descr = $devdetails->snmpVar($dd->oiddef('hrStorageDescr')
125                                              . '.' . $INDEX);
126
127             my $used =  $devdetails->snmpVar($dd->oiddef('hrStorageUsed')
128                                              . '.' . $INDEX);
129
130             if( defined( $used ) and $storageTypes{$typeNum} )
131             {
132                 my $ref = { 'param' => {}, 'templates' => [] };
133                 $data->{'hrStorage'}{$INDEX} = $ref;
134                 my $param = $ref->{'param'};
135
136                 $param->{'storage-description'} = $descr;
137
138                 my $comment = $storageTypes{$typeNum};
139                 if( $descr =~ /^\// )
140                 {
141                     $comment .= ' (' . $descr . ')';
142                 }
143                 $param->{'comment'} = $comment;
144
145                 if( $storageDescTranslate{$descr}{'subtree'} )
146                 {
147                     $descr = $storageDescTranslate{$descr}{'subtree'};
148                 }
149                 $descr =~ s/^\///;
150                 $descr =~ s/\W/_/g;
151                 $param->{'storage-nick'} = $descr;
152
153                 my $units =
154                     $devdetails->snmpVar
155                     ($dd->oiddef('hrStorageAllocationUnits') . '.' . $INDEX);
156
157                 $param->{'collector-scale'} = sprintf('%d,*', $units);
158
159                 my $size =
160                     $devdetails->snmpVar
161                     ($dd->oiddef('hrStorageSize') . '.' . $INDEX);
162
163                 if( $size )
164                 {
165                     if( $storageGraphTop > 0 )
166                     {
167                         $param->{'graph-upper-limit'} =
168                             sprintf('%e',
169                                     $units * $size * $storageGraphTop / 100 );
170                     }
171
172                     if( $storageHiMark > 0 )
173                     {
174                         $param->{'upper-limit'} =
175                             sprintf('%e',
176                                     $units * $size * $storageHiMark / 100 );
177                     }
178                 }
179
180                 push( @{ $ref->{'templates'} },
181                       'RFC2790_HOST_RESOURCES::hr-storage-usage' );
182             }
183         }
184
185         if( scalar( keys %{$data->{'hrStorage'}} ) > 0 )
186         {
187             $devdetails->setCap('hrStorage');
188         }
189     }
190
191     return 1;
192 }
193
194
195 sub buildConfig
196 {
197     my $devdetails = shift;
198     my $cb = shift;
199     my $devNode = shift;
200
201     my $data = $devdetails->data();
202
203     { # Anon sub for System Info
204         my $subtreeName =
205             $devdetails->param('RFC2790_HOST_RESOURCES::sysperf-subtree-name');
206         if( not defined( $subtreeName ) )
207         {
208             $subtreeName = 'System_Performance';
209             $devdetails->setParam
210                 ('RFC2790_HOST_RESOURCES::sysperf-subtree-name', $subtreeName);
211         }
212
213         my $param = {};
214
215         my @templates =
216             ('RFC2790_HOST_RESOURCES::hr-system-performance-subtree',
217              'RFC2790_HOST_RESOURCES::hr-system-uptime');
218         if( $devdetails->hasCap('hrSystemNumUsers') )
219         {
220             push( @templates, 'RFC2790_HOST_RESOURCES::hr-system-num-users' );
221         }
222
223         if( $devdetails->hasCap('hrSystemProcesses') )
224         {
225             push( @templates, 'RFC2790_HOST_RESOURCES::hr-system-processes' );
226         }
227
228         my $subtreeNode = $cb->addSubtree( $devNode, $subtreeName,
229                                            $param, \@templates );
230     }
231
232     if( $devdetails->hasCap('hrStorage') )
233     {
234         # Build hrstorage subtree
235         my $subtreeName = 'Storage_Used';
236
237         my $param = {};
238         my @templates = ('RFC2790_HOST_RESOURCES::hr-storage-subtree');
239         my $subtreeNode = $cb->addSubtree( $devNode, $subtreeName,
240                                            $param, \@templates  );
241
242         foreach my $INDEX ( sort {$a<=>$b} keys %{$data->{'hrStorage'}} )
243         {
244             my $ref = $data->{'hrStorage'}{$INDEX};
245
246             #Display in index order, This is generally good(tm)
247             $ref->{'param'}->{'precedence'} = sprintf("%d", 1000 - $INDEX);
248
249             $cb->addLeaf( $subtreeNode, $ref->{'param'}{'storage-nick'},
250                           $ref->{'param'}, $ref->{'templates'} );
251         }
252     }
253 }
254
255
256 1;
257
258
259 # Local Variables:
260 # mode: perl
261 # indent-tabs-mode: nil
262 # perl-indent-level: 4
263 # End: