3 @chdir(dirname(__FILE__).'/../');
4 require_once('mitsql.cfg.php');
5 require_once('mitsql.lib.php');
6 define('MAX_PROC_TIME', 10);
8 $baddbs = $badusers = $bad = array();
9 $result = mysql_query('SHOW FULL PROCESSLIST');
10 while ($row = mysql_fetch_assoc($result)){
11 if ($row['Time']>MAX_PROC_TIME && $row['Command']!='Sleep') {
13 $baddbs[] = $row['db'];
14 $badusers[] = $row['User'];
17 mysql_free_result($result);
19 foreach($bad as $badproc) {
20 $mailtos = $mailnames = array();
21 $baddb = mysql_escape_string($badproc['db']);
22 $badtime = $badproc['Time'];
23 $badquery = $badproc['Info'];
24 mysql_query('KILL '.$badproc['Id']);
25 if (empty($baddb)) continue;
26 $sql = "SELECT User.UserId,User.Name,User.Email
30 WHERE DB.Name = '$baddb'";
31 $r = fetchRows(DBSelect($sql),'UserId');
32 foreach($r as $addy) {
33 $mailtos[] = $addy['Email'];
34 $mailnames[] = $addy['Name'];
36 $mailto = implode(', ',$mailtos);
37 $mailname = implode(', ',$mailnames);
38 $mailsubj = "[sql] Slow Query on $baddb";
39 $mailbody = "Dear $mailname:
41 A slow query was found on your database: $baddb
42 It took $badtime seconds and has been aborted.
44 We do not allow inefficient SQL queries to run this long. Your query has
45 been appended to this message for your records. Please optimize your
46 queries to avoid having your queries killed in the future. If you have any
47 questions, please contact sql@mit.edu.
49 This SQL Service is available at sql.mit.edu.
51 ---------------------------------------------------------------------------
55 $mailto = 'jwp@mit.edu';
56 mail($mailto,$mailsubj,$mailbody,"From: SQL Service <sql@mit.edu>\r\nBcc: sql@mit.edu\r\n");