Subject: Re: getting cpu utilization
To: None <netbsd-users@netbsd.org>
From: Michael Parson <mparson@bl.org>
List: netbsd-users
Date: 01/11/2007 19:16:38
On Thu, Jan 11, 2007 at 07:39:19PM -0500, George Georgalis wrote:
> 
> I'm using a /bin/sh function to generate a the cpu utilization 
> 
> util () { # CPU Utilization
>  idle=$(echo "2 k $(top -b -d2 | grep '^CPU states' | awk '{print $11}' | sed 's/%//') 1 + p" | dc)
>  echo "2 k 1 $idle / p"  | dc ;}

Other suggestions on where to read this are good, but I'm offering up
how to do this in fewer steps (about the same lenght command, but you're
doing more of it in awk and have fewer execs):

 top -b -d2 | awk ' /^CPU states/ { p = gensub("%", "", "1" , $11)} END {p += 1} END {printf "%.2f\n", 1/p}'

-- 
Michael Parson
mparson@bl.org