Last active
August 29, 2015 14:12
-
-
Save jmara/8035c07a86ff111465d9 to your computer and use it in GitHub Desktop.
If distribution is Debian/Ubuntu
This file contains hidden or 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
<?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