3 @chdir(dirname(__FILE__).'/../');
4 require_once('mitsql.cfg.php');
5 require_once('mitsql.lib.php');
8 define('MAX_PROC_TIME', 30);
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);
22 foreach($bad as $badproc) {
23 $mailtos = $mailnames = array();
24 $baddb = mysql_escape_string($badproc['db']);
25 if (empty($baddb)) continue;
26 $badtime = $badproc['Time'];
27 $badquery = $badproc['Info'];
28 $sql = "SELECT User.UserId,User.Name,User.Email
32 WHERE DB.Name = '$baddb'
34 $r = fetchRows(DBSelect($sql),'UserId');
35 foreach($r as $addy) {
36 $mailtos[] = $addy['Email'];
37 $mailnames[] = $addy['Name'];
39 if (empty($mailtos)) {
40 echo "No email found for $baddb\n";
43 $mailto = implode(', ',$mailtos);
44 $mailname = implode(', ',$mailnames);
45 $mailsubj = "[sql.mit.edu] Slow Query on $baddb";
46 $mailbody = "Dear $mailname:
48 A slow query was found on your database: $baddb
49 It took $badtime seconds and has been aborted.
51 We do not allow inefficient SQL queries to run this long. Your query has
52 been appended to this message for your records. Please optimize your
53 queries to avoid having your queries killed in the future. If you have any
54 questions, please contact sql@mit.edu.
58 Web: http://sql.mit.edu/
60 ---------------------------------------------------------------------------
64 mysql_query('KILL '.$badproc['Id']);
65 mail($mailto,$mailsubj,$mailbody,"From: SQL Service <sql@mit.edu>\r\nBcc: sql@mit.edu\r\n");