本文实例讲述了PHP实现的redis主从数据库状态检测功能。分享给大家供大家参考,具体如下:
实例:
PHP;">
array(
'hostname' => '127.0.0.1','port' => 6379,'password' => '',),'db2'=>array(
'hostname' => '192.168.2.179','password' => '111111',);
$content = '';
foreach ($redis_db as $db_key) {
$host = $db_key['hostname'];
$port = $db_key['port'];
$redis = new Redis();
//连接本地的 Redis 服务
$status= $redis->connect($host,$port);
if(!$status) {
$content .= "redis从数据库( $host )无法连接 !
"; continue; } if(!empty($db_key['password'])) { $pass = $redis->auth($db_key['password']); if(!$pass) { $content .= "redis从数据库( $host )密码错误 !
"; continue; } } try { $config = $redis->info(); if('up' == $config['master_link_status']) { } else { $content .= "redis从数据库( $host )挂掉了!
"; } } catch(RedisException $e) { $content .= "redis从数据库( $host )报错:" . $e->getMessage()."
"; } } //若报错信息不为空,发送报错邮件 if(!empty($content)) { $title = '主从redis数据库状态检测报错 '; $content = date("Y-m-d H:i:s",time()) . "
" . $content; $sendurl = "http://localhost/api.com/test.PHP?title=".$title."&content=".$content; $result = file_get_contents($sendurl); if('ok' != $result) { $message = date("Y-m-d H:i:s",time()).' redisSlave.PHP 主从redis数据库状态检测报错 邮件发送失败!'."\n"; $content = str_replace("
","\n",$content); $message .= $content; error_log($message,3,"error.log"); } }
"; continue; } if(!empty($db_key['password'])) { $pass = $redis->auth($db_key['password']); if(!$pass) { $content .= "redis从数据库( $host )密码错误 !
"; continue; } } try { $config = $redis->info(); if('up' == $config['master_link_status']) { } else { $content .= "redis从数据库( $host )挂掉了!
"; } } catch(RedisException $e) { $content .= "redis从数据库( $host )报错:" . $e->getMessage()."
"; } } //若报错信息不为空,发送报错邮件 if(!empty($content)) { $title = '主从redis数据库状态检测报错 '; $content = date("Y-m-d H:i:s",time()) . "
" . $content; $sendurl = "http://localhost/api.com/test.PHP?title=".$title."&content=".$content; $result = file_get_contents($sendurl); if('ok' != $result) { $message = date("Y-m-d H:i:s",time()).' redisSlave.PHP 主从redis数据库状态检测报错 邮件发送失败!'."\n"; $content = str_replace("
","\n",$content); $message .= $content; error_log($message,3,"error.log"); } }
更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》及《》
希望本文所述对大家PHP程序设计有所帮助。