0

I'm trying to parse iostat -x 1 2 command's output to monitor remote computer from my java application.

This is the output:

Linux 2.6.39-400.126.1.el5uek (abcprod.abc-en.local)         02/27/2014

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           2.57    0.00    0.87    0.01    0.00   96.56

Device:         rrqm/s   wrqm/s   r/s   w/s   rsec/s   wsec/s avgrq-sz avgqu-sz   await  svctm  %util
sda               0.00    54.24  0.35 10.64     6.95   518.97    47.89     0.01    1.06   0.19   0.21
sda1              0.00     0.00  0.00  0.00     0.00     0.00    68.40     0.00    2.08   0.94   0.00
sda2              0.00    54.23  0.35 10.64     6.94   518.97    47.89     0.01    1.06   0.19   0.21
dm-0              0.00     0.00  0.18 48.77     4.18   390.19     8.06     0.08    1.63   0.02   0.10
dm-1              0.00     0.00  0.00  0.00     0.00     0.00     8.00     0.00    1.06   0.18   0.00
dm-2              0.00     0.00  0.00  0.00     0.00     0.00     8.00     0.00    1.78   0.14   0.00
dm-3              0.00     0.00  0.17 16.10     2.76   128.78     8.09     0.03    2.10   0.07   0.11

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          11.61    0.00    3.20    0.00    0.00   85.19

Device:         rrqm/s   wrqm/s   r/s   w/s   rsec/s   wsec/s avgrq-sz avgqu-sz   await  svctm  %util
sda               0.00   191.00  0.00 43.00     0.00  1872.00    43.53     0.00    0.09   0.05   0.20
sda1              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00
sda2              0.00   191.00  0.00 43.00     0.00  1872.00    43.53     0.00    0.09   0.05   0.20
dm-0              0.00     0.00  0.00 234.00     0.00  1872.00     8.00     0.00    0.02   0.01   0.20
dm-1              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00
dm-2              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00
dm-3              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00

I only need the second part of the output(beginning from fourth paragraph):

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          11.61    0.00    3.20    0.00    0.00   85.19

Device:         rrqm/s   wrqm/s   r/s   w/s   rsec/s   wsec/s avgrq-sz avgqu-sz   await  svctm  %util
sda               0.00   191.00  0.00 43.00     0.00  1872.00    43.53     0.00    0.09   0.05   0.20
sda1              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00
sda2              0.00   191.00  0.00 43.00     0.00  1872.00    43.53     0.00    0.09   0.05   0.20
dm-0              0.00     0.00  0.00 234.00     0.00  1872.00     8.00     0.00    0.02   0.01   0.20
dm-1              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00
dm-2              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00
dm-3              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00

2 Answers 2

1

Do not how to use grep to do it, but you can use perl:

iostat -x 1 2 | perl -e 'local $/=""; @p=<STDIN>; print @p[3,4];'

Some explanations:

  1. local, see perlsub

    A local modifies its listed variables to be "local" to the enclosing block, eval, or do FILE --and to any subroutine called from within that block. A local just gives temporary values to global (meaning package) variables.

  2. $/, see perlvar

    The input record separator, newline by default. This influences Perl's idea of what a "line" is.

Sign up to request clarification or add additional context in comments.

1 Comment

That solves the problem. What does this "local" parameter do?
1

If the number of the lines for the first paragraph is fixed you can use sed like this

iostat -x 1 2 | sed 'N,$!d' - where N is the number of the lines of first paragraph

2 Comments

+1 for temporary solution. But when there is more devices, this code won't work right?
Yes, somehow you have to figure out the number of the devices first

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.