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 15 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶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 15 our $VERSION = '0.0.1';
20  
21 # Where sockstat is on FreeBSD. Set by tests.
22 15 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 117 ▶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 117 ▶sub validate {
Subroutine validate: called CC=7 SCAR=19.5
Pod documented
67 117 my ( $self, $options ) = @_;
68  
69 117 my @problems;
70 117 foreach my $name ( 'port', 'ignore' ) {
71 234 ▶ foreach my $port ( @{ $options->{$name} // [] } ) {
Condition100%2/2
$options->{$name} // []
l!l
117117
MC/DC100%1/1
$options->{$name} // []
$options->{$name}
Truth table100%2/2
A
A= $options->{$name}
Aresult
11
00
72 286 ▶ push( @problems, '--' . $name . ' "' . $port . '" is not [protocol/][address:]port' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined _wanted($port) 26 260 286
73 if !defined( _wanted($port) );
74 }
75 }
76 117 ▶ 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"} 13 104 117
Condition66%2/3
defined $options->{"port"} or $options->{"only"}
l!l&&r!l&&!r
104013
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 117 return @problems;
79 } ## end sub validate
80  
81 =head1 METHODS
82  
83 =head2 check
84  
85 Does the check.
86  
87 =cut
88  
89 78 ▶sub check {
Subroutine check: called CC=15 SCAR=27.2
Pod documented
90 78 my ($self) = @_;
91  
92 78 my @listeners = $self->listeners;
93 77 ▶ my @wanted = map { _wanted($_) } @{ $self->opt('port') // [] };
Condition50%1/2
$self->opt("port") // []
l!l
770
MC/DC0%0/1
$self->opt("port") // []
$self->opt("port")
Truth table50%1/2
A
A= $self->opt("port")
Aresult
11
00
94 77 ▶ my @ignored = map { _wanted($_) } @{ $self->opt('ignore') // [] };
Condition100%2/2
$self->opt("ignore") // []
l!l
1364
MC/DC100%1/1
$self->opt("ignore") // []
$self->opt("ignore")
Truth table100%2/2
A
A= $self->opt("ignore")
Aresult
11
00
95 77 my @problems;
96 77 foreach my $want (@wanted) {
97 243 ▶ push( @problems, $want->{text} . ' is not listened on' ) if !grep { _matches( $want, $_ ) } @listeners;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {_matches($want, $_);} @listeners 39 204 243
98 }
99  
100 77 ▶ if ( $self->opt('only') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opt("only")) 51 26 77
101 51 my %extra;
102 51 foreach my $listener (@listeners) {
103 243 ▶ next if grep { _matches( $_, $listener ) } @wanted, @ignored;
Branch: 1 branch
BranchTrueFalseTotal
if grep {_matches($_, $listener);} @wanted, @ignored 139 104 243
104 104 ▶ 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 52 52 104
Condition100%3/3
$self->opt("ignore-loopback") and $listener->{"address"} =~ /\A(?:127\.|::1\z|::ffff:127\.)/u
!ll&&!rl&&r
391352
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 52 $extra{ $listener->{protocol} . '/' . _join_address( $listener->{address}, $listener->{port} ) }
106 = $listener;
107 }
108 my @extra = sort {
109 51 $extra{$a}{protocol} cmp $extra{$b}{protocol}
110 || $extra{$a}{port} <=> $extra{$b}{port}
111 39 ▶ || $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
3900
$extra{$a}{"protocol"} cmp $extra{$b}{"protocol"} or $extra{$a}{"port"} <=> $extra{$b}{"port"}
l!l&&r!l&&!r
3900
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 51 ▶ push( @problems, 'also listening on ' . join( ', ', @extra ) ) if defined( $extra[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $extra[0] 26 25 51
114 } ## end if ( $self->opt('only') )
115  
116 77 my $message = scalar(@listeners) . ' listening';
117 77 ▶ $message = $message . ', ' . join( ', ', @problems ) if defined( $problems[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $problems[0] 39 38 77
118  
119 77 ▶ return $self->finish( defined( $problems[0] ) ? $self->CRITICAL : $self->OK, $message );
Branch: 1 branch
BranchTrueFalseTotal
defined $problems[0] ? : 39 38 77
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 78 ▶sub listeners {
Subroutine listeners: called CC=19 SCAR=30.1
Pod documented
130 78 my ($self) = @_;
131  
132 78 ▶ if ( $self->os eq 'linux' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "linux") 65 13 78
133 65 my @listeners;
134 65 foreach my $protocol ( 'tcp', 'tcp6', 'udp', 'udp6' ) {
135 260 ▶ my $table = eval { $self->proc_file( 'net/' . $protocol ) } // next;
Condition50%1/2
eval { do { $self->proc_file('net/' . $protocol) } } // (next)
l!l
2600
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 260 ▶ my $family = $protocol =~ /6\z/ ? 6 : 4;
Branch: 1 branch
BranchTrueFalseTotal
$protocol =~ /6\z/u ? : 130 130 260
137  
138 # TCP_LISTEN is 0A, and a unconnected UDP socket is TCP_CLOSE, 07
139 260 ▶ my $listening = $protocol =~ /\Atcp/ ? '0A' : '07';
Branch: 1 branch
BranchTrueFalseTotal
$protocol =~ /\Atcp/u ? : 130 130 260
140 260 foreach my $line ( split( /\n/, $table ) ) {
141 650 my ( $local, $state ) = ( split( /\s+/, $line =~ s/\A\s+//r ) )[ 1, 3 ];
142 650 ▶ 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 325 325 650
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
3250325
not defined $state or $state ne $listening
l!l&&r!l&&!r
0325325
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 325 my ( $hex, $port ) = ( $1, hex($2) );
144 325 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 65 return @listeners;
155 } ## end if ( $self->os eq 'linux' )
156 13 ▶ if ( $self->os eq 'freebsd' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "freebsd") 13 0 13
157 13 my ( $exit, $output ) = $self->run_command( $SOCKSTAT, '-46lqn' );
158 12 ▶ die( 'sockstat exited ' . $exit . "\n" ) if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 0 12 12
159  
160 # USER COMMAND PID FD PROTO LOCAL FOREIGN, with tcp46 and the like for both versions
161 12 my @listeners;
162 12 foreach my $line ( split( /\n/, $output ) ) {
163 36 my ( $protocol, $local ) = ( split( /\s+/, $line ) )[ 4, 5 ];
164 36 ▶ next if !defined($local);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $local 0 36 36
165 36 ▶ 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 36 0 36
166 36 ▶ my ( $address, $port ) = $local =~ /\A(.*):([0-9]+)\z/ or next;
Branch: 1 branch
BranchTrueFalseTotal
() = $local =~ /\A(.*):([0-9]+)\z/u or next 36 0 36
167 36 foreach my $family ( split( //, $families ) ) {
168 48 ▶ my $at = $address eq '*' ? ( $family eq '6' ? '::' : '0.0.0.0' ) : $address;
Branch: 2 branches
BranchTrueFalseTotal
$address eq '*' ? : 24 24 48
$family eq '6' ? : 12 12 24
169 48 push( @listeners, { protocol => $kind . $family, address => $at, port => $port + 0 } );
170 }
171 } ## end foreach my $line ( split( /\n/, $output ) )
172 12 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 542 ▶sub _wanted {
Subroutine _wanted: called CC=7 SCAR=19.5
192 542 my ($text) = @_;
193  
194 542 ▶ 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 13 529 542
195 529 ▶ my ( $kind, $family, $address, $port ) = ( $1 // 'tcp', $2, $3, $4 );
Condition100%2/2
$1 // "tcp"
l!l
257272
MC/DC100%1/1
$1 // "tcp"
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
196 529 ▶ return undef if $port < 1 || $port > 65535;
Branch: 1 branch
BranchTrueFalseTotal
if $port < 1 or $port > 65535 13 516 529
Condition66%2/3
$port < 1 or $port > 65535
l!l&&r!l&&!r
013516
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 516 ▶ text => $text,
Branch: 1 branch
BranchTrueFalseTotal
defined $address ? : 129 387 516
Condition100%2/2
$family // ""
l!l
257259
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 1814 ▶sub _matches {
Subroutine _matches: called CC=7 SCAR=19.5
220 1814 my ( $want, $listener ) = @_;
221  
222 1814 ▶ return 0 if $want->{port} != $listener->{port};
Branch: 1 branch
BranchTrueFalseTotal
if $want->{"port"} != $listener->{"port"} 1384 430 1814
223 430 ▶ return 0 if index( $listener->{protocol}, $want->{protocol} ) != 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($listener->{"protocol"}, $want->{"protocol"}) != 0 74 356 430
224 356 ▶ return 1 if !defined( $want->{address} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $want->{"address"} 280 76 356
225 76 ▶ 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 "::" 13 63 76
Condition66%2/3
$listener->{"address"} eq "0.0.0.0" or $listener->{"address"} eq "::"
l!l&&r!l&&!r
13063
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 63 ▶ return lc( $listener->{address} ) eq $want->{address} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
lc $listener->{'address'} eq $want->{'address'} ? : 50 13 63
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 325 ▶sub _hex_address {
Subroutine _hex_address: called CC=2 SCAR=6.9
246 325 my ( $hex, $family ) = @_;
247  
248 325 my $packed = join( '', map { pack( 'L', hex($_) ) } $hex =~ /(.{8})/g );
249  
250 325 ▶ return inet_ntop( $family == 6 ? AF_INET6 : AF_INET, $packed );
Branch: 1 branch
BranchTrueFalseTotal
$family == 6 ? : 130 195 325
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 52 ▶sub _join_address {
Subroutine _join_address: called CC=2 SCAR=6.9
267 52 my ( $address, $port ) = @_;
268  
269 52 ▶ return ( $address =~ /:/ ? '[' . $address . ']' : $address ) . ':' . $port;
Branch: 1 branch
BranchTrueFalseTotal
$address =~ /:/u ? : 13 39 52
270 }
271  
272 15 1;
« blib/lib/Monitoring/Sneck/Checks/file_mode.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/hosts.pm »