2015-10-21 19:05:14 +00:00
|
|
|
package aerospike
|
|
|
|
|
|
|
|
import (
|
2018-05-23 21:28:17 +00:00
|
|
|
"crypto/tls"
|
2015-10-21 19:05:14 +00:00
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2016-07-15 05:12:32 +00:00
|
|
|
"time"
|
2015-10-21 19:05:14 +00:00
|
|
|
|
2016-07-15 05:12:32 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2018-05-23 21:28:17 +00:00
|
|
|
tlsint "github.com/influxdata/telegraf/internal/tls"
|
2016-07-15 05:12:32 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-10-21 19:05:14 +00:00
|
|
|
|
2016-08-24 07:41:12 +00:00
|
|
|
as "github.com/aerospike/aerospike-client-go"
|
2015-10-21 19:05:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Aerospike struct {
|
2018-05-23 21:28:17 +00:00
|
|
|
Servers []string `toml:"servers"`
|
|
|
|
|
|
|
|
Username string `toml:"username"`
|
|
|
|
Password string `toml:"password"`
|
|
|
|
|
|
|
|
EnableTLS bool `toml:"enable_tls"`
|
|
|
|
EnableSSL bool `toml:"enable_ssl"` // deprecated in 1.7; use enable_tls
|
|
|
|
tlsint.ClientConfig
|
|
|
|
|
|
|
|
initialized bool
|
|
|
|
tlsConfig *tls.Config
|
2015-10-21 19:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## Aerospike servers to connect to (with port)
|
|
|
|
## This plugin will query all namespaces the aerospike
|
|
|
|
## server has configured and get stats for them.
|
2015-10-26 12:54:31 +00:00
|
|
|
servers = ["localhost:3000"]
|
2018-05-23 21:28:17 +00:00
|
|
|
|
|
|
|
# username = "telegraf"
|
|
|
|
# password = "pa$$word"
|
|
|
|
|
|
|
|
## Optional TLS Config
|
|
|
|
# enable_tls = false
|
|
|
|
# tls_ca = "/etc/telegraf/ca.pem"
|
|
|
|
# tls_cert = "/etc/telegraf/cert.pem"
|
|
|
|
# tls_key = "/etc/telegraf/key.pem"
|
|
|
|
## If false, skip chain & host verification
|
|
|
|
# insecure_skip_verify = true
|
2015-10-21 19:05:14 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (a *Aerospike) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Aerospike) Description() string {
|
2016-07-15 05:12:32 +00:00
|
|
|
return "Read stats from aerospike server(s)"
|
2015-10-21 19:05:14 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (a *Aerospike) Gather(acc telegraf.Accumulator) error {
|
2018-05-23 21:28:17 +00:00
|
|
|
if !a.initialized {
|
|
|
|
tlsConfig, err := a.ClientConfig.TLSConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if tlsConfig == nil && (a.EnableTLS || a.EnableSSL) {
|
|
|
|
tlsConfig = &tls.Config{}
|
|
|
|
}
|
|
|
|
a.tlsConfig = tlsConfig
|
|
|
|
a.initialized = true
|
|
|
|
}
|
|
|
|
|
2015-10-21 19:05:14 +00:00
|
|
|
if len(a.Servers) == 0 {
|
|
|
|
return a.gatherServer("127.0.0.1:3000", acc)
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
2016-07-15 05:12:32 +00:00
|
|
|
wg.Add(len(a.Servers))
|
2015-10-21 19:05:14 +00:00
|
|
|
for _, server := range a.Servers {
|
2016-07-15 05:12:32 +00:00
|
|
|
go func(serv string) {
|
2015-10-21 19:05:14 +00:00
|
|
|
defer wg.Done()
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.AddError(a.gatherServer(serv, acc))
|
2015-10-21 19:05:14 +00:00
|
|
|
}(server)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
2017-04-24 18:13:26 +00:00
|
|
|
return nil
|
2015-10-21 19:05:14 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 05:12:32 +00:00
|
|
|
func (a *Aerospike) gatherServer(hostport string, acc telegraf.Accumulator) error {
|
|
|
|
host, port, err := net.SplitHostPort(hostport)
|
2015-10-21 19:05:14 +00:00
|
|
|
if err != nil {
|
2016-07-15 05:12:32 +00:00
|
|
|
return err
|
2015-10-21 19:05:14 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 05:12:32 +00:00
|
|
|
iport, err := strconv.Atoi(port)
|
2015-10-21 19:05:14 +00:00
|
|
|
if err != nil {
|
2016-07-15 05:12:32 +00:00
|
|
|
iport = 3000
|
2015-10-21 19:05:14 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 21:28:17 +00:00
|
|
|
policy := as.NewClientPolicy()
|
|
|
|
policy.User = a.Username
|
|
|
|
policy.Password = a.Password
|
|
|
|
policy.TlsConfig = a.tlsConfig
|
|
|
|
c, err := as.NewClientWithPolicy(policy, host, iport)
|
2015-10-21 19:05:14 +00:00
|
|
|
if err != nil {
|
2016-07-15 05:12:32 +00:00
|
|
|
return err
|
2015-10-21 19:05:14 +00:00
|
|
|
}
|
2016-07-15 05:12:32 +00:00
|
|
|
defer c.Close()
|
2015-10-21 19:05:14 +00:00
|
|
|
|
2016-07-15 05:12:32 +00:00
|
|
|
nodes := c.GetNodes()
|
|
|
|
for _, n := range nodes {
|
|
|
|
tags := map[string]string{
|
|
|
|
"aerospike_host": hostport,
|
2017-06-14 00:09:38 +00:00
|
|
|
"node_name": n.GetName(),
|
2015-10-21 19:05:14 +00:00
|
|
|
}
|
2017-06-14 00:09:38 +00:00
|
|
|
fields := make(map[string]interface{})
|
2016-07-15 05:12:32 +00:00
|
|
|
stats, err := as.RequestNodeStats(n)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-10-21 19:05:14 +00:00
|
|
|
}
|
2016-07-15 05:12:32 +00:00
|
|
|
for k, v := range stats {
|
2018-10-12 21:37:30 +00:00
|
|
|
val := parseValue(v)
|
|
|
|
fields[strings.Replace(k, "-", "_", -1)] = val
|
2016-07-15 05:12:32 +00:00
|
|
|
}
|
|
|
|
acc.AddFields("aerospike_node", fields, tags, time.Now())
|
2015-10-21 19:05:14 +00:00
|
|
|
|
2016-07-15 05:12:32 +00:00
|
|
|
info, err := as.RequestNodeInfo(n, "namespaces")
|
2015-10-21 19:05:14 +00:00
|
|
|
if err != nil {
|
2016-07-15 05:12:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
namespaces := strings.Split(info["namespaces"], ";")
|
|
|
|
|
|
|
|
for _, namespace := range namespaces {
|
2016-07-19 10:15:09 +00:00
|
|
|
nTags := map[string]string{
|
|
|
|
"aerospike_host": hostport,
|
2017-06-14 00:09:38 +00:00
|
|
|
"node_name": n.GetName(),
|
2016-07-19 10:15:09 +00:00
|
|
|
}
|
2016-07-15 05:12:32 +00:00
|
|
|
nTags["namespace"] = namespace
|
2017-06-14 00:09:38 +00:00
|
|
|
nFields := make(map[string]interface{})
|
2016-07-15 05:12:32 +00:00
|
|
|
info, err := as.RequestNodeInfo(n, "namespace/"+namespace)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
stats := strings.Split(info["namespace/"+namespace], ";")
|
|
|
|
for _, stat := range stats {
|
|
|
|
parts := strings.Split(stat, "=")
|
|
|
|
if len(parts) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-12 21:37:30 +00:00
|
|
|
val := parseValue(parts[1])
|
|
|
|
nFields[strings.Replace(parts[0], "-", "_", -1)] = val
|
2016-07-15 05:12:32 +00:00
|
|
|
}
|
|
|
|
acc.AddFields("aerospike_namespace", nFields, nTags, time.Now())
|
2015-10-21 19:05:14 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-15 05:12:32 +00:00
|
|
|
return nil
|
2015-10-21 19:05:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 21:37:30 +00:00
|
|
|
func parseValue(v string) interface{} {
|
2016-07-19 10:15:09 +00:00
|
|
|
if parsed, err := strconv.ParseInt(v, 10, 64); err == nil {
|
2018-10-12 21:37:30 +00:00
|
|
|
return parsed
|
|
|
|
} else if parsed, err := strconv.ParseUint(v, 10, 64); err == nil {
|
|
|
|
return parsed
|
2016-07-19 10:15:09 +00:00
|
|
|
} else if parsed, err := strconv.ParseBool(v); err == nil {
|
2018-10-12 21:37:30 +00:00
|
|
|
return parsed
|
2016-07-19 10:15:09 +00:00
|
|
|
} else {
|
2018-10-12 21:37:30 +00:00
|
|
|
// leave as string
|
|
|
|
return v
|
2016-07-19 10:15:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-15 05:12:32 +00:00
|
|
|
func copyTags(m map[string]string) map[string]string {
|
|
|
|
out := make(map[string]string)
|
|
|
|
for k, v := range m {
|
|
|
|
out[k] = v
|
|
|
|
}
|
|
|
|
return out
|
2015-10-21 19:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("aerospike", func() telegraf.Input {
|
2015-10-21 19:05:14 +00:00
|
|
|
return &Aerospike{}
|
|
|
|
})
|
|
|
|
}
|