| 1 | package Monitoring::Livestatus::INET; | ||||||||||||||||||||||||||||
| 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::IP (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Socket qw(IPPROTO_TCP TCP_NODELAY); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use parent 'Monitoring::Livestatus'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Monitoring::Livestatus::INET - connector with tcp sockets | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | use Monitoring::Livestatus; | ||||||||||||||||||||||||||||
| 17 | my $nl = Monitoring::Livestatus::INET->new( 'localhost:9999' ); | ||||||||||||||||||||||||||||
| 18 | my $hosts = $nl->selectall_arrayref("GET hosts"); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head2 new ( [ARGS] ) | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | Creates an C<Monitoring::Livestatus::INET> object. C<new> takes at least the server. | ||||||||||||||||||||||||||||
| 25 | Arguments are the same as in C<Monitoring::Livestatus>. | ||||||||||||||||||||||||||||
| 26 | If the constructor is only passed a single argument, it is assumed to | ||||||||||||||||||||||||||||
| 27 | be a the C<server> specification. Use either socker OR server. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.3
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 4 | my($class, @args) = @_; | |||||||||||||||||||||||||||
| 33 | 4 | ▶ | unshift(@args, "peer") if scalar @args == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 4 | my(%options) = @args; | |||||||||||||||||||||||||||
| 35 | 4 | ▶ | $options{'name'} = $options{'peer'} unless defined $options{'name'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 4 | $options{'backend'} = $class; | |||||||||||||||||||||||||||
| 38 | 4 | my $self = Monitoring::Livestatus->new(%options); | |||||||||||||||||||||||||||
| 39 | 4 | bless $self, $class; | |||||||||||||||||||||||||||
| 40 | 4 | ▶ | confess('not a scalar') if ref $self->{'peer'} ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 4 | ▶ | if(($self->{'peer'}//$self->{'server'}) =~ m|^tls://|mx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"peer"} // $self->{"server"}
MC/DC0%0/2
$self->{"peer"} // $self->{"server"}
$self->{"peer"}
$self->{"server"}
Truth table66%2/3
A // B
A= $self->{"peer"}B= $self->{"server"}
| |||||||||||||||||||||||||||||
| 43 | 0 | require IO::Socket::SSL; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 4 | return $self; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | ######################################## | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =head1 METHODS | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =cut | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | sub _open { | ||||||||||||||||||||||||||
|
Subroutine
_open: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||
| 57 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 58 | 0 | my $sock; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | my $options = { | ||||||||||||||||||||||||||||
| 61 | PeerAddr => $self->{'peer'}, | ||||||||||||||||||||||||||||
| 62 | Type => IO::Socket::IP::SOCK_STREAM, | ||||||||||||||||||||||||||||
| 63 | 0 | Timeout => $self->{'connect_timeout'}, | |||||||||||||||||||||||||||
| 64 | }; | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | my $tls = 0; | |||||||||||||||||||||||||||
| 67 | 0 | my $peer_addr = $self->{'peer'}; | |||||||||||||||||||||||||||
| 68 | 0 | ▶ | if($peer_addr =~ s|tls://||mx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | #$IO::Socket::SSL::DEBUG = 2 if $ENV{'THRUK_VERBOSE'} && $ENV{'THRUK_VERBOSE'} >= 2; | ||||||||||||||||||||||||||||
| 70 | #$IO::Socket::SSL::DEBUG = 3 if $ENV{'THRUK_VERBOSE'} && $ENV{'THRUK_VERBOSE'} >= 3; | ||||||||||||||||||||||||||||
| 71 | 0 | $options->{'PeerAddr'} = $peer_addr; | |||||||||||||||||||||||||||
| 72 | 0 | $options->{'SSL_cert_file'} = $self->{'cert'}; | |||||||||||||||||||||||||||
| 73 | 0 | $options->{'SSL_key_file'} = $self->{'key'}; | |||||||||||||||||||||||||||
| 74 | 0 | $options->{'SSL_ca_file'} = $self->{'ca_file'}; | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | $options->{'SSL_verify_mode'} = 0 if(defined $self->{'verify'} && $self->{'verify'} == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->{"verify"} and $self->{"verify"} == 0
MC/DC0%0/2
defined $self->{"verify"} and $self->{"verify"} == 0
defined $self->{"verify"}
$self->{"verify"} == 0
Truth table0%0/3
A and B
A= defined $self->{"verify"}B= $self->{"verify"} == 0
| |||||||||||||||||||||||||||||
| 76 | 0 | $options->{'SSL_verifycn_name'} = $self->{'verifycn_name'}; | |||||||||||||||||||||||||||
| 77 | 0 | $tls = 1; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | eval { | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | if($tls) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | $sock = IO::Socket::SSL->new(%{$options}); | |||||||||||||||||||||||||||
| 83 | } else { | ||||||||||||||||||||||||||||
| 84 | 0 | $sock = IO::Socket::IP->new(%{$options}); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 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
| |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my $msg = "failed to connect to $peer_addr: ".($tls ? IO::Socket::SSL::errstr() : $!); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if($self->{'errors_are_fatal'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | confess($msg); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 0 | $Monitoring::Livestatus::ErrorCode = 500; | |||||||||||||||||||||||||||
| 92 | 0 | $Monitoring::Livestatus::ErrorMessage = $msg; | |||||||||||||||||||||||||||
| 93 | 0 | return; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | setsockopt($sock, IPPROTO_TCP, TCP_NODELAY, 1); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | }; | ||||||||||||||||||||||||||||
| 99 | 0 | my $err = $@; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if($err) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | $Monitoring::Livestatus::ErrorCode = 500; | |||||||||||||||||||||||||||
| 103 | 0 | $Monitoring::Livestatus::ErrorMessage = $err; | |||||||||||||||||||||||||||
| 104 | 0 | return; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if(defined $self->{'query_timeout'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | # set timeout | ||||||||||||||||||||||||||||
| 109 | 0 | $sock->timeout($self->{'query_timeout'}); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | return($sock); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | ######################################## | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub _close { | ||||||||||||||||||||||||||
|
Subroutine
_close: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 119 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 120 | 0 | my $sock = shift; | |||||||||||||||||||||||||||
| 121 | 0 | ▶ | return unless defined $sock; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | return close($sock); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 4 | 1; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Sven Nierlein, 2009-present, <sven@nierlein.org> | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | Copyright (C) by Sven Nierlein | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 137 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =cut | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | __END__ | ||||||||||||||||||||||||||||