| 1 | package Nagios::Passive::ResultPath; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use File::Temp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Fcntl qw/:DEFAULT :flock/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Any::Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | extends 'Nagios::Passive::Base'; | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | has 'checkresults_dir' => ( is => 'ro', isa => 'Str | Undef', required => 1); | ||||||||||||||||||||||||||||
| 12 | has 'check_type' => ( is => 'rw', isa => 'Int', default => 1); | ||||||||||||||||||||||||||||
| 13 | has 'check_options' => ( is => 'rw', isa => 'Int', default => 0); | ||||||||||||||||||||||||||||
| 14 | has 'scheduled_check' => ( is => 'rw', isa => 'Int', default => 0); | ||||||||||||||||||||||||||||
| 15 | has 'latency' => ( is => 'rw', isa => 'Num', default => 0); | ||||||||||||||||||||||||||||
| 16 | has 'start_time' => ( is => 'rw', isa => 'Num', default=>sub { time . ".0" }); | ||||||||||||||||||||||||||||
| 17 | has 'finish_time' => ( is => 'rw', isa => 'Num', default=>sub { time . ".0" }); | ||||||||||||||||||||||||||||
| 18 | has 'early_timeout' => ( is => 'rw', isa => 'Int', default=>0); | ||||||||||||||||||||||||||||
| 19 | has 'exited_ok' => ( is => 'rw', isa => 'Int', default=>1); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | with 'Nagios::Passive::Role::Tempfile'; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | sub BUILD { | ||||||||||||||||||||||||||||
| 24 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=19.8
| |||||||||||||||||||||||||||||
| 25 | 1 | my $cd = $self->checkresults_dir; | |||||||||||||||||||||||||||
| 26 | 0 | ▶ | if(defined $cd and ! -d $cd) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $cd and not -d $cd
MC/DC0%0/2
defined $cd and not -d $cd
defined $cd
not -d $cd
Truth table0%0/3
A and not(B)
A= defined $cdB= not -d $cd
| |||||||||||||||||||||||||||||
| 27 | 0 | croak("$cd is not a directory") | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | }; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | sub _to_string { | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_to_string: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 33 | 0 | my $string = ""; | |||||||||||||||||||||||||||
| 34 | 0 | $string.="### Nagios Service Check Result ###\n"; | |||||||||||||||||||||||||||
| 35 | 0 | $string.=sprintf "# Time: %s\n",scalar localtime $self->time; | |||||||||||||||||||||||||||
| 36 | 0 | $string.=sprintf "host_name=%s\n", $self->host_name; | |||||||||||||||||||||||||||
| 37 | 0 | ▶ | if(defined($self->service_description)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | $string.=sprintf "service_description=%s\n", $self->service_description; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 0 | $string.=sprintf "check_type=%d\n", $self->check_type; | |||||||||||||||||||||||||||
| 41 | 0 | $string.=sprintf "check_options=%d\n", $self->check_options; | |||||||||||||||||||||||||||
| 42 | 0 | $string.=sprintf "scheduled_check=%d\n", $self->scheduled_check; | |||||||||||||||||||||||||||
| 43 | 0 | $string.=sprintf "latency=%f\n", $self->latency; | |||||||||||||||||||||||||||
| 44 | 0 | $string.=sprintf "start_time=%f\n", $self->start_time; | |||||||||||||||||||||||||||
| 45 | 0 | $string.=sprintf "finish_time=%f\n", $self->finish_time; | |||||||||||||||||||||||||||
| 46 | 0 | $string.=sprintf "early_timeout=%d\n", $self->early_timeout; | |||||||||||||||||||||||||||
| 47 | 0 | $string.=sprintf "exited_ok=%d\n", $self->exited_ok; | |||||||||||||||||||||||||||
| 48 | 0 | $string.=sprintf "return_code=%d\n", $self->return_code; | |||||||||||||||||||||||||||
| 49 | 0 | $string.=sprintf "output=%s %s - %s\n", $self->check_name, | |||||||||||||||||||||||||||
| 50 | $self->_status_code, $self->_quoted_output; | ||||||||||||||||||||||||||||
| 51 | 0 | return $string; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub to_string { | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
to_string: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 56 | 0 | my $string = ""; | |||||||||||||||||||||||||||
| 57 | 0 | $string.="### Active Check Result File ###\n"; | |||||||||||||||||||||||||||
| 58 | 0 | $string.=sprintf "file_time=%d\n\n",$self->time; | |||||||||||||||||||||||||||
| 59 | 0 | $string.=$self->_to_string; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub submit { | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
submit: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if(! defined $self->checkresults_dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | croak( | |||||||||||||||||||||||||||
| 66 | "checkresults_dir is not set, you can either set it ". | ||||||||||||||||||||||||||||
| 67 | "or use that object with Nagios::Passive::BulkResult" | ||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 0 | my $fh = $self->tempfile; | |||||||||||||||||||||||||||
| 71 | 0 | my $output = $self->to_string; | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | print $fh $output or croak($!); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | close($fh) or croak($!); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | $self->_touch_file; | |||||||||||||||||||||||||||
| 75 | 0 | return $fh->filename; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 3 | ▶ | no Any::Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 80 | __PACKAGE__->meta->make_immutable; | ||||||||||||||||||||||||||||
| 81 | 1; | ||||||||||||||||||||||||||||
| 82 | __END__ | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head1 NAME | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | Nagios::Passive::ResultPath - drop check results into Nagios' check_result_path. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | my $nw = Nagios::Passive->create( | ||||||||||||||||||||||||||||
| 91 | checkresults_dir => $checkresultsdir, | ||||||||||||||||||||||||||||
| 92 | service_description => $service_description, | ||||||||||||||||||||||||||||
| 93 | check_name => $check_name, | ||||||||||||||||||||||||||||
| 94 | host_name => $hostname, | ||||||||||||||||||||||||||||
| 95 | return_code => 0, # 1 2 3 | ||||||||||||||||||||||||||||
| 96 | output => 'looks (good|bad|horrible) | performancedata' | ||||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||
| 98 | $nw->submit | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | This module gives you the ability to drop checkresults directly | ||||||||||||||||||||||||||||
| 103 | into Nagios' check_result_path. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | The usage is described in L<Nagios::Passive>. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =cut | ||||||||||||||||||||||||||||