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

Contents of /trunk/plugins/check_by_ssh.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 121 - (show annotations) (download)
Thu Nov 12 13:29:04 2009 UTC (11 years ago) by racvision
File MIME type: text/plain
File size: 22767 byte(s)
convert source structure from autotools to rpm-build.sh
1 #!/usr/bin/perl -w
2 #
3 # (c) 2002-2004 St├ęphane Urbanovski <stephane.urbanovski@ac-nancy-metz.fr>
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty
12 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # you should have received a copy of the GNU General Public License
16 # along with this program (or with Netsaint); if not, write to the
17 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 # Boston, MA 02111-1307, USA
19 #
20 #
21 # $Id: check_by_ssh.pl,v 1.10 2006-12-15 12:58:40 uid5000 Exp $
22
23 BEGIN { $ENV{LANG} = "us_US" ;}
24
25 #============================================================================================
26 package CbS::Base;
27
28 # Base check object inherited by all other checks
29
30 use strict; # should never be differently :-)
31 use warnings;
32
33 require Exporter;
34 use vars qw(@ISA @EXPORT);
35 use Carp;
36
37 use File::Basename; # get dirname()
38 use IO::Handle;
39 use lib dirname($0); # to get local libs
40 use libplugins qw(&getBestUnit);
41
42 use IPC::Open3;
43
44 use Data::Dumper;
45
46 @ISA = qw( Exporter );
47 @EXPORT = qw(
48 execCmd setDebug getInfo getState
49 setOk setWarning setCritical setUnknown
50 isOk isWarning isCritical isUnknown
51 setWarnLevel setCritLevel
52 compareLevelKB );
53
54 # Constructor
55 sub new {
56 my $type = shift;
57 my %params = @_;
58 my $this = {};
59 $this->{'state'} = 'UNKNOWN';
60 $this->{'SSH_PATH'} = $params{'SSH_PATH'};
61 $this->{'SSH_TIMEOUT'} = $params{'SSH_TIMEOUT'};
62 $this->{'ssh_host'} = $params{'ssh_host'};
63 $this->{'ssh_user'} = $params{'ssh_user'};
64 $this->{'perf'} = [];
65 bless $this, $type;
66 }
67
68 sub debug() {
69 my ($this,$msg) = @_;
70 if ($this->{'DEBUG'} ) {
71 print STDERR 'DEBUG: '.$msg."\n";
72 };
73 }
74
75 #
76 sub setDebug() {
77 my ($this,$d) = @_;
78 $this->{'DEBUG'} = $d || 1;
79 }
80
81 # Return the nagios information message
82 sub getInfo {
83 my $this = shift;
84 return $this->{'print'};
85 }
86
87 # Return the nagios performance data extention
88 sub getPerf {
89 my $this = shift;
90 if ( @{$this->{'perf'}} ) {
91 return '|'.join(" ",@{$this->{'perf'}});
92 }
93 return '';
94 }
95 sub addPerf {
96 my $this = shift;
97 push @{$this->{'perf'}},@_;
98 }
99
100 # Return the nagios state
101 sub getState {
102 my $this = shift;
103 return $this->{'state'};
104 }
105
106 # Set this check to corresponding level and set the nagios information message
107 sub setOk($) {
108 my ($this,$p) = @_;
109 if ($p) {
110 $this->{'print'} = $p;
111 }
112 $this->{'state'} = 'OK';
113 }
114 sub setWarning($) {
115 my ($this,$p) = @_;
116 if ($p) {
117 $this->{'print'} = $p;
118 }
119 $this->{'state'} = 'WARNING';
120 }
121 sub setCritical($) {
122 my ($this,$p) = @_;
123 if ($p) {
124 $this->{'print'} = $p;
125 }
126 $this->{'state'} = 'CRITICAL';
127 }
128 sub setUnknown($) {
129 my ($this,$p) = @_;
130 if ($p) {
131 $this->{'print'} = $p;
132 }
133 $this->{'state'} = 'UNKNOWN';
134 }
135
136 sub isOk() {
137 my $this = shift;
138 return $this->{'state'} eq 'OK';
139 }
140 sub isWarning() {
141 my $this = shift;
142 return $this->{'state'} eq 'WARNING';
143 }
144 sub isCritical() {
145 my $this = shift;
146 return $this->{'state'} eq 'CRITICAL';
147 }
148 sub isUnknown() {
149 my $this = shift;
150 return $this->{'state'} eq 'UNKNOWN';
151 }
152
153 # Set Warn and Critical level:
154 sub setWarnLevel($) {
155 my $this = shift;
156 $this->{'WarnLevel'} = shift;
157 }
158 sub setCritLevel($) {
159 my $this = shift;
160 $this->{'CritLevel'} = shift;
161 }
162
163 # Execute ssh command
164 sub execCmd {
165 my $this = shift;
166 my ($args) = @_;
167 my $cmd = $this->{'SSH_PATH'}." ".$this->{'ssh_user'}."\@".$this->{'ssh_host'}." \"".$args."\"";
168
169 my $timeout_alarm = 0;
170 my %return;
171 my @stdout = ();
172 my @stderr = ();
173
174 my $cmdstdin = IO::Handle->new();
175 my $cmdstdout = IO::Handle->new();
176 my $cmdstderr = IO::Handle->new();
177
178 $return{"pid"} = 0;
179
180 $SIG{'ALRM'} = sub {
181 $this->debug( "SIG ALRM !");
182 $cmdstdin->close();
183 $cmdstdout->close();
184 $cmdstderr->close();
185 $timeout_alarm = 1;
186 if ( $return{"pid"} > 1) {
187 my $killed = kill 9,$return{"pid"};
188 $this->debug( $killed." process killed");
189 }
190 };
191
192 $this->debug( "Execution : ".$cmd);
193
194 alarm($this->{'SSH_TIMEOUT'});
195 $return{"pid"} = open3($cmdstdin,$cmdstdout,$cmdstderr,$cmd) ;
196 $this->debug( "\$?=".$?);
197 $this->debug( "PID = ".$return{"pid"});
198 $this->debug( "Alarm = ".$this->{'SSH_TIMEOUT'});
199
200 if ( $return{"pid"} == -1 ) {
201 push (@stderr ,sprintf(_("Enable to execute : [%s] %s"),$cmd,$!));
202 } else {
203 if ( $cmdstdout->opened() ) {
204 @stdout = $cmdstdout->getlines();
205 $this->debug( "stdout got ".scalar(@stdout)." lines");
206 }
207 if ( $cmdstderr->opened() ) {
208 @stderr = $cmdstderr->getlines();
209 $this->debug( "stderr got ".scalar(@stderr)." lines");
210 }
211 my $waitpid = waitpid($return{"pid"}, 0);
212 $return{"val"} = $?;
213 $this->debug( "\$?=".$return{"val"});
214 $this->debug( "waitpid=".$waitpid);
215
216 if ( $timeout_alarm ) {
217 push (@stderr ,sprintf(_("Command timeout after %is !"),$this->{'SSH_TIMEOUT'}));
218
219 } else {
220 $return{"val"} = $return{"val"} >> 8;
221 chomp (@stdout);
222 chomp (@stderr);
223 }
224 }
225
226 $cmdstdin->close();
227 $cmdstdout->close();
228 $cmdstderr->close();
229 alarm(0);
230
231 $return{'stdout'} = \@stdout;
232 $return{'stderr'} = \@stderr;
233 $this->debug( Dumper(\%return));
234 return \%return;
235 }
236
237 sub compareLevelKB {
238
239 my $this = shift;
240 my ($what,$val,$total) = @_;
241
242 my $viewVal = getBestUnit($val);
243 my $viewTotal = '';
244 if ($total) {
245 $viewTotal = "/ ".getBestUnit($total)." (".sprintf("%.2f",100*($val/$total))."%)";
246 }
247
248 $this->setOk(sprintf(_("%s is correct:"),$what)." $viewVal $viewTotal");
249
250 my $level = $this->{'WarnLevel'};
251 if ( $level->getValueKB() ) {
252 my $viewLvl = _("Warning level: ").getBestUnit($level->getValueKB());
253 if ( $level->isMax() && ($val >= $level->getValueKB()) ) {
254 $this->setWarning(sprintf(_("%s too high:"),$what)." $viewVal $viewTotal $viewLvl");
255 } elsif ( $level->isMin() && ($val <= $level->getValueKB()) ) {
256 $this->setWarning(sprintf(_("%s too low:"),$what)." $viewVal $viewTotal $viewLvl");
257 }
258 } elsif ( $level->isPercent() && $total && ($total > 0) ) {
259 my $viewLvl = _("Warning level: ").$level->getValue()."%";
260 if ( $level->isMax() && ((100*$val/$total) >= $level->getValue()) ) {
261 $this->setWarning(sprintf(_("%s too high:"),$what)." $viewVal $viewTotal $viewLvl");
262 } elsif ( $level->isMin() && ((100*$val/$total) <= $level->getValue()) ) {
263 $this->setWarning(sprintf(_("%s too low:"),$what)." $viewVal $viewTotal $viewLvl");
264 }
265 }
266
267 $level = $this->{'CritLevel'};
268 if ( $level->getValueKB() ) {
269 my $viewLvl = _("Critical level: ").getBestUnit($level->getValueKB());
270 if ( $level->isMax() && ($val >= $level->getValueKB()) ) {
271 $this->setCritical(sprintf(_("%s too high:"),$what)." $viewVal $viewTotal $viewLvl");
272 } elsif ( $level->isMin() && ($val <= $level->getValueKB()) ) {
273 $this->setCritical(sprintf(_("%s too low:"),$what)." $viewVal $viewTotal $viewLvl");
274 }
275 } elsif ( $level->isPercent() && $total && ($total > 0) ) {
276 my $viewLvl = _("Warning level: ").$level->getValue()."%";
277 if ( $level->isMax() && ((100*$val/$total) >= $level->getValue()) ) {
278 $this->setCritical(sprintf(_("%s too high:"),$what)." $viewVal $viewTotal $viewLvl");
279 } elsif ( $level->isMin() && ((100*$val/$total) <= $level->getValue()) ) {
280 $this->setCritical(sprintf(_("%s too low:"),$what)." $viewVal $viewTotal $viewLvl");
281 }
282 }
283 }
284
285
286 1;
287
288 #============================================================================================
289 package CbS::Osinfo;
290
291 our @ISA = qw( CbS::Base );
292
293 use strict; # should never be differently :-)
294 use warnings;
295
296 sub new {
297 my $type = shift;
298 my $this = CbS::Base->new(@_);
299 $this->{'description'} = _("Get os informations");
300 $this->{'syntaxe'} = 'osinfo';
301 $this->{'command'} = '/bin/uname -a';
302 bless $this, $type;
303 }
304
305 sub check {
306 my $this = shift;
307 my ($args) = @_;
308 my $ret = $this->execCmd($this->{'command'});
309 if ( ! $ret->{'val'}) {
310 $this->setOk($ret->{'stdout'}->[0]);
311 } else {
312 $this->setCritical(_("Execution failed : ").join('',@{$ret->{'stderr'}}));
313 }
314 return $ret->{'val'};
315 }
316
317 1;
318 #============================================================================================
319 package CbS::MailQueue;
320
321 our @ISA = qw( CbS::Base );
322
323 use strict; # should never be differently :-)
324 use warnings;
325
326 use File::Basename; # get dirname()
327 use lib dirname($0); # to get local libs
328 use libplugins qw(&getBestUnit);
329
330 sub new {
331 my $type = shift;
332 my $this = CbS::Base->new(@_);
333 $this->{'description'} = _("Get number of messages in mailq");
334 $this->{'syntaxe'} = 'mailq ';
335 $this->{'command'} = '/usr/bin/sudo /home/racvision/mailqsize';
336 bless $this, $type;
337 }
338
339 sub check {
340 my $this = shift;
341 my ($args) = @_;
342
343 my $ret = $this->execCmd($this->{'command'});
344
345 if ( ! $ret->{'val'}) {
346 my %mq;
347 $mq{'Incomming'} = -1;
348 $mq{'Active'} = -1;
349 $mq{'Deferred'} = -1;
350
351 # parse results
352 foreach my $l (@{$ret->{'stdout'}}) {
353 if ($l =~ /(\w+)\:\s+(\d+)/) {
354 $mq{$1}=$2;
355 }
356 $this->debug("Command output: $l");
357 }
358
359 $this->setOk(sprintf(_("Mailqueue size correct (Incomming/Active/Deferred): %s/%s/%s"),$mq{'Incomming'},$mq{'Active'},$mq{'Deferred'}));
360
361 my $level = $this->{'WarnLevel'};
362 if ( $level->isMax() && ($mq{'Deferred'} >= $level->getValue()) ) {
363 if ( $this->isOk() ) {
364 $this->setWarning();
365 }
366 $this->{'print'} = sprintf(_("%s too high:"),'Deferred')." ".$mq{'Deferred'};
367 }
368
369
370 } else {
371 $this->setCritical(_("Execution failed : ").join('',@{$ret->{'stderr'}}));
372 }
373 return $ret->{'val'};
374
375 }
376
377 1;
378 #============================================================================================
379 package CbS::Mem;
380
381 our @ISA = qw( CbS::Base );
382
383 use strict; # should never be differently :-)
384 use warnings;
385
386 use File::Basename; # get dirname()
387 use lib dirname($0); # to get local libs
388 use libplugins qw(&getBestUnit);
389
390 sub new {
391 my $type = shift;
392 my $this = CbS::Base->new(@_);
393 $this->{'description'} = _("Get memory usage");
394 $this->{'syntaxe'} = 'mem [MemTotal|MemFree|MemShared|Buffers|Cached|SwapTotal|SwapFree|RealFree|SwapUsedPercent]';
395 $this->{'command'} = 'cat /proc/meminfo';
396 bless $this, $type;
397 }
398
399 sub check {
400 my $this = shift;
401 my ($args) = @_;
402
403 if ( ! $args ) {
404 $this->setUnknown( _("Missing argument"));
405 return 1;
406 }
407
408 my $ret = $this->execCmd($this->{'command'});
409
410 if ( ! $ret->{'val'}) {
411 my %mem;
412 $mem{'MemTotal'} = 0;
413 $mem{'Buffers'} = 0;
414 $mem{'Cached'} = 0;
415 $mem{'SwapTotal'} = 0;
416 $mem{'SwapFree'} = 0;
417
418 # parse /proc/meminfo
419 foreach my $l (@{$ret->{stdout}}) {
420 if ($l =~ /(\w+)\:\s+(\d+) kB/) {
421 $mem{$1}=$2;
422 }
423 }
424
425 $this->setUnknown( _("Unkown argument: ").$args );
426
427 if ( ($args eq "MemFree")||($args eq "MemRealFree")||($args eq "MemRealUsed")||($args eq "Cached")||($args eq "Buffers")||($args eq "MemTotal") ) {
428
429 $mem{'MemRealFree'} = $mem{'MemFree'} + $mem{'Buffers'} + $mem{'Cached'};
430 $mem{'MemRealUsed'} = $mem{'MemTotal'} - $mem{'MemRealFree'};
431 $this->compareLevelKB(sprintf("%s usage",$args),$mem{$args},$mem{MemTotal});
432 $this->addPerf('used='.$mem{'MemRealUsed'}.'KB','total='.$mem{'MemTotal'}.'KB');
433
434 } elsif ( ($args eq "SwapFree")||($args eq "SwapUsed")||($args eq "SwapTotal") ) {
435
436 $this->compareLevelKB(sprintf("%s usage",$args),$mem{$args},$mem{SwapTotal});
437 $this->addPerf('used='.$mem{'SwapUsed'}.'KB;;;0;'.$mem{'SwapTotal'});
438 }
439
440 } else {
441 $this->setCritical(_("Execution failed : ").join('',@{$ret->{'stderr'}}));
442 }
443 return $ret->{'val'};
444
445 }
446
447 1;
448 #============================================================================================
449 package CbS::Disk;
450
451 our @ISA = qw( CbS::Base );
452
453 use strict; # should never be differently :-)
454 use warnings;
455
456 use File::Basename; # get dirname()
457 use lib dirname($0); # to get local libs
458 use libplugins qw(%EXIT_CODES &showUsage &showVersion &getBestUnit);
459
460
461 sub new {
462 my $type = shift;
463 my $this = CbS::Base->new(@_);
464 $this->{'description'} = _("Get disk usage");
465 $this->{'syntaxe'} = 'disk <mount point>';
466 $this->{'command'} = '/bin/df';
467 bless $this, $type;
468 }
469
470 sub check {
471 my $this = shift;
472 my ($args) = @_;
473
474 if ( ! $args ) {
475 $this->setUnknown( _("Missing argument"));
476 return;
477 }
478
479 my $mpoint;
480 my $what;
481
482 if ($args =~ /(^\/[\/\w\d\-]*)\s+(\w+)$/) {
483 ($mpoint,$what) = ($1,$2);
484 } elsif ($args =~ /(^[\/\w\d\-]*)$/) {
485 ($mpoint,$what) = ($1,'used');
486 } else {
487 $this->setUnknown( _("Unkown argument: ").$args );
488 return;
489 }
490
491 my $ret = $this->execCmd($this->{'command'});
492
493 if ( ! $ret->{'val'}) {
494 my %disks;
495
496 # parse df results
497 #Filesystem 1k-blocks Used Available Use% Mounted on
498 #/dev/sda1 30957 10826 18533 37% /boot
499
500 foreach my $l (@{$ret->{stdout}}) {
501 if ($l =~ /([\/\w\d\-]+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)%\s+([\/\w\d\-]+)/) {
502 my %t = (
503 device => $1,
504 total => $2,
505 used => $3,
506 free => $4,
507 usedPercent => $5,
508 mountPoint => $6,
509 );
510 $disks{$6} = \%t;
511 if ($this->{'DEBUG'}) {
512 print "df: $l\n";
513 }
514 }
515 }
516
517 if ( $mpoint eq "all" ) {
518 if ( !$this->{'WarnLevel'}->isPercent() || !$this->{'CritLevel'}->isPercent() ) {
519 $this->setUnknown( _("Warn or/and Crit level percent not set !"));
520 return ;
521 }
522 $this->setOk();
523 my @info = ();
524 foreach my $mp (keys %disks) {
525 my $mpView = sprintf(_("Disk <b>%s</b> usage"),$mp);
526 my $val = $disks{$mp}{used};
527 my $viewVal = getBestUnit($val);
528 my $total = $disks{$mp}{total};
529 my $viewTotal = "/ ".getBestUnit($total)." (".sprintf("%.2f",100*($val/$total))."%)";
530
531 my $level = $this->{'WarnLevel'};
532
533 if ( $level->isMax() && ((100*$val/$total) >= $level->getValue()) ) {
534 if ( $this->isOk() ) {
535 $this->setWarning();
536 }
537 push (@info, sprintf(_("%s too high:"),$mpView)." $viewVal $viewTotal");
538 } elsif ( $level->isMin() && ((100*$val/$total) <= $level->getValue()) ) {
539 if ( $this->isOk() ) {
540 $this->setWarning();
541 }
542 push (@info, sprintf(_("%s too low:"),$mpView)." $viewVal $viewTotal");
543 }
544
545 $level = $this->{'CritLevel'};
546
547 if ( $level->isMax() && ((100*$val/$total) >= $level->getValue()) ) {
548 $this->setCritical();
549 push (@info, sprintf(_("%s too high:"),$mpView)." $viewVal $viewTotal");
550 } elsif ( $level->isMin() && ((100*$val/$total) <= $level->getValue()) ) {
551 $this->setCritical();
552 push (@info, sprintf(_("%s too low:"),$mpView)." $viewVal $viewTotal");
553 }
554 }
555 if ( $this->isOk() ) {
556 $this->{'print'} = sprintf(_("All disks (%s) are inside fixed limits."),join(",",keys %disks),$this->{'WarnLevel'}->getValue());
557
558 } elsif ( $this->isWarning() ) {
559 my $viewLvl = _("Warning level: ").$this->{'WarnLevel'}->getValue()."%";
560 $this->{'print'} = "Warning: ".join(", ",@info)." ".$viewLvl;
561
562 } else {
563 my $viewLvl = _("Critical level: ").$this->{'CritLevel'}->getValue()."%";
564 $this->{'print'} = "Critical: ".join(", ",@info)." ".$viewLvl;
565 }
566
567 } elsif ( exists($disks{$mpoint}{used})) {
568 $this->compareLevelKB("Disk <b>$mpoint</b> usage",$disks{$mpoint}{used},$disks{$mpoint}{total});
569 $this->addPerf('used='.$disks{$mpoint}{used}.'KB;;;0;'.$disks{$mpoint}{total});
570 } else {
571 $this->setUnknown( _("Unkown mount point: ").$mpoint );
572 return;
573 }
574
575 } else {
576 $this->setCritical(_("Execution failed : ").join('',@{$ret->{'stderr'}}));
577 }
578
579 }
580
581 1;
582 #============================================================================================
583 package CbS::Level;
584
585 use strict; # should never be differently :-)
586 use warnings;
587
588 our %EXP_B = (
589 K => 1024,
590 M => 2**20,
591 G => 2**30,
592 T => 2**40,
593 );
594 our %EXP_N = (
595 K => 10**3,
596 M => 10**6,
597 G => 10**9,
598 T => 10**12,
599 );
600
601 # Constructor
602 sub new {
603 my $type = shift;
604 my $this = {};
605 $this->{'string'} = shift;
606 $this->{'err'} = undef;
607 $this->{'type'} = "?";
608 $this->{'minmax'} = 'max';
609
610 bless $this, $type;
611 }
612
613 sub parse {
614 my $this = shift;
615 my $s = $this->{'string'};
616
617 if ( ! $s ) {
618
619 } elsif ( $s =~ /^(\-?)([\d\.]+)$/ ) {
620 $this->{'type'} = "N";
621 if ( $1 eq "-" ) {
622 $this->{'minmax'} = 'min';
623 }
624 $this->{'val'} = abs($2);
625
626 } elsif ( $s =~ /^(\-?)([\d\.]+)\%$/ ) {
627 $this->{'type'} = "%";
628 if ( $1 eq "-" ) {
629 $this->{'minmax'} = 'min';
630 }
631 $this->{'val'} = abs($2);
632
633 } elsif ( $s =~ /^(\-?)([\d\.]+)([TGMKmu])$/ ) {
634 $this->{'type'} = "N";
635 if ( $1 eq "-" ) {
636 $this->{'minmax'} = 'min';
637 }
638 $this->{'val'} = abs($2);
639 if ( exists($EXP_N{$3}) ) {
640 $this->{'val'} *= $EXP_N{$3};
641 }
642
643 } elsif ( $s =~ /^(\-?)([\d\.]+)([TGMKmu])B$/ ) {
644 $this->{'type'} = "B";
645 if ( $1 eq "-" ) {
646 $this->{'minmax'} = 'min';
647 }
648 $this->{'val'} = abs($2);
649 if ( exists($EXP_B{$3}) ) {
650 $this->{'val'} *= $EXP_B{$3};
651 }
652
653 } else {
654 $this->{'err'} = _("Parse error");
655
656 }
657 return $this->{'err'};
658 }
659
660 sub getValue {
661 my $this = shift;
662 return $this->{'val'};
663 }
664
665 sub getValueKB {
666 my $this = shift;
667 if ( ($this->{'type'} eq 'B') || ($this->{'type'} eq 'N') ) {
668 return $this->{'val'}/1024;
669 }
670 return undef;
671 }
672
673 sub isPercent {
674 my $this = shift;
675 return ($this->{'type'} eq '%');
676 }
677
678 sub isMax {
679 my $this = shift;
680 return ($this->{'minmax'} eq 'max');
681 }
682 sub isMin {
683 my $this = shift;
684 return ($this->{'minmax'} eq 'min');
685 }
686
687 sub getError {
688 my $this = shift;
689 return $this->{'err'};
690 }
691
692
693 1;
694 #============================================================================================
695 package main;
696
697 use strict; # should never be differently :-)
698 use warnings;
699
700 use File::Basename; # get dirname()
701 use lib dirname($0); # to get local libs
702 use libplugins qw(%EXIT_CODES &showUsage &showVersion &getBestUnit);
703
704
705 use Data::Dumper; # debug tool
706
707
708 # Default values :
709 my %DEFVALUES = (
710 'SSH_USER' => {
711 'v' => "racvision",
712 'desc' => _("SSH User"),
713 },
714 'SSH_PATH' => {
715 'v' => '/usr/bin/env - /usr/bin/ssh -ax -o BatchMode=yes -c blowfish ',
716 'desc' => _("SSH Path and arguments"),
717 },
718 'SSH_PORT' => {
719 'v' => 22,
720 'desc' => _("SSH Port"),
721 },
722 'SSH_TIMEOUT' => {
723 'v' => 5,
724 'desc' => _("SSH Timeout"),
725 },
726 'VERSION' => {
727 'v' => "1.1",
728 'desc' => _("Plugin Version"),
729 },
730 );
731
732 my $DEBUG = 0;
733
734 # Get the options :
735 my ($opt_H, $opt_u, $opt_c, $opt_w, $opt_a);
736 use vars qw($opt_V $opt_h $opt_m $opt_perf $opt_H $opt_p $opt_C);
737
738 Getopt::Long::GetOptions (
739 "V" => \$opt_V, "version" => \$opt_V,
740 "h|?" => \$opt_h, "help" => \$opt_m,
741 "man" => \$opt_m,
742 "perf" => \$opt_perf,
743
744 "d" => \$DEBUG, "debug=i" => \$DEBUG,
745 "w=s" => \$opt_w, "warning=s" => \$opt_w,
746 "c=s" => \$opt_c, "critical=s" => \$opt_c,
747
748 "H=s" => \$opt_H, "hostname=s" => \$opt_H,
749 "u=s" => \$opt_u, "user=s" => \$opt_u,
750 "C=s" => \$opt_C, "command=s" => \$opt_C,
751 "a=s" => \$opt_a, "args=s" => \$opt_a,
752 );
753
754 if ( defined ($opt_h) || defined ($opt_m) ) {
755 &showUsage($opt_m,\%DEFVALUES);
756 } elsif (defined ($opt_V)) {
757 &showVersion($DEFVALUES{VERSION}{v});
758 }
759
760 if ( (!defined ($opt_H)) || ($opt_H eq "")) {
761 print _("Missing remote hostname parameter !")."\n";
762 &showUsage();
763 }
764 if ( (!defined ($opt_C)) || ($opt_C eq "")) {
765 print _("Missing command parameter !")."\n";
766 &showUsage();
767 }
768
769 $opt_u = $opt_u || $DEFVALUES{SSH_USER}{v};
770
771 my $os='linux';
772 my $args = $opt_a;
773
774 my $state = "UNKNOWN";
775
776 my %inits = (
777 SSH_PATH => $DEFVALUES{SSH_PATH}{v},
778 ssh_host => $opt_H,
779 ssh_port => $DEFVALUES{SSH_PORT}{v},
780 ssh_user => $opt_u,
781 SSH_TIMEOUT => $DEFVALUES{SSH_TIMEOUT}{v},
782 );
783
784
785 my $checkObj ;
786
787
788 my $wLevel = new CbS::Level($opt_w);
789 if ( $wLevel->parse() ) {
790 print $wLevel->getError();
791 exit $EXIT_CODES{$state};
792 }
793 my $cLevel = new CbS::Level($opt_c);
794 if ( $cLevel->parse() ) {
795 print $cLevel->getError();
796 exit $EXIT_CODES{$state};
797 }
798
799
800 if ( $opt_C eq "osinfo" ) {
801 $checkObj = new CbS::Osinfo(%inits);
802
803 } elsif ( $opt_C eq "mailq" ) {
804 $checkObj = new CbS::MailQueue(%inits);
805 $checkObj->setWarnLevel($wLevel);
806 $checkObj->setCritLevel($cLevel);
807
808 } elsif ( $opt_C eq "mem" ) {
809 $checkObj = new CbS::Mem(%inits);
810 $checkObj->setWarnLevel($wLevel);
811 $checkObj->setCritLevel($cLevel);
812
813 } elsif ( $opt_C eq "disk" ) {
814 $checkObj = new CbS::Disk(%inits);
815 $checkObj->setWarnLevel($wLevel);
816 $checkObj->setCritLevel($cLevel);
817
818 }
819
820 if ($DEBUG) {
821 print $checkObj->setDebug($DEBUG);
822 }
823
824
825 if ( $checkObj ) {
826
827 $checkObj->check($opt_a);
828
829 if ($checkObj->getInfo()) {
830 print $checkObj->getInfo();
831 if ( $opt_perf ) {
832 print $checkObj->getPerf();
833 }
834 print "\n";
835 } else {
836 print _("No Output !")."\n";
837 }
838 $state = $checkObj->getState();
839
840 if ($DEBUG) {
841 print Dumper($checkObj);
842 }
843
844 } else {
845 print _("Unknown command !")."\n";
846 }
847
848
849 exit $EXIT_CODES{$state};
850
851
852 __END__
853
854 =head1 NAME
855
856 Check remote services using ssh access.
857
858 =head1 SYNOPSIS
859
860 B<check_by_ssh.pl> S<-H I<remote host>> S<[-u I<ssh user>]> S<-C I<command>> S<-a I<args>> S<[-w I<warn>]> S<[-c I<crit>]>
861
862 =head1 OPTIONS
863
864 =over 4
865
866 =item B<-H> I<remote host>
867
868 Remote host.
869
870 =item B<-C> I<command>
871
872 Command (see examples).
873
874 =item B<-a> I<args>
875
876 Command's arguments.
877
878 =item B<-w> I<warn>
879
880 WARNING level.
881
882 =item B<-c> I<crit>
883
884 CRITICAL level.
885
886 =item B<-h>
887
888 A short help. Also give default values.
889
890 =item B<--help> | B<--man>
891
892 The full man page.
893
894 =item B<--perf>
895
896 Add Nagios performances datas.
897
898 =back
899
900 =head1 NAGIOS CONGIGURATIONS
901
902 In F<checkcommands.cfg> you have to add :
903
904 # 'check_by_ssh'
905 # Check remote services using ssh access.
906 # @param $ARG1$ command {osinfo|mem|disk}
907 # @param $ARG2$ command args (see examples)
908 # @param $ARG3$ warning level
909 # @param $ARG4$ critical level
910 define command {
911 command_name check_by_ssh
912 command_line $USER1$/check_by_ssh.pl -H $HOSTADDRESS$ -u racvision -C $ARG1$ -a $ARG2$ -w $ARG3$ -c $ARG4$
913 }
914 define command {
915 command_name check_by_ssh_p
916 command_line $USER1$/check_by_ssh.pl -H $HOSTADDRESS$ -u racvision -C $ARG1$ -a $ARG2$ -w $ARG3$ -c $ARG4$ --perf
917 }
918
919
920 =head2 Examples:
921
922 # Get Os infos (no alerts)
923 check_command check_by_ssh!osinfo
924 # Get free mem (without buufers/cache). Minus menns the alert level is a minimal value
925 check_command check_by_ssh!mem!MemRealFree!-100MB!-10MB
926 # Get swap use
927 check_command check_by_ssh!mem!SwapUsed!60%!90%
928 # Check all disks (from df) against given levels (80% warning, 90% critical)
929 check_command check_by_ssh!disk!all!80%!90%
930 # Check /var/log mount point :
931 check_command check_by_ssh!disk!/var/log!50MB!10MB
932
933 =head1 SSH CONGIGURATIONS
934
935 You need to configure SSH access to the remote host without using pasword but with a properly configured key pair.
936
937 # cat ~racvision/.ssh/authorized_keys
938 from="<racvision IP>",no-port-forwarding,no-X11-forwarding,no-agent-forwarding ssh-rsa AAA[..]U= racvision@yourdomain.fr
939
940 =head1 AUTHOR
941
942 St├ęphane Urbanovski <stephane.urbanovski@ac-nancy-metz.fr>
943
944 =cut

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.8