You could parse (awk it and grep it) the output of top:
top -n 1
or you could try something like this(should work for single cpu):
vmstat 1 2 | awk '
($1 == "r") { getline; process++; next; }
(process >= 1) { print "u="$14",s="$15",i="$16 }
'
For multiple cpu's you could try something like the following:
# REQUIRES sysstat rpm
########################################################
#
MPSTAT=/usr/bin/mpstat
AWK=/usr/bin/awk
# This command will return the cpu load per cpu as follows:
# <cpunumber>:usr,sys,wait,idl <cpunumber>:...
# since mpstat prints the average "set" first, we need
# to scroll past the first cpu "1" line until we hit the
# second cpu "1" line which indicates the actual set we
# want
CPUCOUNT=`grep -c processor /proc/cpuinfo`
if [ $CPUCOUNT -gt 1 ]; then
$MPSTAT -P ALL 1 2 | $AWK '
BEGIN { process=0; pm = 0; }
($3 == "all") { process++; pm=1; next; }
($2 == "all") { process++; pm=0; next; }
(process == 2 && NF > 1 && $1 != "Average:") { ORS=" ";
if (pm) {
print $3":u="$4",s="$6",i="$7;
}
else {
print $2":u="$3",s="$5",i="$6 }
}
END { ORS="\n"; print ""; }
'
else
$MPSTAT 1 2 | $AWK '
BEGIN { process=0; pm = 0; }
(process == 1 && NF > 1 && $1 != "Average:") { ORS=" ";
if (pm) {
print "0:u="$4",s="$6",i="$7;
}
else {
print "0:u="$3",s="$5",i="$6 }
}
($3 == "all") { process++; pm=1; next; }
($2 == "all") { process++; pm=0; next; }
END { ORS="\n"; print ""; }
'
fi
Also,
for nice trending graphs you could use munin (open source), just make a
search on sourceforge.
>>> [EMAIL PROTECTED] 11/15/04 12:19 PM >>>
This is my first post to the uug so forgive me if I in some way make
mistakes in my posting method.
I am administrating a 64 node cluster and would like to use a shell
script to retrieve the current user cpu usage of each node to display in
a web page. I have looked at several options and so far nothing gives me
what I want/need. Each of these nodes are dual Opteron blades and what I
would like to get is the user cpu percentage that is displayed in the
summary of top. If there is swapping, system processes, etc... I do not
want that to be returned. This removes uptime because it does total
load. Other suggestions are to look in /proc, but I would have to parse
through all the pids to find the user processes, etc. I also looked at
ps, but it gives total cpu time over wall time. I would like to get
possibly the last minute's user load. Before I start trying to hack the
top source code, I thought I'd pose the question to the experts here.
Thank you,
Robert LeBlanc
____________________
BYU Unix Users Group
http://uug.byu.edu/
___________________________________________________________________
List Info: http://uug.byu.edu/cgi-bin/mailman/listinfo/uug-list
____________________
BYU Unix Users Group
http://uug.byu.edu/
___________________________________________________________________
List Info: http://uug.byu.edu/cgi-bin/mailman/listinfo/uug-list