20 |
rodolico |
1 |
#!/usr/bin/env perl
|
|
|
2 |
use warnings;
|
26 |
rodolico |
3 |
use strict;
|
2 |
rodolico |
4 |
|
26 |
rodolico |
5 |
our $VERSION = '1.0';
|
20 |
rodolico |
6 |
|
|
|
7 |
# Linux disks module for sysinfo client
|
|
|
8 |
# Author: R. W. Rodolico
|
|
|
9 |
# Date: 2016-04-08
|
|
|
10 |
|
2 |
rodolico |
11 |
# gets information on disks and partitions. Returns them as a hash
|
|
|
12 |
# uses standard df -kT and parses the output
|
|
|
13 |
# skips anything that does not have a device with /dev in it
|
|
|
14 |
# assumes the structure of the output is:
|
|
|
15 |
# device fstype size usedSpace availableSpace percentSpace mountPoint
|
|
|
16 |
# however, the output can sometimes wrap to multi lines, especially if the
|
|
|
17 |
# device is long. So, we will replace all newlines with spaces, then
|
|
|
18 |
# remove space duplications, then split on spaces and process each item in
|
|
|
19 |
# turn.
|
|
|
20 |
# NOTE: this will totally break if you have spaces in your mount point
|
|
|
21 |
|
|
|
22 |
BEGIN {
|
|
|
23 |
push @INC, shift;
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
use library;
|
|
|
27 |
|
|
|
28 |
my $command = &validCommandOnSystem('df');
|
|
|
29 |
|
|
|
30 |
exit 1 unless $command;
|
|
|
31 |
|
|
|
32 |
my $CATEGORY = 'diskinfo';
|
|
|
33 |
|
|
|
34 |
# process physical partitions
|
|
|
35 |
my $temp = qx/df -kT/; # execute the system command df, returned values in kilobytes, showing file system types
|
|
|
36 |
@temp = split("\n", $temp ); # get array of lines
|
|
|
37 |
shift @temp; # get rid of the first line . . . it is nothing but header info
|
|
|
38 |
$temp = join( ' ', @temp ); # rejoin everything back with spaces
|
|
|
39 |
$temp =~ s/ +/ /gi; # remove all duplicate spaces
|
|
|
40 |
@temp = split( ' ', $temp ); # turn it back into an array of space separated values
|
|
|
41 |
while (@temp) {
|
|
|
42 |
my $device = shift @temp; # get the device name
|
|
|
43 |
my $output = '';
|
|
|
44 |
$output .= "$CATEGORY\t$device\tfstype\t" . (shift @temp) . "\n"; # next is fs type
|
|
|
45 |
$output .= "$CATEGORY\t$device\tsize\t" . (shift @temp) . "\n"; # total partition size, in k
|
|
|
46 |
$output .= "$CATEGORY\t$device\tused\t" . (shift @temp) . "\n"; # disk usage, in k
|
|
|
47 |
shift @temp; # $available, not recorded
|
|
|
48 |
shift @temp; # $percent, not recorded
|
|
|
49 |
$output .= "$CATEGORY\t$device\tmount\t" . (shift @temp) . "\n"; # mount point
|
|
|
50 |
# now, if it is a /dev device, we add it to the diskInfo hash
|
|
|
51 |
print $output if $device =~ m/\/dev/;
|
|
|
52 |
}
|