本文實例講述了PHP實現的redis主從數據庫狀態檢測功能。分享給大家供大家參考,具體如下:
實例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
<?php /** * 檢測多個主從redis數據庫是否掛掉 * 建立從數據庫$redis_db的二維數組,內容包含每個從服務器的配置數據 */ header( "Content-Type: text/html; charset=utf-8" ); set_time_limit(0); $redis_db = array ( 'db1' => array ( 'hostname' => '127.0.0.1' , 'port' => 6379, 'password' => '' , ), 'db2' => array ( 'hostname' => '192.168.2.179' , 'port' => 6379, '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 )無法連接 ! <br/>" ; continue ; } if (! empty ( $db_key [ 'password' ])) { $pass = $redis ->auth( $db_key [ 'password' ]); if (! $pass ) { $content .= "redis從數據庫( $host )密碼錯誤 ! <br/>" ; continue ; } } try { $config = $redis ->info(); if ( 'up' == $config [ 'master_link_status' ]) { } else { $content .= "redis從數據庫( $host )掛掉了! <br/>" ; } } catch (RedisException $e ) { $content .= "redis從數據庫( $host )報錯:" . $e ->getMessage(). "<br/>" ; } } //若報錯信息不為空,發送報錯郵件 if (! empty ( $content )) { $title = '主從redis數據庫狀態檢測報錯 ' ; $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 主從redis數據庫狀態檢測報錯 郵件發送失敗!' . "\n" ; $content = str_replace ( "<br/>" , "\n" , $content ); $message .= $content ; error_log ( $message ,3, "error.log" ); } } |
希望本文所述對大家PHP程序設計有所幫助。