Question: Vmstat output does not display date and time, which is a pain when trying to store the vmstat output to do some performance analysis.
So, how do I add timestamp to Linux vmstat output ?
Answer: You can display timestamp on every line of the vmstat command output using a simple perl script as shown below.
Default Vmstat output (without timestamp)
$ vmstat 1 5 procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu---- r b swpd free buff cache si so bi bo in cs us sy id wa 0 0 33308 450080 59020 6757044 1 1 551 148 1 6 26 9 60 5 0 0 33308 449960 59020 6757044 0 0 0 0 1068 1576 1 1 98 0 1 0 33308 453928 59020 6757044 0 0 0 48 1049 1628 2 1 97 0 0 0 33308 454120 59020 6757044 0 0 0 0 1034 1663 2 1 96 0 0 0 33308 454112 59020 6757044 0 0 0 0 1025 1535 1 1 98 0
Vmstat output with timestamp
Create the following simple timestamp.pl
$ vi timestamp.pl
#!/usr/bin/perl
while (<>) { print localtime() . ": $_"; }
Now pipe the vmstat command output to this timestamp.pl to display the timestamp as shown below.
$ vmstat 1 5 | timestamp.pl Sat Aug 22 20:50:36 2009: procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu---- Sat Aug 22 20:50:36 2009: r b swpd free buff cache si so bi bo in cs us sy id wa Sat Aug 22 20:50:36 2009: 2 0 33308 452080 59084 6761140 1 1 551 148 1 6 26 9 60 5 Sat Aug 22 20:50:37 2009: 0 0 33308 454368 59084 6761140 0 0 0 0 1028 1713 2 1 96 0 Sat Aug 22 20:50:38 2009: 0 0 33308 454368 59084 6761140 0 0 0 0 1010 1432 0 0 100 0 Sat Aug 22 20:50:39 2009: 0 0 33308 454368 59084 6761140 0 0 0 12 1021 1481 1 0 99 0 Sat Aug 22 20:50:40 2009: 0 0 33308 457184 59084 6761140 0 0 0 0 1029 1549 1 1 98 0
Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.Get the Linux Sysadmin Course Now!
If you enjoyed this article, you might also like..
|
|
|
|






My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about
{ 10 comments… read them below or add one }
Thanks for great website.
Unfortunately I’m unable to get this to work. One part that seems to be missing is following:
chmod +x timestamp.pl
But when I type in your command “vmstat 1 5 | timestamp.pl” I get the following error message:
bash: timestamp.pl: command not found
What am I doing wrong?
[root ~]# vmstat 1 5 | timestamp.pl
bash: timestamp.pl: command not found
[root@220 ~]# vmstat 1 5 | perl timestamp.pl
Fri Aug 28 13:53:23 2009: procs memory page disks faults cpu
Fri Aug 28 13:53:23 2009: r b w avm fre flt re pi po fr sr ad18 ad20 in sy cs us sy id
Fri Aug 28 13:53:23 2009: 0 2 0 606280 170548 3308 0 0 1 3041 65 0 0 222 17009 5351 6 2 92
…
2-nd variant:
vmstat 1 5 | ./timestamp.pl
another way is to copy script or program to bin catalog
(csh):
~ ~> cp timestamp.pl bin/timestamp
~ ~> rehash
~ ~> vmstat 1 5 | timestamp
I hope so:
vmstat |awk ‘{print system (“date +%D\” \”%H:%M:|tr -d ‘\”\n’\””)$0}’
RNDr. Michael Banzet
Another way is :
vmstat 1 5 | awk ‘{print strftime(“%D %H:%M:%S”, systime()) ” ” $0 }’
here is a suggestion for awk in a shell:
:~> vmstat 1 5 | awk ‘{system(“/bin/echo -n `/bin/date`”); print “: ” $0}’
Fri Aug 28 17:58:17 CEST 2009: procs ———–memory———- —swap– —–io—- -system– —-cpu—-
Fri Aug 28 17:58:17 CEST 2009: r b swpd free buff cache si so bi bo in cs us sy id wa
Fri Aug 28 17:58:17 CEST 2009: 3 0 104 88460 280268 1038164 0 0 67 13 35 30 1 0 99 1
Fri Aug 28 17:58:18 CEST 2009: 0 0 104 87760 280276 1038168 0 0 0 100 103 248 2 1 97 1
Fri Aug 28 17:58:19 CEST 2009: 0 0 104 87636 280276 1038168 0 0 0 0 114 235 0 0 100 0
Fri Aug 28 17:58:20 CEST 2009: 0 0 104 88124 280276 1038168 0 0 0 0 70 160 1 0 99 0
Fri Aug 28 17:58:21 CEST 2009: 0 0 104 88092 280276 1038168 0 0 0 0 104 231 1 1 99 0
you can modify the date output with the date command and the vmstat output with awk’s print and printf command (below is only an example for proof of concept – not meant to make sense
):
:~> vmstat 1 5 | awk ‘{system(“/bin/echo -n `/bin/date +’%A-%D-%H:%M:%S’`”); print ” ” $3 ” ” $4 ” ” $5 ” ” $6}’
Friday-08/28/09-18:04:09 —swap– —–io—- -system– —-cpu—-
Friday-08/28/09-18:04:09 swpd free buff cache
Friday-08/28/09-18:04:09 104 56596 281372 1068124
Friday-08/28/09-18:04:10 104 55716 281372 1068124
Friday-08/28/09-18:04:11 104 55596 281372 1068124
Friday-08/28/09-18:04:12 104 55456 281380 1068124
Friday-08/28/09-18:04:13 104 55828 281380 1068124
Ulrich
Ok, Michael’s suggestion is much better than mine.
I would set the system command out of the print command, because you get an ugly zero at the end of the date. Here is what I mean:
:~> vmstat 1 5 |awk ‘{print system (“date +%A:|tr -d ‘\”\n’\”")$0}’
Friday:0procs ———–memory———- —swap– —–io—- -system– —-cpu—-
Friday:0 r b swpd free buff cache si so bi bo in cs us sy id wa
Friday:0 4 0 104 50060 283668 1070152 0 0 67 13 35 30 1 0 99 1
Friday:0 0 0 104 49476 283676 1070152 0 0 0 100 84 206 2 1 98 1
Friday:0 4 0 104 49684 283676 1070152 0 0 0 0 115 246 0 0 100 0
Friday:0 0 0 104 49160 283676 1070152 0 0 0 0 80 256 2 0 99 0
What I would do:
:~> vmstat 1 5 |awk ‘{system (“date +%A:|tr -d ‘\”\n’\”") ; print $0}’
Friday:procs ———–memory———- —swap– —–io—- -system– —-cpu—-
Friday: r b swpd free buff cache si so bi bo in cs us sy id wa
Friday: 2 0 104 49960 283708 1070156 0 0 67 13 35 30 1 0 99 1
Friday: 0 0 104 49132 283708 1070156 0 0 0 0 98 213 2 0 98 0
Friday: 3 0 104 49752 283708 1070156 0 0 0 0 110 247 0 0 100 0
Friday: 0 0 104 49216 283708 1070156 0 0 0 0 87 364 1 1 98 0
Ulrich
You can do it just in bash and no awk
vmstat 1 5 | while read line; do echo “`date`$line”; done
@All,
Thanks a lot for sharing your alternative ways to display the timestap on vmstat output. It looks great.
Another solution in Perl:
vmstat 1 4 | perl -pe ‘print localtime().” “‘