Nagios/Plugin/CheckHost.pm

filter: Statementstmt 47.7% 32 / 67 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 64.7% 11 / 17 Podpod 0.0% 0 / 5 totaltotal 40.5% 43 / 106 CC 7 SCAR 21.9
CC
7
Cov
40%
_check
30.0
__ANON__
12.0
run
2.0
CRAP 8.9
↑ Summary Nagios/Plugin/CheckHost/Result/Ping.pm »
1 package Nagios::Plugin::CheckHost;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = 0.05;
7 2 our $URL = 'https://check-host.net/';
8  
9 2 ▶use Net::CheckHost;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Monitoring::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Class::Load qw(load_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Nagios::Plugin::CheckHost::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
16 2 my ($class, %args) = @_;
17  
18 2 my $self = bless {
19 checkhost => Net::CheckHost->new(),
20 delay => 2,
21 max_waittime => 30,
22 %args,
23 }, $class;
24  
25 2 $self->_initialize();
26 }
27  
28 2 ▶sub _initialize_nagios {
Subroutine _initialize_nagios: called CC=1 SCAR=0.0
29 2 my ($self, %args) = @_;
30  
31 2 $self->{nagios} = Monitoring::Plugin->new(
32 shortname => 'CHECKHOST',
33 usage => 'Usage: %s -H <host> -w <warning> -c <critical>',
34 url => $URL,
35 version => $VERSION,
36 %args
37 );
38 }
39  
40 2 ▶sub nagios { $_[0]->{nagios} }
Subroutine nagios: called CC=1 SCAR=0.0
Pod undocumented
41  
42 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
43 0 my $self = shift;
44 0 my $np = $self->{nagios};
45  
46 0 $np->getopts;
47 0 my $opts = $np->opts;
48 0 my $host = $opts->get('host');
49 0 my $max_nodes = $opts->get('max_nodes');
50  
51 my $result = $self->_check(
52 0 $self->{check}, $host,
53 max_nodes => $max_nodes,
54 );
55 0 $self->process_check_result($result);
56 }
57  
58 0 ▶sub _result_class {
Subroutine _result_class: not called CC=1 SCAR=6.9
59 0 my ($self, $type) = @_;
60  
61 0 "Nagios::Plugin::CheckHost::Result::" . ucfirst($type)
62 }
63  
64 0 ▶sub _check {
Subroutine _check: not called CC=5 SCAR=34.0
65 0 my ($self, $type, $host, %args) = @_;
66  
67 0 ▶ my $max_nodes = delete $args{max_nodes} || 3;
Condition0%0/2
delete $args{'max_nodes'} || 3
l!l
00
MC/DC0%0/1
delete $args{'max_nodes'} || 3
delete $args{'max_nodes'}
Truth table0%0/2
A
A= delete $args{'max_nodes'}
Aresult
11
00
68 0 my $max_failed_nodes = delete $args{max_failed_nodes};
69 0 ▶ $max_failed_nodes = 1 unless defined $max_failed_nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max_failed_nodes 0 0 0
70 0 ▶ my $result_args = delete $args{result_args} || {};
Condition0%0/2
delete $args{'result_args'} || {}
l!l
00
MC/DC0%0/1
delete $args{'result_args'} || {}
delete $args{'result_args'}
Truth table0%0/2
A
A= delete $args{'result_args'}
Aresult
11
00
71  
72 0 my $result;
73  
74 0 ▶ try {
Subroutine __ANON__: not called CC=3 SCAR=24.8
75 my $check = $self->{checkhost}
76 0 ->request("check-$type", host => $host, max_nodes => $max_nodes);
77  
78 0 my $rid = $check->{request_id};
79 0 $self->{request_id} = $rid;
80  
81 0 my $result_class = $self->_result_class($type);
82 0 load_class($result_class);
83 $result = $result_class->new(%$result_args,
84 0 nodes => $self->nodes_class($check->{nodes}));
85  
86 0 my $start = time();
87 do {
88 0 sleep $self->{delay};
89 $result->store_result(
90 0 $self->{checkhost}->request("check-result/$rid"));
91 } while (time() - $start < $self->{max_waittime}
92 0 ▶ && $result->unfinished_nodes);
Branch: 1 branch
BranchTrueFalseTotal
time - $start < $self->{"max_waittime"} and $result->unfinished_nodes and do { do { sleep $self->{"delay"}; $result->store_result($self->{"checkhost"}->request("check-result/$rid")) } } 0 0 0
Condition0%0/3
time - $start < $self->{"max_waittime"} and $result->unfinished_nodes
!ll&&!rl&&r
000
MC/DC0%0/2
time - $start < $self->{"max_waittime"} and $result->unfinished_nodes
time - $start < $self->{"max_waittime"} $result->unfinished_nodes
Truth table0%0/3
A and B
A= time - $start < $self->{"max_waittime"}B= $result->unfinished_nodes
ABresult
0X0
100
111
93 }
94 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
95 0 $self->{nagios}->die($_);
96 0 };
97  
98 0 $result->remove_unfinished_nodes;
99 0 ▶ $self->{nagios}->die("No check results. Report " . $self->report_url)
Branch: 1 branch
BranchTrueFalseTotal
unless $result->nodes 0 0 0
100 unless $result->nodes;
101  
102 0 return $result;
103 }
104  
105 0 ▶sub nodes_class {
Subroutine nodes_class: not called CC=1 SCAR=6.9
Pod undocumented
106 0 my ($self, $nodes_list) = @_;
107 my @nodes =
108 0 map { Nagios::Plugin::CheckHost::Node->new($_ => $nodes_list->{$_}) }
109 keys %$nodes_list;
110 0 \@nodes;
111 }
112  
113 8 ▶sub report_url {
Subroutine report_url: called CC=1 SCAR=0.0
Pod undocumented
114 8 my $self = shift;
115  
116 8 $URL . "check-report/" . $self->{request_id};
117 }
118  
119 2 1;
120 __END__
↑ Summary Nagios/Plugin/CheckHost/Result/Ping.pm »