Mini Shell

Direktori : /opt/sharedrads/oldrads/crons/
Upload File :
Current File : //opt/sharedrads/oldrads/crons/mysql-robot.pl

#!/usr/bin/perl
use Time::Local

#this is a cleaner rewrite of parse-mysql attempting to encapsulate all sections.
$maxrunload=0;
$myqps=0;
$maxrunload=&maxRunLoad();
$myqps=&curQPS();
$user_qps_limit=20;
$mysql_qps_baseline=125;
$curload=&getLoad();
$logdate=0;
#Check qps
if ($myqps > $mysql_qps_baseline){
# print "mysql - qps greater than threshold...  continuing\n";
#Check load
   if ($maxrunload > $curload){
#       print "Load is low enough...                    continuing\n";
#Check for parse-file
    if (-e '/var/log/mysqld.log.parse'){
# print "$curload\n";
&tempName();
`rm -f /var/log/mysqld.log.parse`;
}
}
}

#################################################
###  vvv   All sub-routines live here   vvv   ###
#################################################

sub getLoad{
@load=`cat /proc/loadavg`;
foreach(@load){
  if(/(\d+)/){
    return($1);
}
}
}
sub maxRunLoad{
#check cpuinfo, count #of processors, set number of cpus to execution point.
@cpuinfo=`cat /proc/cpuinfo`;
$cpucount=0;
foreach(@cpuinfo){
if(/processor\t:/){
$cpucount+=1;
}
}
$cpucount*1.5;
}


sub curQPS{
#call mysqladmin status, parse out QPS.
@output_in_array=`mysqladmin status`;
foreach(@output_in_array){
if( /avg: (\d+)/){
$sysqps=$1;
last;
}
}
$sysqps;
}


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: $hostname, $uname, $uqps QPS. \n";
my $content     = "$uname needs to be contacted and or moved for high mysql usage(QPS) - please do not ignore this message.  \n";
my $send_to     = "To: sadmin\@imhadmin.net\n";
my $sendmail    = "/usr/sbin/sendmail -t";

open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $! \n";
print SENDMAIL $reply_to;
print SENDMAIL $subject;
print SENDMAIL $send_to;
print SENDMAIL "Content-type: text/plain\n\n";
print SENDMAIL $content;
close(SENDMAIL);
}

sub logData(){
#write data to /var/log/abused.log
`echo "$logdate - $uname: $uqps QPS" >> /var/log/mysqld.log.history`;
}

sub tempName{
###  Log Processor: calculates howmany seconds long log is, uses duration to calculate QPS for usrs using /opt/sharedrads/mysql/parse-general
#Open mysql log.
open MYFILE, "/var/log/mysqld.log.1" or die;
#read file in line by line - < expecting large log files > (I DONT WANT TO READ 1+gig in to memory)
while (<MYFILE>) {
$count += 1;
if ( /(\d\d)([0-1]\d)([0-3]\d)\s([\s\d]\d):(\d\d):(\d\d)/ ) {
$first_date = timelocal($6,$5,$4,$3,$2,$1);
last;
}
}

# read the end of the file to save time in calculating.
@endoffile=`tail -n 4000 /var/log/mysqld.log.1`;
foreach(@endoffile){
if ( /(\d\d)([0-1]\d)([0-3]\d)\s([\s\d]\d):(\d\d):(\d\d)/ ) {
$last_date= timelocal($6,$5,$4,$3,$2,$1);
$logdate="$1/$2/$3 $4:$5:$6";
}
}

$diff = $last_date - $first_date;

#parse data!!! Call Brandons script, housekeeping may needed.
`/opt/sharedrads/mysql/parse-genqlog -i /var/log/mysqld.log.1 -o /var/log/mysqld.out.tmp`;

#read in data just created.
@parse_general_output=`head -n 15 /var/log/mysqld.out.tmp`;
foreach(@parse_general_output){
  if ( /(\w+)\s::\s(\d+)/ ){
    $uqps= $2/$diff; $uname=$1; $qnum=$2;
#    print "$uname, $qnum, $uqps\n";
      if ( $uqps > $user_qps_limit ){
      &logData();
      #&sendEmail();
      }
  }
}
}

Zerion Mini Shell 1.0