Last active
December 19, 2015 10:19
-
-
Save Synchro/5939110 to your computer and use it in GitHub Desktop.
Simple PHP MySQL slow query (i.e. most likely stuck waiting for a lock) monitor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
$mysqli = new mysqli('localhost', 'user', 'pass'); | |
if ($mysqli->connect_errno) { | |
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; | |
} | |
$out = false; | |
$output = array(); | |
$ignorecommands = array('Sleep', 'Binlog Dump', 'Connect'); | |
foreach($mysqli->query('SHOW FULL PROCESSLIST') as $row) { | |
if (in_array($row['Command'], $ignorecommands) or $row['Info'] == 'SHOW FULL PROCESSLIST') { | |
continue; | |
} | |
//Spot anything older than 5 mins | |
if ($row['Time'] > 300) { | |
$out = true; | |
} | |
$output[] = $row; | |
} | |
if ($out and count($output) > 0) { | |
echo json_encode($output)."\n"; | |
} | |
if (count($output) > 0) { | |
file_put_contents('lockcheck.log', json_encode($output)."\n", FILE_APPEND | LOCK_EX); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment