You will want to run this script as a cronjob, probably every 15 minutes or so.
#!/usr/bin/perl
use strict;
my $mailprog="/usr/lib/sendmail"; # your MTA
my $mailto="syscritical\@blah.com"; # Where the emails indicating the applications should be mailed to.
my $from="nobody\@blah.com"; # Who the email should be from
my $ps_prog="/usr/ucb/ps"; # the full pathname to the ps program
my (@name,@pid,@cpu,$first,$second,$third,$one,$mail);
unlink ("cpu.txt");
system ("$ps_prog -aux > cpu.txt");
open (FILE,"cpu.txt");
while (<FILE>){
($one)=split(/\./);
($first,$second,$third)=/(\w+)\W+(\w+)\W+(\w+)/, $one;
# if over 60% , let's complain to syscaution
if ($third > 59){
push (@name,$first);
push (@pid,$second);
push (@cpu,$third);
$mail = "yes";
}
}
&mail_out(\@name,\@pid,\@cpu,$mailprog,$mailto,$from,$ps_prog) if ($mail eq "yes");
exit(0);
sub mail_out{
my ($name,$pid,$cpu,$mailprog,$mailto,$from,$ps_prog)=@_;
# I know the above isn't necessary, but just in case....
my ($x);
open (MAIL, "|$mailprog -t");
print MAIL "To: $mailto\n";
print MAIL "From: $from\n";
print MAIL "Subject: CPU Intensive Application!\n\n";
print MAIL "While running, I found the following information and I thought\n";
print MAIL "I should tell you:\n";
for ($x=0;$x<@$name;$x++){
print MAIL "\n\nName: $name->[$x] PID: $pid->[$x] CPU: ~ $cpu->[$x]%";
system ("$ps_prog -aux | grep $pid->[$x] >> cpufound.txt");
}
print MAIL "\n\n\nBelow is a more detailed report showing applications related to the above:\n";
print MAIL "\n\nUSER PID %CPU %MEM SZ RSS TT S START TIME COMMAND\n";
open (F, "cpufound.txt");
while (<F>){
print MAIL $_;
}
close (F);
close (MAIL);
unlink "cpufound.txt";
}