Example of the redis master-slave database status detection function implemented by PHP, redis master-slave
This example describes the redis master-slave database status detection function implemented by PHP. We will share this with you for your reference. The details are as follows:
Instance:
<? Php/*** check whether multiple master-slave redis databases are down * create a two-dimensional array of slave database $ redis_db, the Content contains configuration data for each slave server */header ("Content-Type: text/html; charset = UTF-8"); set_time_limit (0 ); $ redis_db = array ('db1' => array ('hostname' => '2017. 0.0.1 ', 'Port' => 6379, 'Password' => '',), 'db2' => array ('hostname' => '2017. 168.2.179 ', 'Port' => 6379, 'Password' => '000000',),); $ content = ''; foreach ($ redis_db as $ db_key) {$ host = $ db_key ['hostname']; $ po Rt = $ db_key ['Port']; $ redis = new Redis (); // connect to the local Redis service $ status = $ redis-> connect ($ host, $ port); if (! $ Status) {$ content. = "redis cannot be connected from the database ($ host! <Br/> "; continue;} if (! Empty ($ db_key ['Password']) {$ pass = $ redis-> auth ($ db_key ['Password']); if (! $ Pass) {$ content. = "redis slave database ($ host) Password error! <Br/> "; continue; }}try {$ config = $ redis-> info (); if ('up' = $ config ['master _ link_status ']) {} else {$ content. = "redis crashed from the database ($ host! <Br/> ";}} catch (RedisException $ e) {$ content. = "redis slave database ($ host) error :". $ e-> getMessage (). "<br/>" ;}}// if the error message is not blank, send an error email if (! Empty ($ content) {$ title = 'error in Master/Slave redis database status detection '; $ content = date ("Y-m-d H: I: s ", time ()). "<br/> ". $ 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 Master/Slave redis database status detection error email sending failed! '. "\ N"; $ content = str_replace ("<br/>", "\ n", $ content); $ message. = $ content; error_log ($ message, 3, "error. log ");}}