]> andersk Git - sql-web.git/blob - cron/checkProcesses.php
signup: lower minimum UIDs
[sql-web.git] / cron / checkProcesses.php
1 <?php
2
3 @chdir(dirname(__FILE__).'/../');
4 require_once('mitsql.cfg.php');
5 require_once('mitsql.lib.php');
6 isOffline() && exit;
7
8 define('MAX_PROC_TIME', 30);
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 print_r($bad);
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
29                         FROM `User`
30                         NATURAL JOIN DBOwner
31                         NATURAL JOIN DB
32                         WHERE DB.Name = '$baddb'
33                           AND User.UL < 10";
34         $r = fetchRows(DBSelect($sql),'UserId');
35         foreach($r as $addy) {
36                 $mailtos[] = $addy['Email'];
37                 $mailnames[] = $addy['Name'];
38         }
39         if (empty($mailtos)) {
40                 echo "No email found for $baddb\n";
41                 continue;
42         }
43         $mailto = implode(', ',$mailtos);
44         $mailname = implode(', ',$mailnames);
45         $mailsubj = "[sql.mit.edu] Slow Query on $baddb";
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
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.
55
56 The SQL Service
57 Email: <sql@mit.edu>
58 Web: http://sql.mit.edu/
59
60 ---------------------------------------------------------------------------
61
62 $badquery";
63
64         mysql_query('KILL '.$badproc['Id']);
65         mail($mailto,$mailsubj,$mailbody,"From: SQL Service <sql@mit.edu>\r\nBcc: sql@mit.edu\r\n");
66 }
67
68 ?>
This page took 0.034462 seconds and 5 git commands to generate.