Monitor.pm

filter: Statementstmt 50.3% 66 / 131 Branchbran 29.2% 24 / 82 Conditioncond 83.3% 10 / 12 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 83.3% 10 / 12 Podpod 100% 6 / 6 totaltotal 47.9% 119 / 248 CC 48 SCAR 47.1
CC
48
Cov
44%
include
306.0
new
69.7
load
12.0
CRAP 111.2
« Monitor/Backends/FreeBSD.pm ↑ Summary
1 package OSLV::Monitor;
2  
3 13 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Time::HiRes qw(gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 OSLV::Monitor - OS level virtualization monitoring extend for LibreNMS.
13  
14 =head1 VERSION
15  
16 Version 1.0.7
17  
18 =cut
19  
20 13 our $VERSION = '1.0.7';
21  
22 =head1 SYNOPSIS
23  
24 Quick summary of what the module does.
25  
26 Perhaps a little code snippet.
27  
28 use OSLV::Monitor;
29  
30 my $monitor = OSLV::Monitor->new();
31  
32 eval { $monitor->load; };
33 if ($@) {
34 print encode_json( { version => 1, data => {}, error => 1, errorString => 'load failed... ' . $@ } ) . "\n";
35 exit 1;
36 }
37  
38 my $data = encode_json( $monitor->run );
39  
40 print $data."\n";
41  
42  
43 =head1 METHODS
44  
45 =head2 new
46  
47 Inits the object.
48  
49 One option is taken and that is a hash ref.
50  
51 # init with the cbsd backend
52 my $monitor->new(backend=>'FreeBSD');
53  
54 The keys are list as below.
55  
56 - backend :: The name of the backend to use. If undef, it is choosen based on $^O
57 freebsd :: FreeBSD
58 linux :: cgroups
59  
60 - base_dir :: The dir to use for any caches as well as output.
61 Default :: /var/cache/oslv_monitor
62  
63 - include :: An array of regexps to match names against for seeing if they should
64 be included or not. By default everything is return.
65 - Default :: ['^.+$']
66  
67 - exclude :: An array of regexps to exclude. By default this array is empty. It is
68 processed after the include array.
69 - Default :: []
70  
71 - time_divider :: What to pass to the backend for the time_divider for that if needed.
72 Default :: 1000000
73  
74 - lock :: If true, run takes a exclusive lock on $base_dir/lock so that
75 overlapping runs do not clobber each other's cache. If another run
76 holds it, run returns a error right away. See lock_failed.
77 Default :: 0
78  
79 =cut
80  
81 14 ▶sub new {
Subroutine new: called CC=17 SCAR=42.4
Pod documented
82 14 my ( $blank, %opts ) = @_;
83  
84 14 ▶ if ( !defined( $opts{time_divider} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{'time_divider'}) { } 14 0 14
85 14 $opts{time_divider} = 1000000;
86 } else {
87 0 ▶ if ( !looks_like_number( $opts{time_divider} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($opts{"time_divider"})) 0 0 0
88 0 die('time_divider is not a number');
89 }
90 }
91  
92 14 ▶ if ( !defined( $opts{backend} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"backend"}) 9 5 14
93 9 $opts{backend} = 'FreeBSD';
94 9 ▶ if ( $^O eq 'freebsd' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'freebsd') { } 0 9 9
elsif ($^O eq 'linux') { } 9 0 9
95 0 $opts{backend} = 'FreeBSD';
96 } elsif ( $^O eq 'linux' ) {
97 9 $opts{backend} = 'cgroups';
98 }
99 }
100  
101 14 ▶ if ( !defined( $opts{include} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{'include'}) { } 14 0 14
102 14 my @include = ('^.+$');
103 14 $opts{include} = \@include;
104 } else {
105 0 ▶ if ( !defined( $opts{include}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"include"}[0]) 0 0 0
106 0 $opts{include}[0] = '^.+$';
107 }
108  
109 0 my $int = 0;
110 0 ▶ while ( defined( $opts{include}[$int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"include"}[$int]) 0 0 0
111 0 ▶ if ( ref( $opts{include}[$int] ) ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"include"}[$int] ne "") 0 0 0
112 0 die( 'ref for $opts{include}[' . $int . '] is ' . ref( $opts{include}[$int] ) . ' and not ""' );
113 }
114 0 $int++;
115 }
116 } ## end else [ if ( !defined( $opts{include} ) ) ]
117  
118 14 ▶ if ( !defined( $opts{exclude} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{'exclude'}) { } 14 0 14
119 14 my @exclude;
120 14 $opts{exclude} = \@exclude;
121 } else {
122 0 my $int = 0;
123 0 ▶ while ( defined( $opts{exclude}[$int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"exclude"}[$int]) 0 0 0
124 0 ▶ if ( ref( $opts{exclude}[$int] ) ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"exclude"}[$int] ne "") 0 0 0
125 0 die( 'ref for $opts{exclude}[' . $int . '] is ' . ref( $opts{exclude}[$int] ) . ' and not ""' );
126 }
127 0 $int++;
128 }
129 }
130  
131 14 ▶ if ( !defined( $opts{base_dir} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"base_dir"}) 0 14 14
132 0 $opts{base_dir} = '/var/cache/oslv_monitor';
133 }
134  
135 14 ▶ if ( !-d $opts{base_dir} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $opts{"base_dir"}) 0 14 14
136 0 ▶ mkdir( $opts{base_dir} ) || die( $opts{base_dir} . ' was not a directory and could not be created' );
Branch: 1 branch
BranchTrueFalseTotal
mkdir $opts{"base_dir"} or die $opts{"base_dir"} . ' was not a directory and could not be created' 0 0 0
137 }
138  
139 my $self = {
140 time_divider => $opts{time_divider},
141 version => 1,
142 backend => $opts{backend},
143 base_dir => $opts{base_dir},
144 include => $opts{include},
145 exclude => $opts{exclude},
146 14 ▶ lock => $opts{lock} ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$opts{'lock'} ? : 1 13 14
147 lock_failed => 0,
148 };
149 14 bless $self;
150  
151 14 return $self;
152 } ## end sub new
153  
154 =head2 load
155  
156 This loads the specified backend.
157  
158 eval{ $monitor->load; };
159 if ( $@ ){
160 print "Failed to load the backend... ".$@;
161 }
162  
163 =cut
164  
165 0 ▶sub load {
Subroutine load: not called CC=3 SCAR=24.8
Pod documented
166 0 my $self = $_[0];
167  
168 0 my $loaded = 0;
169  
170 0 my $backend_test;
171 my $usable;
172 my $test_string = '
173 use OSLV::Monitor::Backends::' . $self->{backend} . ';
174 $backend_test=OSLV::Monitor::Backends::'
175 0 . $self->{backend} . '->new(base_dir=>$self->{base_dir}, obj=>$self, time_divider=>$self->{time_divider});
176 $usable=$backend_test->usable;
177 ';
178 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
179 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': test string "' . $test_string . '"' );
180 }
181 0 eval($test_string);
182  
183 0 ▶ if ($usable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usable) { } 0 0 0
184 0 $self->{backend_mod} = $backend_test;
185 0 $loaded = 1;
186 } else {
187 0 die( 'Failed to load backend... ' . $@ );
188 }
189  
190 0 return $loaded;
191 } ## end sub load
192  
193 =head2 run
194  
195 Runs the poller backend and report the results.
196  
197 If nothing is nothing is loaded, load will be called.
198  
199 my $status=$monitor->run;
200  
201 =cut
202  
203 4 ▶sub run {
Subroutine run: called CC=9 SCAR=23.2
Pod documented
204 4 my $self = $_[0];
205  
206 4 ▶ if ( !defined( $self->{backend_mod} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"backend_mod"}) 0 4 4
207 return {
208 version => $self->{version},
209 0 data => {},
210 error => 1,
211 errorString => 'No module loaded',
212 };
213 }
214  
215 # held until $lock_fh goes out of scope when this returns... the lock is on a
216 # dedicated file as the caches are replaced by atomic writes, which would
217 # leave a lock on the cache itself on the old, replaced file
218 4 $self->{lock_failed} = 0;
219 4 my $lock_fh;
220 4 ▶ if ( $self->{lock} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lock"}) 3 1 4
221 3 my $lock_file = $self->{base_dir} . '/lock';
222 3 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 3 3
223 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': locking "' . $lock_file . '"' );
224 }
225 3 my $lock_error;
226 3 ▶ if ( !open( $lock_fh, '>>', $lock_file ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not open($lock_fh, '>>', $lock_file)) { } 0 3 3
elsif (not flock($lock_fh, 6)) { } 1 2 3
227 0 $lock_error = 'Failed to open lock file "' . $lock_file . '"... ' . $!;
228 } elsif ( !flock( $lock_fh, LOCK_EX | LOCK_NB ) ) {
229 1 $lock_error = 'Another run holds the lock on "' . $lock_file . '"';
230 }
231 3 ▶ if ( defined($lock_error) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lock_error) 1 2 3
232 1 $self->{lock_failed} = 1;
233 return {
234 version => $self->{version},
235 1 data => {},
236 error => 1,
237 errorString => $lock_error,
238 };
239 }
240 } ## end if ( $self->{lock} )
241  
242 3 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 3 3
243 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': calling backend run' );
244 }
245  
246 3 my $to_return_data;
247 3 eval { $to_return_data = $self->{backend_mod}->run };
248 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
249 return {
250 version => $self->{version},
251 0 data => {},
252 error => 1,
253 errorString => 'Failed to run backend... ' . $@,
254 };
255 }
256  
257 3 $to_return_data->{backend} = $self->{backend};
258  
259 return {
260 version => $self->{version},
261 3 data => $to_return_data,
262 error => 0,
263 errorString => ''
264 };
265 } ## end sub run
266  
267 =head2 lock_failed
268  
269 Returns 1 if the last call to run returned early because the lock could not be
270 taken, either due to another run holding it or the lock file not being
271 openable, otherwise 0. Always 0 if lock was not enabled.
272  
273 This lets a caller tell a skipped run apart from a failed one, such as to keep
274 the output of the previous run instead of replacing it with the error.
275  
276 my $status = $monitor->run;
277 if ( $monitor->lock_failed ) {
278 exit 1;
279 }
280  
281 =cut
282  
283 4 ▶sub lock_failed {
Subroutine lock_failed: called CC=1 SCAR=0.0
Pod documented
284 4 my $self = $_[0];
285  
286 4 return $self->{lock_failed};
287 }
288  
289 =head2 interval_since
290  
291 Returns the number of seconds between the run that saved a backend's cache and
292 the current run, for turning counter deltas into per second rates.
293  
294 Two args are taken. The first is the decoded cache hash ref, which holds the time
295 of the run that saved it as C<< {__oslv_monitor__}{time} >>. The second is the
296 time of the current run, as returned by L<Time::HiRes/time>.
297  
298 300, the expected cron interval, is returned if the cache is undef, lacks a
299 time, as one written by a older version would, or if the time is not before the
300 current one, as happens if the clock was set backwards.
301  
302 my $run_time = Time::HiRes::time;
303 my $interval = $monitor->interval_since( $cache, $run_time );
304  
305 # 1000 / 300 for a run 5 minutes after the previous
306 my $rate = $counter_delta / $interval;
307  
308 =cut
309  
310 11 ▶sub interval_since {
Subroutine interval_since: called CC=6 SCAR=18.0
Pod documented
311 11 my $self = $_[0];
312 11 my $cache = $_[1];
313 11 my $run_time = $_[2];
314  
315 11 my $interval = 300;
316 11 ▶ if ( ref($cache) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cache eq "HASH" and ref $cache->{"__oslv_monitor__"} eq "HASH" and &looks_like_number($cache->{"__oslv_monitor__"}{"time"}) and &looks_like_number($run_time) and $run_time > $cache->{"__oslv_monitor__"}{"time"}) 3 8 11
Condition83%10/12
ref $cache eq "HASH" and ref $cache->{"__oslv_monitor__"} eq "HASH" and &looks_like_number($cache->{"__oslv_monitor__"}{"time"}) and &looks_like_number($run_time) and $run_time > $cache->{"__oslv_monitor__"}{"time"}
!ll&&!rl&&r
713
ref $cache eq "HASH" and ref $cache->{"__oslv_monitor__"} eq "HASH" and &looks_like_number($cache->{"__oslv_monitor__"}{"time"}) and &looks_like_number($run_time)
!ll&&!rl&&r
704
ref $cache eq "HASH" and ref $cache->{"__oslv_monitor__"} eq "HASH" and &looks_like_number($cache->{"__oslv_monitor__"}{"time"})
!ll&&!rl&&r
704
ref $cache eq "HASH" and ref $cache->{"__oslv_monitor__"} eq "HASH"
!ll&&!rl&&r
254
MC/DC60%3/5
ref $cache eq "HASH" and ref $cache->{"__oslv_monitor__"} eq "HASH" and &looks_like_number($cache->{"__oslv_monitor__"}{"time"}) and &looks_like_number($run_time) and $run_time > $cache->{"__oslv_monitor__"}{"time"}
ref $cache eq "HASH" ref $cache->{"__oslv_monitor__"} eq "HASH" &looks_like_number($cache->{"__oslv_monitor__"}{"time"}) &looks_like_number($run_time) $run_time > $cache->{"__oslv_monitor__"}{"time"}
Truth table66%4/6
A and B and C and D and E
A= ref $cache eq "HASH"B= ref $cache->{"__oslv_monitor__"} eq "HASH"C= &looks_like_number($cache->{"__oslv_monitor__"}{"time"})D= &looks_like_number($run_time)E= $run_time > $cache->{"__oslv_monitor__"}{"time"}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
317 && ref( $cache->{__oslv_monitor__} ) eq 'HASH'
318 && looks_like_number( $cache->{__oslv_monitor__}{time} )
319 && looks_like_number($run_time)
320 && $run_time > $cache->{__oslv_monitor__}{time} )
321 {
322 3 $interval = $run_time - $cache->{__oslv_monitor__}{time};
323 }
324  
325 11 return $interval;
326 } ## end sub interval_since
327  
328 =head2 include
329  
330 Checks if a name should be included, as decided by the include and exclude
331 regexps passed to L</new>. The backends call this for each name found.
332  
333 One arg is taken, the name to check.
334  
335 Returns 1 if the name matches a include and no excludes, otherwise 0. 0 is also
336 returned if the name is undef, a ref, or "".
337  
338 # with include => ['^s_'] and exclude => ['^s_cron$']
339 $monitor->include('s_ssh'); # 1
340 $monitor->include('s_cron'); # 0
341 $monitor->include('u_1000'); # 0
342  
343 =cut
344  
345 0 ▶sub include {
Subroutine include: not called CC=17 SCAR=57.2
Pod documented
346 0 my $self = $_[0];
347 0 my $name = $_[1];
348  
349 # any of these means it is not meaningfully usable or an error
350 0 ▶ if ( !defined($name) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $name) { } 0 0 0
elsif (ref $name ne '') { } 0 0 0
elsif ($name eq '') { } 0 0 0
351 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
352 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': include name is undef returning 0' );
353 }
354 0 return 0;
355 } elsif ( ref($name) ne '' ) {
356 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
357 0 warn( 'DEBUG, '
358 . join( '.', gettimeofday )
359 . ': include name ref is not "" but "'
360 . ref($name)
361 . '" returning 0' );
362 }
363 0 return 0;
364 } elsif ( $name eq '' ) {
365 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
366 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': include name is "" returning 0' );
367 }
368 0 return 0;
369 }
370  
371 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
372 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': include name being tested is "' . $name . '"' );
373 }
374  
375 # look for mathcing includes
376 0 foreach my $item ( @{ $self->{include} } ) {
377 # check if it matches
378 0 ▶ if ( $name =~ /$item/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ /$item/) { } 0 0 0
elsif ($ENV{'OSLV_MONITOR_DEBUG'}) { } 0 0 0
379 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
380 0 warn( 'DEBUG, '
381 . join( '.', gettimeofday )
382 . ': include name matched include /'
383 . $item
384 . '/ testing for exlcudes now' );
385 }
386 # if we got a match check for excludes
387 0 foreach my $item ( @{ $self->{exclude} } ) {
388 0 ▶ if ( $name =~ /$item/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ /$item/) { } 0 0 0
elsif ($ENV{'OSLV_MONITOR_DEBUG'}) { } 0 0 0
389 0 return 0;
390 } elsif ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
391 0 warn( 'DEBUG, '
392 . join( '.', gettimeofday )
393 . ': include name did not match exclude /'
394 . $item
395 . '/' );
396 }
397 } ## end foreach my $item ( @{ $self->{exclude} } )
398 # if we get here it should means a include matched and no excludes matched
399 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
400 0 warn( 'DEBUG, '
401 . join( '.', gettimeofday )
402 . ': include name matched a include and no exlcudes returning 1' );
403 }
404 0 return 1;
405 } elsif ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
406 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': include name did not match include /' . $item . '/' );
407 }
408  
409 } ## end foreach my $item ( @{ $self->{include} } )
410  
411 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
412 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': include name did not match any includes' );
413 }
414  
415 # if we get here it should mean no includes matched
416 0 return 0;
417 } ## end sub include
418  
419 =head1 AUTHOR
420  
421 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
422  
423 =head1 BUGS
424  
425 Please report any bugs or feature requests to C<bug-oslv-monitor at rt.cpan.org>, or through
426 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=OSLV-Monitor>. I will be notified, and then you'll
427 automatically be notified of progress on your bug as I make changes.
428  
429  
430  
431  
432 =head1 SUPPORT
433  
434 You can find documentation for this module with the perldoc command.
435  
436 perldoc OSLV::Monitor
437  
438  
439 You can also look for information at:
440  
441 =over 4
442  
443 =item * RT: CPAN's request tracker (report bugs here)
444  
445 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=OSLV-Monitor>
446  
447 =item * CPAN Ratings
448  
449 L<https://cpanratings.perl.org/d/OSLV-Monitor>
450  
451 =item * Search CPAN
452  
453 L<https://metacpan.org/release/OSLV-Monitor>
454  
455 =back
456  
457  
458 =head1 ACKNOWLEDGEMENTS
459  
460  
461 =head1 LICENSE AND COPYRIGHT
462  
463 This software is Copyright (c) 2024 by Zane C. Bowers-Hadley.
464  
465 This is free software, licensed under:
466  
467 The Artistic License 2.0 (GPL Compatible)
468  
469  
470 =cut
471  
472 13 1; # End of OSLV::Monitor
« Monitor/Backends/FreeBSD.pm ↑ Summary