Operating System - HP-UX
1822861 Members
3874 Online
109645 Solutions
New Discussion юеВ

Re: Adding time stamp to vmstat output

 
SOLVED
Go to solution
NDO
Super Advisor

Adding time stamp to vmstat output

Hi All!

I am trying to add time stamp on vmstat output, but it is not working at all. Here is what I do:

vmstat 1 | awk '{now=strftime("%Y-%m-%d %T "); print now $0}'

but it returns the following:

%Y-%m-%d %T kthr memory page disk faults cpu
%Y-%m-%d %T r b w swap free re mf pi po fr de sr m1 m1 m1 m2 in sy cs us sy id
%Y-%m-%d %T 1 3 0 27153136 1215512 1558 16803 82 158 163 0 152 0 0 0 5 6852 93219 21292 38 26 36


Please can you help

F.R.
18 REPLIES 18
James R. Ferguson
Acclaimed Contributor

Re: Adding time stamp to vmstat output

Hi:

You need to use GNU's 'awk' to avail yourself of 'strftime' in 'awk'.

However, you can do the same thing with Perl:

# perl -MPOSIX -nle 'print join " ",strftime("%Y-%m-%d %T",localtime),$_'

Regards!

...JRF...

Kenan Erdey
Honored Contributor

Re: Adding time stamp to vmstat output

Hi,

vmstat 1 | awk '{system("echo $(date +\"%x %r %Z\")");print $0;}'
Computers have lots of memory but no imagination
NDO
Super Advisor

Re: Adding time stamp to vmstat output

Hi

Kenan

Your command does work fine, but it does not put th date on the same line of the data.
Would it be possible to make like this:

03/16/11 08:24:42 r b w avm free re at pi po fr de sr in sy cs us sy id

03/16/11 08:24:42 1 1 0 58501 1423937 0 0 0 0 0 0 0 957 2988 36 0 0 100

03/16/11 08:24:43 1 1 0 58501 1424018 0 0 9 0 0 0 0 1002 6107 73 0 1 99

The other query is, if I want to use awk to select/show only values of r, b, sr, us and sy how would I do?


Another point. that command it does not work in solaris 10.



F.R.
Dennis Handly
Acclaimed Contributor

Re: Adding time stamp to vmstat output

>it does not put the date on the same line of the data.

Any reason you use echo within system?
system("date +\"%x %r %Z\"")

If this doesn't work, you may have to use that echo \c trick I used in this thread:
http://h30499.www3.hp.com/t5/Languages-and-Scripting/AWK-script/m-p/5271240#M41496

Dennis Handly
Acclaimed Contributor

Re: Adding time stamp to vmstat output

>if I want to use awk to select/show only values of r, b, sr, us and sy how would I do?

Instead of printing $0, print the specific columns:
...; print $1, $2, $12, $16, $17
NDO
Super Advisor

Re: Adding time stamp to vmstat output

Hi
Dennis!

No particul├Г┬зar reason to use echo, but I have modified to suit your suggestion, and it came up with:

prep03[151]/ #vmstat 1 | awk '{system("date +\"%x %r %Z\"");print $0;}'
03/16/11 11:07:17 AM SAST
procs memory page faults cpu
03/16/11 11:07:17 AM SAST
r b w avm free re at pi po fr de sr in sy cs us sy id
03/16/11 11:07:17 AM SAST
1 0 0 47263 1437359 0 0 0 0 0 0 0 957 2991 36 0 0 100
03/16/11 11:07:18 AM SAST


But I want the date on the same line as the data, and I dont need the (AM SAST) string.
I know I can use print $1 for selected columns, but how to put it toghether in one line or statement.

I want to use this as an input to gnuplot to produce a graph.


Kenan Erdey
Honored Contributor

Re: Adding time stamp to vmstat output

Hi,

how about the date at the end :)

vmstat 1 | awk '{system("echo $(date +\"%x %r \")");printf("%s ",$0);}'
Computers have lots of memory but no imagination
Jim Fraser
New Member

Re: Adding time stamp to vmstat output

I've prefaced output from myprocess with a date/time stamp by

myprocess |awk 'BEGIN {"date +%Y%m%d_%H:%M" | getline now} {printf "%s %s\n", now, $0}' >> $logfile
Dennis Handly
Acclaimed Contributor

Re: Adding time stamp to vmstat output

>I want the date on the same line as the data, and I don't need the (AM SAST) string.

Then you need to use that alternate solution in the URL I gave.

system("X=$(date +\"%x %r %Z\"); echo $X\\\c")

To change the date format, you need to look at the options under date(1). I.e. remove %Z, etc.

