File Coverage

blib/lib/Nagios/Passive/ResultPath.pm
Criterion Covered Total %
statement 20 54 37.0
branch 0 10 0.0
condition 0 3 0.0
subroutine 7 10 70.0
pod n/a
total 27 77 35.0


line stmt bran cond sub pod time code
1             package Nagios::Passive::ResultPath;
2              
3 3     3   3095 use strict;
  3         38  
  3         118  
4 3     3   18 use Carp;
  3         6  
  3         284  
5 3     3   1968 use File::Temp;
  3         42764  
  3         310  
6 3     3   23 use Fcntl qw/:DEFAULT :flock/;
  3         4  
  3         1609  
7 3     3   1258 use Moo;
  3         21795  
  3         26  
8 3     3   6208 use MooX::late;
  3         45088  
  3         27  
9              
10             extends 'Nagios::Passive::Base';
11              
12             has 'checkresults_dir' => ( is => 'ro', isa => 'Str | Undef', required => 1);
13             has 'check_type' => ( is => 'rw', isa => 'Int', default => 1);
14             has 'check_options' => ( is => 'rw', isa => 'Int', default => 0);
15             has 'scheduled_check' => ( is => 'rw', isa => 'Int', default => 0);
16             has 'latency' => ( is => 'rw', isa => 'Num', default => 0);
17             has 'start_time' => ( is => 'rw', isa => 'Num', default=>sub { time . ".0" });
18             has 'finish_time' => ( is => 'rw', isa => 'Num', default=>sub { time . ".0" });
19             has 'early_timeout' => ( is => 'rw', isa => 'Int', default=>0);
20             has 'exited_ok' => ( is => 'rw', isa => 'Int', default=>1);
21              
22             with 'Nagios::Passive::Role::Tempfile';
23              
24             sub BUILD {
25 1     1   2122 my $self = shift;
26 1         143 my $cd = $self->checkresults_dir;
27 0 0 0       if(defined $cd and ! -d $cd) {
28 0           croak("$cd is not a directory")
29             }
30             };
31              
32             sub _to_string {
33 0     0     my $self = shift;
34 0           my $string = "";
35 0           $string.="### Nagios Service Check Result ###\n";
36 0           $string.=sprintf "# Time: %s\n",scalar localtime $self->time;
37 0           $string.=sprintf "host_name=%s\n", $self->host_name;
38 0 0         if(defined($self->service_description)) {
39 0           $string.=sprintf "service_description=%s\n", $self->service_description;
40             }
41 0           $string.=sprintf "check_type=%d\n", $self->check_type;
42 0           $string.=sprintf "check_options=%d\n", $self->check_options;
43 0           $string.=sprintf "scheduled_check=%d\n", $self->scheduled_check;
44 0           $string.=sprintf "latency=%f\n", $self->latency;
45 0           $string.=sprintf "start_time=%f\n", $self->start_time;
46 0           $string.=sprintf "finish_time=%f\n", $self->finish_time;
47 0           $string.=sprintf "early_timeout=%d\n", $self->early_timeout;
48 0           $string.=sprintf "exited_ok=%d\n", $self->exited_ok;
49 0           $string.=sprintf "return_code=%d\n", $self->return_code;
50 0           $string.=sprintf "output=%s %s - %s\n", $self->check_name,
51             $self->_status_code, $self->_quoted_output;
52 0           return $string;
53             }
54              
55             sub to_string {
56 0     0     my $self = shift;
57 0           my $string = "";
58 0           $string.="### Active Check Result File ###\n";
59 0           $string.=sprintf "file_time=%d\n\n",$self->time;
60 0           $string.=$self->_to_string;
61             }
62              
63             sub submit {
64 0     0     my $self = shift;
65 0 0         if(! defined $self->checkresults_dir) {
66 0           croak(
67             "checkresults_dir is not set, you can either set it ".
68             "or use that object with Nagios::Passive::BulkResult"
69             );
70             }
71 0           my $fh = $self->tempfile;
72 0           my $output = $self->to_string;
73 0 0         print $fh $output or croak($!);
74 0 0         close($fh) or croak($!);
75 0           $self->_touch_file;
76 0           return $fh->filename;
77             }
78              
79              
80             1;
81             __END__