2015-08-28 10:26:23 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
consul "github.com/hashicorp/consul/api"
|
2018-09-16 00:48:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2015-08-28 10:26:23 +00:00
|
|
|
)
|
|
|
|
|
2015-12-11 17:01:28 +00:00
|
|
|
const (
|
2015-12-11 21:47:35 +00:00
|
|
|
consulAvailable = "available"
|
|
|
|
consulUnavailable = "unavailable"
|
2015-12-11 17:01:28 +00:00
|
|
|
)
|
|
|
|
|
2016-09-01 20:38:31 +00:00
|
|
|
// ConsulFingerprint is used to fingerprint for Consul
|
2015-08-28 10:26:23 +00:00
|
|
|
type ConsulFingerprint struct {
|
2018-09-16 00:48:59 +00:00
|
|
|
logger log.Logger
|
2015-12-11 17:01:28 +00:00
|
|
|
client *consul.Client
|
|
|
|
lastState string
|
2015-08-28 10:26:23 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 18:02:19 +00:00
|
|
|
// NewConsulFingerprint is used to create a Consul fingerprint
|
2018-09-16 00:48:59 +00:00
|
|
|
func NewConsulFingerprint(logger log.Logger) Fingerprint {
|
|
|
|
return &ConsulFingerprint{logger: logger.Named("consul"), lastState: consulUnavailable}
|
2015-08-28 10:26:23 +00:00
|
|
|
}
|
|
|
|
|
2018-12-01 16:10:39 +00:00
|
|
|
func (f *ConsulFingerprint) Fingerprint(req *FingerprintRequest, resp *FingerprintResponse) error {
|
2015-11-22 21:42:06 +00:00
|
|
|
// Only create the client once to avoid creating too many connections to
|
|
|
|
// Consul.
|
|
|
|
if f.client == nil {
|
2018-01-24 14:09:53 +00:00
|
|
|
consulConfig, err := req.Config.ConsulConfig.ApiConfig()
|
2015-11-22 21:42:06 +00:00
|
|
|
if err != nil {
|
2018-01-24 14:09:53 +00:00
|
|
|
return fmt.Errorf("Failed to initialize the Consul client config: %v", err)
|
2015-11-22 21:42:06 +00:00
|
|
|
}
|
2015-08-28 10:26:23 +00:00
|
|
|
|
2015-11-22 21:42:06 +00:00
|
|
|
f.client, err = consul.NewClient(consulConfig)
|
|
|
|
if err != nil {
|
2018-01-24 14:09:53 +00:00
|
|
|
return fmt.Errorf("Failed to initialize consul client: %s", err)
|
2015-11-22 21:42:06 +00:00
|
|
|
}
|
2015-08-28 10:26:23 +00:00
|
|
|
}
|
|
|
|
|
2015-09-01 02:56:25 +00:00
|
|
|
// We'll try to detect consul by making a query to to the agent's self API.
|
|
|
|
// If we can't hit this URL consul is probably not running on this machine.
|
2015-11-22 21:42:06 +00:00
|
|
|
info, err := f.client.Agent().Self()
|
2015-08-28 10:26:23 +00:00
|
|
|
if err != nil {
|
2018-01-26 19:31:37 +00:00
|
|
|
f.clearConsulAttributes(resp)
|
2015-12-11 17:01:28 +00:00
|
|
|
|
|
|
|
// Print a message indicating that the Consul Agent is not available
|
|
|
|
// anymore
|
2015-12-11 21:47:35 +00:00
|
|
|
if f.lastState == consulAvailable {
|
2018-09-16 00:48:59 +00:00
|
|
|
f.logger.Info("consul agent is unavailable")
|
2015-12-11 17:01:28 +00:00
|
|
|
}
|
2015-12-11 21:47:35 +00:00
|
|
|
f.lastState = consulUnavailable
|
2018-01-24 14:09:53 +00:00
|
|
|
return nil
|
2015-08-28 10:26:23 +00:00
|
|
|
}
|
|
|
|
|
2017-10-12 01:25:54 +00:00
|
|
|
if s, ok := info["Config"]["Server"].(bool); ok {
|
2018-01-26 16:21:07 +00:00
|
|
|
resp.AddAttribute("consul.server", strconv.FormatBool(s))
|
2017-10-12 01:25:54 +00:00
|
|
|
} else {
|
2018-09-16 00:48:59 +00:00
|
|
|
f.logger.Warn("unable to fingerprint consul.server")
|
2017-10-12 01:25:54 +00:00
|
|
|
}
|
|
|
|
if v, ok := info["Config"]["Version"].(string); ok {
|
2018-01-26 16:21:07 +00:00
|
|
|
resp.AddAttribute("consul.version", v)
|
2017-10-12 01:25:54 +00:00
|
|
|
} else {
|
2018-09-16 00:48:59 +00:00
|
|
|
f.logger.Warn("unable to fingerprint consul.version")
|
2017-10-12 01:25:54 +00:00
|
|
|
}
|
|
|
|
if r, ok := info["Config"]["Revision"].(string); ok {
|
2018-01-26 16:21:07 +00:00
|
|
|
resp.AddAttribute("consul.revision", r)
|
2017-10-12 01:25:54 +00:00
|
|
|
} else {
|
2018-09-16 00:48:59 +00:00
|
|
|
f.logger.Warn("unable to fingerprint consul.revision")
|
2017-10-12 01:25:54 +00:00
|
|
|
}
|
|
|
|
if n, ok := info["Config"]["NodeName"].(string); ok {
|
2018-01-26 16:21:07 +00:00
|
|
|
resp.AddAttribute("unique.consul.name", n)
|
2017-10-12 01:25:54 +00:00
|
|
|
} else {
|
2018-09-16 00:48:59 +00:00
|
|
|
f.logger.Warn("unable to fingerprint unique.consul.name")
|
2017-10-12 01:25:54 +00:00
|
|
|
}
|
|
|
|
if d, ok := info["Config"]["Datacenter"].(string); ok {
|
2018-01-26 16:21:07 +00:00
|
|
|
resp.AddAttribute("consul.datacenter", d)
|
2017-10-12 01:25:54 +00:00
|
|
|
} else {
|
2018-09-16 00:48:59 +00:00
|
|
|
f.logger.Warn("unable to fingerprint consul.datacenter")
|
2017-10-12 01:25:54 +00:00
|
|
|
}
|
2020-10-02 19:15:59 +00:00
|
|
|
if g, ok := info["Member"]["Tags"].(map[string]interface{}); ok {
|
|
|
|
if s, ok := g["segment"].(string); ok {
|
|
|
|
resp.AddAttribute("consul.segment", s)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
f.logger.Warn("unable to fingerprint consul.segment")
|
|
|
|
}
|
2018-01-30 17:57:37 +00:00
|
|
|
if dc, ok := resp.Attributes["consul.datacenter"]; ok {
|
|
|
|
if name, ok2 := resp.Attributes["unique.consul.name"]; ok2 {
|
|
|
|
resp.AddLink("consul", fmt.Sprintf("%s.%s", dc, name))
|
|
|
|
}
|
2017-10-12 01:25:54 +00:00
|
|
|
} else {
|
2018-09-16 00:48:59 +00:00
|
|
|
f.logger.Warn("malformed Consul response prevented linking")
|
2017-10-12 01:25:54 +00:00
|
|
|
}
|
2015-08-28 10:35:44 +00:00
|
|
|
|
2015-12-11 17:01:28 +00:00
|
|
|
// If the Consul Agent was previously unavailable print a message to
|
|
|
|
// indicate the Agent is available now
|
2015-12-11 21:47:35 +00:00
|
|
|
if f.lastState == consulUnavailable {
|
2018-09-16 00:48:59 +00:00
|
|
|
f.logger.Info("consul agent is available")
|
2015-12-11 17:01:28 +00:00
|
|
|
}
|
2015-12-11 21:47:35 +00:00
|
|
|
f.lastState = consulAvailable
|
2018-01-31 22:03:55 +00:00
|
|
|
resp.Detected = true
|
2018-01-24 14:09:53 +00:00
|
|
|
return nil
|
2015-08-28 10:26:23 +00:00
|
|
|
}
|
2015-11-05 21:46:02 +00:00
|
|
|
|
2018-01-26 19:31:37 +00:00
|
|
|
// clearConsulAttributes removes consul attributes and links from the passed
|
|
|
|
// Node.
|
2018-12-01 16:10:39 +00:00
|
|
|
func (f *ConsulFingerprint) clearConsulAttributes(r *FingerprintResponse) {
|
2018-01-26 19:31:37 +00:00
|
|
|
r.RemoveAttribute("consul.server")
|
|
|
|
r.RemoveAttribute("consul.version")
|
|
|
|
r.RemoveAttribute("consul.revision")
|
|
|
|
r.RemoveAttribute("unique.consul.name")
|
|
|
|
r.RemoveAttribute("consul.datacenter")
|
2020-10-02 19:15:59 +00:00
|
|
|
r.RemoveAttribute("consul.segment")
|
2018-01-26 19:31:37 +00:00
|
|
|
r.RemoveLink("consul")
|
|
|
|
}
|
|
|
|
|
2015-11-05 21:46:02 +00:00
|
|
|
func (f *ConsulFingerprint) Periodic() (bool, time.Duration) {
|
2015-11-05 21:41:41 +00:00
|
|
|
return true, 15 * time.Second
|
2015-11-05 21:46:02 +00:00
|
|
|
}
|