]> andersk Git - sql-web.git/blob - cron/checkProcesses.php
67d3a2c012a0df3facb46a63e2af115185b85f54
[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 foreach($bad as $badproc) {
22         $mailtos = $mailnames = array();
23         $baddb = mysql_escape_string($badproc['db']);
24         if (empty($baddb)) continue;
25         $badtime = $badproc['Time'];
26         $badquery = $badproc['Info'];
27         $sql = "SELECT User.UserId,User.Name,User.Email
28                         FROM `User`
29                         NATURAL JOIN DBOwner
30                         NATURAL JOIN DB
31                         WHERE DB.Name = '$baddb'
32                           AND User.UL < 10";
33         $r = fetchRows(DBSelect($sql),'UserId');
34         foreach($r as $addy) {
35                 $mailtos[] = $addy['Email'];
36                 $mailnames[] = $addy['Name'];
37         }
38         if (empty($mailtos)) {
39                 echo "No email found for $baddb\n";
40                 continue;
41         }
42         $mailto = implode(', ',$mailtos);
43         $mailname = implode(', ',$mailnames);
44         $mailsubj = "[sql.mit.edu] Slow Query on $baddb";
45         $mailbody = "Dear $mailname:
46
47 A slow query was found on your database: $baddb
48 It took $badtime seconds and has been aborted.
49
50 We do not allow inefficient SQL queries to run this long. Your query has
51 been appended to this message for your records. Please optimize your
52 queries to avoid having your queries killed in the future. If you have any
53 questions, please contact sql@mit.edu.
54
55 The SQL Service
56 Email: <sql@mit.edu>
57 Web: http://sql.mit.edu/
58
59 ---------------------------------------------------------------------------
60
61 $badquery";
62
63         mysql_query('KILL '.$badproc['Id']);
64         mail($mailto,$mailsubj,$mailbody,"From: SQL Service <sql@mit.edu>\r\nBcc: sql@mit.edu\r\n");
65 }
66
67 ?>
This page took 0.033682 seconds and 3 git commands to generate.