#!/usr/bin/env perl use warnings; use strict; use version ; our $VERSION = 'v1.2.0'; # Disk usage for Unix system # Author: R. W. Rodolico # Date: 2016-04-08 # # gets information on disks and 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 # # Revision History # # find our location and use it for searching for libraries. library.pm must be in the same directory as the calling script # or, if run interactively, in the parent of the modules BEGIN { use FindBin; use File::Spec; # prepend the bin directory and its parent use lib File::Spec->catdir($FindBin::Bin), File::Spec->catdir("$FindBin::Bin/.."); eval( 'use library;' ); die sprintf( "Could not find library.pm in %s, INC is %s\n", __FILE__, join( "\n", @INC ) ) if $@; } ##### ##### Change these to match your needs ##### # Make this a list of all the modules we are going to use. You can replace undef with the version you need, if you like my $modulesList = { 'Data::Dumper' => undef, }; # hash of commands that are needed for the system. key is the name of the command and, in some cases, the value will become # the full path (from which or where) my $commandsList = { 'df' => undef, }; # list of operating systems this module can be used on. my $osList = { # 'mswin32' => undef, 'freebsd' => undef, 'linux' => undef, }; # the category the return data should go into. See sysinfo for a list my $CATEGORY = 'diskinfo'; ##### ##### End of required ##### # some variables needed for our system my $errorPrepend = 'error: in ' . __FILE__; # this is prepended to any error messages my @out; # temporary location for each line of output # Try to load the modules we need. If we can not, then make a list of missing modules for error message. for my $module ( keys %$modulesList ) { eval ( "use $module;" ); push @out, "$errorPrepend Could not load $module" if $@; } if ( ! @out && ! checkOS ( $osList ) ) { # check if we are on an acceptible operating system push @out, "$errorPrepend Invalid Operating System"; } if ( !@out && ! validCommandOnSystem ( $commandsList ) ) { push @out, "$errorPrepend Can not find some commands needed"; } if ( !@out ) { # we made it, we have everything, so do the processing ##### ##### Your code starts here. Remember to push all output onto @out ##### # process physical partitions my $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 push @out, $output if $device =~ m/\/dev/; } ##### ##### Your code ends here. ##### } # If we are testing from the command line (caller is undef), print the results for debugging print join( "\n", @out ) . "\n" unless caller; # called by do, which has a value of the last assignment made, so make the assignment. The equivilent of a return my $return = join( "\n", @out );