how to add a timestamp to scrolling lines?

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2005
Posts: 4
Reputation: nwc is an unknown quantity at this point 
Solved Threads: 0
nwc nwc is offline Offline
Newbie Poster

how to add a timestamp to scrolling lines?

 
0
  #1
Jul 21st, 2005
Can anyone give me a one-liner that adds a timestamp to the output of

vmstat -n 1

Perhaps using awk?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 23
Reputation: gritty is an unknown quantity at this point 
Solved Threads: 0
gritty gritty is offline Offline
Newbie Poster

Re: how to add a timestamp to scrolling lines?

 
0
  #2
Jul 22nd, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 23
Reputation: gritty is an unknown quantity at this point 
Solved Threads: 0
gritty gritty is offline Offline
Newbie Poster

Re: how to add a timestamp to scrolling lines?

 
0
  #3
Jul 22nd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 4
Reputation: nwc is an unknown quantity at this point 
Solved Threads: 0
nwc nwc is offline Offline
Newbie Poster

Re: how to add a timestamp to scrolling lines?

 
0
  #4
Jul 22nd, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 23
Reputation: gritty is an unknown quantity at this point 
Solved Threads: 0
gritty gritty is offline Offline
Newbie Poster

Re: how to add a timestamp to scrolling lines?

 
0
  #5
Jul 23rd, 2005
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").
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1
Reputation: greenscientist is an unknown quantity at this point 
Solved Threads: 0
greenscientist greenscientist is offline Offline
Newbie Poster

Re: how to add a timestamp to scrolling lines?

 
0
  #6
Aug 19th, 2005
Here is my solution, inspired from the other post:

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

Will display this:
Shell Scripting Syntax (Toggle Plain Text)
  1. 12:40:20/08/19/05procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
  2. 12:40:20/08/19/05 r b swpd free buff cache si so bi bo in cs us sy id wa
  3. 12:40:20/08/19/05 2 0 98244 44148 20496 427156 1 4 35 28 58 120 12 3 84 1
  4. 12:40:22/08/19/05 0 0 98244 44156 20496 427156 0 0 0 0 1064 1349 5 1 94 0
  5. 12:40:24/08/19/05 0 0 98244 44156 20496 427156 0 0 0 0 1063 1364 2 1 98 0
  6. 12:40:26/08/19/05 0 0 98244 44156 20496 427156 0 0 0 0 1067 1434 4 1 94 0
  7. 12:40:28/08/19/05 0 0 98244 44156 20496 427156 0 0 0 0 1075 1483 2 1 97 0
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 23
Reputation: gritty is an unknown quantity at this point 
Solved Threads: 0
gritty gritty is offline Offline
Newbie Poster

Re: how to add a timestamp to scrolling lines?

 
0
  #7
Aug 19th, 2005
Well done buddy, to be honest I kinda forgot about it. I'm, glad if I was able to help.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



Tag cloud for Shell Scripting
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC