#this is a cleaner rewrite of parse-mysql attempting to encapsulate all sections.
$maxrunload=&maxRunLoad();
if ($myqps > $mysql_qps_baseline){
# print "mysql - qps greater than threshold... continuing\n";
if ($maxrunload > $curload){
# print "Load is low enough... continuing\n";
if (-e '/var/log/mysqld.log.parse'){
`rm -f /var/log/mysqld.log.parse`;
#################################################
### vvv All sub-routines live here vvv ###
#################################################
@load=`cat /proc/loadavg`;
#check cpuinfo, count #of processors, set number of cpus to execution point.
@cpuinfo=`cat /proc/cpuinfo`;
#call mysqladmin status, parse out QPS.
@output_in_array=`mysqladmin status`;
foreach(@output_in_array){
#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 "Content-type: text/plain\n\n";
#write data to /var/log/abused.log
`echo "$logdate - $uname: $uqps QPS" >> /var/log/mysqld.log.history`;
### Log Processor: calculates howmany seconds long log is, uses duration to calculate QPS for usrs using /opt/sharedrads/mysql/parse-general
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)
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);
# read the end of the file to save time in calculating.
@endoffile=`tail -n 4000 /var/log/mysqld.log.1`;
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 ){