Rev 55 | Rev 57 | 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. 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' => ''
);
# check the commands for validity
foreach my $command ( keys %commands ) {
$commands{$command} = &validCommandOnSystem( $command );
}
exit 1 unless $commands{'df'} && $commands{'sysctl'};
my $temp;
my $device;
my @temp;
my $CATEGORY = 'diskinfo';
$temp = getSysctlParameter( $commands{ 'sysctl' }, 'kern.disks' );
my @disks = split( /\s+/, $temp );
foreach $device ( @disks ) {
$temp = qx( diskinfo $device );
chomp $temp;
my @info = split( /\s+/, $temp );
$temp = $info[2] / 1024;
print "$CATEGORY\t/dev/$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
@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/;
}