Livestatus/UNIX.pm

filter: Statementstmt 46.3% 25 / 54 Branchbran 25.0% 5 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 75.0% 6 / 8 Podpod 100% 1 / 1 totaltotal 42.0% 37 / 88 CC 12 SCAR 34.7
CC
12
Cov
39%
_open
72.0
_close
6.0
new
4.0
CRAP 32.1
« Livestatus/INET.pm ↑ Summary
1 package Monitoring::Livestatus::UNIX;
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Carp qw/confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use IO::Socket::UNIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use parent 'Monitoring::Livestatus';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Monitoring::Livestatus::UNIX - connector with unix sockets
12  
13 =head1 SYNOPSIS
14  
15 use Monitoring::Livestatus;
16 my $nl = Monitoring::Livestatus::UNIX->new( '/var/lib/livestatus/livestatus.sock' );
17 my $hosts = $nl->selectall_arrayref("GET hosts");
18  
19 =head1 CONSTRUCTOR
20  
21 =head2 new ( [ARGS] )
22  
23 Creates an C<Monitoring::Livestatus::UNIX> object. C<new> takes at least the socketpath.
24 Arguments are the same as in C<Monitoring::Livestatus>.
25 If the constructor is only passed a single argument, it is assumed to
26 be a the C<socket> specification. Use either socker OR server.
27  
28 =cut
29  
30 10 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
31 10 my($class,@args) = @_;
32 10 ▶ unshift(@args, "peer") if scalar @args == 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args == 1 1 9 10
33 10 my(%options) = @args;
34 10 ▶ $options{'name'} = $options{'peer'} unless defined $options{'name'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{"name"} 2 8 10
35  
36 10 $options{'backend'} = $class;
37 10 my $self = Monitoring::Livestatus->new(%options);
38 10 bless $self, $class;
39 10 ▶ confess('not a scalar') if ref $self->{'peer'} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"peer"} ne "" 0 10 10
40  
41 10 return $self;
42 }
43  
44  
45 ########################################
46  
47 =head1 METHODS
48  
49 =cut
50  
51 0 ▶sub _open {
Subroutine _open: not called CC=8 SCAR=42.8
52 0 my $self = shift;
53  
54 0 ▶ if(!-S $self->{'peer'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-S $self->{"peer"}) 0 0 0
55 0 my $msg = "failed to open socket $self->{'peer'}: $!";
56 0 ▶ if($self->{'errors_are_fatal'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"errors_are_fatal"}) 0 0 0
57 0 confess($msg);
58 }
59 0 $Monitoring::Livestatus::ErrorCode = 500;
60 0 $Monitoring::Livestatus::ErrorMessage = $msg;
61 0 return;
62 }
63 0 my $sock;
64 0 eval {
65 $sock = IO::Socket::UNIX->new(
66 Peer => $self->{'peer'},
67 Type => IO::Socket::UNIX::SOCK_STREAM,
68 0 Timeout => $self->{'connect_timeout'},
69 );
70 0 ▶ if(!defined $sock || !$sock->connected()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sock and $sock->connected) 0 0 0
Condition0%0/3
defined $sock and $sock->connected
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sock and $sock->connected
defined $sock $sock->connected
Truth table0%0/3
A and B
A= defined $sockB= $sock->connected
ABresult
0X0
100
111
71 0 my $msg = "failed to connect to $self->{'peer'}: $!";
72 0 ▶ if($self->{'errors_are_fatal'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"errors_are_fatal"}) 0 0 0
73 0 confess($msg);
74 }
75 0 $Monitoring::Livestatus::ErrorCode = 500;
76 0 $Monitoring::Livestatus::ErrorMessage = $msg;
77 0 return;
78 }
79 };
80  
81 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
82 0 $Monitoring::Livestatus::ErrorCode = 500;
83 0 $Monitoring::Livestatus::ErrorMessage = $@;
84 0 return;
85 }
86  
87 0 ▶ if(defined $self->{'query_timeout'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"query_timeout"}) 0 0 0
88 # set timeout
89 0 $sock->timeout($self->{'query_timeout'});
90 }
91  
92 0 return($sock);
93 }
94  
95  
96 ########################################
97  
98 0 ▶sub _close {
Subroutine _close: not called CC=2 SCAR=17.9
99 0 my $self = shift;
100 0 my $sock = shift;
101 0 ▶ return unless defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
102 0 return close($sock);
103 }
104  
105  
106 4 1;
107  
108 =head1 AUTHOR
109  
110 Sven Nierlein, 2009-present, <sven@nierlein.org>
111  
112 =head1 COPYRIGHT AND LICENSE
113  
114 Copyright (C) by Sven Nierlein
115  
116 This library is free software; you can redistribute it and/or modify
117 it under the same terms as Perl itself.
118  
119 =cut
120  
121 __END__
« Livestatus/INET.pm ↑ Summary