Skip to content

Instantly share code, notes, and snippets.

@jmara
Last active August 29, 2015 14:12
Show Gist options
  • Save jmara/8035c07a86ff111465d9 to your computer and use it in GitHub Desktop.
Save jmara/8035c07a86ff111465d9 to your computer and use it in GitHub Desktop.
If distribution is Debian/Ubuntu
<?php
// Simple socket server
// See http://php.net/manual/en/function.stream-socket-server.php
$port = $argv[1];
$mysql_port = $argv[2];
$mysql = "/usr/bin/mysql";
$query = "SHOW SLAVE STATUS";
function set_weight($lag){
# Write your own rules here
if ($lag == 'NULL'){
return "down";
}
else if ($lag < 10){
return "up 100%";
}
else if ($lag -->= 10 && $lag < 60){
return "up 50%";
}
else
return "up 5%";
}
set_time_limit(0);
$socket = stream_socket_server("tcp://0.0.0.0:$port", $errno, $errstr);
if (!$socket) {
echo "$errstr ($errno)
n";
} else {
while ($conn = stream_socket_accept($socket,9999999999999)) {
$cmd = "$mysql -h127.0.0.1 --defaults-file=/etc/mysql/debian.cnf -Ee \"$query\" | grep Seconds_Behind_Master | cut -d ':' -f2 | tr -d ' '";
exec("$cmd",$lag);
$weight = set_weight($lag[0]);
unset($lag);
fputs ($conn, $weight);
fclose ($conn);
}
fclose($socket);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment