Adding upstream version 1.34.4.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
e393c3af3f
commit
4978089aab
4963 changed files with 677545 additions and 0 deletions
60
plugins/inputs/nats/README.md
Normal file
60
plugins/inputs/nats/README.md
Normal file
|
@ -0,0 +1,60 @@
|
|||
# NATS Server Monitoring Input Plugin
|
||||
|
||||
This plugin gathers metrics of a [NATS][nats] server instance using its
|
||||
[monitoring endpoints][nats_monitoring].
|
||||
|
||||
⭐ Telegraf v1.6.0
|
||||
🏷️ server
|
||||
💻 all
|
||||
|
||||
[nats]: http://www.nats.io
|
||||
[nats_monitoring]: https://docs.nats.io/running-a-nats-service/nats_admin/monitoring
|
||||
|
||||
## Global configuration options <!-- @/docs/includes/plugin_config.md -->
|
||||
|
||||
In addition to the plugin-specific configuration settings, plugins support
|
||||
additional global and plugin configuration settings. These settings are used to
|
||||
modify metrics, tags, and field or create aliases and configure ordering, etc.
|
||||
See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
|
||||
|
||||
[CONFIGURATION.md]: ../../../docs/CONFIGURATION.md#plugins
|
||||
|
||||
## Configuration
|
||||
|
||||
```toml @sample.conf
|
||||
# Provides metrics about the state of a NATS server
|
||||
# This plugin does NOT support FreeBSD
|
||||
[[inputs.nats]]
|
||||
## The address of the monitoring endpoint of the NATS server
|
||||
server = "http://localhost:8222"
|
||||
|
||||
## Maximum time to receive response
|
||||
# response_timeout = "5s"
|
||||
```
|
||||
|
||||
## Metrics
|
||||
|
||||
- nats
|
||||
- tags
|
||||
- server
|
||||
- fields:
|
||||
- uptime (integer, nanoseconds)
|
||||
- mem (integer, bytes)
|
||||
- subscriptions (integer, count)
|
||||
- out_bytes (integer, bytes)
|
||||
- connections (integer, count)
|
||||
- in_msgs (integer, bytes)
|
||||
- total_connections (integer, count)
|
||||
- cores (integer, count)
|
||||
- cpu (integer, count)
|
||||
- slow_consumers (integer, count)
|
||||
- routes (integer, count)
|
||||
- remotes (integer, count)
|
||||
- out_msgs (integer, count)
|
||||
- in_bytes (integer, bytes)
|
||||
|
||||
## Example Output
|
||||
|
||||
```text
|
||||
nats,server=http://localhost:8222 uptime=117158348682i,mem=6647808i,subscriptions=0i,out_bytes=0i,connections=0i,in_msgs=0i,total_connections=0i,cores=2i,cpu=0,slow_consumers=0i,routes=0i,remotes=0i,out_msgs=0i,in_bytes=0i 1517015107000000000
|
||||
```
|
106
plugins/inputs/nats/nats.go
Normal file
106
plugins/inputs/nats/nats.go
Normal file
|
@ -0,0 +1,106 @@
|
|||
//go:generate ../../../tools/readme_config_includer/generator
|
||||
//go:build !freebsd || (freebsd && cgo)
|
||||
|
||||
package nats
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
gnatsd "github.com/nats-io/nats-server/v2/server"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/config"
|
||||
"github.com/influxdata/telegraf/plugins/inputs"
|
||||
)
|
||||
|
||||
//go:embed sample.conf
|
||||
var sampleConfig string
|
||||
|
||||
type Nats struct {
|
||||
Server string `toml:"server"`
|
||||
ResponseTimeout config.Duration `toml:"response_timeout"`
|
||||
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func (*Nats) SampleConfig() string {
|
||||
return sampleConfig
|
||||
}
|
||||
|
||||
func (n *Nats) Gather(acc telegraf.Accumulator) error {
|
||||
address, err := url.Parse(n.Server)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
address.Path = path.Join(address.Path, "varz")
|
||||
|
||||
if n.client == nil {
|
||||
n.client = n.createHTTPClient()
|
||||
}
|
||||
resp, err := n.client.Get(address.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
bytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
stats := new(gnatsd.Varz)
|
||||
err = json.Unmarshal(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 := time.Duration(n.ResponseTimeout)
|
||||
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",
|
||||
}
|
||||
})
|
||||
}
|
3
plugins/inputs/nats/nats_freebsd.go
Normal file
3
plugins/inputs/nats/nats_freebsd.go
Normal file
|
@ -0,0 +1,3 @@
|
|||
//go:build freebsd && !cgo
|
||||
|
||||
package nats
|
111
plugins/inputs/nats/nats_test.go
Normal file
111
plugins/inputs/nats/nats_test.go
Normal file
|
@ -0,0 +1,111 @@
|
|||
//go:build !freebsd || (freebsd && cgo)
|
||||
|
||||
package nats
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
)
|
||||
|
||||
var sampleVarz = `
|
||||
{
|
||||
"server_id": "n2afhLHLl64Gcaj7S7jaNa",
|
||||
"version": "1.0.0",
|
||||
"go": "go1.8",
|
||||
"host": "0.0.0.0",
|
||||
"auth_required": false,
|
||||
"ssl_required": false,
|
||||
"tls_required": false,
|
||||
"tls_verify": false,
|
||||
"addr": "0.0.0.0",
|
||||
"max_connections": 65536,
|
||||
"ping_interval": 120000000000,
|
||||
"ping_max": 2,
|
||||
"http_host": "0.0.0.0",
|
||||
"http_port": 1337,
|
||||
"https_port": 0,
|
||||
"auth_timeout": 1,
|
||||
"max_control_line": 1024,
|
||||
"cluster": {
|
||||
"addr": "0.0.0.0",
|
||||
"cluster_port": 0,
|
||||
"auth_timeout": 1
|
||||
},
|
||||
"tls_timeout": 0.5,
|
||||
"port": 4222,
|
||||
"max_payload": 1048576,
|
||||
"start": "1861-04-12T10:15:26.841483489-05:00",
|
||||
"now": "2011-10-05T15:24:23.722084098-07:00",
|
||||
"uptime": "150y5md237h8m57s",
|
||||
"mem": 15581184,
|
||||
"cores": 48,
|
||||
"cpu": 9,
|
||||
"connections": 5,
|
||||
"total_connections": 109,
|
||||
"routes": 1,
|
||||
"remotes": 2,
|
||||
"in_msgs": 74148556,
|
||||
"out_msgs": 68863261,
|
||||
"in_bytes": 946267004717,
|
||||
"out_bytes": 948110960598,
|
||||
"slow_consumers": 2,
|
||||
"subscriptions": 4,
|
||||
"http_req_stats": {
|
||||
"/": 1,
|
||||
"/connz": 100847,
|
||||
"/routez": 0,
|
||||
"/subsz": 1,
|
||||
"/varz": 205785
|
||||
},
|
||||
"config_load_time": "2017-07-24T10:15:26.841483489-05:00"
|
||||
}
|
||||
`
|
||||
|
||||
func TestMetricsCorrect(t *testing.T) {
|
||||
var acc testutil.Accumulator
|
||||
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/varz" {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
t.Errorf("Cannot handle request, expected: %q, actual: %q", "/varz", r.URL.Path)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := fmt.Fprintln(w, sampleVarz); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
n := &Nats{Server: srv.URL}
|
||||
require.NoError(t, n.Gather(&acc))
|
||||
|
||||
fields := map[string]interface{}{
|
||||
"in_msgs": int64(74148556),
|
||||
"out_msgs": int64(68863261),
|
||||
"in_bytes": int64(946267004717),
|
||||
"out_bytes": int64(948110960598),
|
||||
"uptime": int64(4748742536880600609),
|
||||
"cores": 48,
|
||||
"cpu": float64(9),
|
||||
"mem": int64(15581184),
|
||||
"connections": int(5),
|
||||
"total_connections": uint64(109),
|
||||
"subscriptions": uint32(4),
|
||||
"slow_consumers": int64(2),
|
||||
"routes": int(1),
|
||||
"remotes": int(2),
|
||||
}
|
||||
tags := map[string]string{
|
||||
"server": srv.URL,
|
||||
}
|
||||
acc.AssertContainsTaggedFields(t, "nats", fields, tags)
|
||||
}
|
8
plugins/inputs/nats/sample.conf
Normal file
8
plugins/inputs/nats/sample.conf
Normal file
|
@ -0,0 +1,8 @@
|
|||
# Provides metrics about the state of a NATS server
|
||||
# This plugin does NOT support FreeBSD
|
||||
[[inputs.nats]]
|
||||
## The address of the monitoring endpoint of the NATS server
|
||||
server = "http://localhost:8222"
|
||||
|
||||
## Maximum time to receive response
|
||||
# response_timeout = "5s"
|
Loading…
Add table
Add a link
Reference in a new issue