Monitor/Backends/FreeBSD.pm

filter: Statementstmt 31.2% 74 / 237 Branchbran 14.4% 17 / 118 Conditioncond 0.0% 0 / 66 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 22.4% 106 / 472 CC 93 SCAR 84.2
CC
93
Cov
22%
run
5367.3
usable
5.3
new
4.0
CRAP 4541.9
« Monitor/Backends/cgroups.pm ↑ Summary Monitor.pm »
1 package OSLV::Monitor::Backends::FreeBSD;
2  
3 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use JSON qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use File::Slurp qw(read_file write_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use List::Util qw( uniq );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Time::HiRes qw(gettimeofday time);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 OSLV::Monitor::Backends::FreeBSD - backend for FreeBSD jails
16  
17 =head1 VERSION
18  
19 Version 0.0.6
20  
21 =cut
22  
23 5 our $VERSION = '0.0.6';
24  
25 =head1 SYNOPSIS
26  
27 use OSLV::Monitor::Backends::FreeBSD;
28  
29 my $backend = OSLV::Monitor::Backends::FreeBSD->new;
30  
31 my $usable=$backend->usable;
32 if ( $usable ){
33 $return_hash_ref=$backend->run;
34 }
35  
36 The stats names match those produced by "ps --libxo json".
37  
38 =head1 METHODS
39  
40 =head2 new
41  
42 Initiates the backend object.
43  
44 my $backend=OSLV::MOnitor::Backend::FreeBSD->new(
45 base_dir => $base_dir,
46 );
47  
48 The following arguments are usable.
49  
50 - base_dir :: Path to use for the base dir, where the proc
51 cache, freebsd_proc_cache.json, is is created.
52 Default :: /var/cache/oslv_monitor
53  
54 - obj :: The OSLVM::Monitor object.
55  
56 =cut
57  
58 9 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
59 9 my ( $blank, %opts ) = @_;
60  
61 9 ▶ if ( !defined( $opts{base_dir} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"base_dir"}) 1 8 9
62 1 $opts{base_dir} = '/var/cache/oslv_monitor';
63 }
64  
65 9 ▶ if ( !defined( $opts{obj} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'obj'}) { } 1 8 9
elsif (ref $opts{'obj'} ne 'OSLV::Monitor') { } 1 7 8
66 1 die('$opts{obj} is undef');
67 } elsif ( ref( $opts{obj} ) ne 'OSLV::Monitor' ) {
68 1 die('ref $opts{obj} is not OSLV::Monitor');
69 }
70  
71 7 my $self = { version => 1, proc_cache => $opts{base_dir} . '/freebsd_proc_cache.json', obj => $opts{obj} };
72 7 bless $self;
73  
74 7 return $self;
75 } ## end sub new
76  
77 =head2 run
78  
79 $return_hash_ref=$backend->run;
80  
81 =cut
82  
83 3 ▶sub run {
Subroutine run: called CC=88 SCAR=85.9
Pod documented
84 3 my $self = $_[0];
85  
86 3 my $data = {
87 errors => [],
88 cache_failure => 0,
89 oslvms => {},
90 has => {
91 'linux_mem_stats' => 0,
92 'rwdops' => 0,
93 'rwdbytes' => 0,
94 'rwdblocks' => 1,
95 'signals-taken' => 1,
96 'recv_sent_msgs' => 1,
97 'cows' => 1,
98 'stack-size' => 1,
99 'swaps' => 1,
100 'sock' => 0,
101 },
102 totals => {
103 'copy-on-write-faults' => 0,
104 'cpu-time' => 0,
105 'data-size' => 0,
106 'elapsed-times' => 0,
107 'involuntary-context-switches' => 0,
108 'major-faults' => 0,
109 'minor-faults' => 0,
110 'percent-cpu' => 0,
111 'percent-memory' => 0,
112 'read-blocks' => 0,
113 'received-messages' => 0,
114 'rss' => 0,
115 'sent-messages' => 0,
116 'stack-size' => 0,
117 'swaps' => 0,
118 'system-time' => 0,
119 'text-size' => 0,
120 'user-time' => 0,
121 'virtual-size' => 0,
122 'voluntary-context-switches' => 0,
123 'written-blocks' => 0,
124 'procs' => 0,
125 'signals-taken' => 0,
126 },
127 };
128  
129 3 my $proc_cache;
130 3 my $new_proc_cache = {};
131 3 my $cache_is_new = 0;
132 3 ▶ if ( -f $self->{proc_cache} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->{'proc_cache'}) { } 2 1 3
133 2 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 2 2
134 warn( 'DEBUG, '
135 . join( '.', gettimeofday )
136 . ': backend run loading proc cache "'
137 . $self->{proc_cache}
138 0 . '"' );
139 }
140 2 eval {
141 2 my $raw_cache = read_file( $self->{proc_cache} );
142 2 $proc_cache = decode_json($raw_cache);
143 };
144 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 1 2
145 1 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 1 1
146 warn( 'DEBUG, '
147 . join( '.', gettimeofday )
148 . ': backend run proc cache "'
149 . $self->{proc_cache}
150 0 . '" could not be loaded... '
151 . $@ );
152 }
153 push(
154 1 @{ $data->{errors} },
155 1 'reading proc cache "' . $self->{proc_cache} . '" failed... using a empty one...' . $@
156 );
157 1 $data->{cache_failure} = 1;
158 # treat a corrupt/truncated cache as a fresh one so this run
159 # regenerates and overwrites it with valid data instead of getting
160 # permanently stuck on the bad file
161 1 $proc_cache = {};
162 1 $cache_is_new = 1;
163 } ## end if ($@)
164 } else {
165 1 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 1 1
166 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': backend run proc cache is new' );
167 }
168 1 $cache_is_new = 1;
169 }
170  
171 # rates are computed over the time since the previous run, as saved in the
172 # cache, instead of assuming it was 300 seconds ago
173 3 my $run_time = time;
174 3 my $interval = $self->{obj}->interval_since( $proc_cache, $run_time );
175 3 $new_proc_cache->{__oslv_monitor__} = { time => $run_time };
176  
177 3 my $base_stats = {
178 'copy-on-write-faults' => 0,
179 'cpu-time' => 0,
180 'data-size' => 0,
181 'elapsed-times' => 0,
182 'involuntary-context-switches' => 0,
183 'major-faults' => 0,
184 'minor-faults' => 0,
185 'percent-cpu' => 0,
186 'percent-memory' => 0,
187 'read-blocks' => 0,
188 'received-messages' => 0,
189 'rss' => 0,
190 'sent-messages' => 0,
191 'stack-size' => 0,
192 'swaps' => 0,
193 'system-time' => 0,
194 'text-size' => 0,
195 'user-time' => 0,
196 'virtual-size' => 0,
197 'voluntary-context-switches' => 0,
198 'written-blocks' => 0,
199 'procs' => 0,
200 'signals-taken' => 0,
201 'ip' => [],
202 'path' => [],
203 };
204  
205 # get a list of jails for jid to name mapping
206 3 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 3 3
207 0 warn( 'DEBUG, '
208 . join( '.', gettimeofday )
209 . ': backend run calling "/usr/sbin/jls -h --libxo json 2> /dev/null"' );
210 }
211 3 my $output = `/usr/sbin/jls -h --libxo json 2> /dev/null`;
212 3 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 3 3
213 0 warn( 'DEBUG, '
214 . join( '.', gettimeofday )
215 . ': backend run "/usr/sbin/jls -h --libxo json 2> /dev/null" finished' );
216 }
217 3 my $jls;
218 3 my @IP_keys = ( 'ip4.addr', 'ip6.addr' );
219 3 eval { $jls = decode_json($output) };
220 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 3 0 3
221 3 push( @{ $data->{errors} }, 'decoding output from "jls -h --libxo json 2> /dev/null" failed... ' . $@ );
222 3 return $data;
223 }
224 0 ▶ if ( defined($jls)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $jls and ref $jls eq "HASH" and defined $jls->{"jail-information"} and ref $jls->{"jail-information"} eq "HASH" and defined $jls->{"jail-information"}{"jail"} and ref $jls->{"jail-information"}{"jail"} eq "ARRAY") 0 0 0
Condition0%0/15
defined $jls and ref $jls eq "HASH" and defined $jls->{"jail-information"} and ref $jls->{"jail-information"} eq "HASH" and defined $jls->{"jail-information"}{"jail"} and ref $jls->{"jail-information"}{"jail"} eq "ARRAY"
!ll&&!rl&&r
000
defined $jls and ref $jls eq "HASH" and defined $jls->{"jail-information"} and ref $jls->{"jail-information"} eq "HASH" and defined $jls->{"jail-information"}{"jail"}
!ll&&!rl&&r
000
defined $jls and ref $jls eq "HASH" and defined $jls->{"jail-information"} and ref $jls->{"jail-information"} eq "HASH"
!ll&&!rl&&r
000
defined $jls and ref $jls eq "HASH" and defined $jls->{"jail-information"}
!ll&&!rl&&r
000
defined $jls and ref $jls eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/6
defined $jls and ref $jls eq "HASH" and defined $jls->{"jail-information"} and ref $jls->{"jail-information"} eq "HASH" and defined $jls->{"jail-information"}{"jail"} and ref $jls->{"jail-information"}{"jail"} eq "ARRAY"
defined $jls ref $jls eq "HASH" defined $jls->{"jail-information"} ref $jls->{"jail-information"} eq "HASH" defined $jls->{"jail-information"}{"jail"} ref $jls->{"jail-information"}{"jail"} eq "ARRAY"
Truth table0%0/7
A and B and C and D and E and F
A= defined $jlsB= ref $jls eq "HASH"C= defined $jls->{"jail-information"}D= ref $jls->{"jail-information"} eq "HASH"E= defined $jls->{"jail-information"}{"jail"}F= ref $jls->{"jail-information"}{"jail"} eq "ARRAY"
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
225 && ref($jls) eq 'HASH'
226 && defined( $jls->{'jail-information'} )
227 && ref( $jls->{'jail-information'} ) eq 'HASH'
228 && defined( $jls->{'jail-information'}{jail} )
229 && ref( $jls->{'jail-information'}{jail} ) eq 'ARRAY' )
230 {
231 0 foreach my $jls_jail ( @{ $jls->{'jail-information'}{jail} } ) {
232 0 ▶ if ( defined( $jls_jail->{name} ) && defined( $jls_jail->{jid} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $jls_jail->{"name"} and defined $jls_jail->{"jid"}) 0 0 0
Condition0%0/3
defined $jls_jail->{"name"} and defined $jls_jail->{"jid"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $jls_jail->{"name"} and defined $jls_jail->{"jid"}
defined $jls_jail->{"name"} defined $jls_jail->{"jid"}
Truth table0%0/3
A and B
A= defined $jls_jail->{"name"}B= defined $jls_jail->{"jid"}
ABresult
0X0
100
111
233 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
234 warn( 'DEBUG, '
235 . join( '.', gettimeofday )
236 . ': backend run processing jname="'
237 . $jls_jail->{name}
238 . '" jid="'
239 . $jls_jail->{jid}
240 0 . '"' );
241 }
242  
243 0 my $jname = $jls_jail->{name};
244 0 my $include_jail = $self->{'obj'}->include($jname);
245  
246 0 ▶ if ($include_jail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($include_jail) 0 0 0
247 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
248 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': backend run calling clone($base_stats)' );
249 }
250  
251 0 $data->{oslvms}{$jname} = clone($base_stats);
252  
253 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
254 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': backend run done clone($base_stats) done' );
255 }
256  
257 # record the jail's root path as reported by jls
258 0 ▶ if ( defined( $jls_jail->{path} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $jls_jail->{"path"} and ref $jls_jail->{"path"} eq "" and $jls_jail->{"path"} ne "") 0 0 0
Condition0%0/6
defined $jls_jail->{"path"} and ref $jls_jail->{"path"} eq "" and $jls_jail->{"path"} ne ""
!ll&&!rl&&r
000
defined $jls_jail->{"path"} and ref $jls_jail->{"path"} eq ""
!ll&&!rl&&r
000
MC/DC0%0/3
defined $jls_jail->{"path"} and ref $jls_jail->{"path"} eq "" and $jls_jail->{"path"} ne ""
defined $jls_jail->{"path"} ref $jls_jail->{"path"} eq "" $jls_jail->{"path"} ne ""
Truth table0%0/4
A and B and C
A= defined $jls_jail->{"path"}B= ref $jls_jail->{"path"} eq ""C= $jls_jail->{"path"} ne ""
ABCresult
0XX0
10X0
1100
1111
259 && ref( $jls_jail->{path} ) eq ''
260 && $jls_jail->{path} ne '' )
261 {
262 0 push( @{ $data->{oslvms}{$jname}{path} }, $jls_jail->{path} );
263 }
264  
265 # finds each ip ifconfig shows in a jail
266 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
267 0 warn( 'DEBUG, '
268 . join( '.', gettimeofday )
269 . ': backend run calling "ifconfig -j '
270 . $jname
271 . ' 2> /dev/null"' );
272 }
273 0 my $output = `ifconfig -j $jname 2> /dev/null`;
274 0 my %found_IPv4;
275 my %found_IPv6;
276 0 ▶ if ( $? eq 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($? eq 0) { } 0 0 0
elsif ($ENV{'OSLV_MONITOR_DEBUG'}) { } 0 0 0
277 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
278 0 warn( 'DEBUG, '
279 . join( '.', gettimeofday )
280 . ': backend run processing ifconfig info for jail' );
281 }
282 0 my @output_split = split( /\n/, $output );
283 0 my $interface;
284 0 foreach my $line (@output_split) {
285 0 ▶ if ( $line =~ /^[a-zA-Z].*\:[\ \t]+flags\=/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^[a-zA-Z].*\:[\ \t]+flags\=/) { } 0 0 0
elsif ($line =~ /^[\ \t]+inet6 / and defined $interface) { } 0 0 0
elsif ($line =~ /^[\ \t]+inet / and defined $interface) { } 0 0 0
286 0 $interface = $line;
287 0 $interface =~ s/\:[\ \t]+flags.*//;
288 ▶ } elsif ( $line =~ /^[\ \t]+inet6 /
Condition0%0/3
$line =~ /^[\ \t]+inet6 / and defined $interface
!ll&&!rl&&r
000
MC/DC0%0/2
$line =~ /^[\ \t]+inet6 / and defined $interface
$line =~ /^[\ \t]+inet6 / defined $interface
Truth table0%0/3
A and B
A= $line =~ /^[\ \t]+inet6 /B= defined $interface
ABresult
0X0
100
111
289 && defined($interface) )
290 {
291 0 $line =~ s/^[\ \t]+inet6 //;
292 0 $line =~ s/\ .*$//;
293 0 $line =~ s/\%.*$//;
294 0 $found_IPv6{$line} = $interface;
295 ▶ } elsif ( $line =~ /^[\ \t]+inet /
Condition0%0/3
$line =~ /^[\ \t]+inet / and defined $interface
!ll&&!rl&&r
000
MC/DC0%0/2
$line =~ /^[\ \t]+inet / and defined $interface
$line =~ /^[\ \t]+inet / defined $interface
Truth table0%0/3
A and B
A= $line =~ /^[\ \t]+inet /B= defined $interface
ABresult
0X0
100
111
296 && defined($interface) )
297 {
298 0 $line =~ s/^[\ \t]+inet //;
299 0 $line =~ s/ .*$//;
300 0 $found_IPv4{$line} = $interface;
301 }
302 } ## end foreach my $line (@output_split)
303 } elsif ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
304 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': backend run ifconfig exited non-zero' );
305 }
306 ## end if ( $? eq 0 )
307  
308 0 foreach my $ip_key (@IP_keys) {
309 0 my @current_IPs;
310  
311 0 ▶ if ( $ip_key eq 'ip4.addr' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip_key eq 'ip4.addr') { } 0 0 0
312 0 @current_IPs = keys(%found_IPv4);
313 } else {
314 0 @current_IPs = keys(%found_IPv6);
315 }
316  
317 0 ▶ if ( defined( $jls_jail->{$ip_key} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $jls_jail->{$ip_key} and ref $jls_jail->{$ip_key} eq "ARRAY" and defined $jls_jail->{$ip_key}[0]) 0 0 0
Condition0%0/6
defined $jls_jail->{$ip_key} and ref $jls_jail->{$ip_key} eq "ARRAY" and defined $jls_jail->{$ip_key}[0]
!ll&&!rl&&r
000
defined $jls_jail->{$ip_key} and ref $jls_jail->{$ip_key} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $jls_jail->{$ip_key} and ref $jls_jail->{$ip_key} eq "ARRAY" and defined $jls_jail->{$ip_key}[0]
defined $jls_jail->{$ip_key} ref $jls_jail->{$ip_key} eq "ARRAY" defined $jls_jail->{$ip_key}[0]
Truth table0%0/4
A and B and C
A= defined $jls_jail->{$ip_key}B= ref $jls_jail->{$ip_key} eq "ARRAY"C= defined $jls_jail->{$ip_key}[0]
ABCresult
0XX0
10X0
1100
1111
318 && ref( $jls_jail->{$ip_key} ) eq 'ARRAY'
319 && defined( $jls_jail->{$ip_key}[0] ) )
320 {
321 0 foreach my $ip ( @{ $jls_jail->{$ip_key} } ) {
322 0 ▶ if ( ref($ip) eq '' && !defined( $found_IPv4{$ip} ) && !defined( $found_IPv6{$ip} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ip eq "" and not defined $found_IPv4{$ip} and not defined $found_IPv6{$ip}) 0 0 0
Condition0%0/6
ref $ip eq "" and not defined $found_IPv4{$ip} and not defined $found_IPv6{$ip}
!ll&&!rl&&r
000
ref $ip eq "" and not defined $found_IPv4{$ip}
!ll&&!rl&&r
000
MC/DC0%0/3
ref $ip eq "" and not defined $found_IPv4{$ip} and not defined $found_IPv6{$ip}
ref $ip eq "" not defined $found_IPv4{$ip} not defined $found_IPv6{$ip}
Truth table0%0/4
A and not(B) and not(C)
A= ref $ip eq ""B= not defined $found_IPv4{$ip}C= not defined $found_IPv6{$ip}
ABCresult
0XX0
10X0
1100
1111
323 0 ▶ if ( $ip =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ or $ip =~ /^[\:0-9a-fA-F]+$/) 0 0 0
Condition0%0/3
$ip =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ or $ip =~ /^[\:0-9a-fA-F]+$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$ip =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ or $ip =~ /^[\:0-9a-fA-F]+$/
$ip =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ $ip =~ /^[\:0-9a-fA-F]+$/
Truth table0%0/3
A or B
A= $ip =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/B= $ip =~ /^[\:0-9a-fA-F]+$/
ABresult
1X1
011
000
324 || $ip =~ /^[\:0-9a-fA-F]+$/ )
325 {
326 0 push( @current_IPs, $ip );
327 }
328 }
329 }
330 } ## end if ( defined( $jls_jail->{$ip_key} ) && ref...)
331 0 foreach my $ip (@current_IPs) {
332 0 my $ip_if;
333 my $ip_gw;
334 0 my $ip_gw_if;
335  
336 0 ▶ if ( $ip_key eq 'ip4.addr'
Branch: 2 branches
BranchTrueFalseTotal
if ($ip_key eq 'ip4.addr' and defined $found_IPv4{$ip}) { } 0 0 0
elsif ($ip_key eq 'ip6.addr' and defined $found_IPv6{$ip}) { } 0 0 0
Condition0%0/3
$ip_key eq "ip4.addr" and defined $found_IPv4{$ip}
!ll&&!rl&&r
000
MC/DC0%0/2
$ip_key eq "ip4.addr" and defined $found_IPv4{$ip}
$ip_key eq "ip4.addr" defined $found_IPv4{$ip}
Truth table0%0/3
A and B
A= $ip_key eq "ip4.addr"B= defined $found_IPv4{$ip}
ABresult
0X0
100
111
337 && defined( $found_IPv4{$ip} ) )
338 {
339 0 $ip_if = $found_IPv4{$ip};
340 ▶ } elsif ( $ip_key eq 'ip6.addr'
Condition0%0/3
$ip_key eq "ip6.addr" and defined $found_IPv6{$ip}
!ll&&!rl&&r
000
MC/DC0%0/2
$ip_key eq "ip6.addr" and defined $found_IPv6{$ip}
$ip_key eq "ip6.addr" defined $found_IPv6{$ip}
Truth table0%0/3
A and B
A= $ip_key eq "ip6.addr"B= defined $found_IPv6{$ip}
ABresult
0X0
100
111
341 && defined( $found_IPv6{$ip} ) )
342 {
343 0 $ip_if = $found_IPv6{$ip};
344 }
345 # set the ip type flag for netstat
346 0 my $ip_flag = '-6';
347 0 ▶ if ( $ip_key eq 'ip4.addr' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip_key eq "ip4.addr") 0 0 0
348 0 $ip_flag = '-4';
349 }
350  
351 # fetch route info for the jail
352 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
353 0 warn( 'DEBUG, '
354 . join( '.', gettimeofday )
355 . ': backend run calling "route -n -j '
356 . $jname . ' '
357 . $ip_flag
358 . ' show default 2> /dev/null"' );
359 }
360 0 my $output = `route -n -j $jname $ip_flag show default 2> /dev/null`;
361 0 ▶ if ( $? eq 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($? eq 0) { } 0 0 0
elsif ($ENV{'OSLV_MONITOR_DEBUG'}) { } 0 0 0
362 0 my @output_split = split( /\n/, $output );
363 0 foreach my $line (@output_split) {
364 0 ▶ if ( $line =~ /gateway\:[\ \t]/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /gateway\:[\ \t]/) { } 0 0 0
elsif ($line =~ /interface:[\ \t]/) { } 0 0 0
365 0 $line =~ s/.*gateway\:[\ \t]+//;
366 0 $line =~ s/[\ \t]*$//;
367 0 $ip_gw = $line;
368 } elsif ( $line =~ /interface:[\ \t]/ ) {
369 0 $line =~ s/.*interface\:[\ \t]+//;
370 0 $line =~ s/[\ \t]*$//;
371 0 $ip_gw_if = $line;
372 }
373 } ## end foreach my $line (@output_split)
374 } elsif ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
375 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': backend run route exited non-zero' );
376 }
377  
378 push(
379 0 @{ $data->{oslvms}{$jname}{ip} },
380 {
381 ip => $ip,
382 if => $ip_if,
383 gw => $ip_gw,
384 gw_if => $ip_gw_if,
385 }
386 );
387 } ## end foreach my $ip (@current_IPs)
388 } ## end foreach my $ip_key (@IP_keys)
389 } ## end if ($include_jail)
390 } ## end if ( defined( $jls_jail->{name} ) && defined...)
391 } ## end foreach my $jls_jail ( @{ $jls->{'jail-information'...}})
392 } ## end if ( defined($jls) && ref($jls) eq 'HASH' ...)
393  
394 # remove possible dup paths
395 0 my @found_jails = keys( %{ $data->{oslvms} } );
396 0 foreach my $jail (@found_jails) {
397 0 my @uniq_paths = uniq( @{ $data->{oslvms}{$jail}{path} } );
398 0 $data->{oslvms}{$jail}{path} = \@uniq_paths;
399 }
400  
401 0 my @stats = (
402 'copy-on-write-faults', 'cpu-time',
403 'data-size', 'elapsed-times',
404 'involuntary-context-switches', 'major-faults',
405 'minor-faults', 'percent-cpu',
406 'percent-memory', 'read-blocks',
407 'received-messages', 'rss',
408 'sent-messages', 'stack-size',
409 'swaps', 'system-time',
410 'text-size', 'user-time',
411 'virtual-size', 'voluntary-context-switches',
412 'written-blocks', 'signals-taken',
413 );
414  
415 # values that are time stats that require additional processing
416 0 my $times = { 'cpu-time' => 1, 'system-time' => 1, 'user-time' => 1, };
417 # these are counters and differences needed computed for them
418 0 my $counters = {
419 'cpu-time' => 1,
420 'system-time' => 1,
421 'user-time' => 1,
422 'read-blocks' => 1,
423 'major-faults' => 1,
424 'involuntary-context-switches' => 1,
425 'minor-faults' => 1,
426 'received-messages' => 1,
427 'sent-messages' => 1,
428 'swaps' => 1,
429 'voluntary-context-switches' => 1,
430 'written-blocks' => 1,
431 'copy-on-write-faults' => 1,
432 'signals-taken' => 1,
433 };
434  
435 0 foreach my $jail (@found_jails) {
436 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
437 0 warn( 'DEBUG, '
438 . join( '.', gettimeofday )
439 . ': backend run calling "/bin/ps ax --libxo json -o %cpu,%mem,pid,acflag,cow,dsiz,etimes,inblk,jail,majflt,minflt,msgrcv,msgsnd,nivcsw,nswap,nvcsw,oublk,rss,ssiz,systime,time,tsiz,usertime,vsz,gid,uid,command,nsigs -J '
440 . $jail
441 . ' 2> /dev/null"' );
442 }
443 $output
444 0 = `/bin/ps ax --libxo json -o %cpu,%mem,pid,acflag,cow,dsiz,etimes,inblk,jail,majflt,minflt,msgrcv,msgsnd,nivcsw,nswap,nvcsw,oublk,rss,ssiz,systime,time,tsiz,usertime,vsz,gid,uid,command,nsigs -J $jail 2> /dev/null`;
445 0 my $ps;
446 0 eval { $ps = decode_json($output); };
447 0 ▶ if ( !$@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@) { } 0 0 0
448 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
449 0 warn( 'DEBUG, '
450 . join( '.', gettimeofday )
451 . ': backend run processing proc info for jail '
452 . $jail
453 . '' );
454 }
455 0 foreach my $proc ( @{ $ps->{'process-information'}{process} } ) {
456 # skip the kernel idle process... it is in jail 0, so -J should never
457 # list it, but guard against it defensively as its per-CPU idle
458 # threads make ps report roughly ncpu*100% for percent-cpu, which
459 # would wildly inflate the reported jail/total CPU usage
460 0 ▶ if ( defined( $proc->{command} ) && $proc->{command} eq '[idle]' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proc->{"command"} and $proc->{"command"} eq "[idle]") 0 0 0
Condition0%0/3
defined $proc->{"command"} and $proc->{"command"} eq "[idle]"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $proc->{"command"} and $proc->{"command"} eq "[idle]"
defined $proc->{"command"} $proc->{"command"} eq "[idle]"
Truth table0%0/3
A and B
A= defined $proc->{"command"}B= $proc->{"command"} eq "[idle]"
ABresult
0X0
100
111
461 0 next;
462 }
463 0 ▶ if ( $proc->{'elapsed-times'} ne '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc->{"elapsed-times"} ne "-") 0 0 0
464 my $cache_name
465 0 = $proc->{pid} . '-' . $proc->{uid} . '-' . $proc->{gid} . '-' . $jail . '-' . $proc->{command};
466  
467 0 foreach my $stat (@stats) {
468 0 my $stat_value = $proc->{$stat};
469 # pre-process the stat if it is a time value that requires it
470 0 ▶ if ( $times->{$stat} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($times->{$stat}) 0 0 0
471 # [days-][hours:]minutes:seconds
472 0 my $seconds = 0;
473 0 my $time = $stat_value;
474  
475 0 ▶ if ( $time =~ /-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time =~ /-/) 0 0 0
476 0 my $days = $time;
477 0 $days =~ s/\-.*$//;
478 0 $time =~ s/^.*\-//;
479 0 $seconds = $seconds + ( $days * 86400 );
480 }
481 0 my @time_split = split( /\:/, $time );
482 0 ▶ if ( defined( $time_split[2] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $time_split[2]) { } 0 0 0
483 0 $seconds
484 = $seconds + ( 3600 * $time_split[0] ) + ( 60 * $time_split[1] ) + $time_split[2];
485 } else {
486 0 $seconds = $seconds + ( 60 * $time_split[0] ) + $time_split[1];
487 }
488 0 $stat_value = $seconds;
489 0 $proc->{$stat} = $stat_value;
490 } ## end if ( $times->{$stat} )
491  
492 0 ▶ if ( looks_like_number($stat_value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($stat_value)) { } 0 0 0
493 0 ▶ if ( $counters->{$stat} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($counters->{$stat}) { } 0 0 0
494 0 ▶ if ( defined( $proc_cache->{$cache_name} )
Branch: 3 branches
BranchTrueFalseTotal
if (defined $proc_cache->{$cache_name} and defined $proc_cache->{$cache_name}{$stat} and $stat_value >= $proc_cache->{$cache_name}{$stat}) { } 0 0 0
elsif (defined $proc_cache->{$cache_name} and defined $proc_cache->{$cache_name}{$stat}) { } 0 0 0
elsif (&looks_like_number($proc->{'elapsed-times'}) and $proc->{'elapsed-times'} <= $interval) { } 0 0 0
Condition0%0/6
defined $proc_cache->{$cache_name} and defined $proc_cache->{$cache_name}{$stat} and $stat_value >= $proc_cache->{$cache_name}{$stat}
!ll&&!rl&&r
000
defined $proc_cache->{$cache_name} and defined $proc_cache->{$cache_name}{$stat}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $proc_cache->{$cache_name} and defined $proc_cache->{$cache_name}{$stat} and $stat_value >= $proc_cache->{$cache_name}{$stat}
defined $proc_cache->{$cache_name} defined $proc_cache->{$cache_name}{$stat} $stat_value >= $proc_cache->{$cache_name}{$stat}
Truth table0%0/4
A and B and C
A= defined $proc_cache->{$cache_name}B= defined $proc_cache->{$cache_name}{$stat}C= $stat_value >= $proc_cache->{$cache_name}{$stat}
ABCresult
0XX0
10X0
1100
1111
495 && defined( $proc_cache->{$cache_name}{$stat} )
496 && $stat_value >= $proc_cache->{$cache_name}{$stat} )
497 {
498 0 $stat_value = ( $stat_value - $proc_cache->{$cache_name}{$stat} ) / $interval;
499 ▶ } elsif ( defined( $proc_cache->{$cache_name} )
Condition0%0/3
defined $proc_cache->{$cache_name} and defined $proc_cache->{$cache_name}{$stat}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $proc_cache->{$cache_name} and defined $proc_cache->{$cache_name}{$stat}
defined $proc_cache->{$cache_name} defined $proc_cache->{$cache_name}{$stat}
Truth table0%0/3
A and B
A= defined $proc_cache->{$cache_name}B= defined $proc_cache->{$cache_name}{$stat}
ABresult
0X0
100
111
500 && defined( $proc_cache->{$cache_name}{$stat} ) )
501 {
502 # the counter went backwards (reset/PID reuse), so the raw
503 # value is what has accrued since then
504 0 $stat_value = $stat_value / $interval;
505 ▶ } elsif ( looks_like_number( $proc->{'elapsed-times'} )
Condition0%0/3
&looks_like_number($proc->{"elapsed-times"}) and $proc->{"elapsed-times"} <= $interval
!ll&&!rl&&r
000
MC/DC0%0/2
&looks_like_number($proc->{"elapsed-times"}) and $proc->{"elapsed-times"} <= $interval
&looks_like_number($proc->{"elapsed-times"}) $proc->{"elapsed-times"} <= $interval
Truth table0%0/3
A and B
A= &looks_like_number($proc->{"elapsed-times"})B= $proc->{"elapsed-times"} <= $interval
ABresult
0X0
100
111
506 && $proc->{'elapsed-times'} <= $interval )
507 {
508 # no cached previous value, but the proc is younger than the
509 # polling interval, so the total accrued is from this interval
510 0 $stat_value = $stat_value / $interval;
511 } else {
512 # a old proc with no cached previous value, so zero it to
513 # avoid a spike from the accumulated total... the raw values
514 # cached below provide the delta for the next run
515 0 $stat_value = 0;
516 }
517 $data->{oslvms}{$jail}{$stat}
518 0 = $data->{oslvms}{$jail}{$stat} + $stat_value;
519 0 $data->{totals}{$stat} = $data->{totals}{$stat} + $stat_value;
520 } else {
521 $data->{oslvms}{$jail}{$stat}
522 0 = $data->{oslvms}{$jail}{$stat} + $stat_value;
523 0 $data->{totals}{$stat} = $data->{totals}{$stat} + $stat_value;
524 }
525 } else {
526 0 warn( '"' . $stat_value . '" for ' . $stat . ' does not appear numeric' );
527 }
528 } ## end foreach my $stat (@stats)
529  
530 0 $data->{oslvms}{$jail}{procs}++;
531 0 $data->{totals}{procs}++;
532  
533 0 $new_proc_cache->{$cache_name} = $proc;
534 } ## end if ( $proc->{'elapsed-times'} ne '-' )
535 } ## end foreach my $proc ( @{ $ps->{'process-information'...}})
536 } else {
537 # commonly the jail went away between jls and ps
538 0 push( @{ $data->{errors} }, 'decoding output from "ps" for jail "' . $jail . '" failed... ' . $@ );
539 }
540 } ## end foreach my $jail (@found_jails)
541  
542 # save the proc cache for next run
543 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
544 warn( 'DEBUG, '
545 . join( '.', gettimeofday )
546 . ': backend run writing proc cache to "'
547 . $self->{proc_cache}
548 0 . '"' );
549 }
550 # written atomically so an interrupted write cannot leave behind a
551 # truncated/empty cache file
552 0 eval { write_file( $self->{proc_cache}, { atomic => 1 }, encode_json($new_proc_cache) ); };
553 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
554 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
555 warn( 'DEBUG, '
556 . join( '.', gettimeofday )
557 . ': backend run errored writing proc cache to "'
558 0 . $self->{proc_cache} . '"... '
559 . $@ );
560 }
561 0 push( @{ $data->{errors} }, 'saving proc cache failed, "' . $self->{proc_cache} . '"... ' . $@ );
562 } ## end if ($@)
563  
564 0 ▶ if ($cache_is_new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache_is_new) 0 0 0
565 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
566 0 warn( 'DEBUG, '
567 . join( '.', gettimeofday )
568 . ': backend run cache is new so not returning any oslvm info and zeroing totals' );
569 }
570 0 delete( $data->{oslvms} );
571 0 $data->{oslvms} = {};
572 0 my @total_keys = keys( %{ $data->{totals} } );
573 0 foreach my $total_key (@total_keys) {
574 0 ▶ if ( ref( $data->{totals}{$total_key} ) eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data->{"totals"}{$total_key} eq "") 0 0 0
575 0 $data->{totals}{$total_key} = 0;
576 }
577 }
578 } ## end if ($cache_is_new)
579  
580 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
581 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': backend run done' );
582 }
583  
584 0 return $data;
585 } ## end sub run
586  
587 =head2 usable
588  
589 Dies if not usable.
590  
591 eval{ $backend->usable; };
592 if ( $@ ){
593 print 'Not usable because... '.$@."\n";
594 }
595  
596 =cut
597  
598 1 ▶sub usable {
Subroutine usable: called CC=3 SCAR=16.7
Pod documented
599 1 my $self = $_[0];
600  
601 # make sure it is freebsd
602 1 ▶ if ( $^O !~ 'freebsd' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($^O =~ /freebsd/) 1 0 1
603 1 die '$^O is "' . $^O . '" and not "freebsd"';
604 }
605  
606 # make sure we can locate jls
607 0 my $cmd_bin = `/bin/sh -c 'which jls 2> /dev/null'`;
608 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
609 0 die 'The command "jls" is not in the path... ' . $ENV{PATH};
610 }
611  
612 0 return 1;
613 } ## end sub usable
614  
615 =head1 AUTHOR
616  
617 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
618  
619 =head1 BUGS
620  
621 Please report any bugs or feature requests to C<bug-oslv-monitor at rt.cpan.org>, or through
622 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=OSLV-Monitor>. I will be notified, and then you'll
623 automatically be notified of progress on your bug as I make changes.
624  
625  
626  
627  
628 =head1 SUPPORT
629  
630 You can find documentation for this module with the perldoc command.
631  
632 perldoc OSLV::Monitor
633  
634  
635 You can also look for information at:
636  
637 =over 4
638  
639 =item * RT: CPAN's request tracker (report bugs here)
640  
641 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=OSLV-Monitor>
642  
643 =item * CPAN Ratings
644  
645 L<https://cpanratings.perl.org/d/OSLV-Monitor>
646  
647 =item * Search CPAN
648  
649 L<https://metacpan.org/release/OSLV-Monitor>
650  
651 =back
652  
653  
654 =head1 ACKNOWLEDGEMENTS
655  
656  
657 =head1 LICENSE AND COPYRIGHT
658  
659 This software is Copyright (c) 2024 by Zane C. Bowers-Hadley.
660  
661 This is free software, licensed under:
662  
663 The Artistic License 2.0 (GPL Compatible)
664  
665  
666 =cut
667  
668 5 1; # End of OSLV::Monitor
« Monitor/Backends/cgroups.pm ↑ Summary Monitor.pm »