Monitor.pm

filter: Statementstmt 32.0% 34 / 106 Branchbran 11.7% 8 / 68 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 6 / 9 Podpod 50.0% 2 / 4 totaltotal 26.7% 50 / 187 CC 37 SCAR 49.7
CC
37
Cov
24%
include
306.0
new
71.3
run
20.0
CRAP 143.6
« Monitor/Backends/FreeBSD.pm ↑ Summary
1 package OSLV::Monitor;
2  
3 7 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Time::HiRes qw(gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 OSLV::Monitor - OS level virtualization monitoring extend for LibreNMS.
12  
13 =head1 VERSION
14  
15 Version 1.0.6
16  
17 =cut
18  
19 7 our $VERSION = '1.0.6';
20  
21 =head1 SYNOPSIS
22  
23 Quick summary of what the module does.
24  
25 Perhaps a little code snippet.
26  
27 use OSLV::Monitor;
28  
29 my $monitor = OSLV::Monitor->new();
30  
31 eval { $monitor->load; };
32 if ($@) {
33 print encode_json( { version => 1, data => {}, error => 1, errorString => 'load failed... ' . $@ } ) . "\n";
34 exit 1;
35 }
36  
37 my $data = encode_json( $monitor->run );
38  
39 print $data."\n";
40  
41  
42 =head2 METHODS
43  
44 Inits the object.
45  
46 One option is taken and that is a hash ref.
47  
48 # init with the cbsd backend
49 my $monitor->new(backend=>'FreeBSD');
50  
51 The keys are list as below.
52  
53 - backend :: The name of the backend to use. If undef, it is choosen based on $^O
54 freebsd :: FreeBSD
55 linux :: cgroups
56  
57 - base_dir :: The dir to use for any caches as well as output.
58 Default :: /var/cache/oslv_monitor
59  
60 - include :: An array of regexps to match names against for seeing if they should
61 be included or not. By default everything is return.
62 - Default :: ['^.*$']
63  
64 - exclude :: An array of regexps to exclude. By default this array is empty. It is
65 processed after the include array.
66 - Default :: []
67  
68 - time_divider :: What to pass to the backend for the time_divider for that if needed.
69 Default :: 1000000
70  
71 =cut
72  
73 6 ▶sub new {
Subroutine new: called CC=16 SCAR=42.7
Pod undocumented
74 6 my ( $blank, %opts ) = @_;
75  
76 6 ▶ if ( !defined( $opts{time_divider} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{'time_divider'}) { } 6 0 6
77 6 $opts{time_divider} = 1000000;
78 } else {
79 0 ▶ if ( !looks_like_number( $opts{time_divider} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($opts{"time_divider"})) 0 0 0
80 0 die('time_divider is not a number');
81 }
82 }
83  
84 6 ▶ if ( !defined( $opts{backend} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"backend"}) 6 0 6
85 6 $opts{backend} = 'FreeBSD';
86 6 ▶ if ( $^O eq 'freebsd' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'freebsd') { } 0 6 6
elsif ($^O eq 'linux') { } 6 0 6
87 0 $opts{backend} = 'FreeBSD';
88 } elsif ( $^O eq 'linux' ) {
89 6 $opts{backend} = 'cgroups';
90 }
91 }
92  
93 6 ▶ if ( !defined( $opts{include} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{'include'}) { } 6 0 6
94 6 my @include = ('^.+$');
95 6 $opts{include} = \@include;
96 } else {
97 0 ▶ if ( !defined( $opts{include}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"include"}[0]) 0 0 0
98 0 $opts{include}[0] = '^.+$';
99 }
100  
101 0 my $int = 0;
102 0 ▶ while ( defined( $opts{include}[$int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"include"}[$int]) 0 0 0
103 0 ▶ if ( ref( $opts{include}[$int] ) ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"include"}[$int] ne "") 0 0 0
104 0 die( 'ref for $opts{include}[' . $int . '] is ' . ref( $opts{include}[$int] ) . ' and not ""' );
105 }
106 0 $int++;
107 }
108 } ## end else [ if ( !defined( $opts{include} ) ) ]
109  
110 6 ▶ if ( !defined( $opts{exclude} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{'exclude'}) { } 6 0 6
111 6 my @exclude;
112 6 $opts{exclude} = \@exclude;
113 } else {
114 0 my $int = 0;
115 0 ▶ while ( defined( $opts{exclude}[$int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"exclude"}[$int]) 0 0 0
116 0 ▶ if ( ref( $opts{exclude}[$int] ) ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"exclude"}[$int] ne "") 0 0 0
117 0 die( 'ref for $opts{exclude}[' . $int . '] is ' . ref( $opts{exclude}[$int] ) . ' and not ""' );
118 }
119 0 $int++;
120 }
121 }
122  
123 6 ▶ if ( !defined( $opts{base_dir} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"base_dir"}) 0 6 6
124 0 $opts{base_dir} = '/var/cache/oslv_monitor';
125 }
126  
127 6 ▶ if ( !-d $opts{base_dir} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $opts{"base_dir"}) 0 6 6
128 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
129 }
130  
131 my $self = {
132 time_divider => $opts{time_divider},
133 version => 1,
134 backend => $opts{backend},
135 base_dir => $opts{base_dir},
136 include => $opts{include},
137 exclude => $opts{exclude},
138 6 };
139 6 bless $self;
140  
141 6 return $self;
142 } ## end sub new
143  
144 =head2 load
145  
146 This loads the specified backend.
147  
148 eval{ $monitor->load; };
149 if ( $@ ){
150 print "Failed to load the backend... ".$@;
151 }
152  
153 =cut
154  
155 0 ▶sub load {
Subroutine load: not called CC=3 SCAR=24.8
Pod documented
156 0 my $self = $_[0];
157  
158 0 my $loaded = 0;
159  
160 0 my $backend_test;
161 my $usable;
162 my $test_string = '
163 use OSLV::Monitor::Backends::' . $self->{backend} . ';
164 $backend_test=OSLV::Monitor::Backends::'
165 0 . $self->{backend} . '->new(base_dir=>$self->{base_dir}, obj=>$self, time_divider=>$self->{time_divider});
166 $usable=$backend_test->usable;
167 ';
168 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
169 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': test string "' . $test_string . '"' );
170 }
171 0 eval($test_string);
172  
173 0 ▶ if ($usable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usable) { } 0 0 0
174 0 $self->{backend_mod} = $backend_test;
175 0 $loaded = 1;
176 } else {
177 0 die( 'Failed to load backend... ' . $@ );
178 }
179  
180 0 return $loaded;
181 } ## end sub load
182  
183 =head2 run
184  
185 Runs the poller backend and report the results.
186  
187 If nothing is nothing is loaded, load will be called.
188  
189 my $status=$monitor->run;
190  
191 =cut
192  
193 0 ▶sub run {
Subroutine run: not called CC=4 SCAR=30.0
Pod documented
194 0 my $self = $_[0];
195  
196 0 ▶ if ( !defined( $self->{backend_mod} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"backend_mod"}) 0 0 0
197 return {
198 version => $self->{version},
199 0 data => {},
200 error => 1,
201 errorString => 'No module loaded',
202 };
203 }
204  
205 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
206 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': calling backend run' );
207 }
208  
209 0 my $to_return_data;
210 0 eval { $to_return_data = $self->{backend_mod}->run };
211 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
212 return {
213 version => $self->{version},
214 0 data => {},
215 error => 1,
216 errorString => 'Failed to run backend... ' . $@,
217 };
218 }
219  
220 0 $to_return_data->{backend} = $self->{backend};
221  
222 return {
223 version => $self->{version},
224 0 data => $to_return_data,
225 error => 0,
226 errorString => ''
227 };
228 } ## end sub run
229  
230 0 ▶sub include {
Subroutine include: not called CC=17 SCAR=57.2
Pod undocumented
231 0 my $self = $_[0];
232 0 my $name = $_[1];
233  
234 # any of these means it is not meaningfully usable or an error
235 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
236 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
237 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': include name is undef returning 0' );
238 }
239 0 return 0;
240 } elsif ( ref($name) ne '' ) {
241 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
242 0 warn( 'DEBUG, '
243 . join( '.', gettimeofday )
244 . ': include name ref is not "" but "'
245 . ref($name)
246 . '" returning 0' );
247 }
248 0 return 0;
249 } elsif ( $name eq '' ) {
250 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
251 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': include name is "" returning 0' );
252 }
253 0 return 0;
254 }
255  
256 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
257 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': include name being tested is "' . $name . '"' );
258 }
259  
260 # look for mathcing includes
261 0 foreach my $item ( @{ $self->{include} } ) {
262 # check if it matches
263 0 ▶ if ( $name =~ /$item/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ /$item/) { } 0 0 0
elsif ($ENV{'OSLV_MONITOR_DEBUG'}) { } 0 0 0
264 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
265 0 warn( 'DEBUG, '
266 . join( '.', gettimeofday )
267 . ': include name matched include /'
268 . $item
269 . '/ testing for exlcudes now' );
270 }
271 # if we got a match check for excludes
272 0 foreach my $item ( @{ $self->{exclude} } ) {
273 0 ▶ if ( $name =~ /$item/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ /$item/) { } 0 0 0
elsif ($ENV{'OSLV_MONITOR_DEBUG'}) { } 0 0 0
274 0 return 0;
275 } elsif ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
276 0 warn( 'DEBUG, '
277 . join( '.', gettimeofday )
278 . ': include name did not match exclude /'
279 . $item
280 . '/' );
281 }
282 } ## end foreach my $item ( @{ $self->{exclude} } )
283 # if we get here it should means a include matched and no excludes matched
284 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
285 0 warn( 'DEBUG, '
286 . join( '.', gettimeofday )
287 . ': include name matched a include and no exlcudes returning 1' );
288 }
289 0 return 1;
290 } elsif ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
291 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': include name did not match include /' . $item . '/' );
292 }
293  
294 } ## end foreach my $item ( @{ $self->{include} } )
295  
296 0 ▶ if ( $ENV{'OSLV_MONITOR_DEBUG'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OSLV_MONITOR_DEBUG"}) 0 0 0
297 0 warn( 'DEBUG, ' . join( '.', gettimeofday ) . ': include name did not match any includes' );
298 }
299  
300 # if we get here it should mean no includes matched
301 0 return 0;
302 } ## end sub include
303  
304 =head1 AUTHOR
305  
306 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
307  
308 =head1 BUGS
309  
310 Please report any bugs or feature requests to C<bug-oslv-monitor at rt.cpan.org>, or through
311 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=OSLV-Monitor>. I will be notified, and then you'll
312 automatically be notified of progress on your bug as I make changes.
313  
314  
315  
316  
317 =head1 SUPPORT
318  
319 You can find documentation for this module with the perldoc command.
320  
321 perldoc OSLV::Monitor
322  
323  
324 You can also look for information at:
325  
326 =over 4
327  
328 =item * RT: CPAN's request tracker (report bugs here)
329  
330 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=OSLV-Monitor>
331  
332 =item * CPAN Ratings
333  
334 L<https://cpanratings.perl.org/d/OSLV-Monitor>
335  
336 =item * Search CPAN
337  
338 L<https://metacpan.org/release/OSLV-Monitor>
339  
340 =back
341  
342  
343 =head1 ACKNOWLEDGEMENTS
344  
345  
346 =head1 LICENSE AND COPYRIGHT
347  
348 This software is Copyright (c) 2024 by Zane C. Bowers-Hadley.
349  
350 This is free software, licensed under:
351  
352 The Artistic License 2.0 (GPL Compatible)
353  
354  
355 =cut
356  
357 7 1; # End of OSLV::Monitor
« Monitor/Backends/FreeBSD.pm ↑ Summary