Skip to content

Instantly share code, notes, and snippets.

@jtarleton
Created September 17, 2013 21:54
Show Gist options
  • Save jtarleton/6601219 to your computer and use it in GitHub Desktop.
Save jtarleton/6601219 to your computer and use it in GitHub Desktop.
Simple batch job to write MySQL's slave status to a file
<?php
abstract class Base
{
//An array of DB config values
protected static $config;
//Path to configuration
protected static $iniPath = 'C:\Users\winuser\Desktop\dbconfig.ini';
}
/**
* Logging Utility class
* @package Utils
* @author
*/
class Logger extends Base
{
/**
* Clear log
* @param None
* @return None
*/
public static function Clear()
{
parent::$config = parse_ini_file(parent::$iniPath);
file_put_contents(parent::$config['log_path'], null);
}
/**
* Write to log
* @param None
* @return None
*/
public static function Write(array $msgs)
{
foreach($msgs as $row){
file_put_contents(parent::$config['log_path'],
date('Y-m-d H:i:s ') . $row . PHP_EOL,
FILE_APPEND | LOCK_EX
);
}
}
}
/**
* Provides a connection to MySQL using PDO_MYSQL
* @package Utils
* @author
*/
class DAL extends Base
{
//A PDO instance
private static $db;
/**
* Return a PDO instance
* @param none
* @return Object PDO
*/
public static function getConnection()
{
Logger::clear();
Logger::write(array('Trying to connect.'));
if(!isset(self::$db))
{
try
{
self::$db = new PDO(
sprintf('mysql:dbname=%s;host=%s',
@parent::$config['db_name'],
@parent::$config['db_host'] ),
@parent::$config['db_user'],
@parent::$config['db_password']
);
if(self::$db){
Logger::write(array('Connected.'));
}
self::$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(PDOException $pdoe)
{
Logger::write(array($pdoe->getMessage()));
Logger::write(array('Terminating on error.'));
exit(1);
}
}
return self::$db;
}
}
/**
* MySQLReplicationUtils
* @package Utils
* @author
*/
class MySQLReplicationUtils extends DAL
{
/**
* Get status
*
* Write result of SHOW STATUS
*/
static public function logStatus()
{
// If no result, log error and exit
try{
$db = DAL::getConnection();
$result = $db->query("SHOW STATUS");
//Loop through query result, writing status values.
$statusInfo = $result->fetchAll();
foreach($statusInfo as $row){
foreach($row as $field=>$value){
Logger::write(array($value));
}
}
return;
}
catch(PDOException $pdoe)
{
$errorInfo=$db->errorInfo();
Logger::write(array('Query failed.'));
//Write extended MySQL error information to log.
Logger::write(array('SQLSTATE: ' . @$errorInfo[0])); //SQLSTATE error code (a five characters alphanumeric identifier defined in the ANSI SQL standard).
Logger::write(array('Error Code: ' . @$errorInfo[1])); //Driver-specific error code.
Logger::write(array('Error Message: '. @$errorInfo[2])); //Driver-specific error message.
Logger::write(array('Terminating on error.'));
exit('Database error!');
}
}
/**
* Get Slave Status
*
* Return a Boolean value indicating if the Slave MySQL server is running.
* @param None
* @return Booleam
*/
static public function getSlaveIOStatus()
{
//Assume slave is down - query to confirm it is up.
$slaveIORunning = false;
try{
// Return PDO instance.
$db = DAL::getConnection();
// Execute query
$result = $db->query("SHOW SLAVE STATUS");
// Loop over status info query result and log results
foreach($result->fetchAll(PDO::FETCH_ASSOC) as $row){
foreach($row as $key=>$value){
Logger::write(array(str_pad($key, 35, '.') . $value));
if($key == 'Slave_IO_Running' && $value == 'Yes'){
$slaveIORunning = true;
}
}
}
//Return status
return $slaveIORunning;
}
catch(PDOException $pdoe)
{
$errorInfo=$db->errorInfo();
Logger::write(array('Query failed.'));
//Write extended MySQL error information to log.
Logger::write(array('SQLSTATE: ' . @$errorInfo[0])); //SQLSTATE error code (a five characters alphanumeric identifier defined in the ANSI SQL standard).
Logger::write(array('Error Code: ' . @$errorInfo[1])); //Driver-specific error code.
Logger::write(array('Error Message: '. @$errorInfo[2])); //Driver-specific error message.
Logger::write(array('Terminating on error.'));
exit('Database error!');
}
}
}
if(MySQLReplicationUtils::getSlaveIOStatus()){
Logger::write(array('Slave_IO_Running: Yes'));
//sendMail();
}
else{
Logger::write(array('Warning - slave does not appear to be running!'));
//sendMail();
}
Logger::write(array('Terminating normally.'));
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment