| 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
| |||||||||||||||||||||||||||||
| 33 | 10 | my(%options) = @args; | |||||||||||||||||||||||||||
| 34 | 10 | ▶ | $options{'name'} = $options{'peer'} unless defined $options{'name'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 55 | 0 | my $msg = "failed to open socket $self->{'peer'}: $!"; | |||||||||||||||||||||||||||
| 56 | 0 | ▶ | if($self->{'errors_are_fatal'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $sock and $sock->connected
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
| |||||||||||||||||||||||||||||
| 71 | 0 | my $msg = "failed to connect to $self->{'peer'}: $!"; | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | if($self->{'errors_are_fatal'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||