| 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.04; | |||||||||||||||||||||||||||
| 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 | sub new { | ||||||||||||||||||||||||||||
| 16 | 2 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 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 | sub _initialize_nagios { | ||||||||||||||||||||||||||||
| 29 | 2 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_initialize_nagios: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | sub run { | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 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 | sub _check { | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | my ($self, $type, $host, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_check: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | my $max_nodes = delete $args{max_nodes} || 3; | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $args{'max_nodes'} || 3
MC/DC0%0/1
delete $args{'max_nodes'} || 3
delete $args{'max_nodes'}
Truth table0%0/2
A
A= delete $args{'max_nodes'}
| |||||||||||||||||||||||||||||
| 62 | 0 | my $max_failed_nodes = delete $args{max_failed_nodes}; | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | $max_failed_nodes = 1 unless defined $max_failed_nodes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | my $result_args = delete $args{result_args} || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $args{'result_args'} || {}
MC/DC0%0/1
delete $args{'result_args'} || {}
delete $args{'result_args'}
Truth table0%0/2
A
A= delete $args{'result_args'}
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | my $result; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | try { | ||||||||||||||||||||||||||||
| 69 | my $check = $self->{checkhost} | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | ->request("check-$type", host => $host, max_nodes => $max_nodes); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | my $rid = $check->{request_id}; | |||||||||||||||||||||||||||
| 73 | 0 | $self->{request_id} = $rid; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | my $result_class = | |||||||||||||||||||||||||||
| 76 | "Nagios::Plugin::CheckHost::Result::" . ucfirst($type); | ||||||||||||||||||||||||||||
| 77 | 0 | load_class($result_class); | |||||||||||||||||||||||||||
| 78 | $result = $result_class->new(%$result_args, | ||||||||||||||||||||||||||||
| 79 | 0 | nodes => $self->nodes_class($check->{nodes})); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | my $start = time(); | |||||||||||||||||||||||||||
| 82 | do { | ||||||||||||||||||||||||||||
| 83 | 0 | sleep $self->{delay}; | |||||||||||||||||||||||||||
| 84 | $result->store_result( | ||||||||||||||||||||||||||||
| 85 | 0 | $self->{checkhost}->request("check-result/$rid")); | |||||||||||||||||||||||||||
| 86 | } while ($start - time() < $self->{max_waittime} | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | && $result->unfinished_nodes); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$start - time < $self->{"max_waittime"} and $result->unfinished_nodes
MC/DC0%0/2
$start - time < $self->{"max_waittime"} and $result->unfinished_nodes
$start - time < $self->{"max_waittime"}
$result->unfinished_nodes
Truth table0%0/3
A and B
A= $start - time < $self->{"max_waittime"}B= $result->unfinished_nodes
| |||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | catch { | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | $self->{nagios}->die($_); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 91 | 0 | }; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | return $result; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | sub nodes_class { | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | my ($self, $nodes_list) = @_; | ||||||||||||||||||||||||||
|
Subroutine
nodes_class: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 98 | my @nodes = | ||||||||||||||||||||||||||||
| 99 | 0 | map { Nagios::Plugin::CheckHost::Node->new($_ => $nodes_list->{$_}) } | |||||||||||||||||||||||||||
| 100 | keys %$nodes_list; | ||||||||||||||||||||||||||||
| 101 | 0 | \@nodes; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | sub report_url { | ||||||||||||||||||||||||||||
| 105 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
report_url: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 8 | $URL . "check-report/" . $self->{request_id}; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 2 | 1; | |||||||||||||||||||||||||||
| 111 | __END__ | ||||||||||||||||||||||||||||