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:

# 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" }
P.S.: Sorry for bad english.


In reply to Proper way to work with columns by nikita.af

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.