Can anyone give me a one-liner that adds a timestamp to the output of

vmstat -n 1

Perhaps using awk?

do you need just the time/date? added to it? If so use
awk "BEGIN{now=strftime("%T/%m/%d/%y"); print now}'
then either append it w/ the vmstat to a file.

sorry about the last post, it didn't work (at least with my version of awk) This timescript will give you both outputs in a file called time_statfile.

first run
bash-2.05$ timescript>time_statfile

here is timescript:
#!/bin/bash
perl -e '$TheDate=localtime;
print "The time is $TheDate.\n";'
vmstat

you can see the output here:
bash-2.05$ cat time_statfile
The time is Fri Jul 22 15:09:07 2005.
kthr memory page disk faults cpu
r b w swap free re mf pi po fr de sr s6 sd -- -- in sy cs us sy id
0 0 0 2531368 683488 28 51 111 7 7 0 0 0 3 0 0 539 1232 1338 1 6 92

just rememeber that if you do timescript>time_statfile it will rewrite the file, otherwise append it so you can keep a log of it. Be careful how much this log grows.
Good luck with it

Hi gritty, thanks for taking the time to pitch in. Here's the output I'm trying to get:

bash$ vmstat -n 1 | some-fantastic-time-appending-script
2005-07-22 20:52:50 procs memory swap io system cpu
2005-07-22 20:52:50 r b swpd free buff cache si so bi bo in cs us sy id wa
2005-07-22 20:52:50 1 0 92 544688 248964 1864408 0 0 0 7 1 11 1 0 11 0
2005-07-22 20:52:51 0 0 92 544688 248964 1864408 0 0 0 0 106 54 0 0 100 0
2005-07-22 20:52:52 0 0 92 544688 248964 1864408 0 0 0 0 105 56 0 0 100 0
2005-07-22 20:52:53 0 0 92 544688 248964 1864408 0 0 0 40 106 60 0 0 100 0
2005-07-22 20:52:54 0 0 92 544688 248964 1864408 0 0 0 0 105 56 0 0 100 0
...

The reason this is necessary is because if you run vmstat with "-n 1" for 24 hours, you will not end up with 24 * 60 * 60 + 2 lines of output; instead you'll have something less--several hundred lines less probably. So given any line of that output it is impossible to tell the exact second when the sample was taken, and so it is impossible to definitively match that up with other things going on on the server, and so it is impossible to tell for sure what is causing problems.

Ok, what are you running on, because on Solaris the vmstat -n didn't give me dookie. Did you try the awk statement? I'll try it on my Linux and see what I can do. Let me know if you still need help with it, or if you tried the awk one liner, or if you can at least use the perl script section for your script (perl $TheDate=localtime;
print "The time is $TheDate.\n").

Here is my solution, inspired from the other post:

vmstat 2 |awk '{now=strftime("%T/%m/%d/%y"); print now $0}'

Will display this:

12:40:20/08/19/05procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
12:40:20/08/19/05 r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa
12:40:20/08/19/05 2  0  98244  44148  20496 427156    1    4    35    28   58   120 12  3 84  1
12:40:22/08/19/05 0  0  98244  44156  20496 427156    0    0     0     0 1064  1349  5  1 94  0
12:40:24/08/19/05 0  0  98244  44156  20496 427156    0    0     0     0 1063  1364  2  1 98  0
12:40:26/08/19/05 0  0  98244  44156  20496 427156    0    0     0     0 1067  1434  4  1 94  0
12:40:28/08/19/05 0  0  98244  44156  20496 427156    0    0     0     0 1075  1483  2  1 97  0

Well done buddy, to be honest I kinda forgot about it. I'm, glad if I was able to help.

This article has been dead for over six months. Start a new discussion instead.