You are not logged in.

#1 2005-12-13 00:14:36

giddygiddyBA
Member
From: Montreal
Registered: 2004-12-30
Posts: 66

cpu usage

is this the proper way to calc. cpu usage?

#!/bin/sh
total=0
for process in `ps u -eww|awk '{print $3}'|sort -rn|head -n 45`; do 
  total=`awk "BEGIN {print $total+$process}"`
done
echo "usage:$total%"

any suggestions?

Offline

#2 2005-12-13 00:18:45

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: cpu usage

the 'uptime' command should yeild a reasonable measure of cpu usage.. which is the same that is reported by the top command..

I believe it is load average over the last 1 minute, 5 minutes, and 15 minutes...or something like that...


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#3 2005-12-13 00:54:30

giddygiddyBA
Member
From: Montreal
Registered: 2004-12-30
Posts: 66

Re: cpu usage

i dont think you can get cpu usage from the load average...

there is a way to do it with top

top -n 1 | grep Cpu | awk '{print $2}'

which works from a terminal (using top -d 1 instead of top -n 1), but when run in the background, say through screen, it doesnt seem display at all and when run in a while [1] loop, the value doesnt change

any more input is appreciated

Offline

#4 2006-01-09 17:22:21

giddygiddyBA
Member
From: Montreal
Registered: 2004-12-30
Posts: 66

Re: cpu usage

here is a better solution

#!/bin/sh

while true; do
        raw1=`cat /proc/stat | grep "cpu "`
        sleep 1
        raw2=`cat /proc/stat | grep "cpu "`
       
        _usr1=`echo $raw1 | awk '{print $2}'`
        _usr2=`echo $raw2 | awk '{print $2}'`
        ((usr=$_usr2 - $_usr1))
       
        _nic1=`echo $raw1 | awk '{print $3}'`
        _nic2=`echo $raw2 | awk '{print $3}'`
        ((nic=$_nic2 - $_nic1))
       
        _sys1=`echo $raw1 | awk '{print $4}'`
        _sys2=`echo $raw2 | awk '{print $4}'`
        ((sys=$_sys2 - $_sys1))
       
        _idl1=`echo $raw1 | awk '{print $5}'`
        _idl2=`echo $raw2 | awk '{print $5}'`
        ((idl=$_idl2 - $_idl1))
       
        _iowait1=`echo $raw1 | awk '{print $6}'`
        _iowait2=`echo $raw2 | awk '{print $6}'`
        ((iowait=$_iowait2 - $_iowait1))
       
        _irq1=`echo $raw1 | awk '{print $7}'`
        _irq2=`echo $raw2 | awk '{print $7}'`
        ((irq=$_irq2 - $_irq1))
       
        _softirq1=`echo $raw1 | awk '{print $8}'`
        _softirq2=`echo $raw2 | awk '{print $8}'`
        ((softirq=$_softirq2 - $_softirq1))
       
        ((total=$usr + $nic + $sys + $idl + $iowait + $irq + $softirq))
       
        echo $usr $total | awk '{printf "%4.2f ",$1/$2*100}'
        #echo $nic $total | awk '{printf "%4.3f ",$1/$2*100}'
        echo $sys $total | awk '{printf "%4.2f ",$1/$2*100}'
        echo $idl $total | awk '{printf "%4.2f",$1/$2*100}' && echo "%"
        sleep 10
done

Offline

Board footer

Powered by FluxBB