line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Nagios::Plugin::CheckHost; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
9
|
use strict; |
|
2
|
|
|
|
|
1
|
|
|
2
|
|
|
|
|
42
|
|
4
|
2
|
|
|
2
|
|
4
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
69
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = 0.05; |
7
|
|
|
|
|
|
|
our $URL = 'https://check-host.net/'; |
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
573
|
use Net::CheckHost; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
42
|
|
10
|
2
|
|
|
2
|
|
30
|
use Monitoring::Plugin; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
126
|
|
11
|
2
|
|
|
2
|
|
812
|
use Class::Load qw(load_class); |
|
2
|
|
|
|
|
16457
|
|
|
2
|
|
|
|
|
88
|
|
12
|
2
|
|
|
2
|
|
9
|
use Nagios::Plugin::CheckHost::Node; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
28
|
|
13
|
2
|
|
|
2
|
|
5
|
use Try::Tiny; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
952
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
16
|
2
|
|
|
2
|
0
|
688
|
my ($class, %args) = @_; |
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
|
|
8
|
my $self = bless { |
19
|
|
|
|
|
|
|
checkhost => Net::CheckHost->new(), |
20
|
|
|
|
|
|
|
delay => 2, |
21
|
|
|
|
|
|
|
max_waittime => 30, |
22
|
|
|
|
|
|
|
%args, |
23
|
|
|
|
|
|
|
}, $class; |
24
|
|
|
|
|
|
|
|
25
|
2
|
|
|
|
|
4063
|
$self->_initialize(); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub _initialize_nagios { |
29
|
2
|
|
|
2
|
|
4
|
my ($self, %args) = @_; |
30
|
|
|
|
|
|
|
|
31
|
2
|
|
|
|
|
12
|
$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
|
668
|
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 _result_class { |
59
|
0
|
|
|
0
|
|
0
|
my ($self, $type) = @_; |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
0
|
"Nagios::Plugin::CheckHost::Result::" . ucfirst($type) |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub _check { |
65
|
0
|
|
|
0
|
|
0
|
my ($self, $type, $host, %args) = @_; |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
0
|
|
|
0
|
my $max_nodes = delete $args{max_nodes} || 3; |
68
|
0
|
|
|
|
|
0
|
my $max_failed_nodes = delete $args{max_failed_nodes}; |
69
|
0
|
0
|
|
|
|
0
|
$max_failed_nodes = 1 unless defined $max_failed_nodes; |
70
|
0
|
|
0
|
|
|
0
|
my $result_args = delete $args{result_args} || {}; |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
0
|
my $result; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
try { |
75
|
|
|
|
|
|
|
my $check = $self->{checkhost} |
76
|
0
|
|
|
0
|
|
0
|
->request("check-$type", host => $host, max_nodes => $max_nodes); |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
0
|
my $rid = $check->{request_id}; |
79
|
0
|
|
|
|
|
0
|
$self->{request_id} = $rid; |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
0
|
my $result_class = $self->_result_class($type); |
82
|
0
|
|
|
|
|
0
|
load_class($result_class); |
83
|
|
|
|
|
|
|
$result = $result_class->new(%$result_args, |
84
|
0
|
|
|
|
|
0
|
nodes => $self->nodes_class($check->{nodes})); |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
0
|
my $start = time(); |
87
|
|
|
|
|
|
|
do { |
88
|
0
|
|
|
|
|
0
|
sleep $self->{delay}; |
89
|
|
|
|
|
|
|
$result->store_result( |
90
|
0
|
|
|
|
|
0
|
$self->{checkhost}->request("check-result/$rid")); |
91
|
|
|
|
|
|
|
} while (time() - $start < $self->{max_waittime} |
92
|
0
|
|
0
|
|
|
0
|
&& $result->unfinished_nodes); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
catch { |
95
|
0
|
|
|
0
|
|
0
|
$self->{nagios}->die($_); |
96
|
0
|
|
|
|
|
0
|
}; |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
0
|
$result->remove_unfinished_nodes; |
99
|
0
|
0
|
|
|
|
0
|
$self->{nagios}->die("No check results. Report " . $self->report_url) |
100
|
|
|
|
|
|
|
unless $result->nodes; |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
0
|
return $result; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub nodes_class { |
106
|
0
|
|
|
0
|
0
|
0
|
my ($self, $nodes_list) = @_; |
107
|
|
|
|
|
|
|
my @nodes = |
108
|
0
|
|
|
|
|
0
|
map { Nagios::Plugin::CheckHost::Node->new($_ => $nodes_list->{$_}) } |
|
0
|
|
|
|
|
0
|
|
109
|
|
|
|
|
|
|
keys %$nodes_list; |
110
|
0
|
|
|
|
|
0
|
\@nodes; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub report_url { |
114
|
8
|
|
|
8
|
0
|
6
|
my $self = shift; |
115
|
|
|
|
|
|
|
|
116
|
8
|
|
|
|
|
33
|
$URL . "check-report/" . $self->{request_id}; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
1; |
120
|
|
|
|
|
|
|
__END__ |