2019-11-13 22:45:56 +00:00
|
|
|
// +build !freebsd freebsd,cgo
|
2018-01-30 20:03:16 +00:00
|
|
|
|
2018-01-26 23:14:54 +00:00
|
|
|
package nats
|
|
|
|
|
|
|
|
import (
|
2019-11-13 22:45:56 +00:00
|
|
|
"encoding/json"
|
2018-01-26 23:14:54 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/internal"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2020-02-20 22:30:04 +00:00
|
|
|
gnatsd "github.com/nats-io/nats-server/v2/server"
|
2018-01-26 23:14:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Nats struct {
|
|
|
|
Server string
|
|
|
|
ResponseTimeout internal.Duration
|
|
|
|
|
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
|
|
|
## The address of the monitoring endpoint of the NATS server
|
2018-01-27 01:15:02 +00:00
|
|
|
server = "http://localhost:8222"
|
2018-01-26 23:14:54 +00:00
|
|
|
|
|
|
|
## Maximum time to receive response
|
|
|
|
# response_timeout = "5s"
|
|
|
|
`
|
|
|
|
|
|
|
|
func (n *Nats) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Nats) Description() string {
|
|
|
|
return "Provides metrics about the state of a NATS server"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Nats) Gather(acc telegraf.Accumulator) error {
|
|
|
|
url, err := url.Parse(n.Server)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
url.Path = path.Join(url.Path, "varz")
|
|
|
|
|
|
|
|
if n.client == nil {
|
|
|
|
n.client = n.createHTTPClient()
|
|
|
|
}
|
|
|
|
resp, err := n.client.Get(url.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
bytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stats := new(gnatsd.Varz)
|
|
|
|
err = json.Unmarshal([]byte(bytes), &stats)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
acc.AddFields("nats",
|
|
|
|
map[string]interface{}{
|
|
|
|
"in_msgs": stats.InMsgs,
|
|
|
|
"out_msgs": stats.OutMsgs,
|
|
|
|
"in_bytes": stats.InBytes,
|
|
|
|
"out_bytes": stats.OutBytes,
|
|
|
|
"uptime": stats.Now.Sub(stats.Start).Nanoseconds(),
|
|
|
|
"cores": stats.Cores,
|
|
|
|
"cpu": stats.CPU,
|
|
|
|
"mem": stats.Mem,
|
|
|
|
"connections": stats.Connections,
|
|
|
|
"total_connections": stats.TotalConnections,
|
|
|
|
"subscriptions": stats.Subscriptions,
|
|
|
|
"slow_consumers": stats.SlowConsumers,
|
|
|
|
"routes": stats.Routes,
|
|
|
|
"remotes": stats.Remotes,
|
|
|
|
},
|
|
|
|
map[string]string{"server": n.Server},
|
|
|
|
time.Now())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Nats) createHTTPClient() *http.Client {
|
|
|
|
transport := &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
}
|
|
|
|
timeout := n.ResponseTimeout.Duration
|
|
|
|
if timeout == time.Duration(0) {
|
|
|
|
timeout = 5 * time.Second
|
|
|
|
}
|
|
|
|
return &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
Timeout: timeout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
inputs.Add("nats", func() telegraf.Input {
|
|
|
|
return &Nats{
|
|
|
|
Server: "http://localhost:8222",
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|