line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Nagios::Plugin::CheckHost; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
13
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
61
|
|
4
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
112
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = 0.04; |
7
|
|
|
|
|
|
|
our $URL = 'https://check-host.net/'; |
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
821
|
use Net::CheckHost; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
46
|
|
10
|
2
|
|
|
2
|
|
40
|
use Monitoring::Plugin; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
151
|
|
11
|
2
|
|
|
2
|
|
825
|
use Class::Load qw(load_class); |
|
2
|
|
|
|
|
17101
|
|
|
2
|
|
|
|
|
102
|
|
12
|
2
|
|
|
2
|
|
13
|
use Nagios::Plugin::CheckHost::Node; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
30
|
|
13
|
2
|
|
|
2
|
|
6
|
use Try::Tiny; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
886
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
16
|
2
|
|
|
2
|
0
|
1266
|
my ($class, %args) = @_; |
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
|
|
12
|
my $self = bless { |
19
|
|
|
|
|
|
|
checkhost => Net::CheckHost->new(), |
20
|
|
|
|
|
|
|
delay => 2, |
21
|
|
|
|
|
|
|
max_waittime => 30, |
22
|
|
|
|
|
|
|
%args, |
23
|
|
|
|
|
|
|
}, $class; |
24
|
|
|
|
|
|
|
|
25
|
2
|
|
|
|
|
3858
|
$self->_initialize(); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub _initialize_nagios { |
29
|
2
|
|
|
2
|
|
4
|
my ($self, %args) = @_; |
30
|
|
|
|
|
|
|
|
31
|
2
|
|
|
|
|
13
|
$self->{nagios} = Monitoring::Plugin->new( |
32
|
|
|
|
|
|
|
shortname => 'CHECKHOST', |
33
|
|
|
|
|
|
|
usage => 'Usage: %s -H -w -c ', |
34
|
|
|
|
|
|
|
url => $URL, |
35
|
|
|
|
|
|
|
version => $VERSION, |
36
|
|
|
|
|
|
|
%args |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
2
|
|
|
2
|
0
|
1127
|
sub nagios { $_[0]->{nagios} } |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub run { |
43
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
44
|
0
|
|
|
|
|
0
|
my $np = $self->{nagios}; |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
0
|
$np->getopts; |
47
|
0
|
|
|
|
|
0
|
my $opts = $np->opts; |
48
|
0
|
|
|
|
|
0
|
my $host = $opts->get('host'); |
49
|
0
|
|
|
|
|
0
|
my $max_nodes = $opts->get('max_nodes'); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
my $result = $self->_check( |
52
|
0
|
|
|
|
|
0
|
$self->{check}, $host, |
53
|
|
|
|
|
|
|
max_nodes => $max_nodes, |
54
|
|
|
|
|
|
|
); |
55
|
0
|
|
|
|
|
0
|
$self->process_check_result($result); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub _check { |
59
|
0
|
|
|
0
|
|
0
|
my ($self, $type, $host, %args) = @_; |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
0
|
|
|
0
|
my $max_nodes = delete $args{max_nodes} || 3; |
62
|
0
|
|
|
|
|
0
|
my $max_failed_nodes = delete $args{max_failed_nodes}; |
63
|
0
|
0
|
|
|
|
0
|
$max_failed_nodes = 1 unless defined $max_failed_nodes; |
64
|
0
|
|
0
|
|
|
0
|
my $result_args = delete $args{result_args} || {}; |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
0
|
my $result; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
try { |
69
|
|
|
|
|
|
|
my $check = $self->{checkhost} |
70
|
0
|
|
|
0
|
|
0
|
->request("check-$type", host => $host, max_nodes => $max_nodes); |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
0
|
my $rid = $check->{request_id}; |
73
|
0
|
|
|
|
|
0
|
$self->{request_id} = $rid; |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
0
|
my $result_class = |
76
|
|
|
|
|
|
|
"Nagios::Plugin::CheckHost::Result::" . ucfirst($type); |
77
|
0
|
|
|
|
|
0
|
load_class($result_class); |
78
|
|
|
|
|
|
|
$result = $result_class->new(%$result_args, |
79
|
0
|
|
|
|
|
0
|
nodes => $self->nodes_class($check->{nodes})); |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
0
|
my $start = time(); |
82
|
|
|
|
|
|
|
do { |
83
|
0
|
|
|
|
|
0
|
sleep $self->{delay}; |
84
|
|
|
|
|
|
|
$result->store_result( |
85
|
0
|
|
|
|
|
0
|
$self->{checkhost}->request("check-result/$rid")); |
86
|
|
|
|
|
|
|
} while ($start - time() < $self->{max_waittime} |
87
|
0
|
|
0
|
|
|
0
|
&& $result->unfinished_nodes); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
catch { |
90
|
0
|
|
|
0
|
|
0
|
$self->{nagios}->die($_); |
91
|
0
|
|
|
|
|
0
|
}; |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
0
|
return $result; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub nodes_class { |
97
|
0
|
|
|
0
|
0
|
0
|
my ($self, $nodes_list) = @_; |
98
|
|
|
|
|
|
|
my @nodes = |
99
|
0
|
|
|
|
|
0
|
map { Nagios::Plugin::CheckHost::Node->new($_ => $nodes_list->{$_}) } |
|
0
|
|
|
|
|
0
|
|
100
|
|
|
|
|
|
|
keys %$nodes_list; |
101
|
0
|
|
|
|
|
0
|
\@nodes; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub report_url { |
105
|
8
|
|
|
8
|
0
|
10
|
my $self = shift; |
106
|
|
|
|
|
|
|
|
107
|
8
|
|
|
|
|
40
|
$URL . "check-report/" . $self->{request_id}; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
1; |
111
|
|
|
|
|
|
|
__END__ |