[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cobalt-users] CPU overload.... URGENT SOLUTION ANYONE?



Annicke wrote:
> 
> A user installed metasearch from http://www.done-right.net
> This runs a cgi called metasearch.cgi....
> Most of the time everything runs fine but about 10 times a day the script
> keeps on running and takes all CPU....

Have you counseled your user?

You are the System Administrator -- no?

> Example:
> 
> 17677 wm-zoek   13   0  2988 2988   992 R       0 94.6  2.3  26:21
> metasearch.cgi
> 
> running 26 minutes and taking up 94.6 % CPU.....
> 
> Now I always kill it by hand which is not the sollution....
> 
> So my question, how to solve it, can I a/ set a max time on CGI scripts
> somewhere or b/ automaticly kill it with a script.....

Since %CPU is your problem, do not skirt the issue by timing processes
-- address the problem head-on!

You could run the following script as a root cron job every fifteen
minutes, or so . . .


#!/usr/bin/perl -w

# kill_cpuhog.plx
#
# kill and log processes consuming excessive %CPU
#
# v1.0, mds (20010126)

use strict;

# Notice, we are *NOT* interested in processes owned by root ;<
my $gather = 'ps -U root -u root -N -o %cpu,pid,time,user,args -U root
-u root';
my $header;
my %kill;
my $log = '/tmp/kill_cpuhog.log';
my $percent = 90;

# Gather list of usual suspects
open LIST, "$gather |"
	or die "\n\tERROR: Cannot invoke \'$gather\' : $! : $?\n\n";
chomp(my @list = <LIST>);
close LIST;

# %CPU > $percent ???
for (@list) {
	$header = $_, next unless $header;
	my @ps = split;
	$ps[0] > $percent
		or next;
	$kill{$ps[1]} = $_;
}

# Nothing to do unless %kill has entries
exit 0 unless %kill;

# Log all that we do to $log
open LOG, ">>$log"
	or die "\n\tERROR: Cannot write to \'$log\' : $! : $?\n\n";
print LOG scalar localtime, "\n";
print LOG $header, "\n";

# kill -9 offending processes
for my $pid (keys %kill) {
	print LOG $kill{$pid}, "\n";
	my $cmd = "kill -9 $pid";
	$cmd = "ps -fp $pid";
	for (@{ get_sys($cmd) }) {
		print LOG $_, "\n"
	}
}
close LOG;

exit 0;

##################
#  Sub Routines  #
##################

##########################################
#  Get output from system level program  #
##########################################
sub get_sys {
	my $prog = shift;
	open PROG, "$prog 2>&1 |" or warn "\n\tERROR: Cannot invoke \'$prog\' !
: ", $? >> 8, "\n\n";
	chomp(my @out = <PROG>);
	close PROG;
	return \@out;
}


-- 

Best Regards,

mds
mds resource
888.250.3987

"Dare to fix things before they break . . . "

"Our capacity for understanding is inversely proportional to how much we
think we know.  The more I know, the more I know I don't know . . . "