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

filter: Statementstmt 100% 148 / 148 Branchbran 87.5% 63 / 72 Conditioncond 71.1% 69 / 97 MC/DCmcdc 44.9% 22 / 49 Subroutinesub 100% 18 / 18 Podpod 100% 10 / 10 totaltotal 83.7% 330 / 394 CC 80 SCAR 26.0
CC
80
Cov
88%
freebsd_info
25.0
interface_status
19.1
linux_info
13.3
CRAP 13.5
« blib/lib/Monitoring/Sneck/Checks/kernel_modules.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/tls_protocols.pm »
1 package Monitoring::Sneck::Checks::interface;
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 File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Monitoring::Sneck::Checks::interface - checks network interfaces are up, at speed, and not erroring
13  
14 =head1 VERSION
15  
16 Version 0.0.1
17  
18 =cut
19  
20 16 our $VERSION = '0.0.1';
21  
22 # Where things are. Set by tests.
23 16 our $NET = '/sys/class/net';
24 16 our $IFCONFIG = '/sbin/ifconfig';
25 16 our $NETSTAT = '/usr/bin/netstat';
26  
27 =head1 SYNOPSIS
28  
29 uplink|perl:interface -i em0 -s 1000 -F -m 9000
30 nics|perl:interface -a -e 0 -E 10
31 bond|perl:interface -i bond0 -i eth0 -i eth1 -s 10000 -r 1 -R 100
32  
33 =head1 DESCRIPTION
34  
35 Checks each interface is up, which is CRITICAL if not, and optionally
36 that it is at a speed, at full duplex, and has a MTU, each WARNING if
37 not, as a link that came up at 100 Mb/s or half duplex works, just
38 badly.
39  
40 Errors and drops are checked as a rate, per second, since the last
41 run, with the counters kept in a state file, see
42 L<Monitoring::Sneck::Checks::base/read_state>. So the first run, or
43 one after the counters reset, has no rate and is not checked for them.
44 They are only kept and checked if one of --errors-warning,
45 --errors-critical, --drops-warning, or --drops-critical is given, so
46 nothing else needs the state file.
47  
48 On Linux, everything is read from /sys/class/net, and --all is every
49 interface backed by a device, so not lo, bridges, VLANs, and the like.
50 On FreeBSD it comes from ifconfig and netstat -I, and --all is
51 ifconfig -l ether. Other OSes are UNKNOWN.
52  
53 =head1 OPTIONS
54  
55 - -i, --interface :: A interface. May be given more than once.
56  
57 - -a, --all :: Every interface backed by a device on Linux, or every
58 Ethernet one, as listed by ifconfig -l ether, on FreeBSD.
59  
60 - -x, --ignore :: A interface for --all to leave out, such as a unused
61 port. May be given more than once.
62  
63 - -s, --speed :: The Mb/s it must be at least.
64  
65 - -F, --full-duplex :: It must be at full duplex.
66  
67 - -m, --mtu :: The MTU it must have.
68  
69 - -e, --errors-warning :: Range of errors per second.
70  
71 - -E, --errors-critical :: Range of errors per second.
72  
73 - -r, --drops-warning :: Range of drops per second.
74  
75 - -R, --drops-critical :: Range of drops per second.
76  
77 One of --interface or --all must be given.
78  
79 =cut
80  
81 176 ▶sub option_spec {
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
82 return (
83 176 'interface|i=s@', 'all|a', 'ignore|x=s@', 'speed|s=i',
84 'full-duplex|F', 'mtu|m=i', 'errors-warning|e=s', 'errors-critical|E=s',
85 'drops-warning|r=s', 'drops-critical|R=s',
86 );
87 }
88  
89 176 ▶sub ranges { return ( 'errors-warning', 'errors-critical', 'drops-warning', 'drops-critical' ) }
Subroutine ranges: called CC=1 SCAR=0.0
Pod documented
90  
91 176 ▶sub validate {
Subroutine validate: called CC=6 SCAR=17.9
Pod documented
92 176 my ( $self, $options ) = @_;
93  
94 176 my @problems;
95 176 ▶ foreach my $interface ( @{ $options->{interface} // [] } ) {
Condition100%2/2
$options->{"interface"} // []
l!l
12452
MC/DC100%1/1
$options->{"interface"} // []
$options->{"interface"}
Truth table100%2/2
A
A= $options->{"interface"}
Aresult
11
00
96 124 ▶ push( @problems, '--interface "' . $interface . '" is not a interface name' )
Branch: 1 branch
BranchTrueFalseTotal
unless $interface =~ /\A[A-Za-z0-9_.:-]+\z/u 0 124 124
97 if $interface !~ /\A[A-Za-z0-9_.:-]+\z/;
98 }
99 push( @problems, 'nothing to check, give --interface or --all' )
100 176 ▶ if !defined( $options->{interface} ) && !$options->{all};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"interface"} or $options->{"all"} 13 163 176
Condition100%3/3
defined $options->{"interface"} or $options->{"all"}
l!l&&r!l&&!r
1243913
MC/DC100%2/2
defined $options->{"interface"} or $options->{"all"}
defined $options->{"interface"} $options->{"all"}
Truth table100%3/3
A or B
A= defined $options->{"interface"}B= $options->{"all"}
ABresult
1X1
011
000
101  
102 176 return @problems;
103 } ## end sub validate
104  
105 =head1 METHODS
106  
107 =head2 check
108  
109 Does the check.
110  
111 =cut
112  
113 163 ▶sub check {
Subroutine check: called CC=10 SCAR=23.1
Pod documented
114 163 my ($self) = @_;
115  
116 163 ▶ die( 'not supported on ' . $self->os . "\n" ) if $self->os ne 'linux' && $self->os ne 'freebsd';
Branch: 1 branch
BranchTrueFalseTotal
if $self->os ne "linux" and $self->os ne "freebsd" 0 163 163
Condition66%2/3
$self->os ne "linux" and $self->os ne "freebsd"
!ll&&!rl&&r
130330
MC/DC0%0/2
$self->os ne "linux" and $self->os ne "freebsd"
$self->os ne "linux" $self->os ne "freebsd"
Truth table66%2/3
A and B
A= $self->os ne "linux"B= $self->os ne "freebsd"
ABresult
0X0
100
111
117 163 ▶ my %names = map { $_ => 1 } @{ $self->opt('interface') // [] }, ( $self->opt('all') ? $self->all : () );
Branch: 1 branch
BranchTrueFalseTotal
$self->opt('all') ? : 39 124 163
Condition100%2/2
$self->opt("interface") // []
l!l
12439
MC/DC100%1/1
$self->opt("interface") // []
$self->opt("interface")
Truth table100%2/2
A
A= $self->opt("interface")
Aresult
11
00
118 162 ▶ delete( @names{ @{ $self->opt('ignore') // [] } } );
Condition100%2/2
$self->opt("ignore") // []
l!l
13149
MC/DC100%1/1
$self->opt("ignore") // []
$self->opt("ignore")
Truth table100%2/2
A
A= $self->opt("ignore")
Aresult
11
00
119 162 my @names = sort( keys(%names) );
120 162 ▶ return $self->finish( $self->UNKNOWN, 'no interfaces found' ) if !defined( $names[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $names[0] 0 162 162
121  
122 162 my @codes;
123 my @parts;
124 162 foreach my $name (@names) {
125 185 ▶ my $info = $self->os eq 'linux' ? $self->linux_info($name) : $self->freebsd_info($name);
Branch: 1 branch
BranchTrueFalseTotal
$self->os eq 'linux' ? : 143 42 185
126 166 ▶ if ( !defined($info) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $info) 14 152 166
127 14 push( @codes, $self->CRITICAL );
128 14 push( @parts, $name . ' is not there' );
129 14 next;
130 }
131 152 my ( $code, $part ) = $self->interface_status( $name, $info );
132 152 push( @codes, $code );
133 152 push( @parts, $part );
134 } ## end foreach my $name (@names)
135  
136 143 return $self->finish( $self->worst(@codes), join( ', ', @parts ) );
137 } ## end sub check
138  
139 =head2 interface_status
140  
141 Works out the status of a interface, as said under L</DESCRIPTION>,
142 adding its perfdata.
143  
144 - name :: The interface.
145  
146 - info :: Hash ref of up, 1 or 0, speed, in Mb/s, duplex, full or half,
147 mtu, errors, and drops, the counters, any undef if not known.
148  
149 Returns the code and what to say about it.
150  
151 =cut
152  
153 152 ▶sub interface_status {
Subroutine interface_status: called CC=19 SCAR=29.5
Pod documented
154 152 my ( $self, $name, $info ) = @_;
155  
156 152 ▶ if ( !$info->{up} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($info->{"up"}) 13 139 152
157 13 return ( $self->CRITICAL, $name . ' is down' );
158 }
159  
160 139 my @codes = ( $self->OK );
161 139 my @problems;
162 139 my @is = ('up');
163 139 ▶ push( @is, $info->{speed} . ' Mb/s' ) if defined( $info->{speed} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"speed"} 111 28 139
164 139 ▶ push( @is, $info->{duplex} ) if defined( $info->{duplex} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"duplex"} 111 28 139
165 139 ▶ if ( defined( $self->opt('speed') ) && ( $info->{speed} // 0 ) < $self->opt('speed') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->opt("speed") and ($info->{"speed"} // 0) < $self->opt("speed")) 17 122 139
Condition80%4/5
defined $self->opt("speed") and ($info->{"speed"} // 0) < $self->opt("speed")
!ll&&!rl&&r
992317
$info->{"speed"} // 0
l!l
400
MC/DC50%1/2
defined $self->opt("speed") and ($info->{"speed"} // 0) < $self->opt("speed")
defined $self->opt("speed") $info->{"speed"}
Truth table66%2/3
A and B
A= defined $self->opt("speed")B= $info->{"speed"}
ABresult
0X0
100
111
166 17 push( @codes, $self->WARNING );
167 17 push( @problems, 'under ' . $self->opt('speed') . ' Mb/s' );
168 }
169 139 ▶ if ( $self->opt('full-duplex') && ( $info->{duplex} // '' ) ne 'full' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opt("full-duplex") and ($info->{"duplex"} // "") ne "full") 17 122 139
Condition80%4/5
$self->opt("full-duplex") and ($info->{"duplex"} // "") ne "full"
!ll&&!rl&&r
992317
$info->{"duplex"} // ""
l!l
400
MC/DC50%1/2
$self->opt("full-duplex") and ($info->{"duplex"} // "") ne "full"
$self->opt("full-duplex") $info->{"duplex"}
Truth table66%2/3
A and B
A= $self->opt("full-duplex")B= $info->{"duplex"}
ABresult
0X0
100
111
170 17 push( @codes, $self->WARNING );
171 17 push( @problems, 'not full duplex' );
172 }
173 139 ▶ if ( defined( $self->opt('mtu') ) && ( $info->{mtu} // 0 ) != $self->opt('mtu') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->opt("mtu") and ($info->{"mtu"} // 0) != $self->opt("mtu")) 13 126 139
Condition80%4/5
defined $self->opt("mtu") and ($info->{"mtu"} // 0) != $self->opt("mtu")
!ll&&!rl&&r
1131313
$info->{"mtu"} // 0
l!l
260
MC/DC50%1/2
defined $self->opt("mtu") and ($info->{"mtu"} // 0) != $self->opt("mtu")
defined $self->opt("mtu") $info->{"mtu"}
Truth table66%2/3
A and B
A= defined $self->opt("mtu")B= $info->{"mtu"}
ABresult
0X0
100
111
174 13 push( @codes, $self->WARNING );
175 13 ▶ push( @problems, 'MTU ' . ( $info->{mtu} // 'unknown' ) . ', not ' . $self->opt('mtu') );
Condition50%1/2
$info->{"mtu"} // "unknown"
l!l
130
MC/DC0%0/1
$info->{"mtu"} // "unknown"
$info->{"mtu"}
Truth table50%1/2
A
A= $info->{"mtu"}
Aresult
11
00
176 }
177  
178 # the rates, and the state they need, only when a range for them was given
179 139 ▶ my $rates = $self->rates_wanted ? $self->rates( $name, $info ) : {};
Branch: 1 branch
BranchTrueFalseTotal
$self->rates_wanted ? : 81 58 139
180 139 foreach my $kind ( 'errors', 'drops' ) {
181 278 ▶ next if !defined( $rates->{$kind} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rates->{$kind} 201 77 278
182 77 my $warning = $self->opt( $kind . '-warning' );
183 77 my $critical = $self->opt( $kind . '-critical' );
184 $self->perfdata(
185 label => $name . ' ' . $kind,
186 77 value => $rates->{$kind},
187 warning => $warning,
188 critical => $critical,
189 min => 0
190 );
191 77 my $code = $self->threshold_status( $rates->{$kind}, $warning, $critical );
192 77 ▶ next if $code == $self->OK;
Branch: 1 branch
BranchTrueFalseTotal
if $code == $self->OK 64 13 77
193 13 push( @codes, $code );
194 13 push( @problems, $rates->{$kind} . ' ' . $kind . '/s' );
195 } ## end foreach my $kind ( 'errors', 'drops' )
196  
197 139 my $part = $name . ' ' . join( ' ', @is );
198 139 ▶ $part = $part . ', ' . join( ', ', @problems ) if defined( $problems[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $problems[0] 30 109 139
199  
200 139 return ( $self->worst(@codes), $part );
201 } ## end sub interface_status
202  
203 =head2 rates_wanted
204  
205 Returns 1 if any of --errors-warning, --errors-critical,
206 --drops-warning, or --drops-critical was given, so the rates are to
207 be kept and checked, else 0.
208  
209 =cut
210  
211 139 ▶sub rates_wanted {
Subroutine rates_wanted: called CC=2 SCAR=6.9
Pod documented
212 139 my ($self) = @_;
213  
214 139 ▶ return ( grep { defined( $self->opt($_) ) }
Branch: 1 branch
BranchTrueFalseTotal
grep({defined $self->opt($_);} 'errors-warning', 'errors-critical', 'drops-warning', 'drops-critical') ? : 81 58 139
215 ( 'errors-warning', 'errors-critical', 'drops-warning', 'drops-critical' ) )
216 ? 1
217 : 0;
218 }
219  
220 =head2 rates
221  
222 Works out the errors and drops per second since the last run, and
223 saves the counters for the next.
224  
225 - name :: The interface.
226  
227 - info :: As for L</interface_status>.
228  
229 Returns a hash ref of errors and drops, each undef if there is no rate
230 yet. Dies if the state can not be kept.
231  
232 =cut
233  
234 81 ▶sub rates {
Subroutine rates: called CC=9 SCAR=22.4
Pod documented
235 81 my ( $self, $name, $info ) = @_;
236  
237 81 my $file
238 = $Monitoring::Sneck::Checks::base::STATE_DIR . '/interface-' . ( $name =~ s/[^A-Za-z0-9_.-]/_/gr ) . '.json';
239 81 ▶ my $old = $self->read_state($file) // {};
Condition50%1/2
$self->read_state($file) // {}
l!l
810
MC/DC0%0/1
$self->read_state($file) // {}
$self->read_state($file)
Truth table50%1/2
A
A= $self->read_state($file)
Aresult
11
00
240 81 my $now = Time::HiRes::time;
241 81 my %rates;
242 81 foreach my $kind ( 'errors', 'drops' ) {
243 162 my ( $was, $is ) = ( $old->{$kind}, $info->{$kind} );
244 162 ▶ next if !defined($was) || !defined($is) || $is < $was || !defined( $old->{time} ) || $now <= $old->{time};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $was && defined $is or $is < $was or not defined $old->{"time"} or $now <= $old->{"time"} 85 77 162
Condition75%9/12
not defined $was && defined $is or $is < $was or not defined $old->{"time"} or $now <= $old->{"time"}
l!l&&r!l&&!r
85077
not defined $was && defined $is or $is < $was or not defined $old->{"time"}
l!l&&r!l&&!r
85077
not defined $was && defined $is or $is < $was
l!l&&r!l&&!r
721377
defined $was && defined $is
!ll&&!rl&&r
72090
MC/DC40%2/5
not defined $was && defined $is or $is < $was or not defined $old->{"time"} or $now <= $old->{"time"}
defined $was defined $is $is < $was not defined $old->{"time"} $now <= $old->{"time"}
Truth table50%3/6
not(A && B) or C or not(D) or E
A= defined $wasB= defined $isC= $is < $wasD= not defined $old->{"time"}E= $now <= $old->{"time"}
ABCDEresult
0XXXX1
10XXX1
111XX1
1101X1
110011
110000
245 77 $rates{$kind} = sprintf( '%.2f', ( $is - $was ) / ( $now - $old->{time} ) );
246 }
247 81 my $problem = $self->write_state( $file, { time => $now, errors => $info->{errors}, drops => $info->{drops} } );
248 81 ▶ die( $problem . "\n" ) if defined($problem);
Branch: 1 branch
BranchTrueFalseTotal
if defined $problem 0 81 81
249  
250 81 return \%rates;
251 } ## end sub rates
252  
253 =head2 all
254  
255 Returns the interfaces --all covers, see DESCRIPTION.
256  
257 =cut
258  
259 39 ▶sub all {
Subroutine all: called CC=5 SCAR=16.2
Pod documented
260 39 my ($self) = @_;
261  
262 39 ▶ if ( $self->os eq 'linux' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "linux") 26 13 39
263 26 ▶ opendir( my $net, $NET ) or return ();
Branch: 1 branch
BranchTrueFalseTotal
opendir my $net, $Monitoring::Sneck::Checks::interface::NET or return () 26 0 26
264 26 ▶ return grep { !/\A\./ && -e $NET . '/' . $_ . '/device' } readdir($net);
Condition100%3/3
!/\A\./u && -e $Monitoring::Sneck::Checks::interface::NET . '/' . $_ . '/device'
!ll&&!rl&&r
522652
MC/DC100%2/2
!/\A\./u && -e $Monitoring::Sneck::Checks::interface::NET . '/' . $_ . '/device'
!/\A\./u -e $Monitoring::Sneck::Checks::interface::NET . '/' . $_ . '/device'
Truth table100%3/3
not(A) && B
A= !/\A\./uB= -e $Monitoring::Sneck::Checks::interface::NET . '/' . $_ . '/device'
ABresult
0X0
100
111
265 }
266 13 my ( $exit, $output ) = $self->run_command( $IFCONFIG, '-l', 'ether' );
267 12 ▶ die( 'ifconfig -l ether exited ' . $exit . "\n" ) if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 0 12 12
268  
269 12 return split( /\s+/, $output =~ s/\A\s+|\s+\z//gr );
270 } ## end sub all
271  
272 =head2 linux_info
273  
274 Returns what is known about a interface from /sys/class/net, as taken
275 by L</interface_status>, or undef if it is not there.
276  
277 =cut
278  
279 143 ▶sub linux_info {
Subroutine linux_info: called CC=13 SCAR=25.8
Pod documented
280 143 my ( $self, $name ) = @_;
281  
282 143 my $dir = $NET . '/' . $name;
283 143 ▶ return undef if $name =~ m{/|\A\.} || !-d $dir;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ m[/|\A\.]u or not -d $dir 13 130 143
Condition66%2/3
$name =~ m[/|\A\.]u or not -d $dir
l!l&&r!l&&!r
013130
MC/DC50%1/2
$name =~ m[/|\A\.]u or not -d $dir
$name =~ m[/|\A\.]u not -d $dir
Truth table66%2/3
A or not(B)
A= $name =~ m[/|\A\.]uB= not -d $dir
ABresult
1X1
011
000
284 1066 ▶ my $read = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
285 1066 my $value = read_file( $dir . '/' . $_[0], err_mode => 'quiet' );
286 1066 ▶ return undef if !defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 52 1014 1066
287 1014 $value =~ s/\s+\z//;
288 1014 return $value;
289 130 };
290 260 ▶ my $count = sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
291 260 my $total = 0;
292 260 foreach my $counter (@_) {
293 520 ▶ my $value = $read->( 'statistics/' . $counter ) // return undef;
Condition50%1/2
&$read('statistics/' . $counter) // (return undef)
l!l
5200
MC/DC0%0/1
&$read('statistics/' . $counter) // (return undef)
&$read('statistics/' . $counter)
Truth table50%1/2
A
A= &$read('statistics/' . $counter)
Aresult
11
00
294 520 $total += $value;
295 }
296 260 return $total;
297 130 };
298  
299 # up, or unknown with a carrier, as for tunnels and the like that do not say
300 130 ▶ my $state = $read->('operstate') // 'unknown';
Condition50%1/2
&$read("operstate") // "unknown"
l!l
1300
MC/DC0%0/1
&$read("operstate") // "unknown"
&$read("operstate")
Truth table50%1/2
A
A= &$read("operstate")
Aresult
11
00
301 130 my $speed = $read->('speed');
302 130 my $duplex = $read->('duplex');
303  
304 return {
305 130 ▶ up => ( $state eq 'up' || ( $state eq 'unknown' && ( $read->('carrier') // '' ) eq '1' ) ) ? 1 : 0,
Branch: 3 branches
BranchTrueFalseTotal
$state eq 'up' || $state eq 'unknown' && (&$read('carrier') // '') eq '1' ? : 117 13 130
defined $speed && $speed =~ /\A[0-9]+\z/u && $speed > 0 ? : 91 39 130
defined $duplex && $duplex =~ /\A(?:full|half)\z/u ? : 91 39 130
Condition82%14/17
$state eq "up" or $state eq "unknown" and (&$read("carrier") // "") eq 1
l!l&&r!l&&!r
912613
$state eq "unknown" and (&$read("carrier") // "") eq 1
!ll&&!rl&&r
13026
&$read("carrier") // ""
l!l
260
defined $speed and $speed =~ /\A[0-9]+\z/u and $speed > 0
!ll&&!rl&&r
39091
defined $speed and $speed =~ /\A[0-9]+\z/u
!ll&&!rl&&r
261391
defined $duplex and $duplex =~ /\A(?:full|half)\z/u
!ll&&!rl&&r
261391
MC/DC66%2/3
$state eq "up" or $state eq "unknown" and (&$read("carrier") // "") eq 1
$state eq "up" $state eq "unknown" &$read("carrier")
MC/DC66%2/3
defined $speed and $speed =~ /\A[0-9]+\z/u and $speed > 0
defined $speed $speed =~ /\A[0-9]+\z/u $speed > 0
MC/DC100%2/2
defined $duplex and $duplex =~ /\A(?:full|half)\z/u
defined $duplex $duplex =~ /\A(?:full|half)\z/u
Truth table75%3/4
A or B and C
A= $state eq "up"B= $state eq "unknown"C= &$read("carrier")
ABCresult
1XX1
0111
00X0
0100
Truth table75%3/4
A and B and C
A= defined $speedB= $speed =~ /\A[0-9]+\z/uC= $speed > 0
ABCresult
0XX0
10X0
1100
1111
Truth table100%3/3
A and B
A= defined $duplexB= $duplex =~ /\A(?:full|half)\z/u
ABresult
0X0
100
111
306 speed => defined($speed) && $speed =~ /\A[0-9]+\z/ && $speed > 0 ? $speed : undef,
307 duplex => defined($duplex) && $duplex =~ /\A(?:full|half)\z/ ? $duplex : undef,
308 mtu => $read->('mtu'),
309 errors => $count->( 'rx_errors', 'tx_errors' ),
310 drops => $count->( 'rx_dropped', 'tx_dropped' ),
311 };
312 } ## end sub linux_info
313  
314 =head2 freebsd_info
315  
316 Returns what is known about a interface from ifconfig and netstat -I,
317 as taken by L</interface_status>, or undef if it is not there.
318  
319 =cut
320  
321 42 ▶sub freebsd_info {
Subroutine freebsd_info: called CC=20 SCAR=32.2
Pod documented
322 42 my ( $self, $name ) = @_;
323  
324 42 my ( $exit, $output ) = $self->run_command( $IFCONFIG, $name );
325 36 ▶ return undef if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 1 35 36
326  
327 35 my ($flags) = $output =~ /flags=[0-9a-f]+<([^>]*)>/;
328 35 my ($status) = $output =~ /^\s*status: (.+?)\s*$/m;
329  
330 # what was negotiated is in parens, as "media: Ethernet autoselect (1000baseT <full-duplex>)", while media
331 # set by hand has none, as "media: Ethernet 1000baseT <full-duplex>", and a lagg or a link that is down
332 # is just "media: Ethernet autoselect", which says no speed
333 35 my ($media) = $output =~ /^\s*media: (.+?)\s*$/m;
334 35 ▶ $media = $1 if defined($media) && $media =~ /\((.+)\)/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $media and $media =~ /\((.+)\)/u 27 8 35
Condition66%2/3
defined $media and $media =~ /\((.+)\)/u
!ll&&!rl&&r
0827
MC/DC50%1/2
defined $media and $media =~ /\((.+)\)/u
defined $media $media =~ /\((.+)\)/u
Truth table66%2/3
A and B
A= defined $mediaB= $media =~ /\((.+)\)/u
ABresult
0X0
100
111
335 35 my ( $speed, $duplex );
336 35 ▶ if ( defined($media) && $media =~ /([0-9]+)(G?)base/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $media and $media =~ /([0-9]+)(G?)base/iu) 32 3 35
Condition66%2/3
defined $media and $media =~ /([0-9]+)(G?)base/iu
!ll&&!rl&&r
0332
MC/DC50%1/2
defined $media and $media =~ /([0-9]+)(G?)base/iu
defined $media $media =~ /([0-9]+)(G?)base/iu
Truth table66%2/3
A and B
A= defined $mediaB= $media =~ /([0-9]+)(G?)base/iu
ABresult
0X0
100
111
337 32 ▶ $speed = $1 * ( $2 ? 1000 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$2 ? : 9 23 32
338 }
339 35 ▶ $duplex = $1 if defined($media) && $media =~ /<(full|half)-duplex/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $media and $media =~ /<(full|half)-duplex/u 32 3 35
Condition66%2/3
defined $media and $media =~ /<(full|half)-duplex/u
!ll&&!rl&&r
0332
MC/DC50%1/2
defined $media and $media =~ /<(full|half)-duplex/u
defined $media $media =~ /<(full|half)-duplex/u
Truth table66%2/3
A and B
A= defined $mediaB= $media =~ /<(full|half)-duplex/u
ABresult
0X0
100
111
340 35 my ($mtu) = $output =~ /\bmtu ([0-9]+)/;
341  
342 # the <Link#> row has the counters, its columns going by the header
343 35 my ( $errors, $drops );
344 35 my ( $netstat_exit, $netstat ) = $self->run_command( $NETSTAT, '-I', $name, '-n', '-d' );
345 22 ▶ if ( !$netstat_exit ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($netstat_exit) 22 0 22
346 22 my ( $header, @rows ) = split( /\n/, $netstat );
347 22 ▶ my @columns = split( /\s+/, $header // '' );
Condition50%1/2
$header // ""
l!l
220
MC/DC0%0/1
$header // ""
$header
Truth table50%1/2
A
A= $header
Aresult
11
00
348 my ($link)
349 22 = grep { ( split(/\s+/) )[2] =~ /\A<Link/ } grep { scalar( split(/\s+/) ) == scalar(@columns) } @rows;
350 22 ▶ if ( defined($link) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $link) 22 0 22
351 22 my %row;
352 22 @row{@columns} = split( /\s+/, $link );
353 22 ▶ $errors = ( $row{Ierrs} // 0 ) + ( $row{Oerrs} // 0 );
Condition50%2/4
$row{"Ierrs"} // 0
l!l
220
$row{"Oerrs"} // 0
l!l
220
MC/DC0%0/1
$row{"Ierrs"} // 0
$row{"Ierrs"}
MC/DC0%0/1
$row{"Oerrs"} // 0
$row{"Oerrs"}
Truth table50%1/2
A
A= $row{"Ierrs"}
Aresult
11
00
Truth table50%1/2
A
A= $row{"Oerrs"}
Aresult
11
00
354 22 ▶ $drops = ( $row{Idrop} // 0 ) + ( $row{Drop} // 0 );
Condition50%2/4
$row{"Idrop"} // 0
l!l
220
$row{"Drop"} // 0
l!l
220
MC/DC0%0/1
$row{"Idrop"} // 0
$row{"Idrop"}
MC/DC0%0/1
$row{"Drop"} // 0
$row{"Drop"}
Truth table50%1/2
A
A= $row{"Idrop"}
Aresult
11
00
Truth table50%1/2
A
A= $row{"Drop"}
Aresult
11
00
355 }
356 } ## end if ( !$netstat_exit )
357  
358 return {
359 22 ▶ up => ( ( ',' . ( $flags // '' ) . ',' ) =~ /,UP,/ && ( !defined($status) || $status eq 'active' ) )
Branch: 1 branch
BranchTrueFalseTotal
(',' . ($flags // '') . ',') =~ /,UP,/u && (!defined($status) || $status eq 'active') ? : 22 0 22
Condition37%3/8
(',' . ($flags // "") . ',') =~ /,UP,/u and !defined($status) || $status eq "active"
!ll&&!rl&&r
0022
$flags // ""
l!l
220
not defined $status or $status eq "active"
l!l&&r!l&&!r
0220
MC/DC0%0/3
(',' . ($flags // "") . ',') =~ /,UP,/u and !defined($status) || $status eq "active"
(',' . ($flags // "") . ',') =~ /,UP,/u not defined $status $status eq "active"
MC/DC0%0/1
$flags // ""
$flags
Truth table25%1/4
A and not(B) or C
A= (',' . ($flags // "") . ',') =~ /,UP,/uB= not defined $statusC= $status eq "active"
ABCresult
0XX0
1000
11X1
1011
Truth table50%1/2
A
A= $flags
Aresult
11
00
360 ? 1
361 : 0,
362 speed => $speed,
363 duplex => $duplex,
364 mtu => $mtu,
365 errors => $errors,
366 drops => $drops,
367 };
368 } ## end sub freebsd_info
369  
370 16 1;
« blib/lib/Monitoring/Sneck/Checks/kernel_modules.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/tls_protocols.pm »