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

filter: Statementstmt 98.7% 154 / 156 Branchbran 87.8% 65 / 74 Conditioncond 79.4% 58 / 73 MC/DCmcdc 60.9% 25 / 41 Subroutinesub 100% 12 / 12 Podpod 100% 6 / 6 totaltotal 88.4% 320 / 362 CC 78 SCAR 29.8
CC
78
Cov
91%
keepalived_instances
27.2
freevrrpd_instances
22.9
check
22.0
CRAP 19.6
« blib/lib/Monitoring/Sneck/Checks/zfs.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/edac.pm »
1 package Monitoring::Sneck::Checks::vrrp;
2  
3 12 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Monitoring::Sneck::Checks::vrrp - checks keepalived, freevrrpd, or FreeBSD's CARP being MASTER or BACKUP as it should
13  
14 =head1 VERSION
15  
16 Version 0.0.1
17  
18 =cut
19  
20 12 our $VERSION = '0.0.1';
21  
22 # Where things are. Set by tests. PID_FILE undef means the first of
23 # PID_FILES that is there.
24 12 our $IFCONFIG = '/sbin/ifconfig';
25 12 our $PID_FILE;
26 12 our @PID_FILES = ( '/run/keepalived.pid', '/var/run/keepalived.pid' );
27 12 our $DATA_FILE = '/tmp/keepalived.data';
28 12 our $FREEVRRPD_CONF = '/usr/local/etc/freevrrpd.conf';
29 12 our $FREEVRRPD_PID_FILE = '/var/run/freevrrpd.pid';
30  
31 # The seconds keepalived gets to write its state out.
32 12 our $DUMP_WAIT = 3;
33  
34 =head1 SYNOPSIS
35  
36 vrrp|perl:vrrp
37 vrrp_primary|perl:vrrp -s master
38 vrrp_mixed|perl:vrrp -m VI_WEB -b VI_DB
39 carp|perl:vrrp -m em0:1 -b 2
40 freevrrpd|perl:vrrp -T freevrrpd -s master
41  
42 =head1 DESCRIPTION
43  
44 Checks the state of each VRRP instance of keepalived, each VRID of
45 freevrrpd, or each CARP vhid on FreeBSD, catching a failover no one
46 saw, or one that never failed back.
47  
48 - CRITICAL :: A state other than MASTER or BACKUP, such as FAULT,
49 INIT, or PARTIAL. One not in the state --master, --backup, or --state
50 says it should be in. One named by --master or --backup that is not
51 there. keepalived or freevrrpd not running.
52  
53 - UNKNOWN :: keepalived not being able to be signalled, as when not run
54 as root, or not writing its state out. freevrrpd.conf not being
55 readable.
56  
57 - OK :: Everything else, including there being nothing to check.
58  
59 Without --master, --backup, or --state, either of MASTER or BACKUP is
60 fine, so only FAULT and the like are caught.
61  
62 A line for each follows the status line, with its interface, ID, and
63 priority, or advskew for CARP, and addresses for freevrrpd.
64  
65 =head2 keepalived
66  
67 keepalived only says what state it is in when sent USR1, at which it
68 writes /tmp/keepalived.data. So the PID in /run/keepalived.pid, or
69 /var/run/keepalived.pid, is sent that, and the file read once it has
70 been written anew, which is waited on for up to three seconds. This
71 needs root. Instances are named as in keepalived.conf.
72  
73 =head2 CARP
74  
75 Read from ifconfig -a, so root is not needed. A vhid is named by its
76 interface and number, such as em0:1. --master and --backup also take
77 just the number, which is then every interface with that vhid. FreeBSD
78 only.
79  
80 =head2 freevrrpd
81  
82 freevrrpd has no way of being asked its state. What it does as MASTER
83 is hold the addresses of the VRID, so each VRID in
84 /usr/local/etc/freevrrpd.conf is MASTER if every one of its addresses
85 is on a interface that is up, going by ifconfig -a, BACKUP if none
86 are, and PARTIAL if only some are. A VRID still starting up, or one
87 freevrrpd gave up on, looks like BACKUP.
88  
89 It is running if the PID in /var/run/freevrrpd.pid is. Root is only
90 needed if freevrrpd.conf, which may have passwords in it, is not
91 readable otherwise. A VRID is named by its interface and serverid, such
92 as em0:1, or just the serverid, as for CARP. FreeBSD only.
93  
94 =head1 OPTIONS
95  
96 - -T, --type :: What to check, keepalived, freevrrpd, or carp.
97 Default :: carp on FreeBSD, else keepalived
98  
99 - -m, --master :: A instance, VRID, or vhid that must be MASTER. May be given
100 more than once.
101  
102 - -b, --backup :: A instance, VRID, or vhid that must be BACKUP. May be given
103 more than once.
104  
105 - -s, --state :: The state, master or backup, the ones not named by
106 --master or --backup must be in.
107  
108 =cut
109  
110 197 ▶sub option_spec { return ( 'type|T=s', 'master|m=s@', 'backup|b=s@', 'state|s=s' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
111  
112 197 ▶sub validate {
Subroutine validate: called CC=8 SCAR=20.8
Pod documented
113 197 my ( $self, $options ) = @_;
114  
115 197 my @problems;
116 push( @problems, '--type must be keepalived, freevrrpd, or carp' )
117 197 ▶ if defined( $options->{type} ) && $options->{type} !~ /\A(?:keepalived|freevrrpd|carp)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"type"} and not $options->{"type"} =~ /\A(?:keepalived|freevrrpd|carp)\z/u 9 188 197
Condition100%3/3
defined $options->{"type"} and not $options->{"type"} =~ /\A(?:keepalived|freevrrpd|carp)\z/u
!ll&&!rl&&r
157319
MC/DC100%2/2
defined $options->{"type"} and not $options->{"type"} =~ /\A(?:keepalived|freevrrpd|carp)\z/u
defined $options->{"type"} not $options->{"type"} =~ /\A(?:keepalived|freevrrpd|carp)\z/u
Truth table100%3/3
A and not(B)
A= defined $options->{"type"}B= not $options->{"type"} =~ /\A(?:keepalived|freevrrpd|carp)\z/u
ABresult
0X0
100
111
118 push( @problems, '--state must be master or backup' )
119 197 ▶ if defined( $options->{state} ) && $options->{state} !~ /\A(?:master|backup)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"state"} and not $options->{"state"} =~ /\A(?:master|backup)\z/iu 9 188 197
Condition100%3/3
defined $options->{"state"} and not $options->{"state"} =~ /\A(?:master|backup)\z/iu
!ll&&!rl&&r
166229
MC/DC100%2/2
defined $options->{"state"} and not $options->{"state"} =~ /\A(?:master|backup)\z/iu
defined $options->{"state"} not $options->{"state"} =~ /\A(?:master|backup)\z/iu
Truth table100%3/3
A and not(B)
A= defined $options->{"state"}B= not $options->{"state"} =~ /\A(?:master|backup)\z/iu
ABresult
0X0
100
111
120 197 ▶ my %as_master = map { $_ => 1 } @{ $options->{master} // [] };
Condition100%2/2
$options->{"master"} // []
l!l
67130
MC/DC100%1/1
$options->{"master"} // []
$options->{"master"}
Truth table100%2/2
A
A= $options->{"master"}
Aresult
11
00
121 197 ▶ foreach my $name ( grep { $as_master{$_} } @{ $options->{backup} // [] } ) {
Condition100%2/2
$options->{"backup"} // []
l!l
50147
MC/DC100%1/1
$options->{"backup"} // []
$options->{"backup"}
Truth table100%2/2
A
A= $options->{"backup"}
Aresult
11
00
122 9 push( @problems, '"' . $name . '" is given to both --master and --backup' );
123 }
124  
125 197 return @problems;
126 } ## end sub validate
127  
128 =head1 METHODS
129  
130 =head2 check
131  
132 Does the check.
133  
134 =cut
135  
136 170 ▶sub check {
Subroutine check: called CC=22 SCAR=30.9
Pod documented
137 170 my ($self) = @_;
138  
139 170 ▶ my $type = $self->opt('type') // ( $self->os eq 'freebsd' ? 'carp' : 'keepalived' );
Branch: 1 branch
BranchTrueFalseTotal
$self->os eq 'freebsd' ? : 30 109 139
Condition100%2/2
$self->opt("type") // ($self->os eq "freebsd" ? "carp" : "keepalived")
l!l
31139
MC/DC100%1/1
$self->opt("type") // ($self->os eq "freebsd" ? "carp" : "keepalived")
$self->opt("type")
Truth table100%2/2
A
A= $self->opt("type")
Aresult
11
00
140 170 ▶ my $instances
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'carp' ? : 39 131 170
$type eq 'freevrrpd' ? : 22 109 131
141 = $type eq 'carp' ? $self->carp_instances
142 : $type eq 'freevrrpd' ? $self->freevrrpd_instances
143 : $self->keepalived_instances;
144 142 ▶ return $self->finish( $self->CRITICAL, $type . ' is not running' ) if !defined($instances);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $instances 25 117 142
145  
146 # the state each name given must be in
147 117 my %wanted;
148 117 ▶ $wanted{$_} = 'MASTER' foreach @{ $self->opt('master') // [] };
Condition100%2/2
$self->opt("master") // []
l!l
5562
MC/DC100%1/1
$self->opt("master") // []
$self->opt("master")
Truth table100%2/2
A
A= $self->opt("master")
Aresult
11
00
149 117 ▶ $wanted{$_} = 'BACKUP' foreach @{ $self->opt('backup') // [] };
Condition100%2/2
$self->opt("backup") // []
l!l
3978
MC/DC100%1/1
$self->opt("backup") // []
$self->opt("backup")
Truth table100%2/2
A
A= $self->opt("backup")
Aresult
11
00
150  
151 117 my @codes = ( $self->OK );
152 117 my @parts;
153 117 ▶ my %have = map { $_ => 1 } map { ( $_->{name}, $_->{alias} // () ) } @{$instances};
Condition66%2/3
$_->{"alias"} // ()
l!l&&r!l&&!r
811260
MC/DC0%0/2
$_->{"alias"} // ()
$_->{"alias"} ()
Truth table66%2/3
A // B
A= $_->{"alias"}B= ()
ABresult
1X1
011
000
154 117 foreach my $name ( grep { !$have{$_} } sort( keys(%wanted) ) ) {
155 25 push( @codes, $self->CRITICAL );
156 25 push( @parts, $name . ' is not there' );
157 }
158  
159 117 my %counts = ( MASTER => 0, BACKUP => 0 );
160 117 foreach my $instance ( @{$instances} ) {
161 207 my $state = $instance->{state};
162 207 ▶ my $wanted = $wanted{ $instance->{name} } // $wanted{ $instance->{alias} // '' } // $self->opt('state');
Condition100%8/8
($wanted{$instance->{"name"}} // $wanted{$instance->{"alias"} // ""}) // $self->opt("state")
l!l&&r!l&&!r
913383
$wanted{$instance->{"name"}} // $wanted{$instance->{"alias"} // ""}
l!l&&r!l&&!r
6526116
$instance->{"alias"} // ""
l!l
6181
MC/DC100%3/3
($wanted{$instance->{"name"}} // $wanted{$instance->{"alias"} // ""}) // $self->opt("state")
$wanted{$instance->{"name"}} $wanted{$instance->{"alias"} // ""} $self->opt("state")
MC/DC100%1/1
$instance->{"alias"} // ""
$instance->{"alias"}
Truth table100%4/4
A // B // C
A= $wanted{$instance->{"name"}}B= $wanted{$instance->{"alias"} // ""}C= $self->opt("state")
ABCresult
1XX1
01X1
0011
0000
Truth table100%2/2
A
A= $instance->{"alias"}
Aresult
11
00
163 207 ▶ $wanted = uc($wanted) if defined($wanted);
Branch: 1 branch
BranchTrueFalseTotal
if defined $wanted 124 83 207
164 207 ▶ $counts{$state}++ if defined( $counts{$state} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $counts{$state} 175 32 207
165  
166 207 my $part = $instance->{name} . ' ' . $state;
167 207 ▶ if ( !defined( $counts{$state} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $counts{$state}) { } 32 175 207
elsif (defined $wanted and $state ne $wanted) { } 28 207 235
168 32 push( @codes, $self->CRITICAL );
169 ▶ } elsif ( defined($wanted) && $state ne $wanted ) {
Condition100%3/3
defined $wanted and $state ne $wanted
!ll&&!rl&&r
608728
MC/DC100%2/2
defined $wanted and $state ne $wanted
defined $wanted $state ne $wanted
Truth table100%3/3
A and B
A= defined $wantedB= $state ne $wanted
ABresult
0X0
100
111
170 28 push( @codes, $self->CRITICAL );
171 28 $part = $part . ', not ' . $wanted;
172 }
173 207 push( @parts, $part );
174 } ## end foreach my $instance ( @{$instances} )
175 117 ▶ if ( !defined( $parts[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $parts[0]) 15 102 117
176 15 my %nothing = ( carp => 'no CARP vhids', freevrrpd => 'no VRIDs', keepalived => 'no VRRP instances' );
177 15 return $self->finish( $self->OK, $nothing{$type} );
178 }
179 102 $self->perfdata( label => 'master', value => $counts{MASTER}, min => 0 );
180 102 $self->perfdata( label => 'backup', value => $counts{BACKUP}, min => 0 );
181  
182 102 return $self->finish( $self->worst(@codes), join( ', ', @parts ), map { $_->{detail} } @{$instances} );
183 } ## end sub check
184  
185 =head2 keepalived_instances
186  
187 Has keepalived write its state out and reads its VRRP instances from
188 it, see L</keepalived>.
189  
190 Returns undef if keepalived is not running, else a array ref of hash
191 refs of...
192  
193 - name :: The instance, as in keepalived.conf.
194  
195 - state :: MASTER, BACKUP, FAULT, and so on.
196  
197 - detail :: The line about it for after the status line.
198  
199 Dies if it can not be signalled or does not write its state out.
200  
201 =cut
202  
203 109 ▶sub keepalived_instances {
Subroutine keepalived_instances: called CC=24 SCAR=33.0
Pod documented
204 109 my ($self) = @_;
205  
206 0 ▶ my $pid_file = $PID_FILE // ( grep { -e $_ } @PID_FILES )[0];
Condition33%1/3
$Monitoring::Sneck::Checks::vrrp::PID_FILE // (grep {-e $_;} @Monitoring::Sneck::Checks::vrrp::PID_FILES)[0]
l!l&&r!l&&!r
10900
MC/DC0%0/2
$Monitoring::Sneck::Checks::vrrp::PID_FILE // (grep {-e $_;} @Monitoring::Sneck::Checks::vrrp::PID_FILES)[0]
$Monitoring::Sneck::Checks::vrrp::PID_FILE (grep {-e $_;} @Monitoring::Sneck::Checks::vrrp::PID_FILES)[0]
Truth table33%1/3
A // B
A= $Monitoring::Sneck::Checks::vrrp::PID_FILEB= (grep {-e $_;} @Monitoring::Sneck::Checks::vrrp::PID_FILES)[0]
ABresult
1X1
011
000
207 109 ▶ my $pid = defined($pid_file) ? read_file( $pid_file, err_mode => 'quiet' ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $pid_file ? : 109 0 109
208 109 ▶ return undef if !defined($pid) || $pid !~ /\A\s*([0-9]+)\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid and $pid =~ /\A\s*([0-9]+)\s*\z/u 10 99 109
Condition66%2/3
defined $pid and $pid =~ /\A\s*([0-9]+)\s*\z/u
!ll&&!rl&&r
10099
MC/DC50%1/2
defined $pid and $pid =~ /\A\s*([0-9]+)\s*\z/u
defined $pid $pid =~ /\A\s*([0-9]+)\s*\z/u
Truth table66%2/3
A and B
A= defined $pidB= $pid =~ /\A\s*([0-9]+)\s*\z/u
ABresult
0X0
100
111
209 99 $pid = $1;
210  
211 # what the file was before, by inode and mtime, so a old one is not taken for what was asked for
212 99 my @before = Time::HiRes::stat($DATA_FILE);
213 99 ▶ if ( !kill( 'USR1', $pid ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (kill "USR1", $pid) 9 90 99
214 9 ▶ return undef if $!{ESRCH};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"ESRCH"} 9 0 9
215 0 die( 'can not signal keepalived, PID ' . $pid . ', ' . $! . "\n" );
216 }
217  
218 # written once it is new and was the same size two looks in a row
219 90 my $give_up = Time::HiRes::time + $DUMP_WAIT;
220 90 my $last_size;
221 90 while (1) {
222 432 Time::HiRes::sleep(0.05);
223 432 my @now = Time::HiRes::stat($DATA_FILE);
224 432 ▶ my $is_new = @now && ( !@before || $now[1] != $before[1] || $now[9] != $before[9] );
Condition77%7/9
@now && (!@before || $now[1] != $before[1] || $now[9] != $before[9])
!ll&&!rl&&r
0270162
not @before or $now[1] != $before[1] or $now[9] != $before[9]
l!l&&r!l&&!r
18144270
not @before or $now[1] != $before[1]
l!l&&r!l&&!r
180414
MC/DC50%2/4
@now && (!@before || $now[1] != $before[1] || $now[9] != $before[9])
@now not @before $now[1] != $before[1] $now[9] != $before[9]
Truth table60%3/5
A && not(B) or C or D
A= @nowB= not @beforeC= $now[1] != $before[1]D= $now[9] != $before[9]
ABCDresult
0XXX0
10000
11XX1
101X1
10011
225 432 ▶ my $new_size = $is_new ? $now[7] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$is_new ? : 162 270 432
226 432 ▶ last if $new_size && defined($last_size) && $new_size == $last_size;
Branch: 1 branch
BranchTrueFalseTotal
if $new_size and defined $last_size and $new_size == $last_size 81 351 432
Condition83%5/6
$new_size and defined $last_size and $new_size == $last_size
!ll&&!rl&&r
351081
$new_size and defined $last_size
!ll&&!rl&&r
2708181
MC/DC66%2/3
$new_size and defined $last_size and $new_size == $last_size
$new_size defined $last_size $new_size == $last_size
Truth table75%3/4
A and B and C
A= $new_sizeB= defined $last_sizeC= $new_size == $last_size
ABCresult
0XX0
10X0
1100
1111
227 351 $last_size = $new_size;
228 351 ▶ die( 'keepalived, PID ' . $pid . ', did not write ' . $DATA_FILE . "\n" ) if Time::HiRes::time > $give_up;
Branch: 1 branch
BranchTrueFalseTotal
if &Time::HiRes::time() > $give_up 9 342 351
229 }
230 81 ▶ my $dump = read_file( $DATA_FILE, err_mode => 'quiet' ) // die( 'can not read ' . $DATA_FILE . ', ' . $! . "\n" );
Condition50%1/2
read_file($Monitoring::Sneck::Checks::vrrp::DATA_FILE, "err_mode", "quiet") // die('can not read ' . $Monitoring::Sneck::Checks::vrrp::DATA_FILE . ', ' . $! . "\n")
l!l
810
MC/DC0%0/1
read_file($Monitoring::Sneck::Checks::vrrp::DATA_FILE, "err_mode", "quiet") // die('can not read ' . $Monitoring::Sneck::Checks::vrrp::DATA_FILE . ', ' . $! . "\n")
read_file($Monitoring::Sneck::Checks::vrrp::DATA_FILE, "err_mode", "quiet")
Truth table50%1/2
A
A= read_file($Monitoring::Sneck::Checks::vrrp::DATA_FILE, "err_mode", "quiet")
Aresult
11
00
231  
232 # each instance starts with " VRRP Instance = VI_1", then its lines as " State = MASTER". The first of
233 # each line wanted is taken, as the last instance runs on into the sync groups after it.
234 81 my @instances;
235 81 foreach my $about_instance ( split( /^(?=\s*VRRP Instance\s*=)/m, $dump ) ) {
236 207 ▶ next if $about_instance !~ /\A\s*VRRP Instance\s*=\s*(\S+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $about_instance =~ /\A\s*VRRP Instance\s*=\s*(\S+)/u 81 126 207
237 126 my $name = $1;
238 126 my %is;
239 126 foreach my $line ( 'State', 'Interface', 'Virtual Router ID', 'Priority', 'Master router' ) {
240 630 ( $is{$line} ) = $about_instance =~ /^\s+\Q$line\E\s*=\s*(\S+)/m;
241 }
242 126 ▶ my $state = uc( $is{State} // 'unknown' );
Condition50%1/2
$is{"State"} // "unknown"
l!l
1260
MC/DC0%0/1
$is{"State"} // "unknown"
$is{"State"}
Truth table50%1/2
A
A= $is{"State"}
Aresult
11
00
243 126 my $detail = $name . ', ' . $state;
244 126 ▶ $detail = $detail . ', ' . $is{Interface} if defined( $is{Interface} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $is{"Interface"} 126 0 126
245 126 ▶ $detail = $detail . ', VRID ' . $is{'Virtual Router ID'} if defined( $is{'Virtual Router ID'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $is{"Virtual Router ID"} 126 0 126
246 126 ▶ $detail = $detail . ', priority ' . $is{Priority} if defined( $is{Priority} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $is{"Priority"} 126 0 126
247 126 ▶ $detail = $detail . ', master router ' . $is{'Master router'} if defined( $is{'Master router'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $is{"Master router"} 63 63 126
248 126 push( @instances, { name => $name, state => $state, detail => $detail } );
249 } ## end foreach my $about_instance ( split( /^(?=\s*VRRP Instance\s*=)/m...))
250  
251 81 return \@instances;
252 } ## end sub keepalived_instances
253  
254 =head2 carp_instances
255  
256 Reads the CARP vhids from ifconfig -a.
257  
258 Returns a array ref of hash refs as for L</keepalived_instances>, name
259 being the interface and vhid, such as em0:1, with also...
260  
261 - alias :: The vhid alone.
262  
263 Dies if not on FreeBSD or ifconfig fails.
264  
265 =cut
266  
267 39 ▶sub carp_instances {
Subroutine carp_instances: called CC=6 SCAR=18.0
Pod documented
268 39 my ($self) = @_;
269  
270 39 ▶ die( 'CARP is only checked on FreeBSD, not ' . $self->os . "\n" ) if $self->os ne 'freebsd';
Branch: 1 branch
BranchTrueFalseTotal
if $self->os ne "freebsd" 9 30 39
271 30 my ( $exit, $output ) = $self->run_command( $IFCONFIG, '-a' );
272 26 ▶ die( 'ifconfig -a exited ' . $exit . "\n" ) if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 0 26 26
273  
274 # a interface is its name and a colon starting a line, then indented lines, among them
275 # one such as "carp: MASTER vhid 1 advbase 1 advskew 0" for each vhid
276 26 my @instances;
277 26 foreach my $about_interface ( split( /\n(?=\S)/, $output ) ) {
278 68 ▶ next if $about_interface !~ /\A(\S+?):/;
Branch: 1 branch
BranchTrueFalseTotal
unless $about_interface =~ /\A(\S+?):/u 0 68 68
279 68 my $interface = $1;
280 68 ▶ while ( $about_interface =~ /^\s+carp: (\S+) vhid ([0-9]+) advbase ([0-9]+) advskew ([0-9]+)/mg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($about_interface =~ /^\s+carp: (\S+) vhid ([0-9]+) advbase ([0-9]+) advskew ([0-9]+)/gmu) 63 68 131
281 63 my ( $state, $vhid, $advbase, $advskew ) = ( uc($1), $2, $3, $4 );
282 63 my $name = $interface . ':' . $vhid;
283 63 push(
284 @instances,
285 {
286 name => $name,
287 alias => $vhid,
288 state => $state,
289 detail => $name . ', ' . $state . ', advbase ' . $advbase . ', advskew ' . $advskew,
290 }
291 );
292 } ## end while ( $about_interface =~ /^\s+carp: (\S+) vhid ([0-9]+) advbase ([0-9]+) advskew ([0-9]+)/mg)
293 } ## end foreach my $about_interface ( split( /\n(?=\S)/...))
294  
295 26 return \@instances;
296 } ## end sub carp_instances
297  
298 =head2 freevrrpd_instances
299  
300 Works out the state of each VRID in freevrrpd.conf from the addresses
301 ifconfig -a shows, see L</freevrrpd>.
302  
303 Returns undef if freevrrpd is not running, else a array ref of hash
304 refs as for L</carp_instances>, name being the interface and serverid,
305 and state MASTER, BACKUP, or PARTIAL.
306  
307 Dies if not on FreeBSD, freevrrpd.conf can not be read, or ifconfig
308 fails.
309  
310 =cut
311  
312 22 ▶sub freevrrpd_instances {
Subroutine freevrrpd_instances: called CC=22 SCAR=31.3
Pod documented
313 22 my ($self) = @_;
314  
315 22 ▶ die( 'freevrrpd is only checked on FreeBSD, not ' . $self->os . "\n" ) if $self->os ne 'freebsd';
Branch: 1 branch
BranchTrueFalseTotal
if $self->os ne "freebsd" 1 21 22
316 21 my $pid = read_file( $FREEVRRPD_PID_FILE, err_mode => 'quiet' );
317 21 ▶ return undef if !defined($pid) || $pid !~ /\A\s*([0-9]+)\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid and $pid =~ /\A\s*([0-9]+)\s*\z/u 5 16 21
Condition66%2/3
defined $pid and $pid =~ /\A\s*([0-9]+)\s*\z/u
!ll&&!rl&&r
5016
MC/DC50%1/2
defined $pid and $pid =~ /\A\s*([0-9]+)\s*\z/u
defined $pid $pid =~ /\A\s*([0-9]+)\s*\z/u
Truth table66%2/3
A and B
A= defined $pidB= $pid =~ /\A\s*([0-9]+)\s*\z/u
ABresult
0X0
100
111
318  
319 # signal 0 only asks if it is there, which it is even if it is not ours to signal
320 16 ▶ return undef if !kill( 0, $1 ) && $!{ESRCH};
Branch: 1 branch
BranchTrueFalseTotal
if not kill(0, $1) and $!{"ESRCH"} 1 15 16
Condition66%2/3
not kill(0, $1) and $!{"ESRCH"}
!ll&&!rl&&r
1501
MC/DC50%1/2
not kill(0, $1) and $!{"ESRCH"}
not kill(0, $1) $!{"ESRCH"}
Truth table66%2/3
not(A) and B
A= not kill(0, $1)B= $!{"ESRCH"}
ABresult
0X0
100
111
321  
322 15 ▶ my $conf = read_file( $FREEVRRPD_CONF, err_mode => 'quiet' )
Condition100%2/2
read_file($Monitoring::Sneck::Checks::vrrp::FREEVRRPD_CONF, "err_mode", "quiet") // die('can not read ' . $Monitoring::Sneck::Checks::vrrp::FREEVRRPD_CONF . ', ' . $! . "\n")
l!l
141
MC/DC100%1/1
read_file($Monitoring::Sneck::Checks::vrrp::FREEVRRPD_CONF, "err_mode", "quiet") // die('can not read ' . $Monitoring::Sneck::Checks::vrrp::FREEVRRPD_CONF . ', ' . $! . "\n")
read_file($Monitoring::Sneck::Checks::vrrp::FREEVRRPD_CONF, "err_mode", "quiet")
Truth table100%2/2
A
A= read_file($Monitoring::Sneck::Checks::vrrp::FREEVRRPD_CONF, "err_mode", "quiet")
Aresult
11
00
323 // die( 'can not read ' . $FREEVRRPD_CONF . ', ' . $! . "\n" );
324 14 my ( $exit, $output ) = $self->run_command( $IFCONFIG, '-a' );
325 10 ▶ die( 'ifconfig -a exited ' . $exit . "\n" ) if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 0 10 10
326  
327 # the addresses on interfaces that are up, a interface being its name and a colon starting a line,
328 # then indented lines. freevrrpd takes its addresses off, and its interface down, as BACKUP.
329 10 my %held;
330 10 foreach my $about_interface ( split( /\n(?=\S)/, $output ) ) {
331 40 ▶ next if $about_interface !~ /\A\S+ flags=[0-9a-f]+<([^>]*)>/ || $1 !~ /\bUP\b/;
Branch: 1 branch
BranchTrueFalseTotal
unless $about_interface =~ /\A\S+ flags=[0-9a-f]+<([^>]*)>/u and $1 =~ /\bUP\b/u 10 30 40
Condition66%2/3
$about_interface =~ /\A\S+ flags=[0-9a-f]+<([^>]*)>/u and $1 =~ /\bUP\b/u
!ll&&!rl&&r
01030
MC/DC50%1/2
$about_interface =~ /\A\S+ flags=[0-9a-f]+<([^>]*)>/u and $1 =~ /\bUP\b/u
$about_interface =~ /\A\S+ flags=[0-9a-f]+<([^>]*)>/u $1 =~ /\bUP\b/u
Truth table66%2/3
A and B
A= $about_interface =~ /\A\S+ flags=[0-9a-f]+<([^>]*)>/uB= $1 =~ /\bUP\b/u
ABresult
0X0
100
111
332 30 $held{$_} = 1 foreach $about_interface =~ /^\s+inet (\S+)/mg;
333 }
334  
335 # a VRID is what follows each "[VRID]" line, as "option = value" lines
336 10 my @instances;
337 10 my ( $before_first, @vrids ) = split( /^\[VRID\][^\n]*\n/m, $conf );
338 10 foreach my $about_vrid (@vrids) {
339 19 my %is;
340 19 foreach my $option ( 'serverid', 'interface', 'priority', 'addr' ) {
341 76 ( $is{$option} ) = $about_vrid =~ /^\s*\Q$option\E\s*=\s*(.*?)\s*$/m;
342 }
343 19 ▶ next if !defined( $is{serverid} ) || !defined( $is{interface} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $is{"serverid"} and defined $is{"interface"} 1 18 19
Condition66%2/3
defined $is{"serverid"} and defined $is{"interface"}
!ll&&!rl&&r
0118
MC/DC50%1/2
defined $is{"serverid"} and defined $is{"interface"}
defined $is{"serverid"} defined $is{"interface"}
Truth table66%2/3
A and B
A= defined $is{"serverid"}B= defined $is{"interface"}
ABresult
0X0
100
111
344  
345 # such as "10.0.0.1/32, vlan100:192.168.1.1/30", the address being between the device and the prefix
346 18 ▶ my @addresses = map { /\A(?:[^:]+:)?([0-9.]+)/ ? $1 : () } split( /\s*,\s*/, $is{addr} // '' );
Branch: 1 branch
BranchTrueFalseTotal
/\A(?:[^:]+:)?([0-9.]+)/u ? : 27 0 27
Condition50%1/2
$is{"addr"} // ""
l!l
180
MC/DC0%0/1
$is{"addr"} // ""
$is{"addr"}
Truth table50%1/2
A
A= $is{"addr"}
Aresult
11
00
347 18 my $held_count = scalar( grep { $held{$_} } @addresses );
348 18 ▶ my $state
Branch: 2 branches
BranchTrueFalseTotal
!$held_count ? : 9 9 18
$held_count == scalar @addresses ? : 7 2 9
349 = !$held_count ? 'BACKUP'
350 : $held_count == scalar(@addresses) ? 'MASTER'
351 : 'PARTIAL';
352 18 my $name = $is{interface} . ':' . $is{serverid};
353 push(
354 @instances,
355 {
356 name => $name,
357 alias => $is{serverid},
358 state => $state,
359 detail => $name . ', '
360 . $state
361 . ', priority '
362 . ( $is{priority} // 'unknown' ) . ', '
363 18 ▶ . join( ' ', map { $_ . ( $held{$_} ? '' : ' (not held)' ) } @addresses ),
Branch: 1 branch
BranchTrueFalseTotal
$held{$_} ? : 16 11 27
Condition50%1/2
$is{"priority"} // "unknown"
l!l
180
MC/DC0%0/1
$is{"priority"} // "unknown"
$is{"priority"}
Truth table50%1/2
A
A= $is{"priority"}
Aresult
11
00
364 }
365 );
366 } ## end foreach my $about_vrid (@vrids)
367  
368 10 return \@instances;
369 } ## end sub freevrrpd_instances
370  
371 12 1;
« blib/lib/Monitoring/Sneck/Checks/zfs.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/edac.pm »