]> andersk Git - sql-web.git/blame_incremental - cron/checkProcesses.php
git-svn-id: svn://presbrey.mit.edu/sql/mitsql@76 a142d4bd-2cfb-0310-9673-cb33a7e74f58
[sql-web.git] / cron / checkProcesses.php
... / ...
CommitLineData
1<?php
2
3@chdir(dirname(__FILE__).'/../');
4require_once('mitsql.cfg.php');
5require_once('mitsql.lib.php');
6define('MAX_PROC_TIME', 10);
7
8$baddbs = $badusers = $bad = array();
9$result = mysql_query('SHOW FULL PROCESSLIST');
10while ($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}
17mysql_free_result($result);
18
19foreach($bad as $badproc) {
20 $mailtos = $mailnames = array();
21 $baddb = mysql_escape_string($badproc['db']);
22 $badtime = $badproc['Time'];
23 $badquery = $badproc['Info'];
24 mysql_query('KILL '.$badproc['Id']);
25 if (empty($baddb)) continue;
26 $sql = "SELECT User.UserId,User.Name,User.Email
27 FROM `User`
28 NATURAL JOIN DBOwner
29 NATURAL JOIN DB
30 WHERE DB.Name = '$baddb'";
31 $r = fetchRows(DBSelect($sql),'UserId');
32 foreach($r as $addy) {
33 $mailtos[] = $addy['Email'];
34 $mailnames[] = $addy['Name'];
35 }
36 $mailto = implode(', ',$mailtos);
37 $mailname = implode(', ',$mailnames);
38 $mailsubj = "[sql] Slow Query on $baddb";
39 $mailbody = "Dear $mailname:
40
41A slow query was found on your database: $baddb
42It took $badtime seconds and has been aborted.
43
44We do not allow inefficient SQL queries to run this long. Your query has
45been appended to this message for your records. Please optimize your
46queries to avoid having your queries killed in the future. If you have any
47questions, please contact sql@mit.edu.
48
49This SQL Service is available at sql.mit.edu.
50
51---------------------------------------------------------------------------
52
53$badquery";
54
55 $mailto = 'jwp@mit.edu';
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.028114 seconds and 5 git commands to generate.