2022-05-24 21:49:47 +08:00
|
|
|
//go:generate ../../../tools/readme_config_includer/generator
|
2016-06-01 18:02:28 +08:00
|
|
|
package consul
|
|
|
|
|
|
|
|
|
|
import (
|
2022-05-24 21:49:47 +08:00
|
|
|
_ "embed"
|
2016-06-01 18:02:28 +08:00
|
|
|
"net/http"
|
2018-05-18 05:24:51 +08:00
|
|
|
"strings"
|
2016-06-01 18:02:28 +08:00
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
2022-05-24 21:49:47 +08:00
|
|
|
|
2016-06-01 18:02:28 +08:00
|
|
|
"github.com/influxdata/telegraf"
|
2020-06-26 02:44:22 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/common/tls"
|
2016-06-01 18:02:28 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
|
)
|
|
|
|
|
|
2022-05-24 21:49:47 +08:00
|
|
|
//go:embed sample.conf
|
|
|
|
|
var sampleConfig string
|
|
|
|
|
|
2016-06-01 18:02:28 +08:00
|
|
|
type Consul struct {
|
|
|
|
|
Address string
|
|
|
|
|
Scheme string
|
|
|
|
|
Token string
|
|
|
|
|
Username string
|
|
|
|
|
Password string
|
2024-06-06 04:19:47 +08:00
|
|
|
Datacentre string `toml:"datacentre" deprecated:"1.10.0;1.35.0;use 'datacenter' instead"`
|
2019-01-24 06:10:38 +08:00
|
|
|
Datacenter string
|
2018-05-05 07:33:23 +08:00
|
|
|
tls.ClientConfig
|
2020-10-07 23:31:02 +08:00
|
|
|
TagDelimiter string
|
|
|
|
|
MetricVersion int
|
|
|
|
|
Log telegraf.Logger
|
2016-06-01 18:02:28 +08:00
|
|
|
|
|
|
|
|
// client used to connect to Consul agnet
|
|
|
|
|
client *api.Client
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 21:49:47 +08:00
|
|
|
func (*Consul) SampleConfig() string {
|
|
|
|
|
return sampleConfig
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-07 23:31:02 +08:00
|
|
|
func (c *Consul) Init() error {
|
|
|
|
|
if c.MetricVersion != 2 {
|
|
|
|
|
c.Log.Warnf("Use of deprecated configuration: 'metric_version = 1'; please update to 'metric_version = 2'")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-01 18:02:28 +08:00
|
|
|
func (c *Consul) createAPIClient() (*api.Client, error) {
|
|
|
|
|
config := api.DefaultConfig()
|
|
|
|
|
|
|
|
|
|
if c.Address != "" {
|
|
|
|
|
config.Address = c.Address
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.Scheme != "" {
|
|
|
|
|
config.Scheme = c.Scheme
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.Datacentre != "" {
|
|
|
|
|
config.Datacenter = c.Datacentre
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-24 06:10:38 +08:00
|
|
|
if c.Datacenter != "" {
|
|
|
|
|
config.Datacenter = c.Datacenter
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-24 03:31:27 +08:00
|
|
|
if c.Token != "" {
|
|
|
|
|
config.Token = c.Token
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-01 18:02:28 +08:00
|
|
|
if c.Username != "" {
|
|
|
|
|
config.HttpAuth = &api.HttpBasicAuth{
|
|
|
|
|
Username: c.Username,
|
|
|
|
|
Password: c.Password,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-05 07:33:23 +08:00
|
|
|
tlsCfg, err := c.ClientConfig.TLSConfig()
|
2016-06-01 18:02:28 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-18 05:24:51 +08:00
|
|
|
config.Transport = &http.Transport{
|
2016-06-01 18:02:28 +08:00
|
|
|
TLSClientConfig: tlsCfg,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return api.NewClient(config)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Consul) GatherHealthCheck(acc telegraf.Accumulator, checks []*api.HealthCheck) {
|
|
|
|
|
for _, check := range checks {
|
|
|
|
|
record := make(map[string]interface{})
|
|
|
|
|
tags := make(map[string]string)
|
|
|
|
|
|
2017-01-29 08:47:25 +08:00
|
|
|
record["passing"] = 0
|
|
|
|
|
record["critical"] = 0
|
|
|
|
|
record["warning"] = 0
|
|
|
|
|
record[check.Status] = 1
|
2016-06-01 18:02:28 +08:00
|
|
|
|
2020-10-07 23:31:02 +08:00
|
|
|
if c.MetricVersion == 2 {
|
|
|
|
|
tags["check_name"] = check.Name
|
|
|
|
|
tags["service_id"] = check.ServiceID
|
|
|
|
|
tags["status"] = check.Status
|
|
|
|
|
} else {
|
|
|
|
|
record["check_name"] = check.Name
|
|
|
|
|
record["service_id"] = check.ServiceID
|
|
|
|
|
record["status"] = check.Status
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-01 18:02:28 +08:00
|
|
|
tags["node"] = check.Node
|
|
|
|
|
tags["service_name"] = check.ServiceName
|
2016-12-20 21:03:31 +08:00
|
|
|
tags["check_id"] = check.CheckID
|
2016-06-01 18:02:28 +08:00
|
|
|
|
2018-05-18 05:24:51 +08:00
|
|
|
for _, checkTag := range check.ServiceTags {
|
|
|
|
|
if c.TagDelimiter != "" {
|
|
|
|
|
splittedTag := strings.SplitN(checkTag, c.TagDelimiter, 2)
|
2019-03-20 04:39:42 +08:00
|
|
|
if len(splittedTag) == 1 && checkTag != "" {
|
2018-05-18 05:24:51 +08:00
|
|
|
tags[checkTag] = checkTag
|
2019-03-20 04:39:42 +08:00
|
|
|
} else if len(splittedTag) == 2 && splittedTag[1] != "" {
|
2018-05-18 05:24:51 +08:00
|
|
|
tags[splittedTag[0]] = splittedTag[1]
|
|
|
|
|
}
|
2019-03-20 04:39:42 +08:00
|
|
|
} else if checkTag != "" {
|
2018-05-18 05:24:51 +08:00
|
|
|
tags[checkTag] = checkTag
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-01 18:02:28 +08:00
|
|
|
acc.AddFields("consul_health_checks", record, tags)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Consul) Gather(acc telegraf.Accumulator) error {
|
|
|
|
|
if c.client == nil {
|
|
|
|
|
newClient, err := c.createAPIClient()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.client = newClient
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checks, _, err := c.client.Health().State("any", nil)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.GatherHealthCheck(acc, checks)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
inputs.Add("consul", func() telegraf.Input {
|
|
|
|
|
return &Consul{}
|
|
|
|
|
})
|
|
|
|
|
}
|