Mini Shell
#!/usr/bin/perl
# Programmed by Clayton.
# by running this script at the end of the day, you can s kip doing math to calculate todays date.
# Check /var/log/mysqld/log/history - if 3 entries for a user - email helpdesk.
$date=`date`;
%abusers=();
chomp($date);
if($date =~ m/(\d\d)\s([\s\d]\d):(\d\d):(\d\d)/){
$day=$1;
$hour=$2;
$min=$3;
$sec=$4;
##print "$date\n";
}
@user_notices=`tail -n 50 /var/log/mysqld.log.history|grep "/$day "`;
foreach(@user_notices){
#if(/(\w+):\s+(\d+)/){
if(/(\d+)\/(\d+)\/(\d+)\s+(\d+):(\d+):(\d+)\s-\s(\w+):\s+(\d+)/){
$year=$1;
$month=$2;
$Qday=$3;
$HH=$4;
$MM=$5;
$SS=$6;
$uname=$7;
$qps=$8;
# print "$1\n";
if ($abusers{$uname}) {
$tmp=$abusers{$uname};
$abusers{$uname} = "$tmp, $HH:00 ($qps qps)";
}
else{
$abusers{$uname} = "$uname, $HH:00 ($qps qps)";
}
}
}
# foreach $k (keys(%abusers)) {
# print $abusers{$k}."\n";
# }
@instance_count=`tail -n 50 /var/log/mysqld.log.history|grep "/$day " | cut -c 21- | cut -d':' -f1| sort| uniq -c`;
foreach(@instance_count){
if(/(\d+)\s+(\w+)/){
$count=$1;
$abuser=$2;
if( $count >= 333 ){
$content= $content . $abusers{$abuser}."\n";
}
}
}
if($content){
&sendEmail();
}
sub sendEmail{
#encapsulated mail function - does not take shit.
#$hostname = `echo \$HOSTNAME`;
chomp($hostname=`echo \$HOSTNAME`);
my $reply_to = "Reply-to: machinemessages\@forum.inmotionhosting.com\n";
my $subject = "Subject: MySQL Abuse on $hostname. \n";
# my $content = "test";
my $send_to = "To: sadmin\@imhadmin.net\n";
my $sendmail = "/usr/sbin/sendmail -t";
open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $! \n";
# print "sending replyto: $reply_to\n";
print SENDMAIL $reply_to;
# print "sending subject: $subject\n";
print SENDMAIL $subject;
# print "sending send_to: $send_to\n";
print SENDMAIL $send_to;
# print "sending content-type:\n";
print SENDMAIL "Content-type: text/plain\n\n";
# print "sending content: $content\n";
print SENDMAIL $content."\n";
close(SENDMAIL);
}
Zerion Mini Shell 1.0