github.com/therealbill/libredis@v0.0.0-20161227004305-7d50abda5ccf/examples/sentinel-cli/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	rclient "github.com/therealbill/libredis/client"
     9  )
    10  
    11  var (
    12  	network  = "tcp"
    13  	address  = "127.0.0.1:26379"
    14  	db       = 1
    15  	password = ""
    16  	timeout  = 5 * time.Second
    17  	maxidle  = 1
    18  	r        *rclient.Redis
    19  )
    20  
    21  func init() {
    22  	client, err := rclient.DialWithConfig(&rclient.DialConfig{Address: address})
    23  	if err != nil {
    24  		log.Fatal("Unable to connect to Sentinel instance:", err)
    25  	}
    26  	r = client
    27  }
    28  
    29  func main() {
    30  	// pull an INFO all call into a RedisInfoAll struct
    31  	all, err := r.SentinelInfo()
    32  	if err != nil {
    33  		log.Fatal("unable to connect and get info:", err)
    34  		return
    35  	}
    36  	// Accessing the Server section is done via all.Server
    37  	fmt.Printf("Redis Server Version: %s\n", all.Server.Version)
    38  	fmt.Printf("Redis Server Mode: %s\n", all.Server.Mode)
    39  	if all.Server.Mode != "sentinel" {
    40  		log.Fatal("Node is NOT a sentinel instance, aborting.")
    41  	}
    42  	// To get the list of managers (pods) under management, call
    43  	// SentinelMasters. It returns a MasterInfo struct.
    44  	pods, err := r.SentinelMasters()
    45  	if err != nil {
    46  		log.Fatal("Unable to run SENTINEL MASTERS command;", err)
    47  	}
    48  	fmt.Printf("Managed Pod count: %d\n", len(pods))
    49  	for _, pod := range pods {
    50  		fmt.Println("Pod Name:", pod.Name)
    51  		fmt.Println("Pod IP:", pod.IP)
    52  		fmt.Println("Pod Port:", pod.Port)
    53  		fmt.Println("Pod Slave Count:", pod.NumSlaves)
    54  		// We can easily to testing of the conditions reported
    55  		// Here we see if our master has any slaves connected This could be
    56  		// extended to talk to connected slaves to get their slave-priority.
    57  		// This would allow us to validate we have promotable slaves.
    58  		if pod.NumSlaves == 0 {
    59  			fmt.Println("!!WARNING!!\n\tThis pod has no slaves. Failover is not possible!")
    60  		}
    61  		// Here we see if our sentinel constellation has quorum on this pod
    62  		fmt.Println("Pod Quorum:", pod.Quorum)
    63  		if pod.Quorum <= pod.NumOtherSentinels {
    64  			fmt.Println("Quorum is possible")
    65  		} else {
    66  			fmt.Printf("!!CRITICAL!!\n\tQuorum is NOT possible! Need %d other sentinels, have %d\n", pod.Quorum, pod.NumOtherSentinels)
    67  		}
    68  		fmt.Println()
    69  	}
    70  
    71  }