blib/lib/Xymon/Monitor/SNMP.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Xymon::Monitor::SNMP;
2 1 ▶use Net::SNMP;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 0 ▶use Config::General;
Subroutine BEGIN: called CC=1 SCAR=2.6
4 use Switch;
5 use Xymon::Client;
6 use strict;
7  
8  
9 BEGIN {
10 use Exporter ();
11 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
12 $VERSION = '0.04';
13 @ISA = qw(Exporter);
14 #Give a hoot don't pollute, do not export more than needed by default
15 @EXPORT = qw();
16 @EXPORT_OK = qw();
17 %EXPORT_TAGS = ();
18 }
19  
20  
21  
22 sub new
23 {
24 my ($class, $parm) = @_;
25  
26 my $self = bless ({}, ref ($class) || $class);
27
28 $parm->{version} = $parm->{version} || "2c";
29
30 ($self->{session}, my $error) = Net::SNMP->session(
31 -hostname => $parm->{hostname},
32 -version => $parm->{version},
33 -community => $parm->{community}, # v1/v2c
34 );
35
36
37 $self->{parm} = $parm;
38 $self->{hostname} = $parm->{hostname};
39
40 #
41 # Read in config file of elements and oids
42 #
43 my $conf = new Config::General(
44 -ConfigFile => $parm->{oidconfig},
45 -MergeDuplicateBlocks => 1,
46 -ForceArray => 1
47 );
48  
49 my %config = $conf->getall;
50 $self->{elements} = \%config;
51
52
53 return $self;
54 }
55  
56 sub getValues {
57
58 my $self = shift;
59 my $rethash = {};
60
61 foreach my $variable ( sort keys %{$self->{elements}} ) {
62
63 foreach my $oid ( @{$self->{elements}->{$variable}->{OIDS}}) {
64 my $result = $self->{session}->get_request(-varbindlist => [$oid]);
65
66 if (!defined $result) {
67 printf "ERROR: %s.\n", $self->{session}->error();
68 $self->{session}->close();
69 exit 1;
70 }
71  
72 push @{$rethash->{$variable}}, $result->{$oid};
73  
74 }
75
76 };
77
78 return $rethash;
79
80 }
81 sub run {
82  
83
84 my $self = shift;
85
86 my $result = getValues($self);
87 my $elements = $self->{elements};
88 my $xymon = Xymon::Client->new({home=>"/home/hobbit/client/"});
89 my $color = "green";
90
91 foreach my $element ( keys %$result) {
92 my $count = 0;
93 my $elementstring = "";
94 foreach my $value ( @{$result->{$element}}) {
95
96 $count = $count + 1;
97
98 my $elementname = $element;
99 if(scalar @{$result->{$element}} > 1 ) {
100 $elementname = $element . $count;
101 $elementstring .= $elementname . ":" . $value ."\n";
102 } else {
103 $elementstring = $elementname . ":" . $value . "\n";
104 }
105
106 # Check Against Threshold.
107 # If already red then skip.
108 if( $color ne "red" ) {
109 if(compare($value,$elements->{$element}->{THRESH},$elements->{$element}->{THRESHDIR})) {
110 $color="red";
111 } else {
112 $color = "green";
113 }
114 }
115
116 }
117
118
119 # Send To Xymon
120
121 $xymon->send_status({
122 server => $self->{hostname},
123 testname => $element,
124 color => $color,
125 msg => "\n\n" . $elementstring
126
127 })
128 }
129
130
131
132
133 }
134  
135  
136  
137  
138 sub compare {
139
140 my ( $left, $right, $operator ) = @_;
141
142 if($operator eq "<") {
143 return $left < $right;
144 }
145
146 if($operator eq ">") {
147 return $left > $right;
148 }
149
150 if($operator eq "=" || $operator eq "==") {
151 return $left == $right;
152 }
153
154 if($operator eq "<>" || $operator eq "!=") {
155 return $left != $right;
156 }
157
158 return -1;
159
160 }
161  
162 sub close {
163
164 my $self = shift;
165 $self->{session}->close();
166 $self = "";
167
168 }
169  
170  
171  
172  
173  
174 #################### main pod documentation begin ###################
175 ## Below is the stub of documentation for your module.
176 ## You better edit it!
177  
178  
179 =head1 NAME
180  
181 Xymon::SNMP - Xymon Interface to SNMP
182  
183 =head1 SYNOPSIS
184  
185 use Xymon::SNMP;
186
187 my $snmp = Xymon::Monitor::SNMP->new({
188 hostname=>"$host",
189 version=>"2c",
190 community=>"public",
191 oidconfig=>"liebert.conf"
192 }
193 );
194  
195 $snmp->run();
196 $snmp->close();
197  
198  
199 =head1 DESCRIPTION
200  
201 Provides an interface for monitoring snmp based devices using
202 a configuration file to determin which oids to retrieve,
203 their thresholds and what tests they are mapped to.
204  
205 =head1 USAGE
206  
207 An example configuration file:
208  
209 #
210 # Output Current
211 #
212 <current>
213 OIDS = 1.3.6.1.2.1.33.1.4.4.1.3.1
214 OIDS = 1.3.6.1.2.1.33.1.4.4.1.3.2
215 OIDS = 1.3.6.1.2.1.33.1.4.4.1.3.3
216
217 THRESH = 350
218 THRESHDIR = >
219 </current>
220
221 #
222 # Percentage of maximum output current
223 #
224 <percentload>
225 OIDS = 1.3.6.1.2.1.33.1.4.4.1.5.1
226 OIDS = 1.3.6.1.2.1.33.1.4.4.1.5.2
227 OIDS = 1.3.6.1.2.1.33.1.4.4.1.5.3
228
229 THRESH = 90
230 THRESHDIR = >
231 </percentload>
232  
233  
234 The config file is loaded when the snmp object is created:
235  
236 my $snmp = Xymon::Monitor::SNMP->new({
237 hostname=>"router1",
238 version=>"2c",
239 community=>"public",
240 oidconfig=>"router.conf"
241 }
242 );
243  
244  
245  
246 This configuration file is used to map the OIDS listed and retrieve the
247 data as below where the first field is the host name, the second is
248 the testname, the third is the field within the test, the fourth is
249 the return value from snmp, the fifth is the comparison operator and
250 the sixth field is the threshold:
251  
252 router1:percentload:percentload1:44:>:90
253 router1:percentload:percentload2:41:>:90
254 router1:percentload:percentload3:31:>:90
255 router1:batterystatus:batterystatus:2:<>:2
256 router1:battcharge:battcharge:100:<:100
257 router1:batteryminutes:batteryminutes:29:<:15,
258 router1:current:current1:152:>:350
259 router1:current:current2:153:>:350
260 router1:current:current3:113:>:350
261  
262 These are used to generate the message to send to Xymon. The first for eaxmple
263 would be equivalent to:
264  
265 bb 127.0.0.1 "status router1.percentload green
266
267 percentload1:44
268 percentload2:41
269 percentload3:31"
270  
271 The above is an explanation of what happens behind the scenes, however the actual
272 generation and sending of the message to Xymon is all taken care of when you perform
273 the run method.
274  
275  
276 =head1 AUTHOR
277  
278 David Peters
279 CPAN ID: DAVIDP
280 davidp@electronf.com
281 http://www.electronf.com
282  
283 =head1 COPYRIGHT
284  
285 This program is free software; you can redistribute
286 it and/or modify it under the same terms as Perl itself.
287  
288 The full text of the license can be found in the
289 LICENSE file included with this module.
290  
291  
292 =head1 SEE ALSO
293  
294 perl(1).
295  
296 =cut
297  
298 #################### main pod documentation end ###################
299  
300  
301 1;
302 # The preceding line will help the module return a true value
303  
↑ Summary