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