File Coverage

blib/lib/Nagios/Passive/CommandFile.pm
Criterion Covered Total %
statement 21 30 70.0
branch 4 12 33.3
condition n/a
subroutine 6 7 85.7
pod 0 3 0.0
total 31 52 59.6


line stmt bran cond sub pod time code
1             package Nagios::Passive::CommandFile;
2              
3 2     2   2596 use strict;
  2         9  
  2         76  
4 2     2   16 use Carp;
  2         7  
  2         192  
5 2     2   1079 use Moo;
  2         11762  
  2         17  
6 2     2   2631 use MooX::late;
  2         27974  
  2         24  
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 3     3 0 273 my $self = shift;
18 3         16 my $cf = $self->command_file;
19 3 100       408 croak("$cf is not a named pipe") unless (-p $cf);
20             };
21              
22             sub to_string {
23 2     2 0 725 my $s = shift;
24 2         5 my $output;
25 2 100       54 if(defined $s->service_description) {
26 1         53 $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 1         42 $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 2         96 return $output;
35             }
36              
37             sub submit {
38 0     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__