Monitor cpu abusers with this simple script
October 15th, 2007
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.


October 15th, 2007 at 8:54 am
Nice script
Going to tell my friends about it. Should be real useful to them.
Anyway, under which licence is this released?
October 15th, 2007 at 9:10 am
Its public domain.
http://en.wikipedia.org/wiki/Public_Domain
October 15th, 2007 at 9:49 am
Thats ironic, you use Windows with Internet Explorer while writing about bash.
October 15th, 2007 at 10:19 am
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.
October 15th, 2007 at 7:44 pm
[…] is how I have done so in this monitor cpu usage script. First I define all my option holding […]
October 15th, 2007 at 10:49 pm
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.
October 16th, 2007 at 12:19 am
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
October 16th, 2007 at 9:18 am
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}.
October 16th, 2007 at 9:18 am
Changing to ${HOSTNAME} also works in Solaris… just checked it.
October 16th, 2007 at 9:22 am
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.
October 16th, 2007 at 9:27 am
Jim, your right I did mean ${HOSTNAME}, woops. I’ll fix that.
Brock
October 16th, 2007 at 9:38 am
Jim,
I think that $$ is probably fine or “/tmp/tmp.cpu.$(date +%S).$$” if you want be a little more safe.
October 16th, 2007 at 11:50 pm
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.
October 19th, 2007 at 1:36 pm
[…] 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. […]
January 1st, 2008 at 2:43 pm
Is this script can be used in Solaris ?
January 22nd, 2008 at 11:27 am
Hi ,
Can you please provide script for solaris?
I am using solaris.
Regards
Manual
January 22nd, 2008 at 12:11 pm
I don’t have access to a Solaris host I can test on. Where is the script having trouble?
February 6th, 2008 at 8:47 am
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.