Rev 56 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
#!/usr/bin/env perl
use warnings;
use strict;
# Description: Disk usage for BSD Unix systems
our $VERSION = '1.1';
# BSD disks module for sysinfo client
# Author: R. W. Rodolico
# Date: 2017-11-24
# gets information on disks and ZFS partitions. Returns them as a hash
# uses standard df -kT and parses the output
# skips anything that does not have a device with /dev in it
# assumes the structure of the output is:
# device fstype size usedSpace availableSpace percentSpace mountPoint
# however, the output can sometimes wrap to multi lines, especially if the
# device is long. So, we will replace all newlines with spaces, then
# remove space duplications, then split on spaces and process each item in
# turn.
# NOTE: this will totally break if you have spaces in your mount point
BEGIN {
push @INC, shift;
}
use library;
# the commands this script will use
my %commands = (
'df' => '',
'sysctl' => ''
);
my $temp;
my $CATEGORY = 'diskinfo';
# check the commands for validity
foreach my $command ( keys %commands ) {
$commands{$command} = &validCommandOnSystem( $command );
}
$temp = getSysctlParameter( $commands{ 'sysctl' }, 'kern.disks' );
my @disks = split( /\s+/, $temp );
foreach my $device ( @disks ) {
$temp = qx( diskinfo $device );
chomp $temp;
my @info = split( /\s+/, $temp );
$temp = $info[2] / 1024;
print "$CATEGORY\t$device\tsize\t$temp\n";
}
# process physical partitions
$temp = qx/df -kT/; # execute the system command df, returned values in kilobytes, showing file system types
my @temp = split("\n", $temp ); # get array of lines
shift @temp; # get rid of the first line . . . it is nothing but header info
$temp = join( ' ', @temp ); # rejoin everything back with spaces
$temp =~ s/ +/ /gi; # remove all duplicate spaces
@temp = split( ' ', $temp ); # turn it back into an array of space separated values
while (@temp) {
my $device = shift @temp; # get the device name
my $output = '';
$output .= "$CATEGORY\t$device\tfstype\t" . (shift @temp) . "\n"; # next is fs type
$output .= "$CATEGORY\t$device\tsize\t" . (shift @temp) . "\n"; # total partition size, in k
$output .= "$CATEGORY\t$device\tused\t" . (shift @temp) . "\n"; # disk usage, in k
shift @temp; # $available, not recorded
shift @temp; # $percent, not recorded
$output .= "$CATEGORY\t$device\tmount\t" . (shift @temp) . "\n"; # mount point
# now, if it is a /dev device, we add it to the diskInfo hash
print $output if $device =~ m/\/dev/;
}
# process zfs
#$line = `zpool list -pH`
#foreach line
# chomp $line;
# ( $name, $size, $allocated, $free, $expandsz, $frag, $cap, $dedup, $health, $altroot ) = split( "\t", $line );
# `zpool list -pHv $name`
#
#OR
#zpool get -pH size data
#data size 3573412790272
Generated by GNU Enscript 1.6.5.90.