View Full Version : getting cpu usage per user
martien
24th July 2008, 00:48
Hello everyone. I need a script or the way to make script for getting the cpu usage per system user. I want when I type the script name and the user name (as parameter) to have the current cpu usage. It can be an already done application in whatever language - php,perl,c,bash or just the way to write one. I have few users on my server and they have their own sites and i want to know if they are too hard. Thanks in advance
burschik
24th July 2008, 10:42
A simple solution would be
top -b -n 1 -u username | awk 'NR>7 { sum += $9; } END { print sum; }'
martien
24th July 2008, 12:43
A simple solution would be
top -b -n 1 -u username | awk 'NR>7 { sum += $9; } END { print sum; }'
That's exactly what i'm looking for. But I have а little lack of understanding - can you explane me this part:
awk 'NR>7 { sum += $9; } END { print sum; }'
10x in advance again.
burschik
24th July 2008, 14:10
AWK is what you might call a pattern processing language. It splits its input into records (delimited by newlines by default), compares each record to a number of patterns and takes appropriate actions if it matches. Each record is further split into fields (delimited by white space by default), which are assigned to the variables $1 to $NF (NF is the number of fields).
In our example, we use two patterns, namely "NR>7" and "END". NR is the number of records, a pre-defined variable. BEGIN and END are special patterns that match the beginning and the end of the input. So, what the small AWK program does is this: If the record number is larger than 7, increment the variable sum by the value of field 9 (the percentage of CPU cycles used). After all records have been processed, print out the value of sum.
martien
16th August 2008, 21:45
Thank you @burschik. You were very helpful fo me.
gracie20
27th August 2008, 11:56
Thanks a lot for the information. It will help me alot.
Gracie Sh
http://hdtvlcdplasma.com
make-fun
1st April 2009, 02:12
A bit of a late reply…
Anyway, a more comfortable way would be using Munin and a plugin from MuninExchange called "cpubyuser".
http://munin.projects.linpro.no/
Plugin CpuByUser (http://muninexchange.projects.linpro.no/?search=&cid=27&os[4]=on&os[7]=on&os[3]=on&os[2]=on&os[5]=on&os[8]=on&os[1]=on&os[6]=on&pid=173)
Works like a charm and gives you a much better picture;-)
Cheers
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.