Hi all, I'm looking for the best practice of processing columns in perl. My use case is to parse Unix iostat command output. The issue is that iostat output changes on different Linux releases however I want to make my script flexible to use it on all hosts. The issue: 1 - iostat output contains different number of columns but I need only some of them; 2 - Output may be different on some Linux releases. I think the proper way is to look into iostat header to find where is a column I need... Here is my code:
P.S.: Sorry for bad english.# Usage: # ./zbx_linux_io.pl <no args specified> - returns a list of avai +lable devices in JSON. # ./zbx_linux_io.pl $DEVICE_NAME OPTION - returns a I/O statisti +cs of given device in JSON. OPTIONs: rps, wps, util. use warnings; use strict; # Get io statistics my @iostat_system = qx(iostat -x -p -k); # Get all block devices and its partitions my @lsblk_system = qx(lsblk -l); # Array to store partitions my @partitions; # Hash to store Zabbix data my %disk_data; # Removing all except disk partitions @lsblk_system = grep /part/, @lsblk_system; # Removing empty lines from iostat output @iostat_system = grep /\S/, @iostat_system; # Filtering out all fields except partition name foreach (@lsblk_system) { # Converting spaces or tabs to comma s/\s+/,/g; # Getting partition name my ($partition) = split (/,/, $_); push @partitions, $partition; } foreach my $partition (@partitions) { foreach my $io (@iostat_system) { $io =~ s/\s+/,/g; $io =~ s/p(\d)/$1/; if ($io =~ /$partition/) { my ($device, $rrqms, $wrqms, $rs, $ws, $rkBs, +$wkBs, $avgrqsz, $avgqusz, $await, $rawait, $wawait, $svctm, $util) = + split (/,/, $io); $disk_data{$device}{rps} = $rs; $disk_data{$device}{wps} = $ws; $disk_data{$device}{util} = $util; } } } if (! @ARGV) { my $JSON = "{ \"data\": [\n" . join(",\n", map { "\t".'{ "{#D +EVICE_NAME}": "'.$_.'" }' } sort keys %disk_data) . "\n]}\n"; print $JSON; } elsif ((defined $ARGV[0]) && (defined $ARGV[1])) { print "$disk_data{$ARGV[0]}{$ARGV[1]}\n" }
In reply to Proper way to work with columns by nikita.af
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |