Operating System - HP-UX
1834843 Members
1955 Online
110070 Solutions
New Discussion

CPU Utilization based on user

 
SOLVED
Go to solution
Hasim.Baba
Trusted Contributor

CPU Utilization based on user

Hi,

This should be a easy 10 pointer... what is teh command to get total cpu utilization for user root or any other user. I am planning to use this command in a script.

Regards,
-Hasim
5 REPLIES 5
Steve Lewis
Honored Contributor

Re: CPU Utilization based on user

Two steps:

1. top -s 10 -d 1 -n 1000 -f /tmp/top.txt

2. awk '{if($4=="USERNAME"){p=1;getline;}if(p==1)cpu[$4]+=$11}END{for(u in cpu)print u,cpu[u]}' /tmp/top.txt

These commands add up all the cpu and outputs the total for each user.
Remember to remove /tmp/top.txt between each run, otherwise top appends to the file.
Hasim.Baba
Trusted Contributor

Re: CPU Utilization based on user

You da Man !!!

I hope this runs on HPUX 11.x ?

Also, can u please tell me if I have to sum the utilization for all the users it can be done wiht the same command ?
I am basically trying to make a script in which I total the utilzation for three users and than compare if it does not take more than 30 percent of total utilzation for all the users.

Thanks again
-Hasim

Steve Lewis
Honored Contributor
Solution

Re: CPU Utilization based on user

Yes it is OK for 11.x

Try this for user's cpu, total and proportion of users into total:

awk '{if($4=="USERNAME"){p=1;getline;}if(p==1){cpu[$4]+=$11;tot+=$11}}END{for(u in cpu)print u,cpu[u],tot,(cpu[u]/tot)}' /tmp/top.txt

Steve Lewis
Honored Contributor

Re: CPU Utilization based on user

Pipe the output into

sort -rnk2 to get the worst users at the top of the list.
Hasim.Baba
Trusted Contributor

Re: CPU Utilization based on user

Thank you !!

Regards,
-Hasim