Skip to content

Commit

Permalink
Add create/drop/alter SQL command in excludable commands + better han…
Browse files Browse the repository at this point in the history
…dling of connection error
  • Loading branch information
Olivier Poitrey committed Jun 7, 2008
1 parent 573123b commit d27b787
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
10 changes: 5 additions & 5 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ OPTIONS
with an exclamation mark (!), all thread not matching this
argument will be excluded. Allowed parameters are:

select, insert, replace, update, delete
select, insert, replace, update, delete, create, drop, alter

Exclude SQL query which command is of the same name.

Expand Down Expand Up @@ -155,10 +155,10 @@ OPTIONS
5 seconds.

DESCRIPTION
mysql-genocide plays with MySQL processlist. It can filter it using
different criterias like execution time, query type, user or regexp
matching of the SQL query. Actions can then be peformed on the result
like killing, sorting or generating statistics.
mysql-genocide helps you play with big MySQL processlists. It can filter
it using different criterias like execution time, query type, user or
regexp matching of the SQL query etc. Actions can then be peformed on
the result like killing, sorting or generating statistics.

EXAMPLES
Kill all selects with execution time geater than 60 seconds:
Expand Down
23 changes: 14 additions & 9 deletions mysql-genocide
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ several times to exclude different kinds of queries. If you prefix the argument
exclamation mark (!), all thread not matching this argument will be excluded.
Allowed parameters are:
B<select, insert, replace, update, delete>
B<select, insert, replace, update, delete, create, drop, alter>
Exclude SQL query which command is of the same name.
Expand Down Expand Up @@ -219,6 +219,8 @@ use Getopt::Long;
use Pod::Usage;

our $VERSION = "0.01";
our @WRITE_CMD = qw(insert replace update delete create drop alter);
our $WRITE_CMD_RE = join('|', @WRITE_CMD);
our %EXCLUDE;
our %INCLUDE;
our %OPTS =
Expand Down Expand Up @@ -287,7 +289,7 @@ if($OPTS{'sort'})

foreach(@{$OPTS{'exclude'}})
{
if(/^(!?)(select|insert|replace|update|delete|write|other|sleep|system)$/i)
if(/^(!?)(select|insert|replace|update|delete|create|drop|alter|write|other|sleep|system)$/i)
{
if($1 eq '!')
{
Expand Down Expand Up @@ -317,7 +319,7 @@ foreach(@{$OPTS{'exclude'}})

if($OPTS{'selects-only'})
{
$EXCLUDE{'!select'} = 1;
$INCLUDE{'select'} = 1;
}

if(defined($OPTS{'interval'}) && $OPTS{'interval'} eq '')
Expand All @@ -331,7 +333,8 @@ $dsn .= 'host=' . $OPTS{'host'} . ';' if($OPTS{'host'});
$dsn .= 'port=' . $OPTS{'port'} . ';' if($OPTS{'port'});
$dsn .= 'mysql_read_default_file=' . $ENV{'HOME'} . '/my.cnf';

my $dbh = DBI->connect($dsn, $OPTS{'user'}, $OPTS{'password'});
my $dbh = DBI->connect($dsn, $OPTS{'user'}, $OPTS{'password'})
or die('Cannot connect to database server');

while(1)
{
Expand All @@ -354,7 +357,7 @@ while(1)
{
$proc->{'QType'} = 'system';
}
elsif($proc->{'Info'} =~ /(INSERT|REPLACE|UPDATE|DELETE)\s/i)
elsif($proc->{'Info'} =~ /($WRITE_CMD_RE)\s/io)
{
$proc->{'QType'} = lc($1);
}
Expand Down Expand Up @@ -406,7 +409,7 @@ while(1)
next;
}

if(defined($OPTS{'min-time'}))
if(defined($OPTS{'min-time'}) && defined($proc->{'Time'}))
{
next if $proc->{'Time'} < $OPTS{'min-time'};
}
Expand All @@ -420,18 +423,18 @@ while(1)
{
next if $EXCLUDE{$proc->{'QType'}};
}
foreach(qw(select insert replace update delete other sleep system))
foreach(@WRITE_CMD, qw(select other sleep system))
{
next PROC if $INCLUDE{$_} && $proc->{'QType'} ne $_;
}

if($EXCLUDE{'write'})
{
next if $proc->{'QType'} =~ /^(insert|replace|update|delete)$/;
next if $proc->{'QType'} =~ /^($WRITE_CMD_RE)$/o;
}
if($INCLUDE{'write'})
{
next if $proc->{'QType'} !~ /^(insert|replace|update|delete)$/;
next if $proc->{'QType'} !~ /^($WRITE_CMD_RE)$/o;
}

foreach my $param (qw(db user state command))
Expand Down Expand Up @@ -545,6 +548,8 @@ while(1)
{
my($title, $count, %stats);

// TODO: total cnx, replication delay

foreach(@proclist)
{
if(defined($threads{$_}->{'QType'}))
Expand Down

0 comments on commit d27b787

Please sign in to comment.