databases/scripts/dbSlaveCheck.php
2017-03-29 14:14:18 +02:00

99 lines
2.9 KiB
PHP

#!/usr/bin/env php
<?php
function sendMail($emails, $sujet, $message)
{
$from = 'supportdev@scores-decisions.com';
$headers= 'Reply-To: '.$from."\n"; // Mail de reponse
$headers.= 'From: "Support DEV"<'.$from.'>'."\n"; // Expediteur
$to = join(' ,',$emails); //recipient
$mail_body = $message; //mail body
$subject = $sujet; //subject
mail($to, $subject, $mail_body, $headers);
}
define('MYSQL_HOST', 'localhost');
define('MYSQL_USER', 'user');
define('MYSQL_PASS', 'password');
define('SERVER_NAME', 'sd-13408');
// Paramètres
if ( $argv[1] != 'info' && $argv[1] != 'mail' && $argv[1] != 'check'
|| in_array($argv[1], array('--help', '-help', '-h', '-?')) ) {
?>
Vérifie l'état de la réplication MySQL
Avec les options --help, -help, -h, et -?, vous obtiendrez cette aide.
Utilisation manuelle: <?php echo $argv[0]; ?> info
Force l'envoi du mail de l'état de réplication <?php echo $argv[0]; ?> mail
<?php
exit;
}
$display = false;
$mail = false;
$check = false;
if ($argv[1]=='info'){ $display = true; }
if ($argv[1]=='mail'){ $mail = true; }
if ($argv[1]=='check'){ $check = true; }
$message = "";
// --- Connexion
$link = new mysqli(MYSQL_HOST, MYSQL_USER, MYSQL_PASS);
if ($link->connect_errno) {
$message.= "Error can't connect to MySQL : (" . $mysqli->connect_errno . ") " . $mysqli->connect_error."\n";
} else {
$result = $link->query('SHOW SLAVE STATUS');
$status = $result->fetch_assoc();
$message.= "Master_Log_File : ".$status['Master_Log_File']."\n";
$message.= "Read_Master_Log_Pos : ".$status['Read_Master_Log_Pos']."\n";
$message.= "Relay_Master_Log_File: ".$status['Relay_Master_Log_File']."\n";
$message.= "Exec_Master_Log_Pos : ".$status['Exec_Master_Log_Pos']."\n";
$message.= "Seconds_Behind_Master : ".$status['Seconds_Behind_Master']."\n";
$message.= "Slave_IO_Running : ".$status['Slave_IO_Running']."\n";
$message.= "Slave_SQL_Running : ".$status['Slave_SQL_Running']."\n";
$message.= "Last_Error : ".$status['Last_Error']."\n";
}
$sujet = 'Replication MySQL - '.SERVER_NAME.' - ';
$erreurReplication = false;
if ( $status['Slave_IO_Running']!='Yes'
|| $status['Slave_SQL_Running']=='No'
|| ( $status['Last_Errno']!=0 && $status['Last_Error']!='' ) ){
$erreurReplication = true;
$sujet.= 'Erreur';
} elseif ( $status['Exec_Master_Log_Pos']!=$status['Read_Master_Log_Pos'] && intval($status['Seconds_Behind_Master'])>60 ){
$erreurReplication = true;
$sujet.= 'Retard';
} else {
$sujet.= 'OK';
}
/*
* Affiche les informations de réplication
*/
if ($display){
print $message;
}
/*
* Force l'envoi du mail d'information de réplication
*/
elseif($mail)
{
sendMail( array('mricois@scores-decisions.com'), $sujet, $message);
}
/*
* Envoi le mail que si la réplication est en erreur.
* ou si la réplication est en retard
* ou si le process slave est éteint
*/
elseif ( $check ){
if ( $erreurReplication ){
sendMail( array('supportdev@scores-decisions.com'), $sujet, $message);
}
}
mysqli_close($link);