File Coverage

blib/lib/Nagios/Passive/CommandFile.pm
Criterion Covered Total %
statement 14 30 46.6
branch 0 12 0.0
condition n/a
subroutine 5 7 71.4
pod n/a
total 19 49 38.7


line stmt bran cond sub pod time code
1             package Nagios::Passive::CommandFile;
2              
3 2     2   2380 use strict;
  2         2  
  2         67  
4 2     2   10 use Carp;
  2         4  
  2         147  
5 2     2   505 use Moo;
  2         8056  
  2         12  
6 2     2   1974 use MooX::late;
  2         15826  
  2         12  
7              
8             extends 'Nagios::Passive::Base';
9              
10             has 'command_file'=>(
11             is => 'ro',
12             isa => 'Str',
13             predicate=>'has_command_file'
14             );
15              
16             sub BUILD {
17 1     1   1424 my $self = shift;
18 1         87 my $cf = $self->command_file;
19 0 0         croak("$cf is not a named pipe") unless (-p $cf);
20             };
21              
22             sub to_string {
23 0     0     my $s = shift;
24 0           my $output;
25 0 0         if(defined $s->service_description) {
26 0           $output = sprintf "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s %s - %s\n",
27             $s->time, $s->host_name, $s->service_description, $s->return_code,
28             $s->check_name, $s->_status_code, $s->_quoted_output;
29             } else {
30 0           $output = sprintf "[%d] PROCESS_HOST_CHECK_RESULT;%s;%d;%s %s - %s\n",
31             $s->time, $s->host_name, $s->return_code,
32             $s->check_name, $s->_status_code, $s->_quoted_output;
33             }
34 0           return $output;
35             }
36              
37             sub submit {
38 0     0     my $s = shift;
39 0 0         croak("no external_command_file given") unless $s->has_command_file;
40 0           my $cf = $s->command_file;
41 0           my $output = $s->to_string;
42              
43             # I hope this is the correct way to deal with named pipes
44 0           local $SIG{PIPE} = 'IGNORE';
45 0 0         open(my $f, ">>", $cf) or croak("cannot open $cf: $!");
46 0 0         print $f $output or croak("cannot write to pipe: $!");
47 0 0         close($f) or croak("cannot close $cf");
48 0           return length($output);
49             }
50              
51             1;
52             __END__