Skip to content

Instantly share code, notes, and snippets.

@Synchro
Last active December 19, 2015 10:19
Show Gist options
  • Save Synchro/5939110 to your computer and use it in GitHub Desktop.
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.
#!/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