2014-01-08 21:52:09 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2014-01-08 21:56:34 +00:00
|
|
|
"fmt"
|
2014-04-04 21:55:44 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2014-01-08 21:52:09 +00:00
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Health endpoint is used to query the health information
|
|
|
|
type Health struct {
|
|
|
|
srv *Server
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChecksInState is used to get all the checks in a given state
|
|
|
|
func (h *Health) ChecksInState(args *structs.ChecksInStateRequest,
|
2014-02-05 21:30:18 +00:00
|
|
|
reply *structs.IndexedHealthChecks) error {
|
2014-04-19 00:17:12 +00:00
|
|
|
if done, err := h.srv.forward("Health.ChecksInState", args, args, reply); done {
|
2014-01-08 21:52:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-01-08 21:56:34 +00:00
|
|
|
// Get the state specific checks
|
2015-10-13 05:21:39 +00:00
|
|
|
state := h.srv.fsm.State()
|
|
|
|
return h.srv.blockingRPC(
|
2015-10-12 07:42:09 +00:00
|
|
|
&args.QueryOptions,
|
2014-04-21 18:04:52 +00:00
|
|
|
&reply.QueryMeta,
|
2015-10-13 03:12:13 +00:00
|
|
|
state.GetQueryWatch("ChecksInState"),
|
2014-04-21 18:18:27 +00:00
|
|
|
func() error {
|
2017-01-14 01:08:43 +00:00
|
|
|
var index uint64
|
|
|
|
var checks structs.HealthChecks
|
|
|
|
var err error
|
|
|
|
if len(args.NodeMetaFilters) > 0 {
|
|
|
|
index, checks, err = state.ChecksInStateByNodeMeta(args.State, args.NodeMetaFilters)
|
|
|
|
} else {
|
|
|
|
index, checks, err = state.ChecksInState(args.State)
|
|
|
|
}
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Index, reply.HealthChecks = index, checks
|
2015-07-27 21:41:46 +00:00
|
|
|
if err := h.srv.filterACL(args.Token, reply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return h.srv.sortNodesByDistanceFrom(args.Source, reply.HealthChecks)
|
2014-02-05 21:30:18 +00:00
|
|
|
})
|
2014-01-08 21:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NodeChecks is used to get all the checks for a node
|
|
|
|
func (h *Health) NodeChecks(args *structs.NodeSpecificRequest,
|
2014-02-05 21:30:18 +00:00
|
|
|
reply *structs.IndexedHealthChecks) error {
|
2014-04-19 00:17:12 +00:00
|
|
|
if done, err := h.srv.forward("Health.NodeChecks", args, args, reply); done {
|
2014-01-08 21:52:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-01-08 21:56:34 +00:00
|
|
|
// Get the node checks
|
2015-10-13 05:21:39 +00:00
|
|
|
state := h.srv.fsm.State()
|
|
|
|
return h.srv.blockingRPC(
|
2015-10-12 07:42:09 +00:00
|
|
|
&args.QueryOptions,
|
2014-04-21 18:04:52 +00:00
|
|
|
&reply.QueryMeta,
|
2015-10-13 03:12:13 +00:00
|
|
|
state.GetQueryWatch("NodeChecks"),
|
2014-04-21 18:18:27 +00:00
|
|
|
func() error {
|
2015-10-12 07:42:09 +00:00
|
|
|
index, checks, err := state.NodeChecks(args.Node)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Index, reply.HealthChecks = index, checks
|
2015-06-11 19:48:38 +00:00
|
|
|
return h.srv.filterACL(args.Token, reply)
|
2014-02-05 21:30:18 +00:00
|
|
|
})
|
2014-01-08 21:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceChecks is used to get all the checks for a service
|
|
|
|
func (h *Health) ServiceChecks(args *structs.ServiceSpecificRequest,
|
2014-02-05 21:30:18 +00:00
|
|
|
reply *structs.IndexedHealthChecks) error {
|
2014-01-08 21:56:34 +00:00
|
|
|
// Reject if tag filtering is on
|
|
|
|
if args.TagFilter {
|
|
|
|
return fmt.Errorf("Tag filtering is not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Potentially forward
|
2014-04-19 00:17:12 +00:00
|
|
|
if done, err := h.srv.forward("Health.ServiceChecks", args, args, reply); done {
|
2014-01-08 21:52:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-01-08 21:56:34 +00:00
|
|
|
// Get the service checks
|
2015-10-13 05:21:39 +00:00
|
|
|
state := h.srv.fsm.State()
|
|
|
|
return h.srv.blockingRPC(
|
2015-10-12 07:42:09 +00:00
|
|
|
&args.QueryOptions,
|
2014-04-21 18:04:52 +00:00
|
|
|
&reply.QueryMeta,
|
2015-10-13 03:12:13 +00:00
|
|
|
state.GetQueryWatch("ServiceChecks"),
|
2014-04-21 18:18:27 +00:00
|
|
|
func() error {
|
2017-01-14 01:08:43 +00:00
|
|
|
var index uint64
|
|
|
|
var checks structs.HealthChecks
|
|
|
|
var err error
|
|
|
|
if len(args.NodeMetaFilters) > 0 {
|
|
|
|
index, checks, err = state.ServiceChecksByNodeMeta(args.ServiceName, args.NodeMetaFilters)
|
|
|
|
} else {
|
|
|
|
index, checks, err = state.ServiceChecks(args.ServiceName)
|
|
|
|
}
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Index, reply.HealthChecks = index, checks
|
2015-07-27 21:41:46 +00:00
|
|
|
if err := h.srv.filterACL(args.Token, reply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return h.srv.sortNodesByDistanceFrom(args.Source, reply.HealthChecks)
|
2014-02-05 21:30:18 +00:00
|
|
|
})
|
2014-01-08 21:52:09 +00:00
|
|
|
}
|
2014-01-08 23:06:13 +00:00
|
|
|
|
|
|
|
// ServiceNodes returns all the nodes registered as part of a service including health info
|
2014-02-05 21:30:18 +00:00
|
|
|
func (h *Health) ServiceNodes(args *structs.ServiceSpecificRequest, reply *structs.IndexedCheckServiceNodes) error {
|
2014-04-19 00:17:12 +00:00
|
|
|
if done, err := h.srv.forward("Health.ServiceNodes", args, args, reply); done {
|
2014-01-08 23:06:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the arguments
|
|
|
|
if args.ServiceName == "" {
|
|
|
|
return fmt.Errorf("Must provide service name")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the nodes
|
2015-10-13 05:21:39 +00:00
|
|
|
state := h.srv.fsm.State()
|
|
|
|
err := h.srv.blockingRPC(
|
2015-10-12 07:42:09 +00:00
|
|
|
&args.QueryOptions,
|
2014-04-21 18:04:52 +00:00
|
|
|
&reply.QueryMeta,
|
2015-10-13 03:12:13 +00:00
|
|
|
state.GetQueryWatch("CheckServiceNodes"),
|
2014-04-21 18:18:27 +00:00
|
|
|
func() error {
|
2015-10-12 07:42:09 +00:00
|
|
|
var index uint64
|
|
|
|
var nodes structs.CheckServiceNodes
|
|
|
|
var err error
|
2014-02-05 21:30:18 +00:00
|
|
|
if args.TagFilter {
|
2015-10-12 07:42:09 +00:00
|
|
|
index, nodes, err = state.CheckServiceTagNodes(args.ServiceName, args.ServiceTag)
|
2014-02-05 21:30:18 +00:00
|
|
|
} else {
|
2015-10-12 07:42:09 +00:00
|
|
|
index, nodes, err = state.CheckServiceNodes(args.ServiceName)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-02-05 21:30:18 +00:00
|
|
|
}
|
2015-07-27 21:41:46 +00:00
|
|
|
|
2015-10-12 07:42:09 +00:00
|
|
|
reply.Index, reply.Nodes = index, nodes
|
2017-01-14 01:08:43 +00:00
|
|
|
if len(args.NodeMetaFilters) > 0 {
|
|
|
|
var filtered structs.CheckServiceNodes
|
|
|
|
for _, node := range nodes {
|
|
|
|
if structs.SatisfiesMetaFilters(node.Node.Meta, args.NodeMetaFilters) {
|
|
|
|
filtered = append(filtered, node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reply.Nodes = filtered
|
|
|
|
}
|
2015-07-27 21:41:46 +00:00
|
|
|
if err := h.srv.filterACL(args.Token, reply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return h.srv.sortNodesByDistanceFrom(args.Source, reply.Nodes)
|
2014-02-05 21:30:18 +00:00
|
|
|
})
|
2014-04-04 21:55:44 +00:00
|
|
|
|
|
|
|
// Provide some metrics
|
|
|
|
if err == nil {
|
|
|
|
metrics.IncrCounter([]string{"consul", "health", "service", "query", args.ServiceName}, 1)
|
|
|
|
if args.ServiceTag != "" {
|
|
|
|
metrics.IncrCounter([]string{"consul", "health", "service", "query-tag", args.ServiceName, args.ServiceTag}, 1)
|
|
|
|
}
|
|
|
|
if len(reply.Nodes) == 0 {
|
|
|
|
metrics.IncrCounter([]string{"consul", "health", "service", "not-found", args.ServiceName}, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
2014-01-08 23:06:13 +00:00
|
|
|
}
|