Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

6 Answers 6

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.

share|improve this answer
add comment
while sleep 1 ; do echo `date '+%Y%m%d.%H%M%S'` `vmstat | head -3 | tail -1` ; done
share|improve this answer
add comment
vmstat -n 1 | (while read; do echo "$(date +%Y%m%d.%H%M%S) $REPLY"; done) | tail -n +3
share|improve this answer
add comment

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
share|improve this answer
    
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
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
    
You can use the -n option to keep the header from repeating. –  Dennis Williamson Aug 27 '09 at 16:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.