]> andersk Git - sql-web.git/blame_incremental - cron/checkProcesses.php
signup: lower minimum UIDs
[sql-web.git] / cron / checkProcesses.php
... / ...
CommitLineData
1<?php
2
3@chdir(dirname(__FILE__).'/../');
4require_once('mitsql.cfg.php');
5require_once('mitsql.lib.php');
6isOffline() && exit;
7
8define('MAX_PROC_TIME', 30);
9
10$baddbs = $badusers = $bad = array();
11$result = mysql_query('SHOW FULL PROCESSLIST');
12while ($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}
19mysql_free_result($result);
20
21print_r($bad);
22foreach($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
48A slow query was found on your database: $baddb
49It took $badtime seconds and has been aborted.
50
51We do not allow inefficient SQL queries to run this long. Your query has
52been appended to this message for your records. Please optimize your
53queries to avoid having your queries killed in the future. If you have any
54questions, please contact sql@mit.edu.
55
56The SQL Service
57Email: <sql@mit.edu>
58Web: 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.03024 seconds and 5 git commands to generate.