• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

script re-write for Perl script to be used in Linux environment

Does anyone know what would need to be changed to the below script to have it worked in a Linux environment?
### WAYNE'S PERL SCRIPT TO MONITOR AND KILL ROGUE MySQL PROCESSES
use DBI;
use Time::HiRes qw(gettimeofday);
$TimeToKill = 60;
$MySQL_Check_Interval = 10; 
if (!$dbh) {$dbh = &connectToForumsDB()};
$LongReadLen = 1024;
$dbh->{LongReadLen} = $LongReadLen;
$dbh->{LongTruncOk} =  1;
$sql_statement = 'SHOW FULL PROCESSLIST';
$counter = 0;
$kill_counter = 0;
 while (1 != 0)
  {
  $dbh = &connectToForumsDB();
  if ($dbh)
   {
   $LongReadLen = 1024;
   $dbh->{LongReadLen} = $LongReadLen;
   $dbh->{LongTruncOk} =  1;  
   $dataObject = $dbh->prepare($sql_statement);
   $dataObject->execute();
   @dbRows = $dataObject->fetchall_arrayref();
   $rows = 0;
   foreach $rowReference (@dbRows)  ### Set a flag to make sure that we matched rows with our SQL command
    {
    foreach $columnReference (@$rowReference)
     {
     $Id[$rows]     = @$columnReference[0];
     $User[$rows]    = @$columnReference[1]; 
     $Host[$rows]    = @$columnReference[2]; 
     $db[$rows]     = @$columnReference[3]; 
     $Command[$rows]    = @$columnReference[4]; 
     $Time[$rows]    = @$columnReference[5]; 
     $State[$rows]    = @$columnReference[6]; 
     $Info[$rows]    = @$columnReference[7];
     $rows++;
     }
    }
   $time_of_longest_running_thread = 0;
   $current_thread_to_kill = "";
 
   for ($i=0;$i<$rows;$i++)
    {
    if (int($Time[$i] > $TimeToKill) and $Info[$i] =~ /SELECT/i and $Time[$i] > $time_of_longest_running_thread)
     {
     $current_thread_to_kill = $i;
     $time_of_longest_running_thread = $Time[$i];
     }
    }
 
   if ($current_thread_to_kill ne "")
    {
    $Info[$current_thread_to_kill] =~ s/\t| / /gi;
    print "Alert: Kill $Id[$current_thread_to_kill], Time: $Time[$current_thread_to_kill], Statement: \n$Info[$current_thread_to_kill]\n\n";
    ($sec,$min,$hour,$mday,$mon,$year,$wday) = localtime(time);
    $year= substr($year,1,2);
    $month = $mon+1;
    $Kill_Log_File = "L:/Auto-Kill Log File-$month-$mday-$year.txt";
    open (LOGFILE,">>$Kill_Log_File");
    print LOGFILE "Alert: Kill $Id[$current_thread_to_kill], Time: $Time[$current_thread_to_kill], Statement: \n$Info[$current_thread_to_kill]\n\n";
    close (LOGFILE);
    $kill_sql_statement = "KILL $Id[$current_thread_to_kill];";
    $dataObject = $dbh->prepare($kill_sql_statement);
    $dataObject->execute();
    @dbRows = ();
    $kill_counter++;
    }
   #$dbh->disconnect();
   print "Rogue SQL Process Counters: $kill_counter/$counter\n";
   $counter++; 
   sleep($MySQL_Check_Interval);
   }
  }
print "Done with loop\n";
 
 
sub connectToForumsDB
 {
 $dbName  = "MyDBName";
 $dbUserName = "MyUsername";
 $dbPassword = "MyPassword";
 $dbDriver = "ODBC";
 
 return (DBI->connect("dbi:ODBC:$dbName", $dbUserName, $dbPassword));
 }

Open in new window

0
PeterErhard
Asked:
PeterErhard
1 Solution
 
ozoCommented:
is DBD::ODBC installed?
0
 
PeterErhardAuthor Commented:
No it isn't, thanks for responding.
0
 
NeilForsythCommented:
1. The Kill Log File needs to be changed to a unix path (i.e. begins with / instead of a drive letter)

2. ODBC is a Microsoft standard. Judging by the variable names in the script, you are using MySQL, so you don't need ODBC anyway. Try using DBI::mysql. It shouldn't require many changes, check the examples at:
http://search.cpan.org/~capttofu/DBD-mysql-4.007/lib/DBD/mysql.pm

You will also want to put a "shebang" line at the top, such as:

#!/usr/bin/perl

then make the file executable:

chmod +x filename.pl

