Subversion Repositories camp_sysinfo_client_3

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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