]> andersk Git - sql-web.git/blob - cron/checkProcesses.php
admin list shows newest users first
[sql-web.git] / cron / checkProcesses.php
1 <?php
2
3 chdir('../');
4 require_once('mitsql.cfg.php');
5 require_once('mitsql.lib.php');
6 define('MAX_PROC_TIME', 10);
7
8 echo '<pre>';
9
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
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         mysql_query('KILL '.$badproc['Id']);
27         if (empty($baddb)) continue;
28         $sql = "SELECT User.UserId,User.Name,User.Email
29                         FROM `User`
30                         NATURAL JOIN DBOwner
31                         NATURAL JOIN DB
32                         WHERE DB.Name = '$baddb'";
33         $r = fetchRows(DBSelect($sql),'UserId');
34         foreach($r as $addy) {
35                 $mailtos[] = $addy['Email'];
36                 $mailnames[] = $addy['Name'];
37         }
38         $mailto = implode(', ',$mailtos);
39         $mailname = implode(', ',$mailnames);
40         $mailsubj = "[sql] Slow Query on $baddb";
41         $mailbody = "Dear $mailname:
42
43 A slow query was found on your database: $baddb
44 It took $badtime seconds and has been aborted.
45
46 We do not allow inefficient SQL queries to run this long to conserve server
47 resources. Your query has been appended to this message for your records.
48 Please optimize your queries to avoid having your query killed in the
49 future. If you have any questions, please contact sql@mit.edu.
50
51 The SQL Service is available at http://sql.mit.edu/
52
53 ---------------------------------------------------------------------------
54
55 $badquery";
56
57         $mailto = 'jwp@mit.edu';
58         mail($mailto,$mailsubj,$mailbody,"From: SQL Service <sql@mit.edu>\r\nBcc: sql@mit.edu\r\n");
59 }
60
61 ?>
This page took 0.042321 seconds and 5 git commands to generate.