Learn the UNIX/Linux command line

Monitor cpu abusers with this simple script

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.