Lumberjack/Client.pm

filter: Statementstmt 43.2% 29 / 67 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 40.0% 4 / 10 Podpod 100% 1 / 1 totaltotal 26.3% 34 / 129 CC 21 SCAR 44.7
CC
21
Cov
26%
_connect
182.0
send_data
30.0
_ensure_connected
20.0
CRAP 87.2
↑ Summary Lumberjack/Reader.pm »
1 package Net::Lumberjack::Client;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: a client for the lumberjack protocol
6 1 our $VERSION = '1.01'; # VERSION
7  
8 1 ▶use IO::Socket::INET6;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Net::Lumberjack::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 1 has 'host' => ( is => 'ro', isa => 'Str', default => '127.0.0.1' );
14 1 has 'port' => ( is => 'ro', isa => 'Int', default => 5044 );
15 1 has 'keepalive' => ( is => 'ro', isa => 'Bool', default => 0 );
16 1 has 'frame_format' => ( is => 'ro', isa => 'Maybe[Str]' );
17  
18 1 has 'max_window_size' => ( is => 'ro', isa => 'Maybe[Int]' );
19  
20 1 has '_conn' => ( is => 'rw', isa => 'Maybe[IO::Handle]' );
21 1 has '_writer' => ( is => 'rw', isa => 'Maybe[Net::Lumberjack::Writer]' );
22  
23 1 has 'use_ssl' => ( is => 'ro', isa => 'Bool', default => 0 );
24 1 has 'ssl_verify' => ( is => 'ro', isa => 'Bool', default => 1 );
25 1 has 'ssl_ca_file' => ( is => 'ro', isa => 'Maybe[Str]' );
26 1 has 'ssl_ca_path' => ( is => 'ro', isa => 'Maybe[Str]' );
27 1 has 'ssl_version' => ( is => 'ro', isa => 'Maybe[Str]' );
28 1 has 'ssl_hostname' => ( is => 'ro', isa => 'Maybe[Str]' );
29  
30 1 has 'ssl_cert' => ( is => 'ro', isa => 'Maybe[Str]' );
31 1 has 'ssl_key' => ( is => 'ro', isa => 'Maybe[Str]' );
32  
33 sub _connect {
34 0 ▶ my $self = shift;
Subroutine _connect: not called CC=13 SCAR=52.0
35 0 my $sock;
36 0 ▶ if( $self->use_ssl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_ssl) { } 0 0 0
37 0 ▶ $sock = IO::Socket::SSL->new(
Branch: 7 branches
BranchTrueFalseTotal
$sock = "IO::Socket::SSL"->new("PeerHost", $self->host, "PeerPort", $self->port, "SSL_verify_mode", $self->ssl_verify ? 1 : 0, defined $self->ssl_version ? ("SSL_version", $self->ssl_version) : (), defined $self->ssl_ca_file ? ("SSL_ca_file", $self->ssl_ca_file) : (), defined $self->ssl_ca_path ? ("SSL_ca_file", $self->ssl_ca_path) : (), defined $self->ssl_cert && defined $self->ssl_key ? ("SSL_use_cert", 1, "SSL_cert_file", $self->ssl_cert, "SSL_key_file", $self->ssl_key) : (), defined $self->ssl_hostname ? ("SSL_hostname", $self->ssl_hostname) : ()) or die 'could not enstablish ssl connection to ' . $self->host . ':' . $self->port . ': ' . $Net::Lumberjack::Client::SSL_ERROR 0 0 0
$self->ssl_verify ? : 0 0 0
defined $self->ssl_version ? : 0 0 0
defined $self->ssl_ca_file ? : 0 0 0
defined $self->ssl_ca_path ? : 0 0 0
defined $self->ssl_cert && defined $self->ssl_key ? : 0 0 0
defined $self->ssl_hostname ? : 0 0 0
Condition0%0/3
defined $self->ssl_cert and defined $self->ssl_key
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->ssl_cert and defined $self->ssl_key
defined $self->ssl_cert defined $self->ssl_key
Truth table0%0/3
A and B
A= defined $self->ssl_certB= defined $self->ssl_key
ABresult
0X0
100
111
38 PeerHost => $self->host,
39 PeerPort => $self->port,
40 SSL_verify_mode => $self->ssl_verify ?
41 SSL_VERIFY_PEER : SSL_VERIFY_NONE ,
42 defined $self->ssl_version ?
43 ( SSL_version => $self->ssl_version ) : (),
44 defined $self->ssl_ca_file ?
45 ( SSL_ca_file => $self->ssl_ca_file ) : (),
46 defined $self->ssl_ca_path ?
47 ( SSL_ca_file => $self->ssl_ca_path ) : (),
48 defined $self->ssl_cert && defined $self->ssl_key ?
49 (
50 SSL_use_cert => 1,
51 SSL_cert_file => $self->ssl_cert,
52 SSL_key_file => $self->ssl_key,
53 ) : (),
54 defined $self->ssl_hostname ?
55 ( SSL_hostname => $self->ssl_hostname ) : (),
56 ) or die('could not enstablish ssl connection to '.$self->host.':'.$self->port.': '.$SSL_ERROR);
57 } else {
58 0 ▶ $sock = IO::Socket::INET6->new(
Branch: 1 branch
BranchTrueFalseTotal
$sock = "IO::Socket::INET6"->new("Proto", "tcp", "PeerAddr", $self->host, "PeerPort", $self->port) or die 'could not connect to ' . $self->host . ':' . $self->port . ': ' . $! 0 0 0
59 Proto => 'tcp',
60 PeerAddr => $self->host,
61 PeerPort => $self->port,
62 ) or die('could not connect to '.$self->host.':'.$self->port.': '.$!);
63 }
64 0 $self->_conn( $sock );
65  
66 0 ▶ my $writer = Net::Lumberjack::Writer->new(
Branch: 2 branches
BranchTrueFalseTotal
defined $self->max_window_size ? : 0 0 0
defined $self->frame_format ? : 0 0 0
67 handle => $sock,
68 defined $self->max_window_size ?
69 ( max_window_size => $self->max_window_size ) : (),
70 defined $self->frame_format ?
71 ( frame_format => $self->frame_format ) : (),
72 );
73 0 $self->_writer( $writer );
74  
75 0 return;
76 }
77  
78 sub _ensure_connected {
79 0 ▶ my $self = shift;
Subroutine _ensure_connected: not called CC=4 SCAR=30.0
80  
81 0 ▶ if( defined $self->_conn
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_conn and $self->_conn->connected and defined $self->_writer) 0 0 0
Condition0%0/6
defined $self->_conn and $self->_conn->connected and defined $self->_writer
!ll&&!rl&&r
000
defined $self->_conn and $self->_conn->connected
!ll&&!rl&&r
000
MC/DC0%0/3
defined $self->_conn and $self->_conn->connected and defined $self->_writer
defined $self->_conn $self->_conn->connected defined $self->_writer
Truth table0%0/4
A and B and C
A= defined $self->_connB= $self->_conn->connectedC= defined $self->_writer
ABCresult
0XX0
10X0
1100
1111
82 && $self->_conn->connected
83 && defined $self->_writer ) {
84 0 return;
85 }
86  
87 0 $self->_connect;
88  
89 0 return;
90 }
91 sub _reconnect {
92 0 ▶ my $self = shift;
Subroutine _reconnect: not called CC=1 SCAR=6.9
93  
94 0 $self->_disconnect;
95 0 $self->_connect;
96  
97 0 return;
98 }
99  
100 sub _disconnect {
101 0 ▶ my $self = shift;
Subroutine _disconnect: not called CC=2 SCAR=17.9
102  
103 0 ▶ if( defined $self->_conn ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_conn) 0 0 0
104 0 $self->_conn->close();
105 0 $self->_conn( undef );
106 }
107 0 $self->_writer( undef );
108  
109 0 return;
110 }
111  
112 sub send_data {
113 0 ▶ my $self = shift;
Subroutine send_data: not called CC=5 SCAR=34.0
Pod documented
114  
115 0 $self->_ensure_connected;
116  
117 0 eval {
118 0 ▶ local $SIG{PIPE} = sub { die "connection reset (broken pipe)" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
119 0 $self->_writer->send_data( @_ );
120 };
121 0 my $e = $@;
122 0 ▶ if( $e ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e) 0 0 0
123 0 ▶ if( $e =~ /^connection reset/
Branch: 1 branch
BranchTrueFalseTotal
if ($e =~ /^connection reset/ or $e =~ /^lost connection/) 0 0 0
Condition0%0/3
$e =~ /^connection reset/ or $e =~ /^lost connection/
l!l&&r!l&&!r
000
MC/DC0%0/2
$e =~ /^connection reset/ or $e =~ /^lost connection/
$e =~ /^connection reset/ $e =~ /^lost connection/
Truth table0%0/3
A or B
A= $e =~ /^connection reset/B= $e =~ /^lost connection/
ABresult
1X1
011
000
124 || $e =~ /^lost connection/ ) {
125 0 $self->_disconnect; # try to cleanup
126 }
127 0 die $e;
128 }
129  
130 0 ▶ if( ! $self->keepalive ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->keepalive) 0 0 0
131 0 $self->_disconnect;
132 }
133 0 return;
134 }
135  
136 1 1;
137  
138 __END__
139  
140 =pod
141  
142 =encoding UTF-8
143  
144 =head1 NAME
145  
146 Net::Lumberjack::Client - a client for the lumberjack protocol
147  
148 =head1 VERSION
149  
150 version 1.01
151  
152 =head1 SYNOPSIS
153  
154 use Net::Lumberjack::Client;
155  
156 my $client = Net::Lumberjack::Client->new(
157 host => '127.0.0.1',
158 port => 5044,
159 # for beats server
160 keepalive => 0,
161 frame_format => 'json',
162 ## for a saftpresse (Log::Saftpresse) server
163 # keepalive => 1, # or 0
164 # frame_format => 'json',
165 ## for a lumberjack (v1) server
166 # keepalive => 1,
167 # frame_format => 'data',
168 );
169  
170 $client->send_data(
171 { message => 'hello world!', lang => 'en' },
172 { message => 'Hallo Welt!', lang => 'de' },
173 );
174  
175 =head1 ATTRIBUTES
176  
177 =head2 host (default: '127.0.0.1')
178  
179 Host to connect to.
180  
181 =head2 port (default: 5044)
182  
183 TCP port to connect to.
184  
185 =head2 keepalive (default: 0)
186  
187 If enabled connection will be keept open between send_data() calls.
188 Otherwise it will be closed and reopened on every call.
189  
190 Needs to be disabled for logstash-input-beats since it expects only
191 one bulk of frames per connection.
192  
193 =head2 frame_formt (default: 'json')
194  
195 The following frame formats are supported:
196  
197 =over
198  
199 =item 'json', 'v2'
200  
201 Uses json formatted data frames as defined in lumberjack protocol v2. (type 'J')
202  
203 =item 'data', 'v1'
204  
205 Uses lumberjack DATA (type 'D') frames as defined in lumberjack protocol v1.
206  
207 This format only supports a flat hash structure.
208  
209 =back
210  
211 =head2 max_window_size (default: 2048)
212  
213 Maximum number of frames the clients sends in one bulk.
214  
215 When hitting this limit the client will split up the stream
216 into smaller bulks.
217  
218 =head2 use_ssl (default: 0)
219  
220 Enable SSL transport encryption.
221  
222 =head2 ssl_verify (default: 1)
223  
224 Enable verification of SSL server certificate.
225  
226 =head2 ssl_ca_file (default: emtpy)
227  
228 Use a non-default CA file to retrieve list of trusted root CAs.
229  
230 Otherwise the system wide default will be used.
231  
232 =head2 ssl_ca_path (default: emtpy)
233  
234 Use a non-default CA path to retrieve list of trusted root CAs.
235  
236 Otherwise the system wide default will be used.
237  
238 =head2 ssl_version (default: empty)
239  
240 Use a non-default SSL protocol version string.
241  
242 Otherwise the system wide default will be used.
243  
244 Check L<IO::Socket::SSL> for string format.
245  
246 =head2 ssl_hostname (default: emtpy)
247  
248 Use a hostname other than the hostname give in 'host' for
249 SSL certificate verification.
250  
251 This could be used if you use a IP address to connecting to
252 server that only lists the hostname in its certificate.
253  
254 =head2 ssl_cert (default: empty)
255  
256 =head2 ssl_key (default: empty)
257  
258 If 'ssl_cert' and 'ssl_key' is the client will enable
259 client side authentication and use the supplied certificate/key.
260  
261 =head1 METHODS
262  
263 =head2 send_data( HashRef1, ... )
264  
265 This method takes a list of HashRefs and sends them to the server.
266  
267 =head1 AUTHOR
268  
269 Markus Benning <ich@markusbenning.de>
270  
271 =head1 COPYRIGHT AND LICENSE
272  
273 This software is Copyright (c) 2016 by Markus Benning.
274  
275 This is free software, licensed under:
276  
277 The Apache License, Version 2.0, January 2004
278  
279 =cut
↑ Summary Lumberjack/Reader.pm »