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