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/time_sync.pm »
1 package Monitoring::Sneck::Checks::interface;
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 File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶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 15 our $VERSION = '0.0.1';
21  
22 # Where things are. Set by tests.
23 15 our $NET = '/sys/class/net';
24 15 our $IFCONFIG = '/sbin/ifconfig';
25 15 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 164 ▶sub option_spec {
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
82 return (
83 164 '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 164 ▶sub ranges { return ( 'errors-warning', 'errors-critical', 'drops-warning', 'drops-critical' ) }
Subroutine ranges: called CC=1 SCAR=0.0
Pod documented
90  
91 164 ▶sub validate {
Subroutine validate: called CC=6 SCAR=17.9
Pod documented
92 164 my ( $self, $options ) = @_;
93  
94 164 my @problems;
95 164 ▶ foreach my $interface ( @{ $options->{interface} // [] } ) {
Condition100%2/2
$options->{"interface"} // []
l!l
11648
MC/DC100%1/1
$options->{"interface"} // []
$options->{"interface"}
Truth table100%2/2
A
A= $options->{"interface"}
Aresult
11
00
96 116 ▶ push( @problems, '--interface "' . $interface . '" is not a interface name' )
Branch: 1 branch
BranchTrueFalseTotal
unless $interface =~ /\A[A-Za-z0-9_.:-]+\z/u 0 116 116
97 if $interface !~ /\A[A-Za-z0-9_.:-]+\z/;
98 }
99 push( @problems, 'nothing to check, give --interface or --all' )
100 164 ▶ if !defined( $options->{interface} ) && !$options->{all};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"interface"} or $options->{"all"} 12 152 164
Condition100%3/3
defined $options->{"interface"} or $options->{"all"}
l!l&&r!l&&!r
1163612
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 164 return @problems;
103 } ## end sub validate
104  
105 =head1 METHODS
106  
107 =head2 check
108  
109 Does the check.
110  
111 =cut
112  
113 152 ▶sub check {
Subroutine check: called CC=10 SCAR=23.1
Pod documented
114 152 my ($self) = @_;
115  
116 152 ▶ 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 152 152
Condition66%2/3
$self->os ne "linux" and $self->os ne "freebsd"
!ll&&!rl&&r
120320
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 152 ▶ my %names = map { $_ => 1 } @{ $self->opt('interface') // [] }, ( $self->opt('all') ? $self->all : () );
Branch: 1 branch
BranchTrueFalseTotal
$self->opt('all') ? : 36 116 152
Condition100%2/2
$self->opt("interface") // []
l!l
11636
MC/DC100%1/1
$self->opt("interface") // []
$self->opt("interface")
Truth table100%2/2
A
A= $self->opt("interface")
Aresult
11
00
118 151 ▶ delete( @names{ @{ $self->opt('ignore') // [] } } );
Condition100%2/2
$self->opt("ignore") // []
l!l
12139
MC/DC100%1/1
$self->opt("ignore") // []
$self->opt("ignore")
Truth table100%2/2
A
A= $self->opt("ignore")
Aresult
11
00
119 151 my @names = sort( keys(%names) );
120 151 ▶ return $self->finish( $self->UNKNOWN, 'no interfaces found' ) if !defined( $names[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $names[0] 0 151 151
121  
122 151 my @codes;
123 my @parts;
124 151 foreach my $name (@names) {
125 172 ▶ my $info = $self->os eq 'linux' ? $self->linux_info($name) : $self->freebsd_info($name);
Branch: 1 branch
BranchTrueFalseTotal
$self->os eq 'linux' ? : 132 40 172
126 154 ▶ if ( !defined($info) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $info) 13 141 154
127 13 push( @codes, $self->CRITICAL );
128 13 push( @parts, $name . ' is not there' );
129 13 next;
130 }
131 141 my ( $code, $part ) = $self->interface_status( $name, $info );
132 141 push( @codes, $code );
133 141 push( @parts, $part );
134 } ## end foreach my $name (@names)
135  
136 133 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 141 ▶sub interface_status {
Subroutine interface_status: called CC=19 SCAR=29.5
Pod documented
154 141 my ( $self, $name, $info ) = @_;
155  
156 141 ▶ if ( !$info->{up} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($info->{"up"}) 12 129 141
157 12 return ( $self->CRITICAL, $name . ' is down' );
158 }
159  
160 129 my @codes = ( $self->OK );
161 129 my @problems;
162 129 my @is = ('up');
163 129 ▶ push( @is, $info->{speed} . ' Mb/s' ) if defined( $info->{speed} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"speed"} 103 26 129
164 129 ▶ push( @is, $info->{duplex} ) if defined( $info->{duplex} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"duplex"} 103 26 129
165 129 ▶ 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")) 16 113 129
Condition80%4/5
defined $self->opt("speed") and ($info->{"speed"} // 0) < $self->opt("speed")
!ll&&!rl&&r
922116
$info->{"speed"} // 0
l!l
370
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 16 push( @codes, $self->WARNING );
167 16 push( @problems, 'under ' . $self->opt('speed') . ' Mb/s' );
168 }
169 129 ▶ if ( $self->opt('full-duplex') && ( $info->{duplex} // '' ) ne 'full' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opt("full-duplex") and ($info->{"duplex"} // "") ne "full") 16 113 129
Condition80%4/5
$self->opt("full-duplex") and ($info->{"duplex"} // "") ne "full"
!ll&&!rl&&r
922116
$info->{"duplex"} // ""
l!l
370
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 16 push( @codes, $self->WARNING );
171 16 push( @problems, 'not full duplex' );
172 }
173 129 ▶ 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")) 12 117 129
Condition80%4/5
defined $self->opt("mtu") and ($info->{"mtu"} // 0) != $self->opt("mtu")
!ll&&!rl&&r
1051212
$info->{"mtu"} // 0
l!l
240
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 12 push( @codes, $self->WARNING );
175 12 ▶ push( @problems, 'MTU ' . ( $info->{mtu} // 'unknown' ) . ', not ' . $self->opt('mtu') );
Condition50%1/2
$info->{"mtu"} // "unknown"
l!l
120
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 129 ▶ my $rates = $self->rates_wanted ? $self->rates( $name, $info ) : {};
Branch: 1 branch
BranchTrueFalseTotal
$self->rates_wanted ? : 75 54 129
180 129 foreach my $kind ( 'errors', 'drops' ) {
181 258 ▶ next if !defined( $rates->{$kind} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rates->{$kind} 186 72 258
182 72 my $warning = $self->opt( $kind . '-warning' );
183 72 my $critical = $self->opt( $kind . '-critical' );
184 $self->perfdata(
185 label => $name . ' ' . $kind,
186 72 value => $rates->{$kind},
187 warning => $warning,
188 critical => $critical,
189 min => 0
190 );
191 72 my $code = $self->threshold_status( $rates->{$kind}, $warning, $critical );
192 72 ▶ next if $code == $self->OK;
Branch: 1 branch
BranchTrueFalseTotal
if $code == $self->OK 60 12 72
193 12 push( @codes, $code );
194 12 push( @problems, $rates->{$kind} . ' ' . $kind . '/s' );
195 } ## end foreach my $kind ( 'errors', 'drops' )
196  
197 129 my $part = $name . ' ' . join( ' ', @is );
198 129 ▶ $part = $part . ', ' . join( ', ', @problems ) if defined( $problems[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $problems[0] 28 101 129
199  
200 129 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 129 ▶sub rates_wanted {
Subroutine rates_wanted: called CC=2 SCAR=6.9
Pod documented
212 129 my ($self) = @_;
213  
214 129 ▶ return ( grep { defined( $self->opt($_) ) }
Branch: 1 branch
BranchTrueFalseTotal
grep({defined $self->opt($_);} 'errors-warning', 'errors-critical', 'drops-warning', 'drops-critical') ? : 75 54 129
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 75 ▶sub rates {
Subroutine rates: called CC=9 SCAR=22.4
Pod documented
235 75 my ( $self, $name, $info ) = @_;
236  
237 75 my $file
238 = $Monitoring::Sneck::Checks::base::STATE_DIR . '/interface-' . ( $name =~ s/[^A-Za-z0-9_.-]/_/gr ) . '.json';
239 75 ▶ my $old = $self->read_state($file) // {};
Condition50%1/2
$self->read_state($file) // {}
l!l
750
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 75 my $now = Time::HiRes::time;
241 75 my %rates;
242 75 foreach my $kind ( 'errors', 'drops' ) {
243 150 my ( $was, $is ) = ( $old->{$kind}, $info->{$kind} );
244 150 ▶ 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"} 78 72 150
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
78072
not defined $was && defined $is or $is < $was or not defined $old->{"time"}
l!l&&r!l&&!r
78072
not defined $was && defined $is or $is < $was
l!l&&r!l&&!r
661272
defined $was && defined $is
!ll&&!rl&&r
66084
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 72 $rates{$kind} = sprintf( '%.2f', ( $is - $was ) / ( $now - $old->{time} ) );
246 }
247 75 my $problem = $self->write_state( $file, { time => $now, errors => $info->{errors}, drops => $info->{drops} } );
248 75 ▶ die( $problem . "\n" ) if defined($problem);
Branch: 1 branch
BranchTrueFalseTotal
if defined $problem 0 75 75
249  
250 75 return \%rates;
251 } ## end sub rates
252  
253 =head2 all
254  
255 Returns the interfaces --all covers, see DESCRIPTION.
256  
257 =cut
258  
259 36 ▶sub all {
Subroutine all: called CC=5 SCAR=16.2
Pod documented
260 36 my ($self) = @_;
261  
262 36 ▶ if ( $self->os eq 'linux' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "linux") 24 12 36
263 24 ▶ opendir( my $net, $NET ) or return ();
Branch: 1 branch
BranchTrueFalseTotal
opendir my $net, $Monitoring::Sneck::Checks::interface::NET or return () 24 0 24
264 24 ▶ return grep { !/\A\./ && -e $NET . '/' . $_ . '/device' } readdir($net);
Condition100%3/3
!/\A\./u && -e $Monitoring::Sneck::Checks::interface::NET . '/' . $_ . '/device'
!ll&&!rl&&r
482448
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 12 my ( $exit, $output ) = $self->run_command( $IFCONFIG, '-l', 'ether' );
267 11 ▶ die( 'ifconfig -l ether exited ' . $exit . "\n" ) if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 0 11 11
268  
269 11 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 132 ▶sub linux_info {
Subroutine linux_info: called CC=13 SCAR=25.8
Pod documented
280 132 my ( $self, $name ) = @_;
281  
282 132 my $dir = $NET . '/' . $name;
283 132 ▶ return undef if $name =~ m{/|\A\.} || !-d $dir;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ m[/|\A\.]u or not -d $dir 12 120 132
Condition66%2/3
$name =~ m[/|\A\.]u or not -d $dir
l!l&&r!l&&!r
012120
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 984 ▶ my $read = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
285 984 my $value = read_file( $dir . '/' . $_[0], err_mode => 'quiet' );
286 984 ▶ return undef if !defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 48 936 984
287 936 $value =~ s/\s+\z//;
288 936 return $value;
289 120 };
290 240 ▶ my $count = sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
291 240 my $total = 0;
292 240 foreach my $counter (@_) {
293 480 ▶ my $value = $read->( 'statistics/' . $counter ) // return undef;
Condition50%1/2
&$read('statistics/' . $counter) // (return undef)
l!l
4800
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 480 $total += $value;
295 }
296 240 return $total;
297 120 };
298  
299 # up, or unknown with a carrier, as for tunnels and the like that do not say
300 120 ▶ my $state = $read->('operstate') // 'unknown';
Condition50%1/2
&$read("operstate") // "unknown"
l!l
1200
MC/DC0%0/1
&$read("operstate") // "unknown"
&$read("operstate")
Truth table50%1/2
A
A= &$read("operstate")
Aresult
11
00
301 120 my $speed = $read->('speed');
302 120 my $duplex = $read->('duplex');
303  
304 return {
305 120 ▶ 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' ? : 108 12 120
defined $speed && $speed =~ /\A[0-9]+\z/u && $speed > 0 ? : 84 36 120
defined $duplex && $duplex =~ /\A(?:full|half)\z/u ? : 84 36 120
Condition82%14/17
$state eq "up" or $state eq "unknown" and (&$read("carrier") // "") eq 1
l!l&&r!l&&!r
842412
$state eq "unknown" and (&$read("carrier") // "") eq 1
!ll&&!rl&&r
12024
&$read("carrier") // ""
l!l
240
defined $speed and $speed =~ /\A[0-9]+\z/u and $speed > 0
!ll&&!rl&&r
36084
defined $speed and $speed =~ /\A[0-9]+\z/u
!ll&&!rl&&r
241284
defined $duplex and $duplex =~ /\A(?:full|half)\z/u
!ll&&!rl&&r
241284
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 40 ▶sub freebsd_info {
Subroutine freebsd_info: called CC=20 SCAR=32.2
Pod documented
322 40 my ( $self, $name ) = @_;
323  
324 40 my ( $exit, $output ) = $self->run_command( $IFCONFIG, $name );
325 34 ▶ return undef if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 1 33 34
326  
327 33 my ($flags) = $output =~ /flags=[0-9a-f]+<([^>]*)>/;
328 33 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 33 my ($media) = $output =~ /^\s*media: (.+?)\s*$/m;
334 33 ▶ $media = $1 if defined($media) && $media =~ /\((.+)\)/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $media and $media =~ /\((.+)\)/u 25 8 33
Condition66%2/3
defined $media and $media =~ /\((.+)\)/u
!ll&&!rl&&r
0825
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 33 my ( $speed, $duplex );
336 33 ▶ if ( defined($media) && $media =~ /([0-9]+)(G?)base/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $media and $media =~ /([0-9]+)(G?)base/iu) 30 3 33
Condition66%2/3
defined $media and $media =~ /([0-9]+)(G?)base/iu
!ll&&!rl&&r
0330
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 30 ▶ $speed = $1 * ( $2 ? 1000 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$2 ? : 8 22 30
338 }
339 33 ▶ $duplex = $1 if defined($media) && $media =~ /<(full|half)-duplex/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $media and $media =~ /<(full|half)-duplex/u 30 3 33
Condition66%2/3
defined $media and $media =~ /<(full|half)-duplex/u
!ll&&!rl&&r
0330
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 33 my ($mtu) = $output =~ /\bmtu ([0-9]+)/;
341  
342 # the <Link#> row has the counters, its columns going by the header
343 33 my ( $errors, $drops );
344 33 my ( $netstat_exit, $netstat ) = $self->run_command( $NETSTAT, '-I', $name, '-n', '-d' );
345 21 ▶ if ( !$netstat_exit ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($netstat_exit) 21 0 21
346 21 my ( $header, @rows ) = split( /\n/, $netstat );
347 21 ▶ my @columns = split( /\s+/, $header // '' );
Condition50%1/2
$header // ""
l!l
210
MC/DC0%0/1
$header // ""
$header
Truth table50%1/2
A
A= $header
Aresult
11
00
348 my ($link)
349 21 = grep { ( split(/\s+/) )[2] =~ /\A<Link/ } grep { scalar( split(/\s+/) ) == scalar(@columns) } @rows;
350 21 ▶ if ( defined($link) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $link) 21 0 21
351 21 my %row;
352 21 @row{@columns} = split( /\s+/, $link );
353 21 ▶ $errors = ( $row{Ierrs} // 0 ) + ( $row{Oerrs} // 0 );
Condition50%2/4
$row{"Ierrs"} // 0
l!l
210
$row{"Oerrs"} // 0
l!l
210
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 21 ▶ $drops = ( $row{Idrop} // 0 ) + ( $row{Drop} // 0 );
Condition50%2/4
$row{"Idrop"} // 0
l!l
210
$row{"Drop"} // 0
l!l
210
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 21 ▶ up => ( ( ',' . ( $flags // '' ) . ',' ) =~ /,UP,/ && ( !defined($status) || $status eq 'active' ) )
Branch: 1 branch
BranchTrueFalseTotal
(',' . ($flags // '') . ',') =~ /,UP,/u && (!defined($status) || $status eq 'active') ? : 21 0 21
Condition37%3/8
(',' . ($flags // "") . ',') =~ /,UP,/u and !defined($status) || $status eq "active"
!ll&&!rl&&r
0021
$flags // ""
l!l
210
not defined $status or $status eq "active"
l!l&&r!l&&!r
0210
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 15 1;
« blib/lib/Monitoring/Sneck/Checks/kernel_modules.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/time_sync.pm »