In preparation from a few upcoming articles I created a script called monitorCpuUsage.sh. It is meant to be called from crontab and to inform you of processes or users who maybe using excessive amounts of cpu.

Here is the help screen:

# ./monitorCpuUsage.sh -h
Usage: monitorCpuUsage.sh
-u starting uid, must be an integer greater than or equal to 0 (only used with "-w users")
-m max cpu, must be an integer greater than or equal to 0 and less than 100
-w what to watch, must be "users" or "procs"
-e must contain an email address
-d specifies debug mode in which -e, -m, and -u do not need to be specified.

Here the script in use. I used the -d option which means the script sets the “maximum cpu allowed to -1″ and prints to the screen rather than emailing.


# ./monitorCpuUsage.sh -w users -d -u 100
Mon Oct 15 03:15:54 EDT 2007
User brock is using 0% cpu.
User nfsnobody is using 0% cpu.
User USER is using 0% cpu.

Watching processes…again with -d specified.

# ./monitorCpuUsage.sh -w procs -d
Mon Oct 15 03:17:20 EDT 2007
Process aio/0 is using 0% cpu.
Process atd is using 0% cpu.
Process bash is using 0% cpu.
Process crond is using 0% cpu.
Process dhclient is using 0% cpu.
Process events/0 is using 0% cpu.
Process httpd is using 0% cpu.
Process init is using 0% cpu.
Process java is using 0% cpu.
Process jfsCommit is using 0% cpu.
Process jfsIO is using 0% cpu.
Process jfsSync is using 0% cpu.
Process kblockd/0 is using 0% cpu.
Process kcryptd/0 is using 0% cpu.
Process khelper is using 0% cpu.
Process kjournald is using 0% cpu.
Process klogd is using 0% cpu.
Process ksnapd is using 0% cpu.
Process ksoftirqd/0 is using 0% cpu.
Process kswapd0 is using 0% cpu.
Process kthreadd is using 0% cpu.
Process lockd is using 0% cpu.
Process mingetty is using 0% cpu.
Process monitorCpuUsage is using 0% cpu.
Process mysqld is using 0% cpu.
Process mysqld_safe is using 0% cpu.
Process nfsd is using 0% cpu.
Process pdflush is using 0% cpu.
Process portmap is using 0% cpu.
Process ps is using 0% cpu.
Process rpc.mountd is using 0% cpu.
Process rpc.rquotad is using 0% cpu.
Process rpc.statd is using 0% cpu.
Process rpciod/0 is using 0% cpu.
Process sendmail is using 0% cpu.
Process sshd is using 0% cpu.
Process su is using 0% cpu.
Process syslogd is using 0% cpu.
Process xfs is using 0% cpu.
Process xfsdatad/0 is using 0% cpu.
Process xfslogd/0 is using 0% cpu.

