Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

awk print system date and time

2 views
Skip to first unread message

moonhkt

unread,
May 8, 2008, 3:18:53 AM5/8/08
to
Hi All
I am using awk for reformat vmstat data.
Do you know to print system date and time

my outout as below. the the time value no change ? Always
20080508_151351 in below session

Interval=2 , Count=20
System Configuration: lcpu=4 mem=7817MB
memory page
faults cpu
-------------- -------------------------------------
---------------------- ------------
r b avm fre re pi po fr sr cy in
sy cs us sy id wa
6 14 1314292 327 0 6 10 330 3490429 120 1978 20213
1771 36 46 1 17 20080508_151351
7 13 1313730 362 0 9 9 296 1553745 58 1766 47794
1872 57 34 0 9 20080508_151351
6 13 1313130 443 0 6 0 190 1598586 50 1769 56427
1825 62 30 0 8 20080508_151351
4 14 1313181 333 0 4 2 262 3460351 112 1978 34615
1660 51 41 0 8 20080508_151351
6 17 1313356 324 0 7 3 344 3125466 112 1951 25643
1589 53 42 0 4 20080508_151351
10 16 1313704 330 0 16 20 565 3530416 135 2050 41217
1735 52 45 0 2 20080508_151351
13 14 1314643 922 0 51 48 1277 1581288 62 1904 44890
2296 68 28 0 4 20080508_151351
9 14 1315012 323 0 19 23 258 2228074 87 1968 79867
2040 66 32 0 2 20080508_151351

PROCESS ()
{
# Interval , Count

echo Interval=$INT , Count=$CNT
(( CNT_A = CNT - 1 ))
TIME=`date +%Y/%m/%d.%H:%M:%S`
vmstat ${INT} ${CNT}| awk -v CYC=${CNT_A} -v TX="$TIME" '
NR == 1 { print $0 }
NR == 2 { print("memory
page faults cpu")
print("--------------
------------------------------------- ----------------------
------------"
)
}
NR == 3 { print(" r b avm fre re pi po fr
sr cy in sy cs us sy id wa "
) }
# NR == 4 { print("-- -- ------- ----- --- ---- ---- ----- ------
----- ----- ----- ----- -- -- -- -- ")}
NR == 5 { next }
{
if($1 ~ /^[0-9].*/) {
dt="'`date +%Y%m%d_%H%M%S`'"
printf ("%2s %2s %8s %4s %4s %4s %4s %5s %7s %6s %6s %6s %5s
%2s %2s %2s %2s %18s\n",

$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,dt)
(totr+=$1);
(totb+=$2);
(totavm+=$3);
(totfre+=$4);
(totpi+=$6);
(totpo+=$7);
(totfr+=$8);
(totsr+=$9);
(totsy+=$14+$15);
(totid+=$16+$17);
}
(avr=totr/CYC)
(avb=totb/CYC)
(avavm=totavm/CYC)
(avfre=totfre/CYC)
(avpi=totpi/CYC)
(avpo=totpo/CYC)
(avfr=totfr/CYC)
(avsr=totsr/CYC)
};
END {
avecpu=(totsy)/(totsy+totid) * 100
aveid=100 - avecpu
avr=int(avr)
avb=int(avb)
avpi=int(avpi)
avpo=int(avpo)
avfr=int(avfr)
avsr=int(avsr)
printf ("%2s %2s %8.0f %4.0f %4s %4s %4s %5s %7s %6s %6s
%6s %5s %2s %2s %2s %2s SUM %15s\n",
avr,avb,avavm,avfre,$5,avpi,avpo,avfr,avsr,
$10,$11,$12,$13,$14,$15,$16,$17,TX)
print "\nSummary"
print "The average usage of CPU is : " avecpu "%"
print ("");
}'
}

Ed Morton

unread,
May 8, 2008, 7:41:15 AM5/8/08
to
On 5/8/2008 2:18 AM, moonhkt wrote:
> Hi All
> I am using awk for reformat vmstat data.
> Do you know to print system date and time
>
> my outout as below. the the time value no change ? Always
> 20080508_151351 in below session

What makes you think your command takes more than 1 second to run?

Ed.

pk

unread,
May 8, 2008, 7:46:32 AM5/8/08
to
On Thursday 8 May 2008 13:41, Ed Morton wrote:

> On 5/8/2008 2:18 AM, moonhkt wrote:
>> Hi All
>> I am using awk for reformat vmstat data.
>> Do you know to print system date and time
>>
>> my outout as below. the the time value no change ? Always
>> 20080508_151351 in below session
>
> What makes you think your command takes more than 1 second to run?

He's running "vmstat 2 20" which outputs a line every two seconds, and pipes
the output to awk.

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.

moonhkt

unread,
May 8, 2008, 10:39:23 AM5/8/08
to

I think dt="'`date +%Y%m%d_%H%M%S`'" will get the current time.

0 new messages