]> andersk Git - sql-web.git/blame - cron/checkProcesses.php
signup exits rather than die'ing
[sql-web.git] / cron / checkProcesses.php
CommitLineData
6eacda6d
JP
1<?php
2
aaaa7a8f 3@chdir(dirname(__FILE__).'/../');
6eacda6d
JP
4require_once('mitsql.cfg.php');
5require_once('mitsql.lib.php');
5852fbc4
JP
6isOffline() && exit;
7
8835695c 8define('MAX_PROC_TIME', 10);
6eacda6d 9
6eacda6d
JP
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
21foreach($bad as $badproc) {
22 $mailtos = $mailnames = array();
23 $baddb = mysql_escape_string($badproc['db']);
24 $badtime = $badproc['Time'];
25 $badquery = $badproc['Info'];
6eacda6d
JP
26 if (empty($baddb)) continue;
27 $sql = "SELECT User.UserId,User.Name,User.Email
28 FROM `User`
29 NATURAL JOIN DBOwner
30 NATURAL JOIN DB
82ffc3b5
JP
31 WHERE DB.Name = '$baddb'
32 AND User.UL < 10";
6eacda6d
JP
33 $r = fetchRows(DBSelect($sql),'UserId');
34 foreach($r as $addy) {
35 $mailtos[] = $addy['Email'];
36 $mailnames[] = $addy['Name'];
37 }
8ed5cf01 38 if (empty($mailtos)) continue;
6eacda6d
JP
39 $mailto = implode(', ',$mailtos);
40 $mailname = implode(', ',$mailnames);
41 $mailsubj = "[sql] Slow Query on $baddb";
42 $mailbody = "Dear $mailname:
43
44A slow query was found on your database: $baddb
45It took $badtime seconds and has been aborted.
46
ba8e5719
JP
47We do not allow inefficient SQL queries to run this long. Your query has
48been appended to this message for your records. Please optimize your
49queries to avoid having your queries killed in the future. If you have any
50questions, please contact sql@mit.edu.
6eacda6d 51
ba8e5719 52This SQL Service is available at sql.mit.edu.
6eacda6d
JP
53
54---------------------------------------------------------------------------
55
56$badquery";
57
8ed5cf01 58 mysql_query('KILL '.$badproc['Id']);
6eacda6d
JP
59 mail($mailto,$mailsubj,$mailbody,"From: SQL Service <sql@mit.edu>\r\nBcc: sql@mit.edu\r\n");
60}
61
62?>
This page took 0.293128 seconds and 5 git commands to generate.