2

Is there a way to run the VMSTAT command (or something like it) and have a timestamp be included in the line? I know that I can pipe the output of the command and manipulate the results, but if I do that, I'll get junk information; I'm told that the first row of VMSTAT contains bogus data, and I'd be essentially getting a repeated list of first rows.

Is there a way to get this kind of data, either with VMSTAT or something similar:

20090826.134908 0  0      0 241832 120064 3023856    0    0     0     1    0     0  0  0 100  0

Thanks!
IVR Avenger

theotherreceive
  • 8,235
  • 1
  • 30
  • 44
IVR Avenger
  • 325
  • 1
  • 5
  • 15

6 Answers6

2
vmstat -n 1 | (while read; do echo "$(date +%Y%m%d.%H%M%S) $REPLY"; done) | tail -n +3
Juliano
  • 5,402
  • 27
  • 28
2
while sleep 1 ; do echo `date '+%Y%m%d.%H%M%S'` `vmstat | head -3 | tail -1` ; done
0

Not sure what you mean by "if I [manipulate the results], I'll get junk information". You can do something like this:

vmstat 8 | sed '3d'

This will remove that line that one is always told to ignore. Alternately, if you want to remove that and the header as well:

vmstat 8 | sed '1,3d'

This will take care of the header too. If the header repeats, you can use something like this:

vmstat 8 | sed '3d; /memory/d; /free/d;'

In my case, "memory" was on line 1 and "free" on line 2; replace with your own appropriate strings.

Mei
  • 4,560
  • 8
  • 44
  • 53
0

Here's a quick and dirty solution:

 while true; do date;vmstat 1 2|tail -n1; done

The first line, by the way, isn't bogus - it contains averages since the last reboot.

Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
0

I know that I can pipe the output of the command and manipulate the results, but if I do that, I'll get junk information

I'm not sure what you mean by that. If the commands won't give you the output you want I can't think of anything do to other than pipe and format the output.

Anyway, this produces the format you want.

$ date=`date "+%Y%m%d.%H%M%S"` && vmstat=`vmstat | tail -1` && echo $date$vmstat
20090827.175715 3  1      8 116848 121072 599320    0    0     0     4    3    6  0  0 100  0
theotherreceive
  • 8,235
  • 1
  • 30
  • 44
  • Reading some comments about VMSTAT, it was mentioned that the first line of data output from the command is always "garbage", and that subsequent lines are legit information. If I concatenate a date and time variable onto the output of VMSTAT, and then run it in a loop, I'll get a loop of only the first line of output from VMSTAT. Thus, junk information. – IVR Avenger Aug 27 '09 at 17:19
  • Yes (the first line is a header), so that will happen *unless* you pipe the output of vmstat to process it, which is what i am suggesting. – theotherreceive Aug 27 '09 at 17:39
0

Sar Might Work Better:
I would recommend the sar command, which is part of the sysstat package. This will by default include timestamps, and there are switches to get all sorts of data. The most basic usage would just be:

sar 1 3 #interval of 1, for a count of 3

See 'man sar' to get memory and paging statistics.

If you do use vmstat:
If you are going to use vmstat, once the terminal is full it will print the header again (at least my GNU Version), so use the -n switch to suppress this.

Kyle Brandt
  • 82,107
  • 71
  • 302
  • 444