Also consider making the script a CRON job to run it regularly, or perhaps make it a 'service', so if the script dies, it is automatically re-started. (Making it a service depends on your specific distro, and is much more involved)
$Kill_Log_File = "L:/Auto-Kill Log File-$month-$mday-$year.txt";
 
...
 
$dbDriver = "ODBC";
return (DBI->connect("dbi:ODBC:$dbName", $dbUserName, $dbPassword));

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Adam314Commented:
For the log file, filenames with spaces are not common (and many don't like them).  You might want to consider using underscores instead of spaces.  And you will likely not want to put the log at the root - /var/log is a common place for logs.
0
 
PeterErhardAuthor Commented:
Thanks for the comments guys, but I'm still a bit confused.

What parts of the script do I need to change exactly?
0
 
PeterErhardAuthor Commented:
Anyone?
0
 
Adam314Commented:

$Kill_Log_File = "/var/log/Auto-Kill_Log_File-$month-$mday-$year.txt";

Open in new window

0
 
PeterErhardAuthor Commented:
Thanks for getting back to me Adam, what about this part though.

Would you mind re-posting the whole script with the changes needed?
sub connectToForumsDB
 {
 $dbName  = "MyDBName";
 $dbUserName = "MyUsername";
 $dbPassword = "MyPassword";
 $dbDriver = "ODBC";
 
 return (DBI->connect("dbi:ODBC:$dbName", $dbUserName, $dbPassword));
 }

Open in new window

0
 
Adam314Commented:
You can still use ODBC.  ODBC is similar to DBI that microsoft created - so it's an extra layer.  It might be more efficient to use a mysql driver instead:

return (DBI->connect("dbi:mysql:database=$dbName;host=$host", $dbUserName, $dbPassword));

Open in new window

0
 
PeterErhardAuthor Commented:
So the below should work fine under Linux?
### WAYNE'S PERL SCRIPT TO MONITOR AND KILL ROGUE MySQL PROCESSES
use DBI;
use Time::HiRes qw(gettimeofday);
$TimeToKill = 60;
$MySQL_Check_Interval = 10; 
if (!$dbh) {$dbh = &connectToForumsDB()};
$LongReadLen = 1024;
$dbh->{LongReadLen} = $LongReadLen;
$dbh->{LongTruncOk} =  1;
$sql_statement = 'SHOW FULL PROCESSLIST';
$counter = 0;
$kill_counter = 0;
 while (1 != 0)
  {
  $dbh = &connectToForumsDB();
  if ($dbh)
   {
   $LongReadLen = 1024;
   $dbh->{LongReadLen} = $LongReadLen;
   $dbh->{LongTruncOk} =  1;  
   $dataObject = $dbh->prepare($sql_statement);
   $dataObject->execute();
   @dbRows = $dataObject->fetchall_arrayref();
   $rows = 0;
   foreach $rowReference (@dbRows)  ### Set a flag to make sure that we matched rows with our SQL command
    {
    foreach $columnReference (@$rowReference)
     {
     $Id[$rows]     = @$columnReference[0];
     $User[$rows]    = @$columnReference[1]; 
     $Host[$rows]    = @$columnReference[2]; 
     $db[$rows]     = @$columnReference[3]; 
     $Command[$rows]    = @$columnReference[4]; 
     $Time[$rows]    = @$columnReference[5]; 
     $State[$rows]    = @$columnReference[6]; 
     $Info[$rows]    = @$columnReference[7];
     $rows++;
     }
    }
   $time_of_longest_running_thread = 0;
   $current_thread_to_kill = "";
 
   for ($i=0;$i<$rows;$i++)
    {
    if (int($Time[$i] > $TimeToKill) and $Info[$i] =~ /SELECT/i and $Time[$i] > $time_of_longest_running_thread)
     {
     $current_thread_to_kill = $i;
     $time_of_longest_running_thread = $Time[$i];
     }
    }
 
   if ($current_thread_to_kill ne "")
    {
    $Info[$current_thread_to_kill] =~ s/\t| / /gi;
    print "Alert: Kill $Id[$current_thread_to_kill], Time: $Time[$current_thread_to_kill], Statement: \n$Info[$current_thread_to_kill]\n\n";
    ($sec,$min,$hour,$mday,$mon,$year,$wday) = localtime(time);
    $year= substr($year,1,2);
    $month = $mon+1;
    $Kill_Log_File = "L:/Auto-Kill Log File-$month-$mday-$year.txt";
    open (LOGFILE,">>$Kill_Log_File");
    print LOGFILE "Alert: Kill $Id[$current_thread_to_kill], Time: $Time[$current_thread_to_kill], Statement: \n$Info[$current_thread_to_kill]\n\n";
    close (LOGFILE);
    $kill_sql_statement = "KILL $Id[$current_thread_to_kill];";
    $dataObject = $dbh->prepare($kill_sql_statement);
    $dataObject->execute();
    @dbRows = ();
    $kill_counter++;
    }
   #$dbh->disconnect();
   print "Rogue SQL Process Counters: $kill_counter/$counter\n";
   $counter++; 
   sleep($MySQL_Check_Interval);
   }
  }
print "Done with loop\n";
 
 
sub connectToForumsDB
 {
 $dbName  = "MyDBName";
 $dbUserName = "MyUsername";
 $dbPassword = "MyPassword";
 $dbDriver = "ODBC";
 
 return (DBI->connect("dbi:mysql:database=$dbName;host=$host", $dbUserName, $dbPassword));
 }

Open in new window

0
 
Adam314Commented:
This:
    $Kill_Log_File = "L:/Auto-Kill Log File-$month-$mday-$year.txt";
Should be:
    $Kill_Log_File = "/var/log/Auto-Kill_Log_File-$month-$mday-$year.txt";

The $host isn't defined in the connectToForumsDB subroutine.  It should be the hostname of the server running MySQL.  And the $dbDriver isn't needed.

0
 
PeterErhardAuthor Commented:
Thanks for that, have posted below.

What you say the $dbDriver isn't required, what are you referring to?
### WAYNE'S PERL SCRIPT TO MONITOR AND KILL ROGUE MySQL PROCESSES
use DBI;
use Time::HiRes qw(gettimeofday);
$TimeToKill = 60;
$MySQL_Check_Interval = 10; 
if (!$dbh) {$dbh = &connectToForumsDB()};
$LongReadLen = 1024;
$dbh->{LongReadLen} = $LongReadLen;
$dbh->{LongTruncOk} =  1;
$sql_statement = 'SHOW FULL PROCESSLIST';
$counter = 0;
$kill_counter = 0;
 while (1 != 0)
  {
  $dbh = &connectToForumsDB();
  if ($dbh)
   {
   $LongReadLen = 1024;
   $dbh->{LongReadLen} = $LongReadLen;
   $dbh->{LongTruncOk} =  1;  
   $dataObject = $dbh->prepare($sql_statement);
   $dataObject->execute();
   @dbRows = $dataObject->fetchall_arrayref();
   $rows = 0;
   foreach $rowReference (@dbRows)  ### Set a flag to make sure that we matched rows with our SQL command
    {
    foreach $columnReference (@$rowReference)
     {
     $Id[$rows]     = @$columnReference[0];
     $User[$rows]    = @$columnReference[1]; 
     $Host[$rows]    = @$columnReference[2]; 
     $db[$rows]     = @$columnReference[3]; 
     $Command[$rows]    = @$columnReference[4]; 
     $Time[$rows]    = @$columnReference[5]; 
     $State[$rows]    = @$columnReference[6]; 
     $Info[$rows]    = @$columnReference[7];
     $rows++;
     }
    }
   $time_of_longest_running_thread = 0;
   $current_thread_to_kill = "";
 
   for ($i=0;$i<$rows;$i++)
    {
    if (int($Time[$i] > $TimeToKill) and $Info[$i] =~ /SELECT/i and $Time[$i] > $time_of_longest_running_thread)
     {
     $current_thread_to_kill = $i;
     $time_of_longest_running_thread = $Time[$i];
     }
    }
 
   if ($current_thread_to_kill ne "")
    {
    $Info[$current_thread_to_kill] =~ s/\t| / /gi;
    print "Alert: Kill $Id[$current_thread_to_kill], Time: $Time[$current_thread_to_kill], Statement: \n$Info[$current_thread_to_kill]\n\n";
    ($sec,$min,$hour,$mday,$mon,$year,$wday) = localtime(time);
    $year= substr($year,1,2);
    $month = $mon+1;
    $Kill_Log_File = "/var/log/Auto-Kill_Log_File-$month-$mday-$year.txt";
    open (LOGFILE,">>$Kill_Log_File");
    print LOGFILE "Alert: Kill $Id[$current_thread_to_kill], Time: $Time[$current_thread_to_kill], Statement: \n$Info[$current_thread_to_kill]\n\n";
    close (LOGFILE);
    $kill_sql_statement = "KILL $Id[$current_thread_to_kill];";
    $dataObject = $dbh->prepare($kill_sql_statement);
    $dataObject->execute();
    @dbRows = ();
    $kill_counter++;
    }
   #$dbh->disconnect();
   print "Rogue SQL Process Counters: $kill_counter/$counter\n";
   $counter++; 
   sleep($MySQL_Check_Interval);
   }
  }
print "Done with loop\n";
 
 
sub connectToForumsDB
 {
 $dbName  = "MyDBName";
 $dbUserName = "MyUsername";
 $dbPassword = "MyPassword";
 $dbDriver = "ODBC";
 
 return (DBI->connect("dbi:mysql:database=$dbName;host=$host", $dbUserName, $dbPassword));
 }

Open in new window

0
 
Adam314Commented:
In your connectToForumsDB, you set $dbDriver to "ODBC".  This variable isn't used.  Also, you use $host, which you don't define.
sub connectToForumsDB
{
    $dbName  = "MyDBName";
    $dbUserName = "MyUsername";
    $dbPassword = "MyPassword";
    $host = "Your_mysql_server";
    
    return (DBI->connect("dbi:mysql:database=$dbName;host=$host", $dbUserName, $dbPassword));
}

Open in new window

0
 
PeterErhardAuthor Commented:
Thanks Adam, so this should be the final result?
### WAYNE'S PERL SCRIPT TO MONITOR AND KILL ROGUE MySQL PROCESSES
use DBI;
use Time::HiRes qw(gettimeofday);
$TimeToKill = 60;
$MySQL_Check_Interval = 10; 
if (!$dbh) {$dbh = &connectToForumsDB()};
$LongReadLen = 1024;
$dbh->{LongReadLen} = $LongReadLen;
$dbh->{LongTruncOk} =  1;
$sql_statement = 'SHOW FULL PROCESSLIST';
$counter = 0;
$kill_counter = 0;
 while (1 != 0)
  {
  $dbh = &connectToForumsDB();
  if ($dbh)
   {
   $LongReadLen = 1024;
   $dbh->{LongReadLen} = $LongReadLen;
   $dbh->{LongTruncOk} =  1;  
   $dataObject = $dbh->prepare($sql_statement);
   $dataObject->execute();
   @dbRows = $dataObject->fetchall_arrayref();
   $rows = 0;
   foreach $rowReference (@dbRows)  ### Set a flag to make sure that we matched rows with our SQL command
    {
    foreach $columnReference (@$rowReference)
     {
     $Id[$rows]     = @$columnReference[0];
     $User[$rows]    = @$columnReference[1]; 
     $Host[$rows]    = @$columnReference[2]; 
     $db[$rows]     = @$columnReference[3]; 
     $Command[$rows]    = @$columnReference[4]; 
     $Time[$rows]    = @$columnReference[5]; 
     $State[$rows]    = @$columnReference[6]; 
     $Info[$rows]    = @$columnReference[7];
     $rows++;
     }
    }
   $time_of_longest_running_thread = 0;
   $current_thread_to_kill = "";
 
   for ($i=0;$i<$rows;$i++)
    {
    if (int($Time[$i] > $TimeToKill) and $Info[$i] =~ /SELECT/i and $Time[$i] > $time_of_longest_running_thread)
     {
     $current_thread_to_kill = $i;
     $time_of_longest_running_thread = $Time[$i];
     }
    }
 
   if ($current_thread_to_kill ne "")
    {
    $Info[$current_thread_to_kill] =~ s/\t| / /gi;
    print "Alert: Kill $Id[$current_thread_to_kill], Time: $Time[$current_thread_to_kill], Statement: \n$Info[$current_thread_to_kill]\n\n";
    ($sec,$min,$hour,$mday,$mon,$year,$wday) = localtime(time);
    $year= substr($year,1,2);
    $month = $mon+1;
    $Kill_Log_File = "/var/log/Auto-Kill_Log_File-$month-$mday-$year.txt";
    open (LOGFILE,">>$Kill_Log_File");
    print LOGFILE "Alert: Kill $Id[$current_thread_to_kill], Time: $Time[$current_thread_to_kill], Statement: \n$Info[$current_thread_to_kill]\n\n";
    close (LOGFILE);
    $kill_sql_statement = "KILL $Id[$current_thread_to_kill];";
    $dataObject = $dbh->prepare($kill_sql_statement);
    $dataObject->execute();
    @dbRows = ();
    $kill_counter++;
    }
   #$dbh->disconnect();
   print "Rogue SQL Process Counters: $kill_counter/$counter\n";
   $counter++; 
   sleep($MySQL_Check_Interval);
   }
  }
print "Done with loop\n";
 
sub connectToForumsDB
{
    $dbName  = "MyDBName";
    $dbUserName = "MyUsername";
    $dbPassword = "MyPassword";
    $host = "Your_mysql_server";
    
    return (DBI->connect("dbi:mysql:database=$dbName;host=$host", $dbUserName, $dbPassword));
}

Open in new window

0
 
Adam314Commented:
Looks good.  You will have to replace the "Your_mysql_server" with the name/ip of your server....
0
 
PeterErhardAuthor Commented:
Thanks Adam
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now