Poll SHOW FULL PROCESSLIST for SQL Statements.

Developed In: Perl — Contributed by: Partha Dutta

Script that will poll MySQL processlist for SQL statements. Sleep interval is in milliseconds offering finer granularity. Output can be loaded into a MySQL database (pipe delimited output)
Partha Dutta
Perl
  1. #!/usr/bin/perl -w
  2.  
  3. ###############################################################################
  4. #
  5. # Perl script to repeatedly poll SHOW FULL PROCESSLIST to view running SQL
  6. # statements.
  7. #
  8. # Author : Partha Dutta
  9. # Version : 1.0
  10. # Date : July 22, 2006
  11. #
  12. ###############################################################################
  13.  
  14. use DBI;
  15. use Time::HiRes qw(usleep ualarm gettimeofday tv_interval);
  16. use Getopt::Long;
  17.  
  18. # Declare all possible command line options
  19. my $help = 0;
  20. my $host = "";
  21. my $port = 3306;
  22. my $user = "";
  23. my $password = "";
  24. my $count = 1000;
  25. my $interval = 500;
  26.  
  27. Getopt::Long::GetOptions(
  28. "help|\?" => \$help,
  29. "h|host=s" => \$host,
  30. "P|port=i" => \$port,
  31. "u|user=s" => \$user,
  32. "password:s" => \$password,
  33. "c|count=i" => \$count,
  34. "i|sleep=i" => \$interval
  35. ) or printhelp();
  36.  
  37. printhelp() if ($help);
  38.  
  39. my %o = (); # All final options kept here
  40.  
  41. $o{'host'} = $host if ($host ne "");
  42. $o{'port'} = $port if ($port ne "");
  43. $o{'user'} = $user if ($user ne "");
  44. $o{'password'} = $password if ($password ne "");
  45. $o{'count'} = $count if ($count ne "");
  46. $o{'sleep'} = $interval if ($interval ne "");
  47.  
  48. # Set defaults if values are still missing
  49. $o{'host'} ||= "localhost";
  50. $o{'port'} ne "" || ($o{'port'} = 3306);
  51. $o{'user'} ||= "root";
  52. $o{'count'} ne "" || ($o{'count'} = 1000);
  53. $o{'sleep'} ne "" || ($o{'sleep'} = 500);
  54.  
  55. printhelp() if (!defined $o{'password'} || $o{'password'} eq "");
  56.  
  57. # Connect to the database.
  58. # This needs to be changed based on the target to connect to.
  59. $db_handle = DBI->connect("dbi:mysql:host=$o{'host'};port=$o{'port'}", $o{'user'}, $o{'password'}) or
  60. die("Could not connect: $DBI::errstr\n");
  61.  
  62. # Find out our current connection id.
  63. @conn_id = $db_handle->selectrow_array("SELECT CONNECTION_ID()") or
  64. die("Failed to get connid: $DBI::errstr\n");
  65.  
  66.  
  67. $sth = $db_handle->prepare("SHOW FULL PROCESSLIST") or
  68. die("Could not prepare stmt: $DBI::errstr\n");
  69.  
  70. for ($n = 0; $n < $o{'count'}; $n++) { usleep($o{'sleep'}*1000);
  71. $sth->execute() or die("Failed to exec: $DBI::errstr\n");
  72. while (@data = $sth->fetchrow_array()) {
  73. $mysql_id = $data[0];
  74. next if ($mysql_id == $conn_id[0]);
  75. $mysql_user = $data[1];
  76. $mysql_host = $data[2];
  77. $mysql_db = $data[3];
  78. $mysql_command = $data[4];
  79. $mysql_time = $data[5];
  80. $mysql_state = $data[6];
  81. $mysql_info = $data[7];
  82.  
  83. next if ($mysql_command eq "Connect" || $mysql_command eq "Sleep" ||
  84. $mysql_command eq "Binlog Dump");
  85.  
  86. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
  87. $wday = $isdst = $yday = 0;
  88.  
  89. ($seconds , $milliseconds) =
  90. gettimeofday;
  91. $seconds = 0;
  92.  
  93. printf("%d-%s-%d %02d:%02d:%02d.%06s|%d|%s|%s|%s|%s|%d|%s|%s\n",
  94. $year+1900, ($mon > 9 ? $mon : "0" . $mon),
  95. ($mday > 9 ? $mday : "0" . $mday),
  96. ($hour > 9 ? $hour : "0" . $hour),
  97. ($min > 9 ? $min : "0" . $min),
  98. ($sec > 9 ? $sec : "0" . $sec),
  99. $milliseconds,
  100. $mysql_id, $mysql_user, $mysql_host, $mysql_db,
  101. $mysql_command, $mysql_time, $mysql_state,
  102. $mysql_info);
  103. }
  104. $sth->finish();
  105. }
  106. $db_handle->disconnect();
  107.  
  108.  
  109. sub printhelp {
  110. print "Usage : perl proclist.pl [options]\n";
  111. print " -h, --host=hostname Connect to host (localhost)\n";
  112. print " -P, --port=# Port number to use for connection (3306)\n";
  113. print " -u, --user=name User for login (root)\n";
  114. print " --password=name Password for user\n";
  115. print " -c, --count=# # of iterations (1000)\n";
  116. print " -i, --sleep=# Delay in ms between iterations (500)\n";
  117. exit 0;
  118. }

Current Tags

You must be logged in to tag this tool

No Comments yet

Votes

Not yet rated.
You must be logged in to vote.

Watches

0 members are watching this tool
You must be logged in to track this tool.

Provide Feedback

Please note:
HTML will be purified, but we allow for a number of HTML tags so that you have the flexibility to decorate your comment text to some extent. The comments allow the following HTML tags:

strong, b, em, blockquote, a, code, pre

To put code into your comment, simply encapsulate your code with
[code language="XXX"][/code], where XXX is any common language, for instance "PHP", "SQL", "C", etc.



You must be logged in to comment