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