File Coverage

blib/lib/OSLV/Monitor.pm
Criterion Covered Total %
statement 11 80 13.7
branch 0 42 0.0
condition n/a
subroutine 4 8 50.0
pod 2 4 50.0
total 17 134 12.6


line stmt bran cond sub pod time code
1             package OSLV::Monitor;
2              
3 1     1   122312 use 5.006;
  1         5  
4 1     1   11 use strict;
  1         2  
  1         37  
5 1     1   7 use warnings;
  1         2  
  1         109  
6 1     1   25 use Scalar::Util qw(looks_like_number);
  1         3  
  1         1112  
7              
8             =head1 NAME
9              
10             OSLV::Monitor - OS level virtualization monitoring extend for LibreNMS.
11              
12             =head1 VERSION
13              
14             Version 1.0.2
15              
16             =cut
17              
18             our $VERSION = '1.0.2';
19              
20             =head1 SYNOPSIS
21              
22             Quick summary of what the module does.
23              
24             Perhaps a little code snippet.
25              
26             use OSLV::Monitor;
27              
28             my $monitor = OSLV::Monitor->new();
29              
30             eval { $monitor->load; };
31             if ($@) {
32             print encode_json( { version => 1, data => {}, error => 1, errorString => 'load failed... ' . $@ } ) . "\n";
33             exit 1;
34             }
35              
36             my $data = encode_json( $monitor->run );
37              
38             print $data."\n";
39              
40              
41             =head2 METHODS
42              
43             Inits the object.
44              
45             One option is taken and that is a hash ref.
46              
47             # init with the cbsd backend
48             my $monitor->new(backend=>'FreeBSD');
49              
50             The keys are list as below.
51              
52             - backend :: The name of the backend to use. If undef, it is choosen based on $^O
53             freebsd :: FreeBSD
54             linux :: cgroups
55              
56             - base_dir :: The dir to use for any caches as well as output.
57             Default :: /var/cache/oslv_monitor
58              
59             - include :: An array of regexps to match names against for seeing if they should
60             be included or not. By default everything is return.
61             - Default :: ['^.*$']
62              
63             - exclude :: An array of regexps to exclude. By default this array is empty. It is
64             processed after the include array.
65             - Default :: []
66              
67             - time_divider :: What to pass to the backend for the time_divider for that if needed.
68             Default :: 1000000
69              
70             =cut
71              
72             sub new {
73 0     0 0   my ( $blank, %opts ) = @_;
74              
75 0 0         if ( !defined( $opts{time_divider} ) ) {
76 0           $opts{time_divider} = 1000000;
77             } else {
78 0 0         if ( ! looks_like_number( $opts{time_divider} ) ) {
79 0           die('time_divider is not a number');
80             }
81             }
82              
83 0 0         if ( !defined( $opts{backend} ) ) {
84 0           $opts{backend} = 'FreeBSD';
85 0 0         if ( $^O eq 'freebsd' ) {
    0          
86 0           $opts{backend} = 'FreeBSD';
87             } elsif ( $^O eq 'linux' ) {
88 0           $opts{backend} = 'cgroups';
89             }
90             }
91              
92 0 0         if ( !defined( $opts{include} ) ) {
93 0           my @include = ('^.+$');
94 0           $opts{include} = \@include;
95             } else {
96 0 0         if ( !defined( $opts{include}[0] ) ) {
97 0           $opts{include}[0] = '^.+$';
98             }
99              
100 0           my $int = 0;
101 0           while ( defined( $opts{include}[$int] ) ) {
102 0 0         if ( ref( $opts{include}[$int] ) ne '' ) {
103 0           die( 'ref for $opts{include}[' . $int . '] is ' . ref( $opts{include}[$int] ) . ' and not ""' );
104             }
105 0           $int++;
106             }
107             } ## end else [ if ( !defined( $opts{include} ) ) ]
108              
109 0 0         if ( !defined( $opts{exclude} ) ) {
110 0           my @exclude;
111 0           $opts{exclude} = \@exclude;
112             } else {
113 0           my $int = 0;
114 0           while ( defined( $opts{exclude}[$int] ) ) {
115 0 0         if ( ref( $opts{exclude}[$int] ) ne '' ) {
116 0           die( 'ref for $opts{exclude}[' . $int . '] is ' . ref( $opts{exclude}[$int] ) . ' and not ""' );
117             }
118 0           $int++;
119             }
120             }
121              
122 0 0         if ( !defined( $opts{base_dir} ) ) {
123 0           $opts{base_dir} = '/var/cache/oslv_monitor';
124             }
125              
126 0 0         if ( !-d $opts{base_dir} ) {
127 0 0         mkdir( $opts{base_dir} ) || die( $opts{base_dir} . ' was not a directory and could not be created' );
128             }
129              
130             my $self = {
131             time_divider => $opts{time_divider},
132             version => 1,
133             backend => $opts{backend},
134             base_dir => $opts{base_dir},
135             include => $opts{include},
136             exclude => $opts{exclude},
137 0           };
138 0           bless $self;
139              
140 0           return $self;
141             } ## end sub new
142              
143             =head2 load
144              
145             This loads the specified backend.
146              
147             eval{ $monitor->load; };
148             if ( $@ ){
149             print "Failed to load the backend... ".$@;
150             }
151              
152             =cut
153              
154             sub load {
155 0     0 1   my $self = $_[0];
156              
157 0           my $loaded = 0;
158              
159 0           my $backend_test;
160             my $usable;
161             my $test_string = '
162             use OSLV::Monitor::Backends::' . $self->{backend} . ';
163             $backend_test=OSLV::Monitor::Backends::'
164             . $self->{backend}
165 0           . '->new(base_dir=>$self->{base_dir}, obj=>$self, time_divider=>$self->{time_divider});
166             $usable=$backend_test->usable;
167             ';
168 0           eval($test_string);
169              
170 0 0         if ($usable) {
171 0           $self->{backend_mod} = $backend_test;
172 0           $loaded = 1;
173             } else {
174 0           die( 'Failed to load backend... ' . $@ );
175             }
176              
177 0           return $loaded;
178             } ## end sub load
179              
180             =head2 run
181              
182             Runs the poller backend and report the results.
183              
184             If nothing is nothing is loaded, load will be called.
185              
186             my $status=$monitor->run;
187              
188             =cut
189              
190             sub run {
191 0     0 1   my $self = $_[0];
192              
193 0 0         if ( !defined( $self->{backend_mod} ) ) {
194             return {
195             version => $self->{version},
196 0           data => {},
197             error => 1,
198             errorString => 'No module loaded',
199             };
200             }
201              
202 0           my $to_return_data;
203             # eval { $to_return_data = $self->{backend_mod}->run };
204 0           $to_return_data = $self->{backend_mod}->run;
205 0 0         if ($@) {
206             return {
207             version => $self->{version},
208 0           data => {},
209             error => 1,
210             errorString => 'Failed to run backend... ' . $@,
211             };
212             }
213              
214 0           $to_return_data->{backend} = $self->{backend};
215              
216             return {
217             version => $self->{version},
218 0           data => $to_return_data,
219             error => 0,
220             errorString => ''
221             };
222             } ## end sub run
223              
224             sub include {
225 0     0 0   my $self = $_[0];
226 0           my $name = $_[1];
227              
228             # return undef if any of these are true
229 0 0         if ( !defined($name) ) {
    0          
    0          
230 0           return 0;
231             } elsif ( ref($name) ne '' ) {
232 0           return 0;
233             } elsif ( $name eq '' ) {
234 0           return 0;
235             }
236              
237             # look for mathcing includes
238 0           foreach my $item ( @{ $self->{include} } ) {
  0            
239             # check if it matches
240 0 0         if ( $name =~ /$item/ ) {
241             # if we got a match check for excludes
242 0           foreach my $item ( @{ $self->{exclude} } ) {
  0            
243 0 0         if ( $name =~ /$item/ ) {
244 0           return 0;
245             }
246             }
247             # if we get here it should means a include matched and no excludes matched
248 0           return 1;
249             } ## end if ( $name =~ /$item/ )
250             } ## end foreach my $item ( @{ $self->{include} } )
251              
252             # if we get here it should mean no includes matched
253 0           return 0;
254             } ## end sub include
255              
256             =head1 AUTHOR
257              
258             Zane C. Bowers-Hadley, C<< >>
259              
260             =head1 BUGS
261              
262             Please report any bugs or feature requests to C, or through
263             the web interface at L. I will be notified, and then you'll
264             automatically be notified of progress on your bug as I make changes.
265              
266              
267              
268              
269             =head1 SUPPORT
270              
271             You can find documentation for this module with the perldoc command.
272              
273             perldoc OSLV::Monitor
274              
275              
276             You can also look for information at:
277              
278             =over 4
279              
280             =item * RT: CPAN's request tracker (report bugs here)
281              
282             L
283              
284             =item * CPAN Ratings
285              
286             L
287              
288             =item * Search CPAN
289              
290             L
291              
292             =back
293              
294              
295             =head1 ACKNOWLEDGEMENTS
296              
297              
298             =head1 LICENSE AND COPYRIGHT
299              
300             This software is Copyright (c) 2024 by Zane C. Bowers-Hadley.
301              
302             This is free software, licensed under:
303              
304             The Artistic License 2.0 (GPL Compatible)
305              
306              
307             =cut
308              
309             1; # End of OSLV::Monitor