]>
Commit | Line | Data |
---|---|---|
6eacda6d JP |
1 | <?php |
2 | ||
aaaa7a8f | 3 | @chdir(dirname(__FILE__).'/../'); |
6eacda6d JP |
4 | require_once('mitsql.cfg.php'); |
5 | require_once('mitsql.lib.php'); | |
5852fbc4 JP |
6 | isOffline() && exit; |
7 | ||
70776def | 8 | define('MAX_PROC_TIME', 30); |
6eacda6d | 9 | |
6eacda6d JP |
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') { | |
14 | $bad[] = $row; | |
15 | $baddbs[] = $row['db']; | |
16 | $badusers[] = $row['User']; | |
17 | } | |
18 | } | |
19 | mysql_free_result($result); | |
20 | ||
70776def | 21 | print_r($bad); |
6eacda6d JP |
22 | foreach($bad as $badproc) { |
23 | $mailtos = $mailnames = array(); | |
24 | $baddb = mysql_escape_string($badproc['db']); | |
70776def | 25 | if (empty($baddb)) continue; |
6eacda6d JP |
26 | $badtime = $badproc['Time']; |
27 | $badquery = $badproc['Info']; | |
6eacda6d JP |
28 | $sql = "SELECT User.UserId,User.Name,User.Email |
29 | FROM `User` | |
30 | NATURAL JOIN DBOwner | |
31 | NATURAL JOIN DB | |
82ffc3b5 JP |
32 | WHERE DB.Name = '$baddb' |
33 | AND User.UL < 10"; | |
6eacda6d JP |
34 | $r = fetchRows(DBSelect($sql),'UserId'); |
35 | foreach($r as $addy) { | |
36 | $mailtos[] = $addy['Email']; | |
37 | $mailnames[] = $addy['Name']; | |
38 | } | |
70776def JP |
39 | if (empty($mailtos)) { |
40 | echo "No email found for $baddb\n"; | |
41 | continue; | |
42 | } | |
6eacda6d JP |
43 | $mailto = implode(', ',$mailtos); |
44 | $mailname = implode(', ',$mailnames); | |
70776def | 45 | $mailsubj = "[sql.mit.edu] Slow Query on $baddb"; |
6eacda6d JP |
46 | $mailbody = "Dear $mailname: |
47 | ||
48 | A slow query was found on your database: $baddb | |
49 | It took $badtime seconds and has been aborted. | |
50 | ||
ba8e5719 JP |
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. | |
6eacda6d | 55 | |
70776def JP |
56 | The SQL Service |
57 | Email: <sql@mit.edu> | |
58 | Web: http://sql.mit.edu/ | |
6eacda6d JP |
59 | |
60 | --------------------------------------------------------------------------- | |
61 | ||
62 | $badquery"; | |
63 | ||
8ed5cf01 | 64 | mysql_query('KILL '.$badproc['Id']); |
6eacda6d JP |
65 | mail($mailto,$mailsubj,$mailbody,"From: SQL Service <sql@mit.edu>\r\nBcc: sql@mit.edu\r\n"); |
66 | } | |
67 | ||
68 | ?> |