Monitor/Backends/FreeBSD.pm

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