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

filter: Statementstmt 100% 99 / 99 Branchbran 95.6% 44 / 46 Conditioncond 75.0% 30 / 40 MC/DCmcdc 55.0% 11 / 20 Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 90.3% 196 / 217 CC 46 SCAR 26.9
CC
46
Cov
94%
check
17.0
freebsd_bonds
16.4
linux_bonds
15.2
CRAP 14.8
« blib/lib/Monitoring/Sneck/Checks/ldap.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/http.pm »
1 package Monitoring::Sneck::Checks::bond;
2  
3 9 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Monitoring::Sneck::Checks::bond - checks link aggregation, bonding on Linux and lagg on FreeBSD
11  
12 =head1 VERSION
13  
14 Version 0.0.1
15  
16 =cut
17  
18 9 our $VERSION = '0.0.1';
19  
20 # Where ifconfig is on FreeBSD. Set by tests.
21 9 our $IFCONFIG = '/sbin/ifconfig';
22  
23 =head1 SYNOPSIS
24  
25 bond|perl:bond
26 bond_uplink|perl:bond -b bond0 -m 2
27 bond_lagg|perl:bond -b lagg0 -b lagg1
28  
29 =head1 DESCRIPTION
30  
31 Checks every bond and its members. A bond that lost a member keeps
32 passing traffic, and stays up as far as
33 L<Monitoring::Sneck::Checks::interface> can tell, so it tends to go
34 unseen till the last member goes too.
35  
36 - CRITICAL :: A bond that is down, has no members up, or has fewer up
37 than --members. A bond given with --bond that is not there.
38  
39 - WARNING :: A member that is not up, or with LACP, one that is up but
40 not aggregating, as happens when the switch port is not set up for it.
41 With LACP on Linux, there being no LACP partner at all.
42  
43 - OK :: Everything else, including there being no bonds, unless --bond
44 says one should be there.
45  
46 On Linux, bonds are read from /proc/net/bonding. A member is up if its
47 MII status is, and with 802.3ad, if it is in the active aggregator,
48 which the kernel only shows to root. On FreeBSD, laggs are read from
49 ifconfig -a. A member is up if its link is active, and with LACP, if it
50 is active, collecting, and distributing. Other OSes are UNKNOWN.
51  
52 =head1 OPTIONS
53  
54 - -b, --bond :: A bond that must be there, such as bond0 or lagg0. May
55 be given more than once.
56  
57 - -m, --members :: How many members each bond must have up.
58  
59 =cut
60  
61 63 ▶sub option_spec { return ( 'bond|b=s@', 'members|m=i' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
62  
63 =head1 METHODS
64  
65 =head2 check
66  
67 Does the check.
68  
69 =cut
70  
71 63 ▶sub check {
Subroutine check: called CC=17 SCAR=28.3
Pod documented
72 63 my ($self) = @_;
73  
74 my @bonds
75 63 ▶ = $self->os eq 'linux' ? $self->linux_bonds
Branch: 2 branches
BranchTrueFalseTotal
$self->os eq 'linux' ? : 35 28 63
$self->os eq 'freebsd' ? : 27 1 28
76 : $self->os eq 'freebsd' ? $self->freebsd_bonds
77 : die( 'not supported on ' . $self->os . "\n" );
78 55 my %have = map { $_->{name} => 1 } @bonds;
79 55 my @codes;
80 my @parts;
81 55 ▶ foreach my $name ( @{ $self->opt('bond') // [] } ) {
Condition100%2/2
$self->opt("bond") // []
l!l
2728
MC/DC100%1/1
$self->opt("bond") // []
$self->opt("bond")
Truth table100%2/2
A
A= $self->opt("bond")
Aresult
11
00
82 27 ▶ next if $have{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $have{$name} 13 14 27
83 14 push( @codes, $self->CRITICAL );
84 14 push( @parts, $name . ' is not there' );
85 }
86  
87 55 ▶ my $members_wanted = $self->opt('members') // 0;
Condition100%2/2
$self->opt("members") // 0
l!l
1837
MC/DC100%1/1
$self->opt("members") // 0
$self->opt("members")
Truth table100%2/2
A
A= $self->opt("members")
Aresult
11
00
88 55 foreach my $bond (@bonds) {
89 89 my @members = @{ $bond->{members} };
90 89 my @not_up = grep { defined( $_->{problem} ) } @members;
91 89 my $up_count = scalar(@members) - scalar(@not_up);
92 $self->perfdata(
93 89 label => $bond->{name} . ' members',
94 value => $up_count,
95 min => 0,
96 max => scalar(@members)
97 );
98  
99 89 my @says = ( $up_count . ' of ' . scalar(@members) . ' members up' );
100 89 ▶ unshift( @says, 'down' ) if !$bond->{up};
Branch: 1 branch
BranchTrueFalseTotal
unless $bond->{"up"} 10 79 89
101 89 ▶ push( @says, 'wanted ' . $members_wanted ) if $up_count < $members_wanted;
Branch: 1 branch
BranchTrueFalseTotal
if $up_count < $members_wanted 22 67 89
102 89 push( @says, map { $_->{name} . ' ' . $_->{problem} } @not_up );
103 89 ▶ push( @says, $bond->{problem} ) if defined( $bond->{problem} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $bond->{"problem"} 7 82 89
104  
105 89 my $code = $self->OK;
106 89 ▶ $code = $self->WARNING if defined( $not_up[0] ) || defined( $bond->{problem} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $not_up[0] or defined $bond->{"problem"} 49 40 89
Condition100%3/3
defined $not_up[0] or defined $bond->{"problem"}
l!l&&r!l&&!r
42740
MC/DC100%2/2
defined $not_up[0] or defined $bond->{"problem"}
defined $not_up[0] defined $bond->{"problem"}
Truth table100%3/3
A or B
A= defined $not_up[0]B= defined $bond->{"problem"}
ABresult
1X1
011
000
107 89 ▶ $code = $self->CRITICAL if !$bond->{up} || !$up_count || $up_count < $members_wanted;
Branch: 1 branch
BranchTrueFalseTotal
if not $bond->{"up"} && $up_count or $up_count < $members_wanted 32 57 89
Condition83%5/6
not $bond->{"up"} && $up_count or $up_count < $members_wanted
l!l&&r!l&&!r
102257
$bond->{'up'} && $up_count
!ll&&!rl&&r
10079
MC/DC66%2/3
not $bond->{"up"} && $up_count or $up_count < $members_wanted
$bond->{'up'} $up_count $up_count < $members_wanted
Truth table75%3/4
not(A && B) or C
A= $bond->{'up'}B= $up_countC= $up_count < $members_wanted
ABCresult
0XX1
10X1
1111
1100
108 89 push( @codes, $code );
109 89 push( @parts, $bond->{name} . ' ' . $bond->{mode} . ' ' . join( ', ', @says ) );
110 } ## end foreach my $bond (@bonds)
111 55 ▶ return $self->finish( $self->OK, 'no bonds' ) if !defined( $parts[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parts[0] 9 46 55
112  
113 46 return $self->finish( $self->worst(@codes), join( '; ', @parts ) );
114 } ## end sub check
115  
116 =head2 linux_bonds
117  
118 Returns the bonds under /proc/net/bonding, sorted by name, as hash refs
119 of...
120  
121 - name :: The bond, such as bond0.
122  
123 - mode :: What it is doing, such as 802.3ad or active-backup.
124  
125 - up :: 1 if the bond itself is up, else 0.
126  
127 - problem :: What is wrong with the bond as a whole though it is up,
128 such as having no LACP partner, or undef.
129  
130 - members :: Array ref of hash refs of name and problem, why the member
131 is not counted as up, such as down, or undef if it is.
132  
133 Bonding not being loaded is having no bonds.
134  
135 =cut
136  
137 35 ▶sub linux_bonds {
Subroutine linux_bonds: called CC=15 SCAR=27.2
Pod documented
138 35 my ($self) = @_;
139  
140 35 my $bonding_dir = $Monitoring::Sneck::Checks::base::PROC . '/net/bonding';
141 35 ▶ opendir( my $bonding, $bonding_dir ) or return ();
Branch: 1 branch
BranchTrueFalseTotal
opendir my $bonding, $bonding_dir or return () 21 14 35
142 21 my @names = sort( grep { !/\A\./ } readdir($bonding) );
143 21 closedir($bonding);
144  
145 21 my @bonds;
146 21 foreach my $name (@names) {
147 # what is before the first "Slave Interface: " is about the bond, each after it about a member
148 56 my ( $about_bond, @about_members ) = split( /^Slave Interface: /m, $self->proc_file( 'net/bonding/' . $name ) );
149  
150 # "fault-tolerance (active-backup)" and the like have the short name in parens, while
151 # "IEEE 802.3ad Dynamic link aggregation" and the two load balancing ones have none
152 56 my ($mode) = $about_bond =~ /^Bonding Mode: (.+?)\s*$/m;
153 56 ▶ $mode //= 'unknown';
Condition50%1/2
$mode //= "unknown"
l!l
560
MC/DC0%0/1
$mode //= "unknown"
$mode
Truth table50%1/2
A
A= $mode
Aresult
11
00
154 56 ▶ if ( $mode =~ /\(([^)]+)\)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode =~ /\(([^)]+)\)/u) { } 28 28 56
elsif ($mode =~ /802\.3ad/u) { } 28 0 28
155 28 $mode = $1;
156 } elsif ( $mode =~ /802\.3ad/ ) {
157 28 $mode = '802.3ad';
158 }
159 56 my ($bond_status) = $about_bond =~ /^MII Status: (.+?)\s*$/m;
160 56 my ($active_aggregator) = $about_bond =~ /^Active Aggregator Info:\s*\n\s*Aggregator ID: ([0-9]+)/m;
161  
162 56 my @members;
163 56 foreach my $about_member (@about_members) {
164 112 my ($member_name) = $about_member =~ /\A(\S+)/;
165 112 my ($member_status) = $about_member =~ /^MII Status: (.+?)\s*$/m;
166 112 my ($aggregator) = $about_member =~ /^Aggregator ID: ([0-9]+)/m;
167 112 ▶ $member_status //= 'unknown';
Condition50%1/2
$member_status //= "unknown"
l!l
1120
MC/DC0%0/1
$member_status //= "unknown"
$member_status
Truth table50%1/2
A
A= $member_status
Aresult
11
00
168  
169 112 my $problem;
170 112 ▶ if ( $member_status ne 'up' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($member_status ne 'up') { } 21 91 112
elsif (defined $active_aggregator and defined $aggregator and $aggregator != $active_aggregator) { } 7 119 126
171 21 $problem = $member_status;
172 ▶ } elsif ( defined($active_aggregator) && defined($aggregator) && $aggregator != $active_aggregator ) {
Condition83%5/6
defined $active_aggregator and defined $aggregator and $aggregator != $active_aggregator
!ll&&!rl&&r
35497
defined $active_aggregator and defined $aggregator
!ll&&!rl&&r
35056
MC/DC66%2/3
defined $active_aggregator and defined $aggregator and $aggregator != $active_aggregator
defined $active_aggregator defined $aggregator $aggregator != $active_aggregator
Truth table75%3/4
A and B and C
A= defined $active_aggregatorB= defined $aggregatorC= $aggregator != $active_aggregator
ABCresult
0XX0
10X0
1100
1111
173 7 $problem = 'not in the active aggregator';
174 }
175 112 push( @members, { name => $member_name, problem => $problem } );
176 } ## end foreach my $about_member (@about_members)
177  
178 push(
179 56 ▶ @bonds,
Branch: 2 branches
BranchTrueFalseTotal
($bond_status // '') eq 'up' ? : 49 7 56
$about_bond =~ /^\s*Partner Mac Address: 00:00:00:00:00:00\s*$/mu ? : 7 49 56
Condition50%1/2
$bond_status // ""
l!l
560
MC/DC0%0/1
$bond_status // ""
$bond_status
Truth table50%1/2
A
A= $bond_status
Aresult
11
00
180 {
181 name => $name,
182 mode => $mode,
183 up => ( $bond_status // '' ) eq 'up' ? 1 : 0,
184 problem => $about_bond =~ /^\s*Partner Mac Address: 00:00:00:00:00:00\s*$/m
185 ? 'no LACP partner'
186 : undef,
187 members => \@members,
188 }
189 );
190 } ## end foreach my $name (@names)
191  
192 21 return @bonds;
193 } ## end sub linux_bonds
194  
195 =head2 freebsd_bonds
196  
197 Returns the laggs ifconfig -a shows, sorted by name, as for
198 L</linux_bonds>. Dies if ifconfig fails.
199  
200 =cut
201  
202 27 ▶sub freebsd_bonds {
Subroutine freebsd_bonds: called CC=16 SCAR=28.0
Pod documented
203 27 my ($self) = @_;
204  
205 27 my ( $exit, $output ) = $self->run_command( $IFCONFIG, '-a' );
206 21 ▶ die( 'ifconfig -a exited ' . $exit . "\n" ) if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 1 20 21
207  
208 # a interface is its name and a colon starting a line, then indented lines
209 20 my %about;
210 20 foreach my $about_interface ( split( /\n(?=\S)/, $output ) ) {
211 121 ▶ $about{$1} = $about_interface if $about_interface =~ /\A(\S+?):/;
Branch: 1 branch
BranchTrueFalseTotal
if $about_interface =~ /\A(\S+?):/u 121 0 121
212 }
213  
214 20 my @bonds;
215 20 foreach my $name ( sort( keys(%about) ) ) {
216 121 my ($protocol) = $about{$name} =~ /^\s+laggproto (\S+)/m;
217 121 ▶ next if !defined($protocol);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $protocol 88 33 121
218  
219 # such as "laggport: igb0 flags=1c<ACTIVE,COLLECTING,DISTRIBUTING>". Only LACP flags every
220 # working port, failover flagging just the one in use, so the port's own link is gone by.
221 33 my @members;
222 33 ▶ while ( $about{$name} =~ /^\s+laggport: (\S+) flags=[0-9a-f]+<([^>]*)>/mg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($about{$name} =~ /^\s+laggport: (\S+) flags=[0-9a-f]+<([^>]*)>/gmu) 66 33 99
223 66 my ( $port, $port_flags ) = ( $1, $2 );
224 66 ▶ my ($link) = ( $about{$port} // '' ) =~ /^\s+status: (.+?)\s*$/m;
Condition50%1/2
$about{$port} // ""
l!l
660
MC/DC0%0/1
$about{$port} // ""
$about{$port}
Truth table50%1/2
A
A= $about{$port}
Aresult
11
00
225  
226 66 my $problem;
227 66 ▶ if ( defined($link) && $link ne 'active' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $link and $link ne 'active') { } 15 51 66
elsif ($protocol eq 'lacp' and grep {not $port_flags =~ /\b$_\b/u;} 'ACTIVE', 'COLLECTING', 'DISTRIBUTING') { } 9 63 72
Condition66%2/3
defined $link and $link ne "active"
!ll&&!rl&&r
05115
MC/DC50%1/2
defined $link and $link ne "active"
defined $link $link ne "active"
Truth table66%2/3
A and B
A= defined $linkB= $link ne "active"
ABresult
0X0
100
111
228 15 $problem = $link;
229 90 ▶ } elsif ( $protocol eq 'lacp' && grep { $port_flags !~ /\b$_\b/ }
Condition100%3/3
$protocol eq "lacp" and grep {not $port_flags =~ /\b$_\b/u;} "ACTIVE", "COLLECTING", "DISTRIBUTING"
!ll&&!rl&&r
21219
MC/DC100%2/2
$protocol eq "lacp" and grep {not $port_flags =~ /\b$_\b/u;} "ACTIVE", "COLLECTING", "DISTRIBUTING"
$protocol eq "lacp" grep {not $port_flags =~ /\b$_\b/u;} "ACTIVE", "COLLECTING", "DISTRIBUTING"
Truth table100%3/3
A and B
A= $protocol eq "lacp"B= grep {not $port_flags =~ /\b$_\b/u;} "ACTIVE", "COLLECTING", "DISTRIBUTING"
ABresult
0X0
100
111
230 ( 'ACTIVE', 'COLLECTING', 'DISTRIBUTING' ) )
231 {
232 9 $problem = 'not aggregating';
233 }
234 66 push( @members, { name => $port, problem => $problem } );
235 } ## end while ( $about{$name} =~ /^\s+laggport: (\S+) flags=[0-9a-f]+<([^>]*)>/mg)
236  
237 33 my ($flags) = $about{$name} =~ /\A\S+ flags=[0-9a-f]+<([^>]*)>/;
238 33 my ($status) = $about{$name} =~ /^\s+status: (.+?)\s*$/m;
239 33 ▶ push(
Branch: 1 branch
BranchTrueFalseTotal
($flags // '') =~ /\bUP\b/u && ($status // 'active') eq 'active' ? : 30 3 33
Condition57%4/7
($flags // "") =~ /\bUP\b/u and ($status // "active") eq "active"
!ll&&!rl&&r
0330
$flags // ""
l!l
330
$status // "active"
l!l
330
MC/DC0%0/2
($flags // "") =~ /\bUP\b/u and ($status // "active") eq "active"
$flags $status
Truth table33%1/3
A and B
A= $flagsB= $status
ABresult
0X0
100
111
240 @bonds,
241 {
242 name => $name,
243 mode => $protocol,
244 up => ( ( $flags // '' ) =~ /\bUP\b/ && ( $status // 'active' ) eq 'active' ) ? 1 : 0,
245 problem => undef,
246 members => \@members,
247 }
248 );
249 } ## end foreach my $name ( sort( keys(%about) ) )
250  
251 20 return @bonds;
252 } ## end sub freebsd_bonds
253  
254 9 1;
« blib/lib/Monitoring/Sneck/Checks/ldap.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/http.pm »