-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_dual.pl
executable file
·109 lines (88 loc) · 3.04 KB
/
check_dual.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/perl
# nagios: -epn
#
# check_dual.pl - runs the same check for two IP addresses
# Copyright (C) 2012 Peter Meszaros <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
use strict;
use Getopt::Std;
our($opt_H, $opt_c, $opt_1, $opt_t, $opt_h);
my $version = '0.2';
my $libexec = '/usr/local/nagios/libexec/';
use constant {
OK => 0, # Luckily the Nagios status values are numbered from 0 to 3.
WARNING => 1, # The following code is heavily based on this assumption.
CRITICAL => 2,
UNKNOWN => 3,
};
my @statestr = qw(OK WARNING CRITICAL UNKNOWN);
my ($ret, $retstr, $retprf);
# Truth table for the 2of2 checks
# OK WARNING CRITICAL UNKOWN
my @tt2o2 = ([ OK, WARNING, WARNING, WARNING ], # OK
[ WARNING, WARNING, CRITICAL, CRITICAL ], # WARNING
[ WARNING, CRITICAL, CRITICAL, CRITICAL ], # CRITICAL
[ WARNING, CRITICAL, CRITICAL, UNKNOWN ]); # UNKNOWN
# Truth table for the 1of2 checks
# OK WARNING CRITICAL UNKOWN
my @tt1o2 = ([ OK, OK, OK, OK ], # OK
[ OK, WARNING, WARNING, WARNING ], # WARNING
[ OK, WARNING, CRITICAL, CRITICAL ], # CRITICAL
[ OK, WARNING, CRITICAL, UNKNOWN ]); # UNKNOWN
getopts('H:c:1th');
if (!defined($opt_H) || !defined($opt_c)) {
usage();
exit UNKNOWN;
}
my @ips = split(',', $opt_H);
my @crets;
my $n = 1;
foreach (@ips) {
print "$opt_c -H $_ @ARGV\n" if $opt_t;
chomp(my $r = `$libexec/$opt_c -H $_ @ARGV`);
push(@crets, $? >> 8);
print "ret: $r\n" if $opt_t;
my ($s, $p) = split(/\|/, $r);
# collect status
$retstr .= $s . ' ';
# collect performance
if (!($opt_1 && ($n > 1))) {
my @l = split(' ', $p);
foreach (@l) {
s/(.+)=/${1}_$n=/;
}
$p = join(' ', @l);
$retprf .= $p . ' ';
$n++;
}
}
$ret = $opt_1 ? $tt1o2[$crets[0]][$crets[1]] : $tt2o2[$crets[0]][$crets[1]];
print "$statestr[$ret] - $retstr|$retprf\n";
exit $ret;
#-----------------------------------------------------------------------------
sub usage
{
print <<EOD
check_dual.pl - runs the same check for two IP addresses
Version: $version, Copyright (C) 2012 Peter Meszaros <hauptadler\@gmail.com>
Usage: $0 -H ip1,ip2 [-1] [-t] [-h] -c command -- -a p1 -b p2
\t-H ipaddresses separated by comma
\t-1 one of two mode
\t-t trace
\t-c check to execute (the very first command line switch must be preceeded with '--')
\t-h prints this message
EOD
}