55 |
rodolico |
1 |
#!/usr/bin/env perl
|
|
|
2 |
use warnings;
|
|
|
3 |
use strict;
|
|
|
4 |
|
|
|
5 |
# Description: Disk usage for BSD Unix systems
|
|
|
6 |
|
|
|
7 |
our $VERSION = '1.1';
|
|
|
8 |
|
|
|
9 |
# BSD disks module for sysinfo client
|
|
|
10 |
# Author: R. W. Rodolico
|
|
|
11 |
# Date: 2017-11-24
|
|
|
12 |
|
|
|
13 |
# gets information on disks and ZFS partitions. Returns them as a hash
|
|
|
14 |
# uses standard df -kT and parses the output
|
|
|
15 |
# skips anything that does not have a device with /dev in it
|
|
|
16 |
# assumes the structure of the output is:
|
|
|
17 |
# device fstype size usedSpace availableSpace percentSpace mountPoint
|
|
|
18 |
# however, the output can sometimes wrap to multi lines, especially if the
|
|
|
19 |
# device is long. So, we will replace all newlines with spaces, then
|
|
|
20 |
# remove space duplications, then split on spaces and process each item in
|
|
|
21 |
# turn.
|
|
|
22 |
# NOTE: this will totally break if you have spaces in your mount point
|
|
|
23 |
|
|
|
24 |
BEGIN {
|
|
|
25 |
push @INC, shift;
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
use library;
|
|
|
29 |
|
|
|
30 |
# the commands this script will use
|
|
|
31 |
my %commands = (
|
|
|
32 |
'df' => '',
|
|
|
33 |
'sysctl' => ''
|
|
|
34 |
);
|
|
|
35 |
|
|
|
36 |
my $temp;
|
|
|
37 |
my $CATEGORY = 'diskinfo';
|
|
|
38 |
|
|
|
39 |
# check the commands for validity
|
|
|
40 |
foreach my $command ( keys %commands ) {
|
|
|
41 |
$commands{$command} = &validCommandOnSystem( $command );
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
$temp = getSysctlParameter( $commands{ 'sysctl' }, 'kern.disks' );
|
|
|
46 |
my @disks = split( /\s+/, $temp );
|
|
|
47 |
foreach my $device ( @disks ) {
|
|
|
48 |
$temp = qx( diskinfo $device );
|
|
|
49 |
chomp $temp;
|
|
|
50 |
my @info = split( /\s+/, $temp );
|
|
|
51 |
$temp = $info[2] / 1024;
|
|
|
52 |
print "$CATEGORY\t$device\tsize\t$temp\n";
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
# process physical partitions
|
|
|
56 |
$temp = qx/df -kT/; # execute the system command df, returned values in kilobytes, showing file system types
|
|
|
57 |
my @temp = split("\n", $temp ); # get array of lines
|
|
|
58 |
shift @temp; # get rid of the first line . . . it is nothing but header info
|
|
|
59 |
$temp = join( ' ', @temp ); # rejoin everything back with spaces
|
|
|
60 |
$temp =~ s/ +/ /gi; # remove all duplicate spaces
|
|
|
61 |
@temp = split( ' ', $temp ); # turn it back into an array of space separated values
|
|
|
62 |
while (@temp) {
|
|
|
63 |
my $device = shift @temp; # get the device name
|
|
|
64 |
my $output = '';
|
|
|
65 |
$output .= "$CATEGORY\t$device\tfstype\t" . (shift @temp) . "\n"; # next is fs type
|
|
|
66 |
$output .= "$CATEGORY\t$device\tsize\t" . (shift @temp) . "\n"; # total partition size, in k
|
|
|
67 |
$output .= "$CATEGORY\t$device\tused\t" . (shift @temp) . "\n"; # disk usage, in k
|
|
|
68 |
shift @temp; # $available, not recorded
|
|
|
69 |
shift @temp; # $percent, not recorded
|
|
|
70 |
$output .= "$CATEGORY\t$device\tmount\t" . (shift @temp) . "\n"; # mount point
|
|
|
71 |
# now, if it is a /dev device, we add it to the diskInfo hash
|
|
|
72 |
print $output if $device =~ m/\/dev/;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
# process zfs
|
|
|
76 |
#$line = `zpool list -pH`
|
|
|
77 |
|
|
|
78 |
#foreach line
|
|
|
79 |
# chomp $line;
|
|
|
80 |
# ( $name, $size, $allocated, $free, $expandsz, $frag, $cap, $dedup, $health, $altroot ) = split( "\t", $line );
|
|
|
81 |
# `zpool list -pHv $name`
|
|
|
82 |
#
|
|
|
83 |
#OR
|
|
|
84 |
#zpool get -pH size data
|
|
|
85 |
#data size 3573412790272
|