]> andersk Git - sql-web.git/blob - cron/checkProcesses.php
contact: page formatting
[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 define('MAX_PROC_TIME', 10);
7
8 $baddbs = $badusers = $bad = array();
9 $result = mysql_query('SHOW FULL PROCESSLIST');
10 while ($row = mysql_fetch_assoc($result)){
11         if ($row['Time']>MAX_PROC_TIME && $row['Command']!='Sleep') {
12                 $bad[] = $row;
13                 $baddbs[] = $row['db'];
14                 $badusers[] = $row['User'];
15         }
16 }
17 mysql_free_result($result);
18
19 foreach($bad as $badproc) {
20         $mailtos = $mailnames = array();
21         $baddb = mysql_escape_string($badproc['db']);
22         $badtime = $badproc['Time'];
23         $badquery = $badproc['Info'];
24         if (empty($baddb)) continue;
25         $sql = "SELECT User.UserId,User.Name,User.Email
26                         FROM `User`
27                         NATURAL JOIN DBOwner
28                         NATURAL JOIN DB
29                         WHERE DB.Name = '$baddb'";
30         $r = fetchRows(DBSelect($sql),'UserId');
31         foreach($r as $addy) {
32                 $mailtos[] = $addy['Email'];
33                 $mailnames[] = $addy['Name'];
34         }
35         if (empty($mailtos)) continue;
36         $mailto = implode(', ',$mailtos);
37         $mailname = implode(', ',$mailnames);
38         $mailsubj = "[sql] Slow Query on $baddb";
39         $mailbody = "Dear $mailname:
40
41 A slow query was found on your database: $baddb
42 It took $badtime seconds and has been aborted.
43
44 We do not allow inefficient SQL queries to run this long. Your query has
45 been appended to this message for your records. Please optimize your
46 queries to avoid having your queries killed in the future. If you have any
47 questions, please contact sql@mit.edu.
48
49 This SQL Service is available at sql.mit.edu.
50
51 ---------------------------------------------------------------------------
52
53 $badquery";
54
55         mysql_query('KILL '.$badproc['Id']);
56         mail($mailto,$mailsubj,$mailbody,"From: SQL Service <sql@mit.edu>\r\nBcc: sql@mit.edu\r\n");
57 }
58
59 ?>
This page took 0.033118 seconds and 5 git commands to generate.