/[nagios-plugins-perl]/trunk/plugins/check_munin.pl.test
ViewVC logotype

Contents of /trunk/plugins/check_munin.pl.test

Parent Directory Parent Directory | Revision Log Revision Log


Revision 193 - (show annotations) (download)
Mon Dec 18 14:45:38 2017 UTC (2 years, 11 months ago) by racvision
File size: 7361 byte(s)
Remise en ordre XH 201712
1 #!/usr/bin/perl -w
2 # $Id: check_munin.pl
3 #
4 # Copyright (C) 2003 Julien Rottenberg <julien@rottenberg.info>
5 # Copyright (c) 2010 St├ęphane Urbanovski <stephane.urbanovski@ac-nancy-metz.fr>
6 #
7 # check_munin.pl can check various modules of a server running munin-node.
8 #
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # as published by the Free Software Foundation; either version 2
13 # of the License, or (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty
17 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # you should have received a copy of the GNU General Public License
21 # along with this program (or with Nagios); if not, write to the
22 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 # Boston, MA 02111-1307, USA
24
25 use strict;
26 use warnings;
27
28 use Nagios::Plugin;
29 use Locale::gettext;
30 use POSIX qw(setlocale);
31
32 use File::Basename; # get basename()
33 use Time::HiRes qw(time); # get microtime
34 use IO::Socket::INET;
35 use Storable;
36
37 use Data::Dumper;
38
39 my $VERSION = '2.0';
40 my $DEBUG = 0;
41 my $TIMEOUT = 9;
42
43 # We need read/write access to this cache directory
44 my $CACHE_PATH='./check_munin_cache';
45
46 # i18n :
47 setlocale(LC_MESSAGES, '');
48 textdomain('nagios-plugins-perl');
49
50
51
52 my $np = Nagios::Plugin->new(
53 'version' => $VERSION,
54 'blurb' => _gt('Plugin to check a Munin node'),
55 'usage' => "Usage: %s -H <munin host> -m <munin module> [-t <timeout>] [ -c|--critical=<threshold> ] [ -w|--warning=<threshold> ] [ -v|--verbose ]",
56 timeout => $TIMEOUT+1
57 );
58 $np->add_arg (
59 spec => 'debug|d',
60 help => _gt('Debug level'),
61 default => 0,
62 );
63
64 $np->add_arg (
65 spec => 'host|H=s',
66 help => _gt('Munin host'),
67 required => 1,
68 );
69 $np->add_arg (
70 spec => 'module|m=s',
71 help => _gt('Munin module'),
72 required => 1,
73 );
74 $np->add_arg (
75 spec => 'port|P=s',
76 help => _gt('Munin node port'),
77 default => 4949,
78 );
79 $np->add_arg (
80 spec => 'w=f',
81 help => _gt('Warning request time threshold (in seconds)'),
82 default => 2,
83 label => 'FLOAT'
84 );
85 $np->add_arg (
86 spec => 'c=f',
87 help => _gt('Critical request time threshold (in seconds)'),
88 default => 10,
89 label => 'FLOAT'
90 );
91
92 $np->getopts;
93
94 $DEBUG = $np->opts->get('debug');
95 my $verbose = $np->opts->get('verbose');
96
97 # Thresholds :
98 # time
99 my $warn = $np->opts->get('w');
100 my $crit = $np->opts->get('c');
101
102 my $module = $np->opts->get('module');
103 my $host = $np->opts->get('host');
104 my $port = $np->opts->get('port');
105
106
107
108
109 # check if everything is ok
110 # check_options();
111
112 # Connect to host
113 # my ($munin,$error);
114 my %data;
115 # my $status = "0"; # status is ok by default, 1 = warning, 2 = critical
116 # my ($object_name, $object_value);
117 # my $response_text = '';
118
119
120 my $status = OK;
121 my $message = 'Nop';
122
123 logD("Connect to $host on port $port");
124 my $munin = IO::Socket::INET->new('PeerAddr' => $host, 'PeerPort' => $port, 'Timeout' => $TIMEOUT);
125 if ( !defined($munin) ) {
126 $np->nagios_exit(CRITICAL, sprintf(_gt("Can't connect to Munin node on %s:%d !"),$host,$port) );
127 }
128 $munin->autoflush(1); # This is the default but for version >= 1.18
129 logD("Connected");
130
131
132 # Check if we have a prompt
133 my $line = $munin->getline();
134 chomp($line);
135 if ( $line !~ /^\#\ munin\ node\ at\ / ) {
136 $np->nagios_exit(CRITICAL, sprintf(_gt("Can't get Munin prompt on node %s !"),$host) );
137 }
138
139 logD("Got prompt : $line");
140
141 if ( $module eq 'list' ) {
142 print $munin "list\n";
143 $line = $munin->getline();
144 $message = "Available modules : ".$line;
145
146 } else {
147
148 # First, get config
149 logD("Getting config for $module");
150 print $munin "config $module\n";
151
152 while ($line = $munin->getline()) {
153 chomp($line);
154 logD("config[$line]");
155 last if ($line eq '.');
156 if ( $line =~ /^# Unknown/ ) {
157 $np->nagios_exit(UNKNOWN, sprintf(_gt("Unknown module %s !"),$module) );
158
159 } elsif ( $line =~ /^([\w]+)\.(\w+)\ (.*)$/ ) {
160 my ($var,$property,$value) = ($1,$2,$3);
161 $data{$var}{$property} = $value;
162 }
163 }
164
165 # Then, get values
166 logD("Getting values for $module");
167 print $munin "fetch $module\n";
168
169 while ($line = $munin->getline()) {
170 chomp($line);
171 logD("fetch[$line]");
172 last if ($line eq '.');
173 if ( $line =~ /^# Unknown/ ) {
174 $np->nagios_exit(UNKNOWN, sprintf(_gt("Unknown module %s !"),$module) );
175
176 } elsif ( $line =~ /^([\w]+)\.value\ (.*)$/ ) {
177 my ($var,$value) = ($1,$2);
178 $data{$var}{'value'} = $value;
179 } else {
180 logW("Bad line !");
181 }
182 }
183
184 }
185
186 print Dumper(\%data);
187
188
189
190 print $munin "quit\n"; # Let's get out of here
191 close $munin; # Close the connection nicely
192
193 my $prevData = {};
194 my $nextData = {'TIMESTAMP' => time};
195 my $dt = -1;
196 my $cache_file = "$CACHE_PATH/cache-$host.pl";
197
198 if ( -f $cache_file ) {
199 logD("Loading previous values in $cache_file");
200 $prevData = retrieve($cache_file);
201 if ( defined($prevData->{'TIMESTAMP'}) ) {
202 $dt = time - $prevData->{'TIMESTAMP'};
203 }
204 }
205
206
207 foreach my $var ( keys(%data) ) {
208
209 my $val = undef;
210 my $label = $data{$var}{'label'} || $var;
211 if ( defined($data{$var}{'type'}) && $data{$var}{'type'} eq 'DERIVE' && $dt > 0) {
212 if ( defined($prevData->{$var}{'value'}) && defined($data{$var}{'value'})) {
213 my $dv = $data{$var}{'value'} - $prevData->{$var}{'value'};
214 $val = 60*($dv/$dt);
215 logD ("Derive for $label : $dv for ".$dt."s : ".$val." $var/mn");
216
217 $np->add_perfdata(
218 'label' => $var,
219 'value' => sprintf('%.6f',$val),
220 'uom' => "$var/mn",
221 'min' => $data{$var}{'min'}
222 # 'threshold' => $np->threshold()
223 );
224
225 }
226 if ( defined($data{$var}{'value'}) ) {
227 $nextData->{$var}{'value'} = $data{$var}{'value'};
228 }
229 }
230
231 }
232
233 if ( keys(%{$prevData}) > 0 ) {
234 logD("Saving previous values in $cache_file");
235 store ($nextData ,$cache_file);
236
237 }
238
239
240
241 $np->nagios_exit($status, $message );
242
243 # check data
244 # Structure %data : name ==> value
245
246 # foreach my $name ( keys %data) {
247 # my $value = $data{$name};
248 # print "$name $value\n" if $DEBUG;
249 # if (($value> $opt_w) && ($status ne 2))
250 # {
251 # $status = "1";
252 # $object_name = $name;
253 # $object_value = $value;
254 # }
255 # if ( $value > $opt_c)
256 # {
257 # $status = "2";
258 # $object_name = $name;
259 # $object_value = $value;
260 # }
261 # $response_text = $name.": ".$value." ".$response_text ; # this one will print all objects munin see
262 # print "$response_text\n" if $DEBUG;
263 # }
264 #
265 #
266 #
267 # if ($status eq 1)
268 # {
269 # print "$object_name value, $object_value, is above warning treshold $opt_w\n";
270 # $status = $ERRORS{"WARNING"};
271 #
272 # }
273 # elsif ($status eq 2)
274 # {
275 # print "$object_name value, $object_value, is above critical treshold $opt_c\n";
276 # $status = $ERRORS{"CRITICAL"};
277 #
278 # }
279 # else
280 # {
281 # print "$response_text \n";
282 # $status = $ERRORS{"OK"};
283 # }
284
285
286
287
288 sub logD {
289 print STDERR 'DEBUG: '.$_[0]."\n" if ($DEBUG);
290 }
291 sub logW {
292 print STDERR 'WARNING: '.$_[0]."\n" if ($DEBUG);
293 }
294 # Gettext wrapper
295 sub _gt {
296 return gettext($_[0]);
297 }
298
299 sub sanitize {
300 my $var = shift;
301 if ($module eq 'df') {
302 if (($var !~ m/dev/ ) || ($var =~ m/udev/) || ($var =~ m/shm/)) { # Get rid of non physical drives
303 $var = undef;
304 } else {
305 $var =~ s/\_/\//g;
306 }
307 }
308 return $var;
309
310 }
311

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.8