github.imxd.top/hashicorp/consul@v1.4.5/agent/consul/status_endpoint.go (about) 1 package consul 2 3 import ( 4 "fmt" 5 "strconv" 6 7 "github.com/hashicorp/consul/agent/consul/autopilot" 8 ) 9 10 // Status endpoint is used to check on server status 11 type Status struct { 12 server *Server 13 } 14 15 // Ping is used to just check for connectivity 16 func (s *Status) Ping(args struct{}, reply *struct{}) error { 17 return nil 18 } 19 20 // Leader is used to get the address of the leader 21 func (s *Status) Leader(args struct{}, reply *string) error { 22 leader := string(s.server.raft.Leader()) 23 if leader != "" { 24 *reply = leader 25 } else { 26 *reply = "" 27 } 28 return nil 29 } 30 31 // Peers is used to get all the Raft peers 32 func (s *Status) Peers(args struct{}, reply *[]string) error { 33 future := s.server.raft.GetConfiguration() 34 if err := future.Error(); err != nil { 35 return err 36 } 37 38 for _, server := range future.Configuration().Servers { 39 *reply = append(*reply, string(server.Address)) 40 } 41 return nil 42 } 43 44 // Used by Autopilot to query the raft stats of the local server. 45 func (s *Status) RaftStats(args struct{}, reply *autopilot.ServerStats) error { 46 stats := s.server.raft.Stats() 47 48 var err error 49 reply.LastContact = stats["last_contact"] 50 reply.LastIndex, err = strconv.ParseUint(stats["last_log_index"], 10, 64) 51 if err != nil { 52 return fmt.Errorf("error parsing server's last_log_index value: %s", err) 53 } 54 reply.LastTerm, err = strconv.ParseUint(stats["last_log_term"], 10, 64) 55 if err != nil { 56 return fmt.Errorf("error parsing server's last_log_term value: %s", err) 57 } 58 59 return nil 60 }