Monitor/Backends/CBSD.pm untested

filter: Statementstmt 0.0% 0 / 186 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 264 CC 28 SCAR 63.2
CC
28
Cov
0%
run
702.0
usable
12.0
new
2.0
CRAP 554.4
1 package HV::Monitor::Backends::CBSD;
2  
3 use 5.006;
4 use strict;
5 use warnings;
6  
7 =head1 NAME
8  
9 HV::Monitor::Backends::CBSD - CBSD support for HV::Monitor
10  
11 =head1 VERSION
12  
13 Version 0.0.3
14  
15 =cut
16  
17 our $VERSION = '0.0.3';
18  
19 =head1 SYNOPSIS
20  
21 use HV::MOnitor::Backend::CBSD;
22
23 my $backend=HV::MOnitor::Backend::CBSD->new;
24
25 my $usable=$backend->usable;
26 if ( $usable ){
27 $return_hash_ref=$backend->run;
28 }
29  
30 =head1 METHODS
31  
32 =head2 new
33  
34 Initiates the backend object.
35  
36 my $backend=HV::MOnitor::Backend::CBSD->new;
37  
38 =cut
39  
40 sub new {
41 my $self = { version => 1, };
42 bless $self;
43  
44 return $self;
45 }
46  
47 =head2 run
48  
49 $return_hash_ref=$backend->run;
50  
51 =cut
52  
53 sub run {
54 my $self = $_[0];
55  
56 my $bls_raw
57 = `/bin/sh -c "cbsd bls header=0 node= display=jname,jid,vm_ram,vm_curmem,vm_cpus,pcpu,vm_os_type,ip4_addr,status,vnc alljails=0 2> /dev/null" | sed -e 's/\x1b\[[0-9;]*m//g'`;
58 if ( $? != 0 ) {
59 return {
60 data => { hv => 'cbsd' },
61 version => $self->{version},
62 error => 2,
63 errorString =>
64 '"cbsd bls header=0 node= display=jname,jid,vm_ram,vm_curmem,vm_cpus,pcpu,vm_os_type,ip4_addr,status,vnc alljails=0" exited non-zero',
65 };
66 }
67  
68 # get the zfs stats
69 my @zfs_stats = split( /\n/, `sysctl kstat.zfs` );
70  
71 # break down the ZFS and find likely disks
72 my @zfs_list = split( /\n/, `zfs list -p` );
73 my $zfs_list_int = 1;
74 my $zfs = {};
75 my @zfs_keys;
76 foreach my $line (@zfs_list) {
77 chomp($line);
78 my ( $zfs_name, $zfs_used, $zfs_avail, $zfs_refer, $zfs_mount ) = split( /[\ \t]+/, $line, 5 );
79  
80 # make sure it is not mounted and that it ends in raw or vhd
81 if ( $zfs_mount =~ /^\-$/
82 && $zfs_name =~ /[A-Za-z\-\_1-9]+\/[A-Za-z\-\_1-9]+\/[A-Za-z\-\_1-9]+\.[VvRr][HhAa][DdWw]$/ )
83 {
84 $zfs->{$zfs_name} = $zfs_used;
85 push( @zfs_keys, $zfs_name );
86 }
87 $zfs_list_int++;
88 }
89  
90 my $disk_list_raw = `cbsd bhyve-dsk-list header=0 display=jname,dsk_path,dsk_size | sed -e 's/\x1b\[[0-9;]*m//g'`;
91  
92 #remove color codes
93 $bls_raw =~ s/\^.{1,7}?m//g;
94 $disk_list_raw =~ s/\^.{1,7}?m//g;
95  
96 my @disk_list = split( /\n/, $disk_list_raw );
97  
98 my @VMs;
99  
100 my $ifs_raw = `ifconfig | grep '^[A-Za-z]' | cut -d: -f 1`;
101 my @ifs = split( /\n/, $ifs_raw );
102  
103 my $return_hash = {
104 VMs => {},
105 hv => 'CBSD',
106 totals => {
107 'usertime' => 0,
108 'pmem' => 0,
109 'oublk' => 0,
110 'minflt' => 0,
111 'pcpu' => 0,
112 'mem_alloc' => 0,
113 'nvcsw' => 0,
114 'snaps' => 0,
115 'rss' => 0,
116 'snaps_size' => 0,
117 'cpus' => 0,
118 'cow' => 0,
119 'nivcsw' => 0,
120 'systime' => 0,
121 'vsz' => 0,
122 'etimes' => 0,
123 'majflt' => 0,
124 'inblk' => 0,
125 'nswap' => 0,
126 'on' => 0,
127 'off' => 0,
128 'off_hard' => 0,
129 'off_soft' => 0,
130 'unknown' => 0,
131 'paused' => 0,
132 'crashed' => 0,
133 'blocked' => 0,
134 'nostate' => 0,
135 'pmsuspended' => 0,
136 'freqs' => 0,
137 'ftime' => 0,
138 'ipkts' => 0,
139 'ierrs' => 0,
140 'ibytes' => 0,
141 'idrop' => 0,
142 'opkts' => 0,
143 'oerrs' => 0,
144 'obytes' => 0,
145 'coll' => 0,
146 'odrop' => 0,
147 }
148 };
149  
150 # values that should be totaled
151 my @total = (
152 'usertime', 'pmem', 'oublk', 'minflt', 'pcpu', 'mem_alloc',
153 'nvcsw', 'snaps', 'rss', 'snaps_size', 'cpus', 'cow',
154 'nivcsw', 'systime', 'vsz', 'etimes', 'majflt', 'inblk',
155 'nswap', 'disk_alloc', 'disk_in_use', 'rbytes', 'rtime', 'rreqs',
156 'wbytes', 'wreqs', 'ftime', 'freqs', 'wtime', 'disk_on_disk',
157 'snaps', 'freqs', 'ftime', 'ipkts', 'ierrs', 'ibytes',
158 'idrop', 'opkts', 'oerrs', 'obytes', 'coll', 'odrop'
159 );
160  
161 my @bls_split = split( /\n/, $bls_raw );
162 foreach my $line (@bls_split) {
163 chomp($line);
164 my ( $vm, $pid, $mem_alloc, $mem_use, $cpus, $pcpu, $vm_os_type, $ip, $status, $vnc )
165 = split( /[\ \t]+/, $line );
166  
167 # The ones below are linux only, so just zeroing here.
168 # syscw syscw rchar wchar rbytes wbytes cwbytes
169 my $vm_info = {
170 mem_alloc => $mem_alloc,
171 cpus => $cpus,
172 pcpu => $pcpu,
173 os_type => $vm_os_type,
174 ip => $ip,
175 console_type => 'vnc',
176 console => $vnc,
177 snaps_size => 0,
178 snaps => 0,
179 ifs => {},
180 rbytes => 0,
181 wbytes => 0,
182 etimes => 0,
183 pmem => 0,
184 cow => 0,
185 majflt => 0,
186 minflt => 0,
187 nice => 0,
188 nivcsw => 0,
189 nswap => 0,
190 nvcsw => 0,
191 inblk => 0,
192 oublk => 0,
193 pri => 0,
194 rss => 0,
195 systime => 0,
196 usertime => 0,
197 vsz => 0,
198 disk_alloc => 0,
199 disk_in_use => 0,
200 disk_on_disk => 0,
201 disks => {},
202 rtime => 0,
203 rreqs => 0,
204 wreqs => 0,
205 wtime => 0,
206 freqs => 0,
207 ipkts => 0,
208 ierrs => 0,
209 ibytes => 0,
210 idrop => 0,
211 opkts => 0,
212 oerrs => 0,
213 obytes => 0,
214 coll => 0,
215 odrop => 0,
216 };
217  
218 # convert for megabytes to kilobytes
219 if ( defined( $vm_info->{mem_alloc} ) && $vm_info->{mem_alloc} =~ /^[0-9]+$/ ) {
220 $vm_info->{mem_alloc} = $vm_info->{mem_alloc} * 1024 * 1024;
221 }
222 if ( $status =~ /^On/ ) {
223 $vm_info->{status_int} = 1;
224 $return_hash->{totals}{on}++;
225 my $additional
226 = `ps S -o pid,etimes,%mem,cow,majflt,minflt,nice,nivcsw,nswap,nvcsw,inblk,oublk,pri,rss,systime,usertime,vsz | grep -E '^[\ \t]*'$pid'[\ \t]'`;
227  
228 chomp($additional);
229 $additional =~ s/^[\ \t]*//;
230 (
231 $pid, $vm_info->{etimes}, $vm_info->{pmem}, $vm_info->{cow},
232 $vm_info->{majflt}, $vm_info->{minflt}, $vm_info->{nice}, $vm_info->{nivcsw},
233 $vm_info->{nswap}, $vm_info->{nvcsw}, $vm_info->{inblk}, $vm_info->{oublk},
234 $vm_info->{pri}, $vm_info->{rss}, $vm_info->{systime}, $vm_info->{usertime},
235 $vm_info->{vsz}
236 ) = split( /[\ \t]+/, $additional );
237  
238 $vm_info->{rss} = $vm_info->{rss} * 1024;
239 $vm_info->{vsz} = $vm_info->{rss} * 1024;
240  
241 # zero anything undefined
242 my @keys = keys( %{$vm_info} );
243 foreach my $info_key (@keys) {
244 if ( !defined( $vm_info->{$info_key} ) ) {
245 $vm_info->{$info_key} = 0;
246 }
247 }
248  
249 my ( $minutes, $seconds ) = split( /\:/, $vm_info->{systime} );
250 # low time VMs may lack the :, meaning $seconds will be undef
251 if (!defined($seconds)) {
252 $seconds=$minutes;
253 $minutes=0;
254 }
255 $vm_info->{systime} = ( $minutes * 60 ) + $seconds;
256  
257 ( $minutes, $seconds ) = split( /\:/, $vm_info->{usertime} );
258 $vm_info->{usertime} = ( $minutes * 60 ) + $seconds;
259  
260 #
261 # NIC info
262 #
263  
264 my @bnics_raw = split( /\n/,
265 `cbsd bhyve-nic-list display=nic_parent,nic_hwaddr jname=$vm | sed -e 's/\x1b\[[0-9;]*m//g'` );
266 my $bnics_int = 1;
267 while ( defined( $bnics_raw[$bnics_int] ) ) {
268 my $nic = $bnics_int - 1;
269  
270 chomp( $bnics_raw[$bnics_int] );
271 my @line_split = split( /[\ \t]+/, $bnics_raw[$bnics_int] );
272  
273 my $nic_info = {
274 mac => $line_split[1],
275 parent => $line_split[0],
276 if => '',
277 ipkts => 0,
278 ierrs => 0,
279 ibytes => 0,
280 idrop => 0,
281 opkts => 0,
282 oerrs => 0,
283 obytes => 0,
284 coll => 0,
285 odrop => 0,
286 };
287  
288 $vm_info->{ifs}{ 'nic' . $nic } = $nic_info;
289  
290 $bnics_int++;
291 }
292  
293 # go through each
294 my @add_stats = ( 'ipkts', 'ierrs', 'idrop', 'ibytes', 'opkts', 'obytes', 'oerrs', 'coll', 'odrop' );
295 foreach my $interface (@ifs) {
296 my $if_raw = `ifconfig $interface | grep -E 'description: ' | cut -d: -f 2- | head -n 1`;
297 chomp($if_raw);
298 $if_raw =~ s/^[\'\"\ ]+//;
299 $if_raw =~ s/[\'\"]$//;
300 if ( $if_raw =~ /^$vm-nic[0-9]+/ ) {
301 my $nic = $if_raw;
302 $nic =~ s/^$vm\-//;
303  
304 $vm_info->{ifs}{$nic}{if} = $interface;
305  
306 my @netstats = split( /\n/, `netstat -ibdWn -I $interface` );
307 my $netstats_int = 1;
308 while ( defined( $netstats[$netstats_int] ) ) {
309 my $line = $netstats[$netstats_int];
310 chomp($line);
311  
312 my $if_stats = {};
313  
314 (
315 $if_stats->{int}, $if_stats->{mtu}, $if_stats->{network}, $if_stats->{address},
316 $if_stats->{ipkts}, $if_stats->{ierrs}, $if_stats->{idrop}, $if_stats->{ibytes},
317 $if_stats->{opkts}, $if_stats->{oerrs}, $if_stats->{obytes}, $if_stats->{coll},
318 $if_stats->{odrop}
319 ) = split( /[\ \t]+/, $line );
320  
321 foreach my $current_stat (@add_stats) {
322 if ( $if_stats->{$current_stat} =~ /^[0-9]+$/ ) {
323 $vm_info->{ifs}{$nic}{$current_stat} += $if_stats->{$current_stat};
324 $vm_info->{$current_stat} += $if_stats->{$current_stat};
325 }
326 }
327  
328 $netstats_int++;
329 }
330 }
331 }
332  
333 }
334 elsif ( $status =~ /^[Oo][Ff][Ff]/ ) {
335 $vm_info->{status_int} = 8;
336 $return_hash->{totals}{off}++;
337 }
338 else {
339 # CBSD also has some mode called maintenance and slave,
340 # but it is very unclear what those are
341 $vm_info->{status_int} = 9;
342 $return_hash->{totals}{unknown}++;
343 }
344  
345 #
346 # process the snapshots
347 #
348 my $snaplist_raw = `cbsd jsnapshot mode=list jname=$vm | sed -e 's/\x1b\[[0-9;]*m//g'`;
349 my @snaplist = split( /\n/, $snaplist_raw );
350  
351 # line 0 is always the header
352 my $snaplist_int = 1;
353 while ( defined( $snaplist[$snaplist_int] ) ) {
354 chomp( $snaplist[$snaplist_int] );
355 my ( $jname, $snapname, $snap_creation, $refer ) = split( /[\ \t]+/, $snaplist[$snaplist_int] );
356 if ( $refer =~ /[Kk]$/ ) {
357 $refer =~ s/[Kk]$//;
358 $refer = $refer * 1000;
359 }
360 elsif ( $refer =~ /[Mm]$/ ) {
361 $refer =~ s/[Mm]$//;
362 $refer = $refer * 1000000;
363 }
364 elsif ( $refer =~ /[Gg]$/ ) {
365 $refer =~ s/[Gg]$//;
366 $refer = $refer * 1000000000;
367 }
368 elsif ( $refer =~ /[Tt]$/ ) {
369 $refer =~ s/[Tt]$//;
370 $refer = $refer * 1000000000000;
371 }
372 $vm_info->{snaps_size} = $vm_info->{snaps_size} + $refer;
373 $snaplist_int++;
374 }
375 $vm_info->{snaps} = $#snaplist;
376  
377 #
378 # go through the disk list and for matching ones
379 #
380 foreach my $line (@disk_list) {
381 my $disk_info = {
382 in_use => 0,
383 on_disk => 0,
384 alloc => 0,
385 rbytes => 0,
386 rtime => 0,
387 rreqs => 0,
388 wbytes => 0,
389 wtime => 0,
390 wreqs => 0,
391 freqs => 0,
392 ftime => 0,
393 };
394 if ( $line =~ /^$vm[\t\ ]/ ) {
395 my ( $vm2, $disk_name, $size ) = split( /[\t\ ]+/, $line );
396 $size =~ s/\/.*$//;
397 if ( $size =~ /[Kk]$/ ) {
398 $size =~ s/[Kk]$//;
399 $size = $size * 1024;
400 }
401 elsif ( $size =~ /[Mm]$/ ) {
402 $size =~ s/[Mm]$//;
403 $size = $size * 1024 * 1024;
404 }
405 elsif ( $size =~ /[Gg]$/ ) {
406 $size =~ s/[Gg]$//;
407 $size = $size * 1024 * 1024 * 1024;
408 }
409 elsif ( $size =~ /[Tt]$/ ) {
410 $size =~ s/[Tt]$//;
411 $size = $size * 1024 * 1024 * 1024 * 1024;
412 }
413 $disk_info->{alloc} = $size;
414  
415 my $zfs_key_matched = 0;
416 foreach my $zfs_key (@zfs_keys) {
417 if ( $zfs_key =~ /\/$vm\/$disk_name$/ ) {
418 my ($disk_used)
419 = grep( /\/$vm\/$disk_name[\t\ ]+used[\t\ ]+/, split( /\n/, `zfs get -p all $zfs_key` ) );
420 $disk_used =~ s/[\ \t]+\-[\ \t]*$//;
421 $disk_used =~ s/.*[\ \t]+used[\ \t]+//;
422 $disk_info->{on_disk} = $disk_used;
423 $disk_info->{in_use} = $disk_used;
424  
425 my $kstat_int = 0;
426 my $kstat_matched = 0;
427 while ( defined( $zfs_stats[$kstat_int] ) && ( !$kstat_matched ) ) {
428 if ( $zfs_stats[$kstat_int] =~ /^kstat\.zfs\..*dataset.objset\-.*\: $zfs_key/ ) {
429 $kstat_matched = 1;
430 my $zfs_stat_base = $zfs_stats[$kstat_int];
431 $zfs_stat_base =~ s/\.dataset\_name\:.*$//;
432 ( $disk_info->{rreqs} ) = grep( /^$zfs_stat_base\.reads/, @zfs_stats );
433 ( $disk_info->{wreqs} ) = grep( /^$zfs_stat_base\.writes/, @zfs_stats );
434 ( $disk_info->{wbytes} ) = grep( /^$zfs_stat_base\.nwritten/, @zfs_stats );
435 ( $disk_info->{rbytes} ) = grep( /^$zfs_stat_base\.nread/, @zfs_stats );
436 $disk_info->{rreqs} =~ s/^.*\:[\ \t]//;
437 $disk_info->{wreqs} =~ s/^.*\:[\ \t]//;
438 $disk_info->{rbytes} =~ s/^.*\:[\ \t]//;
439 $disk_info->{wbytes} =~ s/^.*\:[\ \t]//;
440  
441 $vm_info->{rreqs} += $disk_info->{rreqs};
442 $vm_info->{rbytes} += $disk_info->{rbytes};
443 $vm_info->{wreqs} += $disk_info->{rreqs};
444 $vm_info->{wbytes} += $disk_info->{rbytes};
445 }
446  
447 $kstat_int++;
448 }
449  
450 $zfs_key_matched = 1;
451 }
452 }
453 if ( !$zfs_key_matched ) {
454 $disk_info->{on_disk} = $size;
455 $disk_info->{in_use} = $size;
456 }
457  
458 $vm_info->{disk_alloc} += $disk_info->{alloc};
459 $vm_info->{disk_on_disk} += $disk_info->{on_disk};
460 $vm_info->{disk_in_use} += $disk_info->{in_use};
461  
462 $vm_info->{disks}{$disk_name} = $disk_info;
463 }
464 }
465  
466 #
467 # put the totals together
468 #
469 foreach my $to_total (@total) {
470 if ( defined( $vm_info->{$to_total} ) ) {
471 if ( defined( $return_hash->{totals}{$to_total} ) ) {
472 $return_hash->{totals}{$to_total} = $return_hash->{totals}{$to_total} + $vm_info->{$to_total};
473 }
474 else {
475 $return_hash->{totals}{$to_total} = $vm_info->{$to_total};
476 }
477 }
478 }
479  
480 $return_hash->{VMs}{$vm} = $vm_info;
481 push( @VMs, $vm );
482 }
483  
484 return {
485 version => $self->{version},
486 error => 0,
487 errorString => '',
488 data => $return_hash,
489 };
490 }
491  
492 =head2 usable
493  
494 Dies if not usable.
495  
496 eval{ $backend->usable; };
497 if ( $@ ){
498 print 'Not usable because... '.$@."\n";
499 }
500  
501 =cut
502  
503 sub usable {
504 my $self = $_[0];
505  
506 # Make sure we are on a OS on which ZFS is usable on.
507 if ( $^O !~ 'freebsd' ) {
508 die '$^O is "' . $^O . '" and not "freebsd"';
509 }
510  
511 # make sure we can locate cbsd
512 # Written like this as which on some Linux distros such as CentOS 7 is broken.
513 my $cmd_bin = `/bin/sh -c 'which cbsd 2> /dev/null'`;
514 if ( $? != 0 ) {
515 die 'The command "cbsd" is not in the path... ' . $ENV{PATH};
516 }
517  
518 return 1;
519 }
520  
521 =head1 AUTHOR
522  
523 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
524  
525 =head1 BUGS
526  
527 Please report any bugs or feature requests to C<bug-hv-monitor at rt.cpan.org>, or through
528 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=HV-Monitor>. I will be notified, and then you'll
529 automatically be notified of progress on your bug as I make changes.
530  
531  
532  
533  
534 =head1 SUPPORT
535  
536 You can find documentation for this module with the perldoc command.
537  
538 perldoc HV::Monitor
539  
540  
541 You can also look for information at:
542  
543 =over 4
544  
545 =item * RT: CPAN's request tracker (report bugs here)
546  
547 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=HV-Monitor>
548  
549 =item * CPAN Ratings
550  
551 L<https://cpanratings.perl.org/d/HV-Monitor>
552  
553 =item * Search CPAN
554  
555 L<https://metacpan.org/release/HV-Monitor>
556  
557 =back
558  
559  
560 =head1 ACKNOWLEDGEMENTS
561  
562  
563 =head1 LICENSE AND COPYRIGHT
564  
565 This software is Copyright (c) 2022 by Zane C. Bowers-Hadley.
566  
567 This is free software, licensed under:
568  
569 The Artistic License 2.0 (GPL Compatible)
570  
571  
572 =cut
573  
574 1;