]> andersk Git - sql-web.git/blame_incremental - cron/checkProcesses.php
cron scripts dont run if offline
[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', 20);
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
21foreach($bad as $badproc) {
22 $mailtos = $mailnames = array();
23 $baddb = mysql_escape_string($badproc['db']);
24 $badtime = $badproc['Time'];
25 $badquery = $badproc['Info'];
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
31 WHERE DB.Name = '$baddb'";
32 $r = fetchRows(DBSelect($sql),'UserId');
33 foreach($r as $addy) {
34 $mailtos[] = $addy['Email'];
35 $mailnames[] = $addy['Name'];
36 }
37 if (empty($mailtos)) continue;
38 $mailto = implode(', ',$mailtos);
39 $mailname = implode(', ',$mailnames);
40 $mailsubj = "[sql] Slow Query on $baddb";
41 $mailbody = "Dear $mailname:
42
43A slow query was found on your database: $baddb
44It took $badtime seconds and has been aborted.
45
46We do not allow inefficient SQL queries to run this long. Your query has
47been appended to this message for your records. Please optimize your
48queries to avoid having your queries killed in the future. If you have any
49questions, please contact sql@mit.edu.
50
51This SQL Service is available at sql.mit.edu.
52
53---------------------------------------------------------------------------
54
55$badquery";
56
57 mysql_query('KILL '.$badproc['Id']);
58 mail($mailto,$mailsubj,$mailbody,"From: SQL Service <sql@mit.edu>\r\nBcc: sql@mit.edu\r\n");
59}
60
61?>
This page took 0.526486 seconds and 5 git commands to generate.