18 Responses to “Monitor cpu abusers with this simple script”

  1. Bug Says:

    Nice script :)
    Going to tell my friends about it. Should be real useful to them.
    Anyway, under which licence is this released?

  2. admin Says:

    Its public domain.

    http://en.wikipedia.org/wiki/Public_Domain

  3. Bug Says:

    Thats ironic, you use Windows with Internet Explorer while writing about bash.

  4. admin Says:

    You are correct, I use IE and Windows. I actually only use IE for gmail beause firefox has some problems when leaving gmail open for extended periods.

    I have never been a huge fan of GUI’s. Linux GUI’s when I tried them out were exceedingly ugly. Granted this was 8 years ago. I’ll probaly give it another try soon as I have heard there are some beautiful UI’s out there.

  5. BASH Cures Cancer » Blog Archive » The 60 second getopts tutorial Says:

    […] is how I have done so in this monitor cpu usage script. First I define all my option holding […]

  6. Dave Says:

    Hi, You’ve used lots of stuff that’s obviously specific to some version of bash/OS I’m not using.

    I’m running on some form of Solaris, I’ve adjusted some of the commands as follows to try to make it work, I don’t know if it will cause breakages on other machines though.

    Your lines are marked with .
    ps -o pcpu= -u $user

    ps -o comm= -o pcpu= -e

    outputCmd=”mail -s ‘CPU Abusers on $HOSTNAME’ $email”

    I haven’t actually gotten it working yet, just got rid of a multitude if the errors it was spamming me with. Perhaps someone else can try and finish it for solaris?

    I’m still getting problems with not having mktemp:
    ++ mktemp -q -t tmp.cpu.XXXXXXXXXXXX
    ./monitorCpuUsage.sh: mktemp: command not found

    and ERR not being valid for trap (I’m sure it’s just a matter of removing it but the other error probably requires some form of restructure that looked like too much work):
    + trap ‘rm -f ; exit’ ERR EXIT SIGINT SIGTERM
    ./monitorCpuUsage.sh: trap: ERR: not a signal specification

    Regards,
    Dave.

  7. admin Says:

    Dave,

    Yes, the script should work on modern linux distros, but has not been tested on anything other than Centos (RHEL) 4.

    I _know_ that it will currently not work on solaris or aix. I think I should be able to fix that tomorrow.

    Brock

  8. Jim Says:

    Just a heads up about this line (43 in my editor):

    outputCmd=”mail -s ‘CPU Abusers on $(HOSTNAME)’ $email”

    the $(HOSTNAME) is a bash call to the “HOSTNAME” command. My guess is that CentOS aliases this with the lowercase “hostname” command.

    It might be more portable to switch the case. That should work on Solaris and AIX as well.

    Unless, you meant to use bash’s updated variable syntax of ${variable name}.

    In that case, I think you mean ${HOSTNAME}.

  9. Jim Says:

    Changing to ${HOSTNAME} also works in Solaris… just checked it.

  10. Jim Says:

    I also had a question about this the use of tmp.cpu.XXXXXXXXXXXX for your.

    If you don’t have a system with mktemp, perhaps an alternative would be to append the process variable $$ to the end of the file tmp.cpu.$$.

    That might be more portable, but a little less safe.

  11. admin Says:

    Jim, your right I did mean ${HOSTNAME}, woops. I’ll fix that.

    Brock

  12. admin Says:

    Jim,

    I think that $$ is probably fine or “/tmp/tmp.cpu.$(date +%S).$$” if you want be a little more safe.

  13. admin Says:

    It works on at least solaris 10 now. I think it will work on lower. The changes I had to make where not using ERR and EXIT in trap, not using –no-headers in ps, and to taking into account if mktemp does not exist.

    If mktemp is not available it creates a temp file that does not exist based on the time, pid, and a counter.

    I knew –no-headers did not exist on solaris and aix, but I was truly surprised that mktemp did not exist.

  14. » Bash cures cancer, aprendiendo Shell script | Informática Práctica | Says:

    […] Bash cures cancer es un completo blog dedicado a los scripts en sistemas Unix/Linux. Aprender script de Unix es bastante interesante, y puede resultar muy útil bajo éstos entornos a la hora de planificar backups, generar queries, etc. Si estás interesado en éste tema no puedes perderte éste blog, ya que encontrarás montones de interesantes scripts. Un interesante script que he encontrado es éste, para poder ver qué procesos gastan demasiada cuota de cpu. […]

  15. Yuvi Says:

    Is this script can be used in Solaris ?

  16. Manual Says:

    Hi ,

    Can you please provide script for solaris?

    I am using solaris.

    Regards
    Manual

  17. admin Says:

    I don’t have access to a Solaris host I can test on. Where is the script having trouble?

  18. Mike Says:

    i am using this script in solaris.
    It is always giving CPU usage 0%.

    I have modified the following

    Before - if [ $id -ge $startAtUid ]

    Modified
    - Before - if [[ $id -ge $startAtUid ]]

    Anyproblem.

Leave a Reply

If Wordpress eats your comment (shell output, loops, ex..) email the text to me.