>how to put it together in one line or statement?

Jim's getline trick may work except you need to get it once per line.
Dennis Handly
Acclaimed Contributor

Re: Adding time stamp to vmstat output

>But I want the date on the same line as the data, and I don't need the (AM SAST) string.

Then you may want this format: system("date +\"%x %T\"")
03/17/11 00:49:02

So:
vmstat 1 | awk '{system("X=$(date +\"%x %T\"); echo $X\\\c");print $0}'

Or you can just give up on handling the two lines in awk and then use sed(1) to join the two lines:
vmstat 1 | awk '{system("date +\"%x %T\"");print $0;}' | sed -n -e 'N; s/\^J/ /; p'

Where you enter that control-J by control-V then control-J.
NDO
Super Advisor

Re: Adding time stamp to vmstat output

Hi

Dennis!

Your suggestion:vmstat 1 | awk '{system("X=$(date +\"%x %T\"); echo $X\\\c");print $0}' work perfectely.

But, now I just want the vmstat to shown only $1, $2, $12, $20 and $21, your suggestion, i.e.
vmstat 1 | awk '{system("X=$(date +\"%x %T\"); echo $X\\\c");print $0}'



F.R.


James R. Ferguson
Acclaimed Contributor

Re: Adding time stamp to vmstat output

Hi (again):

> But, now I just want the vmstat to shown only $1, $2, $12, $20 and $21

This is AWK-101.

Change '$0' (which is the whole line buffer) to:

$1,$2,$12,$20,$21

The comma (',') will be substitute by the the output field separator, which by default is a space.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Adding time stamp to vmstat output

>Your suggestion: vmstat 1 | awk '{system("X=$(date +\"%x %T\"); echo $X\\\c");print $0}' work perfectly.

If you are happy with the answers you were given, please read the following about assigning points:
http://h30499.www3.hp.com/t5/help/faqpage/faq-category-id/kudos#kudos


>now I just want the vmstat to shown only $1, $2, $12, $20 and $21, your suggestion, i.e.
vmstat 1 | awk '{system("X=$(date +\"%x %T\"); echo $X\\\c");print $0}'

As JRF said, just replace that $0 by: $1, $2, $12, $20, $21

>JRF: $1,$2,$12,$20,$21

(I use a space above for readability.)

>ME: awk '{system("date +\"%x %T\"");print $0;}' | sed -n -e 'N; s/\^J/ /; p'
>Where you enter that control-J by ...

I spent a long time figuring this out and today I also needed to join pairs of lines.
I couldn't type ^J in vi, only the shell.
Then I just tried using "\n" and it magically worked. Too much overthinking. :-(

NDO
Super Advisor

Re: Adding time stamp to vmstat output

Hi

Dennis

I always assign points to you guys, the $1,... $21 did not work, but I am fine with $0, I can always extract the colums that I want to create a graph.

Thanks


F.R.
James R. Ferguson
Acclaimed Contributor

Re: Adding time stamp to vmstat output

Hi:

> the $1,... $21 did not work

Exactly what did you do and exactly what was the result?

Simply saying "it didn't work" provides no useful information from which to help you further.

...JRF...
NDO
Super Advisor

Re: Adding time stamp to vmstat output


Hi

James:

This is the result:
prep03[140]/ #vmstat 1 | awk '{system("X=$(date +\"%x %T\"); echo $X\\\c");print $1,$2,$12,$20,$21}'
03/18/11 17:09:34procs memory
03/18/11 17:09:34r b sr
03/18/11 17:09:341 1 0
03/18/11 17:09:351 0 0
03/18/11 17:09:361 0 0
03/18/11 17:09:371 0 0
03/18/11 17:09:381 0 0
03/18/11 17:09:391 0 0
03/18/11 17:09:401 0 0
03/18/11 17:09:411 0 0
prep03[141]/ #


There is no space between seconds and r column, and also does not show the last 2 columns that are "us" and "sy"

F.R.
Dennis Handly
Acclaimed Contributor
Solution

Re: Adding time stamp to vmstat output

>There is no space between seconds and r column

Oops, add a space at the end of the date(1) format or one in the echo or one in print:
vmstat 1 | awk '{system("X=$(date +\"%x %T \"); echo $X\\\c");print $1,$2,$12,$20,$21}'

>does not show the last 2 columns that are "us" and "sy"

You need to use the original values I gave:
..; print $1, $2, $12, $16, $17

It seems your first reply and the second have different columns?
NDO
Super Advisor

Re: Adding time stamp to vmstat output

Hi

Dennis:
Your suggestion worked perfectely, I have already assign points.

F.R.