blib/lib/Monitoring/Sneck/Checks/tcp.pm

filter: Statementstmt 100% 71 / 71 Branchbran 90.0% 27 / 30 Conditioncond 50.0% 8 / 16 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 86.7% 124 / 143 CC 22 SCAR 22.5
CC
22
Cov
91%
check
16.1
validate
6.8
__ANON__
2.0
CRAP 9.4
« blib/lib/Monitoring/Sneck/Checks/file_content.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/packages.pm »
1 package Monitoring::Sneck::Checks::tcp;
2  
3 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Monitoring::Sneck::Checks::tcp - checks a TCP service
12  
13 =head1 VERSION
14  
15 Version 0.0.1
16  
17 =cut
18  
19 3 our $VERSION = '0.0.1';
20  
21 =head1 SYNOPSIS
22  
23 ssh|perl:tcp -H 192.0.2.10 -p 22 -e SSH-
24 smtp|perl:tcp -H mail.example.org -p 25 -e '220 ' -q 'QUIT\r\n'
25 imaps|perl:tcp -H mail.example.org -p 993 -S -e '* OK'
26 redis|perl:tcp -H 127.0.0.1 -p 6379 -s 'PING\r\n' -e +PONG -w 0.1 -c 0.5
27  
28 =head1 DESCRIPTION
29  
30 Connects to a port, optionally over TLS, sends anything given, and
31 looks for what is expected in what comes back. Not being able to
32 connect, or not getting what is expected, is CRITICAL.
33  
34 \r, \n, \t, and \\ in --send and --quit are turned into what they stand for.
35  
36 =head1 OPTIONS
37  
38 - -H, --hostname :: The host. Required.
39  
40 - -p, --port :: The port. Required.
41  
42 - -S, --ssl :: Connect over TLS.
43  
44 - -s, --send :: What to send once connected.
45  
46 - -e, --expect :: Text to look for in what comes back. May be given
47 more than once, any being found is OK.
48  
49 - -A, --all :: All of --expect must be found.
50  
51 - -q, --quit :: What to send before closing.
52  
53 - -w, --warning :: Range of seconds to connect.
54  
55 - -c, --critical :: Range of seconds to connect.
56  
57 - --insecure :: Do not check the certificate for --ssl.
58  
59 - --ca-file :: The CA file to check the certificate against.
60  
61 =cut
62  
63 8 ▶sub option_spec {
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
64 return (
65 8 'hostname|H=s', 'port|p=i', 'ssl|S', 'send|s=s', 'expect|e=s@', 'all|A',
66 'quit|q=s', 'warning|w=s', 'critical|c=s', 'insecure', 'ca-file=s',
67 );
68 }
69  
70 8 ▶sub required { return ( 'hostname', 'port' ) }
Subroutine required: called CC=1 SCAR=0.0
Pod documented
71  
72 8 ▶sub ranges { return ( 'warning', 'critical' ) }
Subroutine ranges: called CC=1 SCAR=0.0
Pod documented
73  
74 8 ▶sub validate {
Subroutine validate: called CC=6 SCAR=19.1
Pod documented
75 8 my ( $self, $options ) = @_;
76  
77 8 my @problems;
78 push( @problems, '--hostname "' . $options->{hostname} . '" is not a IP or hostname' )
79 8 ▶ if defined( $options->{hostname} ) && !$self->is_host( $options->{hostname} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"hostname"} and not $self->is_host($options->{"hostname"}) 0 8 8
Condition33%1/3
defined $options->{"hostname"} and not $self->is_host($options->{"hostname"})
!ll&&!rl&&r
080
MC/DC0%0/2
defined $options->{"hostname"} and not $self->is_host($options->{"hostname"})
defined $options->{"hostname"} not $self->is_host($options->{"hostname"})
Truth table33%1/3
A and not(B)
A= defined $options->{"hostname"}B= not $self->is_host($options->{"hostname"})
ABresult
0X0
100
111
80 push( @problems, '--port must be 1 to 65535' )
81 8 ▶ if defined( $options->{port} ) && ( $options->{port} < 1 || $options->{port} > 65535 );
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"port"} and $options->{"port"} < 1 || $options->{"port"} > 65535 1 7 8
Condition66%4/6
defined $options->{"port"} and $options->{"port"} < 1 || $options->{"port"} > 65535
!ll&&!rl&&r
071
$options->{"port"} < 1 or $options->{"port"} > 65535
l!l&&r!l&&!r
017
MC/DC33%1/3
defined $options->{"port"} and $options->{"port"} < 1 || $options->{"port"} > 65535
defined $options->{"port"} $options->{"port"} < 1 $options->{"port"} > 65535
Truth table50%2/4
A and B or C
A= defined $options->{"port"}B= $options->{"port"} < 1C= $options->{"port"} > 65535
ABCresult
0XX0
1000
11X1
1011
82  
83 8 return @problems;
84 } ## end sub validate
85  
86 =head1 METHODS
87  
88 =head2 check
89  
90 Does the check.
91  
92 =cut
93  
94 7 ▶sub check {
Subroutine check: called CC=16 SCAR=27.8
Pod documented
95 7 my ($self) = @_;
96  
97 7 my $where = $self->opt('hostname') . ':' . $self->opt('port');
98 7 my $start = Time::HiRes::time;
99 7 my $socket;
100 7 ▶ if ( $self->opt('ssl') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opt('ssl')) { } 3 4 7
101 3 require IO::Socket::SSL;
102 3 ▶ my %ssl = (
Branch: 1 branch
BranchTrueFalseTotal
$self->opt('insecure') ? : 1 2 3
103 PeerHost => $self->opt('hostname'),
104 PeerPort => $self->opt('port'),
105 Timeout => $self->timeout,
106 SSL_verify_mode => $self->opt('insecure') ? 0 : 1,
107 );
108 3 ▶ $ssl{SSL_ca_file} = $self->opt('ca-file') if defined( $self->opt('ca-file') );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->opt("ca-file") 1 2 3
109 3 $socket = IO::Socket::SSL->new(%ssl);
110 3 ▶ if ( !defined($socket) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $socket) 1 2 3
111 1 return $self->finish( $self->CRITICAL, $where . ', ' . IO::Socket::SSL::errstr() );
112 }
113 } else {
114 4 require IO::Socket::IP;
115 4 $socket = IO::Socket::IP->new(
116 PeerHost => $self->opt('hostname'),
117 PeerPort => $self->opt('port'),
118 Proto => 'tcp',
119 Timeout => $self->timeout,
120 );
121 4 ▶ if ( !defined($socket) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $socket) 1 3 4
122 1 ▶ return $self->finish( $self->CRITICAL, $where . ', ' . ( $@ || $! ) );
Condition33%1/3
$@ || $!
l!l&&r!l&&!r
100
MC/DC0%0/2
$@ || $!
$@ $!
Truth table33%1/3
A || B
A= $@B= $!
ABresult
1X1
011
000
123 }
124 } ## end else [ if ( $self->opt('ssl') ) ]
125 5 my $seconds = sprintf( '%.3f', Time::HiRes::time - $start );
126  
127 5 $socket->autoflush(1);
128 5 ▶ if ( defined( $self->opt('send') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->opt("send")) 1 4 5
129 1 print $socket _unescape( $self->opt('send') );
130 }
131  
132 5 my @codes = ( $self->threshold_status( $seconds, $self->opt('warning'), $self->opt('critical') ) );
133 5 my @problems;
134 5 ▶ push( @problems, 'slow' ) if $codes[0] != $self->OK;
Branch: 1 branch
BranchTrueFalseTotal
if $codes[0] != $self->OK 0 5 5
135 5 ▶ my @expect = @{ $self->opt('expect') // [] };
Condition50%1/2
$self->opt("expect") // []
l!l
50
MC/DC0%0/1
$self->opt("expect") // []
$self->opt("expect")
Truth table50%1/2
A
A= $self->opt("expect")
Aresult
11
00
136 5 ▶ if ( defined( $expect[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expect[0]) 5 0 5
137 my $received = $self->read_socket(
138 $socket,
139 5 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
140 5 my @found = grep { index( $_[0], $_ ) >= 0 } @expect;
141 5 ▶ return $self->opt('all') ? scalar(@found) == scalar(@expect) : defined( $found[0] );
Branch: 1 branch
BranchTrueFalseTotal
$self->opt('all') ? : 1 4 5
142 }
143 5 );
144 5 my @found = grep { index( $received, $_ ) >= 0 } @expect;
145 5 ▶ if ( $self->opt('all') ? scalar(@found) != scalar(@expect) : !defined( $found[0] ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->opt("all") ? scalar @found != scalar @expect : !defined($found[0])) 1 4 5
$self->opt('all') ? : 1 4 5
146 1 ▶ my $first = ( split( /\r?\n/, $received ) )[0] // '';
Condition50%1/2
(split(/\r?\n/u, $received, 0))[0] // ""
l!l
10
MC/DC0%0/1
(split(/\r?\n/u, $received, 0))[0] // ""
(split(/\r?\n/u, $received, 0))[0]
Truth table50%1/2
A
A= (split(/\r?\n/u, $received, 0))[0]
Aresult
11
00
147 1 push( @codes, $self->CRITICAL );
148 1 push( @problems, 'did not get ' . join( ', ', map { '"' . $_ . '"' } @expect ) . ', got "' . $first . '"' );
149 }
150 } ## end if ( defined( $expect[0] ) )
151  
152 5 ▶ if ( defined( $self->opt('quit') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->opt("quit")) 1 4 5
153 1 print $socket _unescape( $self->opt('quit') );
154 }
155 5 close($socket);
156  
157 5 $self->perfdata(
158 label => 'time',
159 value => $seconds,
160 uom => 's',
161 warning => $self->opt('warning'),
162 critical => $self->opt('critical'),
163 min => 0
164 );
165 5 my $message = 'connected to ' . $where . ' in ' . $seconds . ' seconds';
166 5 ▶ $message = $message . ', ' . join( ', ', @problems ) if defined( $problems[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $problems[0] 1 4 5
167  
168 5 return $self->finish( $self->worst(@codes), $message );
169 } ## end sub check
170  
171 # Turns \r, \n, \t, and \\ into what they stand for.
172 #
173 # Args...
174 #
175 # - text :: The text, from --send or --quit.
176 #
177 # Returns the text with them turned.
178 #
179 # Example...
180 #
181 # _unescape('QUIT\r\n');
182 # # "QUIT\r\n"
183 2 ▶sub _unescape {
Subroutine _unescape: called CC=1 SCAR=0.0
184 2 my ($text) = @_;
185  
186 2 my %turned = ( r => "\r", n => "\n", t => "\t", '\\' => '\\' );
187 2 $text =~ s/\\([rnt\\])/$turned{$1}/g;
188  
189 2 return $text;
190 }
191  
192 3 1;
« blib/lib/Monitoring/Sneck/Checks/file_content.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/packages.pm »