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

filter: Statementstmt 98.9% 95 / 96 Branchbran 91.6% 55 / 60 Conditioncond 69.4% 25 / 36 MC/DCmcdc 50.0% 10 / 20 Subroutinesub 100% 13 / 13 Podpod 100% 4 / 4 totaltotal 88.2% 202 / 229 CC 53 SCAR 24.8
CC
53
Cov
91%
listeners
20.3
check
15.2
validate
7.0
CRAP 11.9
« blib/lib/Monitoring/Sneck/Checks/file_mode.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/hosts.pm »
1 package Monitoring::Sneck::Checks::listening;
2  
3 16 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Socket qw(AF_INET AF_INET6 inet_ntop);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Monitoring::Sneck::Checks::listening - checks what ports are being listened on
12  
13 =head1 VERSION
14  
15 Version 0.0.1
16  
17 =cut
18  
19 16 our $VERSION = '0.0.1';
20  
21 # Where sockstat is on FreeBSD. Set by tests.
22 16 our $SOCKSTAT = '/usr/bin/sockstat';
23  
24 =head1 SYNOPSIS
25  
26 ssh_listening|perl:listening -p 22
27 web_listening|perl:listening -p tcp/80 -p tcp/443 -p udp/443
28 !only_expected||perl:listening -p 22 -p 25 -p udp/123 -o -L
29  
30 =head1 DESCRIPTION
31  
32 Checks each port given is being listened on, and with --only that
33 nothing else is. Either is CRITICAL.
34  
35 Ports are given as [protocol/][address:]port, such as 22, tcp/22,
36 udp/53, 127.0.0.1:6379, or tcp6/[::1]:25. tcp and udp match either IP
37 version, while tcp4, tcp6, udp4, and udp6 match only that one. No
38 address matches any, while a address must match exactly, a wildcard
39 listener, 0.0.0.0 or ::, counting for any address. For UDP, a bound
40 socket not connected to anything counts as listening. That includes
41 clients using unconnected sockets, such as for mDNS, so --only with UDP
42 may need --ignore for those.
43  
44 On Linux, sockets are read from /proc/net. On FreeBSD, sockstat -46lqn
45 is run. Other OSes are UNKNOWN.
46  
47 =head1 OPTIONS
48  
49 - -p, --port :: A port that must be listened on. May be given more
50 than once.
51  
52 - -o, --only :: Nothing but --port may be listened on.
53  
54 - -L, --ignore-loopback :: For --only, leave out what only listens on
55 loopback addresses.
56  
57 - -i, --ignore :: For --only, a port that may be listened on, given
58 the same way as --port. May be given more than once.
59  
60 One of --port or --only must be given.
61  
62 =cut
63  
64 126 ▶sub option_spec { return ( 'port|p=s@', 'only|o', 'ignore-loopback|L', 'ignore|i=s@' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
65  
66 126 ▶sub validate {
Subroutine validate: called CC=7 SCAR=19.5
Pod documented
67 126 my ( $self, $options ) = @_;
68  
69 126 my @problems;
70 126 foreach my $name ( 'port', 'ignore' ) {
71 252 ▶ foreach my $port ( @{ $options->{$name} // [] } ) {
Condition100%2/2
$options->{$name} // []
l!l
126126
MC/DC100%1/1
$options->{$name} // []
$options->{$name}
Truth table100%2/2
A
A= $options->{$name}
Aresult
11
00
72 308 ▶ push( @problems, '--' . $name . ' "' . $port . '" is not [protocol/][address:]port' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined _wanted($port) 28 280 308
73 if !defined( _wanted($port) );
74 }
75 }
76 126 ▶ push( @problems, 'nothing to check, give --port or --only' ) if !defined( $options->{port} ) && !$options->{only};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"port"} or $options->{"only"} 14 112 126
Condition66%2/3
defined $options->{"port"} or $options->{"only"}
l!l&&r!l&&!r
112014
MC/DC50%1/2
defined $options->{"port"} or $options->{"only"}
defined $options->{"port"} $options->{"only"}
Truth table66%2/3
A or B
A= defined $options->{"port"}B= $options->{"only"}
ABresult
1X1
011
000
77  
78 126 return @problems;
79 } ## end sub validate
80  
81 =head1 METHODS
82  
83 =head2 check
84  
85 Does the check.
86  
87 =cut
88  
89 84 ▶sub check {
Subroutine check: called CC=15 SCAR=27.2
Pod documented
90 84 my ($self) = @_;
91  
92 84 my @listeners = $self->listeners;
93 83 ▶ my @wanted = map { _wanted($_) } @{ $self->opt('port') // [] };
Condition50%1/2
$self->opt("port") // []
l!l
830
MC/DC0%0/1
$self->opt("port") // []
$self->opt("port")
Truth table50%1/2
A
A= $self->opt("port")
Aresult
11
00
94 83 ▶ my @ignored = map { _wanted($_) } @{ $self->opt('ignore') // [] };
Condition100%2/2
$self->opt("ignore") // []
l!l
1469
MC/DC100%1/1
$self->opt("ignore") // []
$self->opt("ignore")
Truth table100%2/2
A
A= $self->opt("ignore")
Aresult
11
00
95 83 my @problems;
96 83 foreach my $want (@wanted) {
97 262 ▶ push( @problems, $want->{text} . ' is not listened on' ) if !grep { _matches( $want, $_ ) } @listeners;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {_matches($want, $_);} @listeners 42 220 262
98 }
99  
100 83 ▶ if ( $self->opt('only') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opt("only")) 55 28 83
101 55 my %extra;
102 55 foreach my $listener (@listeners) {
103 262 ▶ next if grep { _matches( $_, $listener ) } @wanted, @ignored;
Branch: 1 branch
BranchTrueFalseTotal
if grep {_matches($_, $listener);} @wanted, @ignored 150 112 262
104 112 ▶ next if $self->opt('ignore-loopback') && $listener->{address} =~ /\A(?:127\.|::1\z|::ffff:127\.)/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->opt("ignore-loopback") and $listener->{"address"} =~ /\A(?:127\.|::1\z|::ffff:127\.)/u 56 56 112
Condition100%3/3
$self->opt("ignore-loopback") and $listener->{"address"} =~ /\A(?:127\.|::1\z|::ffff:127\.)/u
!ll&&!rl&&r
421456
MC/DC100%2/2
$self->opt("ignore-loopback") and $listener->{"address"} =~ /\A(?:127\.|::1\z|::ffff:127\.)/u
$self->opt("ignore-loopback") $listener->{"address"} =~ /\A(?:127\.|::1\z|::ffff:127\.)/u
Truth table100%3/3
A and B
A= $self->opt("ignore-loopback")B= $listener->{"address"} =~ /\A(?:127\.|::1\z|::ffff:127\.)/u
ABresult
0X0
100
111
105 56 $extra{ $listener->{protocol} . '/' . _join_address( $listener->{address}, $listener->{port} ) }
106 = $listener;
107 }
108 my @extra = sort {
109 55 $extra{$a}{protocol} cmp $extra{$b}{protocol}
110 || $extra{$a}{port} <=> $extra{$b}{port}
111 28 ▶ || $a cmp $b
Condition33%2/6
$extra{$a}{'protocol'} cmp $extra{$b}{'protocol'} || $extra{$a}{'port'} <=> $extra{$b}{'port'} || $a cmp $b
l!l&&r!l&&!r
2800
$extra{$a}{"protocol"} cmp $extra{$b}{"protocol"} or $extra{$a}{"port"} <=> $extra{$b}{"port"}
l!l&&r!l&&!r
2800
MC/DC0%0/3
$extra{$a}{'protocol'} cmp $extra{$b}{'protocol'} || $extra{$a}{'port'} <=> $extra{$b}{'port'} || $a cmp $b
$extra{$a}{"protocol"} cmp $extra{$b}{"protocol"} $extra{$a}{"port"} <=> $extra{$b}{"port"} $a cmp $b
Truth table25%1/4
A or B || C
A= $extra{$a}{"protocol"} cmp $extra{$b}{"protocol"}B= $extra{$a}{"port"} <=> $extra{$b}{"port"}C= $a cmp $b
ABCresult
1XX1
01X1
0011
0000
112 } keys(%extra);
113 55 ▶ push( @problems, 'also listening on ' . join( ', ', @extra ) ) if defined( $extra[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $extra[0] 28 27 55
114 } ## end if ( $self->opt('only') )
115  
116 83 my $message = scalar(@listeners) . ' listening';
117 83 ▶ $message = $message . ', ' . join( ', ', @problems ) if defined( $problems[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $problems[0] 42 41 83
118  
119 83 ▶ return $self->finish( defined( $problems[0] ) ? $self->CRITICAL : $self->OK, $message );
Branch: 1 branch
BranchTrueFalseTotal
defined $problems[0] ? : 42 41 83
120 } ## end sub check
121  
122 =head2 listeners
123  
124 Returns what is listening, as hash refs of protocol, tcp4, tcp6, udp4,
125 or udp6, address, and port. Dies if it can not be found.
126  
127 =cut
128  
129 84 ▶sub listeners {
Subroutine listeners: called CC=19 SCAR=30.1
Pod documented
130 84 my ($self) = @_;
131  
132 84 ▶ if ( $self->os eq 'linux' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "linux") 70 14 84
133 70 my @listeners;
134 70 foreach my $protocol ( 'tcp', 'tcp6', 'udp', 'udp6' ) {
135 280 ▶ my $table = eval { $self->proc_file( 'net/' . $protocol ) } // next;
Condition50%1/2
eval { do { $self->proc_file('net/' . $protocol) } } // (next)
l!l
2800
MC/DC0%0/1
eval { do { $self->proc_file('net/' . $protocol) } } // (next)
eval { do { $self->proc_file('net/' . $protocol) } }
Truth table50%1/2
A
A= eval { do { $self->proc_file('net/' . $protocol) } }
Aresult
11
00
136 280 ▶ my $family = $protocol =~ /6\z/ ? 6 : 4;
Branch: 1 branch
BranchTrueFalseTotal
$protocol =~ /6\z/u ? : 140 140 280
137  
138 # TCP_LISTEN is 0A, and a unconnected UDP socket is TCP_CLOSE, 07
139 280 ▶ my $listening = $protocol =~ /\Atcp/ ? '0A' : '07';
Branch: 1 branch
BranchTrueFalseTotal
$protocol =~ /\Atcp/u ? : 140 140 280
140 280 foreach my $line ( split( /\n/, $table ) ) {
141 700 my ( $local, $state ) = ( split( /\s+/, $line =~ s/\A\s+//r ) )[ 1, 3 ];
142 700 ▶ next if !defined($state) || $state ne $listening || $local !~ /\A([0-9A-F]+):([0-9A-F]{4})\z/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $state or $state ne $listening or not $local =~ /\A([0-9A-F]+):([0-9A-F]{4})\z/u 350 350 700
Condition66%4/6
not defined $state or $state ne $listening or not $local =~ /\A([0-9A-F]+):([0-9A-F]{4})\z/u
l!l&&r!l&&!r
3500350
not defined $state or $state ne $listening
l!l&&r!l&&!r
0350350
MC/DC33%1/3
not defined $state or $state ne $listening or not $local =~ /\A([0-9A-F]+):([0-9A-F]{4})\z/u
not defined $state $state ne $listening not $local =~ /\A([0-9A-F]+):([0-9A-F]{4})\z/u
Truth table50%2/4
not(A) or B or not(C)
A= not defined $stateB= $state ne $listeningC= not $local =~ /\A([0-9A-F]+):([0-9A-F]{4})\z/u
ABCresult
1XX1
01X1
0011
0000
143 350 my ( $hex, $port ) = ( $1, hex($2) );
144 350 push(
145 @listeners,
146 {
147 protocol => ( $protocol =~ s/\d\z//r ) . $family,
148 address => _hex_address( $hex, $family ),
149 port => $port
150 }
151 );
152 } ## end foreach my $line ( split( /\n/, $table ) )
153 } ## end foreach my $protocol ( 'tcp', 'tcp6', 'udp', 'udp6')
154 70 return @listeners;
155 } ## end if ( $self->os eq 'linux' )
156 14 ▶ if ( $self->os eq 'freebsd' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "freebsd") 14 0 14
157 14 my ( $exit, $output ) = $self->run_command( $SOCKSTAT, '-46lqn' );
158 13 ▶ die( 'sockstat exited ' . $exit . "\n" ) if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 0 13 13
159  
160 # USER COMMAND PID FD PROTO LOCAL FOREIGN, with tcp46 and the like for both versions
161 13 my @listeners;
162 13 foreach my $line ( split( /\n/, $output ) ) {
163 39 my ( $protocol, $local ) = ( split( /\s+/, $line ) )[ 4, 5 ];
164 39 ▶ next if !defined($local);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $local 0 39 39
165 39 ▶ my ( $kind, $families ) = $protocol =~ /\A(tcp|udp)(4|6|46)\z/ or next;
Branch: 1 branch
BranchTrueFalseTotal
() = $protocol =~ /\A(tcp|udp)(4|6|46)\z/u or next 39 0 39
166 39 ▶ my ( $address, $port ) = $local =~ /\A(.*):([0-9]+)\z/ or next;
Branch: 1 branch
BranchTrueFalseTotal
() = $local =~ /\A(.*):([0-9]+)\z/u or next 39 0 39
167 39 foreach my $family ( split( //, $families ) ) {
168 52 ▶ my $at = $address eq '*' ? ( $family eq '6' ? '::' : '0.0.0.0' ) : $address;
Branch: 2 branches
BranchTrueFalseTotal
$address eq '*' ? : 26 26 52
$family eq '6' ? : 13 13 26
169 52 push( @listeners, { protocol => $kind . $family, address => $at, port => $port + 0 } );
170 }
171 } ## end foreach my $line ( split( /\n/, $output ) )
172 13 return @listeners;
173 } ## end if ( $self->os eq 'freebsd' )
174  
175 0 die( 'not supported on ' . $self->os . "\n" );
176 } ## end sub listeners
177  
178 # Parses a --port.
179 #
180 # Args...
181 #
182 # - text :: The --port, such as 'udp/53' or 'tcp6/[::1]:25'.
183 #
184 # Returns a hash ref of text, protocol, tcp, udp, or one of those with 4
185 # or 6, address, undef for any, and port, or undef if it is not valid.
186 #
187 # Example...
188 #
189 # _wanted('127.0.0.1:6379');
190 # # { text => '127.0.0.1:6379', protocol => 'tcp', address => '127.0.0.1', port => 6379 }
191 584 ▶sub _wanted {
Subroutine _wanted: called CC=7 SCAR=19.5
192 584 my ($text) = @_;
193  
194 584 ▶ return undef if $text !~ m{\A(?:(tcp|udp)([46]?)/)?(?:\[?([0-9A-Fa-f.:]+?)\]?:)?([0-9]{1,5})\z};
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ m(\A(?:(tcp|udp)([46]?)/)?(?:\[?([0-9A-Fa-f.:]+?)\]?:)?([0-9]{1,5})\z)u 14 570 584
195 570 ▶ my ( $kind, $family, $address, $port ) = ( $1 // 'tcp', $2, $3, $4 );
Condition100%2/2
$1 // "tcp"
l!l
277293
MC/DC100%1/1
$1 // "tcp"
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
196 570 ▶ return undef if $port < 1 || $port > 65535;
Branch: 1 branch
BranchTrueFalseTotal
if $port < 1 or $port > 65535 14 556 570
Condition66%2/3
$port < 1 or $port > 65535
l!l&&r!l&&!r
014556
MC/DC50%1/2
$port < 1 or $port > 65535
$port < 1 $port > 65535
Truth table66%2/3
A or B
A= $port < 1B= $port > 65535
ABresult
1X1
011
000
197  
198 return {
199 556 ▶ text => $text,
Branch: 1 branch
BranchTrueFalseTotal
defined $address ? : 139 417 556
Condition100%2/2
$family // ""
l!l
277279
MC/DC100%1/1
$family // ""
$family
Truth table100%2/2
A
A= $family
Aresult
11
00
200 protocol => $kind . ( $family // '' ),
201 address => defined($address) ? lc($address) : undef,
202 port => $port + 0
203 };
204 } ## end sub _wanted
205  
206 # Checks if a listener is what a --port wants.
207 #
208 # Args...
209 #
210 # - want :: From _wanted.
211 #
212 # - listener :: From listeners.
213 #
214 # Returns 1 if it matches, else 0.
215 #
216 # Example...
217 #
218 # _matches( _wanted('tcp/22'), { protocol => 'tcp6', address => '::', port => 22 } ); # 1
219 1956 ▶sub _matches {
Subroutine _matches: called CC=7 SCAR=19.5
220 1956 my ( $want, $listener ) = @_;
221  
222 1956 ▶ return 0 if $want->{port} != $listener->{port};
Branch: 1 branch
BranchTrueFalseTotal
if $want->{"port"} != $listener->{"port"} 1492 464 1956
223 464 ▶ return 0 if index( $listener->{protocol}, $want->{protocol} ) != 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($listener->{"protocol"}, $want->{"protocol"}) != 0 80 384 464
224 384 ▶ return 1 if !defined( $want->{address} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $want->{"address"} 302 82 384
225 82 ▶ return 1 if $listener->{address} eq '0.0.0.0' || $listener->{address} eq '::';
Branch: 1 branch
BranchTrueFalseTotal
if $listener->{"address"} eq "0.0.0.0" or $listener->{"address"} eq "::" 14 68 82
Condition66%2/3
$listener->{"address"} eq "0.0.0.0" or $listener->{"address"} eq "::"
l!l&&r!l&&!r
14068
MC/DC50%1/2
$listener->{"address"} eq "0.0.0.0" or $listener->{"address"} eq "::"
$listener->{"address"} eq "0.0.0.0" $listener->{"address"} eq "::"
Truth table66%2/3
A or B
A= $listener->{"address"} eq "0.0.0.0"B= $listener->{"address"} eq "::"
ABresult
1X1
011
000
226  
227 68 ▶ return lc( $listener->{address} ) eq $want->{address} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
lc $listener->{'address'} eq $want->{'address'} ? : 54 14 68
228 } ## end sub _matches
229  
230 # Turns a address from /proc/net into text. Linux prints each 32 bit
231 # word of it as a number in the host's byte order, so packing each back
232 # that way gives the bytes as they were, in network order.
233 #
234 # Args...
235 #
236 # - hex :: The address as hex, 8 characters for IPv4 or 32 for IPv6.
237 #
238 # - family :: 4 or 6.
239 #
240 # Returns the address, such as 127.0.0.1 or ::1.
241 #
242 # Example...
243 #
244 # _hex_address( '0100007F', 4 ); # '127.0.0.1', on a little endian host
245 350 ▶sub _hex_address {
Subroutine _hex_address: called CC=2 SCAR=6.9
246 350 my ( $hex, $family ) = @_;
247  
248 350 my $packed = join( '', map { pack( 'L', hex($_) ) } $hex =~ /(.{8})/g );
249  
250 350 ▶ return inet_ntop( $family == 6 ? AF_INET6 : AF_INET, $packed );
Branch: 1 branch
BranchTrueFalseTotal
$family == 6 ? : 140 210 350
251 }
252  
253 # Joins a address and port, putting [] around IPv6 addresses.
254 #
255 # Args...
256 #
257 # - address :: The address.
258 #
259 # - port :: The port.
260 #
261 # Returns them joined.
262 #
263 # Example...
264 #
265 # _join_address( '::1', 25 ); # '[::1]:25'
266 56 ▶sub _join_address {
Subroutine _join_address: called CC=2 SCAR=6.9
267 56 my ( $address, $port ) = @_;
268  
269 56 ▶ return ( $address =~ /:/ ? '[' . $address . ']' : $address ) . ':' . $port;
Branch: 1 branch
BranchTrueFalseTotal
$address =~ /:/u ? : 14 42 56
270 }
271  
272 16 1;
« blib/lib/Monitoring/Sneck/Checks/file_mode.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/hosts.pm »