| 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 | 
              | 
        
        
           | 56 | 
           rodolico | 
           13 | 
           # gets information on disks. Returns them as a hash
  | 
        
        
           | 55 | 
           rodolico | 
           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 | 
           # check the commands for validity
  | 
        
        
            | 
            | 
           37 | 
           foreach my $command ( keys %commands ) {
  | 
        
        
            | 
            | 
           38 | 
              $commands{$command} = &validCommandOnSystem( $command );
  | 
        
        
            | 
            | 
           39 | 
           }
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
           | 56 | 
           rodolico | 
           41 | 
           exit 1 unless $commands{'df'} && $commands{'sysctl'};
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
           my $temp;
  | 
        
        
            | 
            | 
           44 | 
           my $device;
  | 
        
        
            | 
            | 
           45 | 
           my @temp;
  | 
        
        
            | 
            | 
           46 | 
           my $CATEGORY = 'diskinfo';
  | 
        
        
           | 55 | 
           rodolico | 
           47 | 
              | 
        
        
           | 56 | 
           rodolico | 
           48 | 
              | 
        
        
           | 55 | 
           rodolico | 
           49 | 
           $temp = getSysctlParameter( $commands{ 'sysctl' }, 'kern.disks' );
  | 
        
        
            | 
            | 
           50 | 
           my @disks =  split( /\s+/, $temp );
  | 
        
        
           | 56 | 
           rodolico | 
           51 | 
           foreach $device ( @disks ) {
  | 
        
        
           | 55 | 
           rodolico | 
           52 | 
              $temp = qx( diskinfo $device );
  | 
        
        
            | 
            | 
           53 | 
              chomp $temp;
  | 
        
        
            | 
            | 
           54 | 
              my @info =  split( /\s+/, $temp );
  | 
        
        
            | 
            | 
           55 | 
              $temp = $info[2] / 1024;
  | 
        
        
           | 56 | 
           rodolico | 
           56 | 
              print "$CATEGORY\t/dev/$device\tsize\t$temp\n";
  | 
        
        
           | 55 | 
           rodolico | 
           57 | 
           }
  | 
        
        
            | 
            | 
           58 | 
              | 
        
        
            | 
            | 
           59 | 
           # process physical partitions
  | 
        
        
            | 
            | 
           60 | 
           $temp = qx/df -kT/; # execute the system command df, returned values in kilobytes, showing file system types
  | 
        
        
           | 56 | 
           rodolico | 
           61 | 
           @temp = split("\n", $temp ); # get array of lines
  | 
        
        
           | 55 | 
           rodolico | 
           62 | 
           shift @temp; # get rid of the first line . . . it is nothing but header info
  | 
        
        
            | 
            | 
           63 | 
           $temp = join( ' ', @temp ); # rejoin everything back with spaces
  | 
        
        
            | 
            | 
           64 | 
           $temp =~ s/ +/ /gi; # remove all duplicate spaces
  | 
        
        
            | 
            | 
           65 | 
           @temp = split( ' ', $temp ); # turn it back into an array of space separated values
  | 
        
        
            | 
            | 
           66 | 
           while (@temp) {
  | 
        
        
            | 
            | 
           67 | 
              my $device = shift @temp; # get the device name
  | 
        
        
            | 
            | 
           68 | 
              my $output = '';
  | 
        
        
            | 
            | 
           69 | 
              $output .=  "$CATEGORY\t$device\tfstype\t" . (shift @temp) . "\n"; # next is fs type
  | 
        
        
            | 
            | 
           70 | 
              $output .=  "$CATEGORY\t$device\tsize\t" . (shift @temp) . "\n"; # total partition size, in k
  | 
        
        
            | 
            | 
           71 | 
              $output .=  "$CATEGORY\t$device\tused\t" . (shift @temp) . "\n"; # disk usage, in k
  | 
        
        
            | 
            | 
           72 | 
              shift @temp; # $available, not recorded
  | 
        
        
            | 
            | 
           73 | 
              shift @temp; # $percent, not recorded
  | 
        
        
            | 
            | 
           74 | 
              $output .=  "$CATEGORY\t$device\tmount\t" . (shift @temp) . "\n"; # mount point
  | 
        
        
            | 
            | 
           75 | 
              # now, if it is a /dev device, we add it to the diskInfo hash
  | 
        
        
            | 
            | 
           76 | 
              print $output if $device =~ m/\/dev/;
  | 
        
        
            | 
            | 
           77 | 
           }
  | 
        
        
            | 
            | 
           78 | 
              |