0

I am gathering statistics data using iostat and vmstat and am running each one for 10 seconds regularly. However, I don't want to print out the whole output. For iostat I want to only show the number of reads and writes and display them as a column. With vmstat, I just want to show the free, cache and buffer columns. How can I do this? Any filters I use just return this result. The systems are ubuntu 12.04 on both desktop terminal and server only version. they are run using vmware player.

ms total merged
0 0 0
0 0 0
0 0 0
0 0 0
758118 836340 1892
0 0 0
0 0 0
paul
  • 197
  • 1
  • 2
  • 12
  • You need to tell us what OS you are using. Output format of `vmstat` and `iostat` are different across different OSes. – 4ae1e1 Mar 22 '15 at 18:35
  • it is in the tags. I am using ubuntu 12.04 on both server version and desktop version – paul Mar 22 '15 at 18:36
  • Okay I see the Ubuntu 12.04 tag. Never mind. (But I would rather see it spelled out in the question body.) – 4ae1e1 Mar 22 '15 at 18:36
  • no problem. edited question to say os now. – paul Mar 22 '15 at 18:39
  • Great. This should be answered by others fairly quickly so I won't devote time to learning the output format on Ubuntu. On comment though: this is the type of job for `awk`. – 4ae1e1 Mar 22 '15 at 18:40
  • i did use awk but can only get %nice etc to print. I only want reads and writes though, do you know how to do this? – paul Mar 22 '15 at 18:52
  • the iostat one I was told is this, but it only prints out %nice etc. `iostat -c $INT $CNT | awk '/^$/ || (/^avg-cpu:/ && a) {next}; NR>1 {a=1;print}'> ioStressUbuntu_$D.log` – paul Mar 22 '15 at 19:39

1 Answers1

2

Assuming the output formats are as follows:

> iostat -dx sda
Linux 3.13.0-45-generic (hostname obscured)     03/22/2015  _x86_64_    (8 CPU)

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
sda               7.02    30.64    4.48    8.32   174.81   789.29   150.64     0.86   67.48   10.76   98.01   1.06   1.36

> vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0 3728772 969952 614416 29911568    3   13    22    99    1    4 48  5 47  0  0

You can do the following for iostat (every 10 seconds if you'd like to):

device_name=sda # or whatever device name you want
iostat -dx ${device_name} | awk 'NR==4 { print $4 " " $5 }'

Example output (r/s w/s):

4.48 8.32

If you need a count greater than 1, do this:

iostat -dx ${device_name} ${interval} ${count} | awk 'NR==1 || /^$/ || /^Device:/ {next}; { print $4 " " $5 }'

Example output (for device_name=sda; interval=1; count=5):

10.24 8.88
0.00 0.00
0.00 2.00
0.00 0.00
0.00 0.00

And you can do the following for vmstat (every 10 seconds if you'd like to):

vmstat | awk 'NR==3 {print $4 " " $5 " " $6}'

Example output (free buff cache):

969952 614416 29911568
4ae1e1
  • 7,228
  • 8
  • 44
  • 77
  • what do you mean by device name? my username? if i do `iostat -dx 1 10${device_name} | awk 'NR==4 { print $4 " " $5 }'` it still just prints it once. any ideas? – paul Mar 22 '15 at 21:01
  • @paul Device name is, as the name suggest, the name of the device you want to gather stats on. Since you don't know what it is, I assume you only have one device; then it's most likely `sda`. Why it prints once: because you didn't tell me you need a count greater than 1. You said you run it every 10 seconds, so I assumed that you run one count every ten seconds. See updated answer. – 4ae1e1 Mar 22 '15 at 21:14
  • I have formatted it now. why does that tell me that the file does not exist yet it works fine on vmstat? – paul Mar 22 '15 at 21:50
  • ok i have tried it with your edits and it runs but when I write to a file, it tells me the file doesn't exist and it is a fatal error-my code `iostat -dx ${sda} $INT $CNT | awk 'NR==1 || /^$/ || /^Device:/ {next}; { print $4 " " $5 }' > ioStressDebian_$D.log & sleep 5 && stress --io $cores --timeout $seconds_to_run echo "This file will be saved to ioStressDebian_$D.log" sleep 5 cat ioStressDebian_$D.log sleep 10;;` the error `awk: cmd. line:1: fatal: cannot open file ioStressDebian_March.22.log' for reading (No such file or directory)` – paul Mar 22 '15 at 21:51
  • @paul I believe I said something stupid just now. Please ignore that. About your problem: `${sda}` is wrong, just replace it with `sda` for now since I don't want to teach Bash. In addition, please give me a minimal example of how you get the error, with variables replaced with their corresponding values. I don't want to debug this (syntactically wrong) huge mass. – 4ae1e1 Mar 22 '15 at 21:58
  • ok it recognises the file now but when I open the file in excel, using the above syntax, the two columns are in one excel column. How can i have the code so that they will automatically separate into two excel columns? – paul Mar 22 '15 at 22:49
  • @paul CSV, comma separated values. Just use comma instead of space as a delimiter. `awk 'NR==1 || /^$/ || /^Device:/ {next}; { print $4 "," $5 }`. Similarly for `vmstat`. – 4ae1e1 Mar 22 '15 at 22:51
  • Ok the csv has worked for excel. How can I edit it so that the heading reads and writes appear above the two columns on my desktop and server? – paul Mar 23 '15 at 17:17
  • @paul What prevents you from doing `echo "reads,writes" >>LOG_FILE` first? – 4ae1e1 Mar 23 '15 at 17:18