line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Monitoring::Livestatus::INET; |
2
|
4
|
|
|
4
|
|
93991
|
use parent 'Monitoring::Livestatus'; |
|
4
|
|
|
|
|
314
|
|
|
4
|
|
|
|
|
29
|
|
3
|
|
|
|
|
|
|
|
4
|
4
|
|
|
4
|
|
265
|
use strict; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
94
|
|
5
|
4
|
|
|
4
|
|
20
|
use warnings; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
95
|
|
6
|
4
|
|
|
4
|
|
2527
|
use IO::Socket::IP (); |
|
4
|
|
|
|
|
61152
|
|
|
4
|
|
|
|
|
131
|
|
7
|
4
|
|
|
4
|
|
41
|
use Socket qw(IPPROTO_TCP TCP_NODELAY); |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
207
|
|
8
|
4
|
|
|
4
|
|
25
|
use Carp qw/confess/; |
|
4
|
|
|
|
|
27
|
|
|
4
|
|
|
|
|
2280
|
|
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 object. C takes at least the server. |
25
|
|
|
|
|
|
|
Arguments are the same as in C. |
26
|
|
|
|
|
|
|
If the constructor is only passed a single argument, it is assumed to |
27
|
|
|
|
|
|
|
be a the C specification. Use either socker OR server. |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=cut |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub new { |
32
|
4
|
|
|
4
|
1
|
811
|
my($class, @args) = @_; |
33
|
4
|
100
|
|
|
|
20
|
unshift(@args, "peer") if scalar @args == 1; |
34
|
4
|
|
|
|
|
12
|
my(%options) = @args; |
35
|
4
|
100
|
|
|
|
18
|
$options{'name'} = $options{'peer'} unless defined $options{'name'}; |
36
|
|
|
|
|
|
|
|
37
|
4
|
|
|
|
|
10
|
$options{'backend'} = $class; |
38
|
4
|
|
|
|
|
23
|
my $self = Monitoring::Livestatus->new(%options); |
39
|
4
|
|
|
|
|
12
|
bless $self, $class; |
40
|
4
|
50
|
|
|
|
19
|
confess('not a scalar') if ref $self->{'peer'} ne ''; |
41
|
|
|
|
|
|
|
|
42
|
4
|
50
|
66
|
|
|
25
|
if(($self->{'peer'}//$self->{'server'}) =~ m|^tls://|mx) { |
43
|
0
|
|
|
|
|
0
|
require IO::Socket::SSL; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
4
|
|
|
|
|
22
|
return $self; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
######################################## |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head1 METHODS |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=cut |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub _open { |
57
|
0
|
|
|
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
|
0
|
|
|
|
|
if($peer_addr =~ s|tls://||mx) { |
69
|
0
|
|
|
|
|
|
$options->{'PeerAddr'} = $peer_addr; |
70
|
0
|
|
|
|
|
|
$options->{'SSL_cert_file'} = $self->{'cert'}; |
71
|
0
|
|
|
|
|
|
$options->{'SSL_key_file'} = $self->{'key'}; |
72
|
0
|
|
|
|
|
|
$options->{'SSL_ca_file'} = $self->{'ca_file'}; |
73
|
0
|
0
|
0
|
|
|
|
$options->{'SSL_verify_mode'} = 0 if(defined $self->{'verify'} && $self->{'verify'} == 0); |
74
|
0
|
|
|
|
|
|
$tls = 1; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
eval { |
78
|
0
|
0
|
|
|
|
|
if($tls) { |
79
|
0
|
|
|
|
|
|
$sock = IO::Socket::SSL->new(%{$options}); |
|
0
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
} else { |
81
|
0
|
|
|
|
|
|
$sock = IO::Socket::IP->new(%{$options}); |
|
0
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
} |
83
|
0
|
0
|
0
|
|
|
|
if(!defined $sock || !$sock->connected()) { |
84
|
0
|
|
|
|
|
|
my $msg = "failed to connect to $peer_addr: $!"; |
85
|
0
|
0
|
|
|
|
|
if($self->{'errors_are_fatal'}) { |
86
|
0
|
|
|
|
|
|
confess($msg); |
87
|
|
|
|
|
|
|
} |
88
|
0
|
|
|
|
|
|
$Monitoring::Livestatus::ErrorCode = 500; |
89
|
0
|
|
|
|
|
|
$Monitoring::Livestatus::ErrorMessage = $msg; |
90
|
0
|
|
|
|
|
|
return; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
setsockopt($sock, IPPROTO_TCP, TCP_NODELAY, 1); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
}; |
96
|
|
|
|
|
|
|
|
97
|
0
|
0
|
|
|
|
|
if($@) { |
98
|
0
|
|
|
|
|
|
$Monitoring::Livestatus::ErrorCode = 500; |
99
|
0
|
|
|
|
|
|
$Monitoring::Livestatus::ErrorMessage = $@; |
100
|
0
|
|
|
|
|
|
return; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
0
|
0
|
|
|
|
|
if(defined $self->{'query_timeout'}) { |
104
|
|
|
|
|
|
|
# set timeout |
105
|
0
|
|
|
|
|
|
$sock->timeout($self->{'query_timeout'}); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
return($sock); |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
######################################## |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub _close { |
115
|
0
|
|
|
0
|
|
|
my $self = shift; |
116
|
0
|
|
|
|
|
|
my $sock = shift; |
117
|
0
|
0
|
|
|
|
|
return unless defined $sock; |
118
|
0
|
|
|
|
|
|
return close($sock); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
1; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 AUTHOR |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Sven Nierlein, 2009-present, |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Copyright (C) by Sven Nierlein |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
133
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=cut |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
__END__ |