Linux Performance Monitoring – Graphing Per-User CPU Usage

cpu-usagegraphlinuxperformance-monitoring

I want to graph (graphical output would be great, i.e. a .png file) the following situation: I have users A, B, and C. I limit their resources so that when all users run a CPU intensive task at the same time, those processes will use 25%, 25%, and 50% of CPU. I know I can get the real-time stats using top but have no idea what to do with them. I've searched through the huge top man page but haven't found much on the subject of outputting data that can be graphed. Ideally, the graph would show a span of maybe 30 seconds. Any ideas how to achieve this?

Best Answer

I know I can get the real-time stats using top but have no idea what to do with them

Batch mode could be useful:

   -b : Batch mode operation
        Starts  top  in ’Batch mode’, which could be useful for sending output from top to other programs or
        to a file.  In this mode, top will not accept input and runs until the iterations limit  you’ve  set
        with the ’-n’ command-line option or until killed.

For example:

$ top -b -n 1 -u <user> | awk 'NR > 7 { sum += $9 } END { print sum }'

Ganglia Gmetric can be used to plot a graph for this.

cpu_per_user_gmetric.sh

#!/bin/bash
USERS="a b c"

for user in $USERS; do
    /usr/bin/gmetric --name CPU_per_"$user"_user --value `top -b -n 1 -u $user | awk 'NR>7 { sum += $9; } END { print sum; }'` --type uint8 --unit Percent
done

crontab -l

* * * * * /path/to/cpu_per_user_gmetric.sh

and here's the result:

enter image description here

Related Topic