getting cpu usage per user

Discussion in 'Programming/Scripts' started by martien, Jul 23, 2008.

  1. martien

    martien New Member

    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
     
    Last edited: Jul 23, 2008
  2. burschik

    burschik New Member

    A simple solution would be

    top -b -n 1 -u username | awk 'NR>7 { sum += $9; } END { print sum; }'
     
  3. martien

    martien New Member

    That's exactly what i'm looking for. But I have а little lack of understanding - can you explane me this part:
    Code:
    awk 'NR>7 { sum += $9; } END { print sum; }'
    10x in advance again.
     
  4. burschik

    burschik New Member

    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.
     
  5. martien

    martien New Member

    Thank you @burschik. You were very helpful fo me.
     
  6. gracie20

    gracie20 New Member

  7. make-fun

    make-fun Member

    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

    Works like a charm and gives you a much better picture;-)

    Cheers
     

Share This Page