2015-10-21 19:05:14 +00:00
|
|
|
package aerospike
|
|
|
|
|
|
|
|
import (
|
2016-09-05 13:29:14 +00:00
|
|
|
"errors"
|
|
|
|
"log"
|
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"
|
|
|
|
"github.com/influxdata/telegraf/internal/errchan"
|
2017-02-04 15:58:02 +00:00
|
|
|
"github.com/influxdata/telegraf/registry/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 {
|
|
|
|
Servers []string
|
|
|
|
}
|
|
|
|
|
|
|
|
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"]
|
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 {
|
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
|
|
|
errChan := errchan.New(len(a.Servers))
|
|
|
|
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()
|
2016-07-15 05:12:32 +00:00
|
|
|
errChan.C <- a.gatherServer(serv, acc)
|
2015-10-21 19:05:14 +00:00
|
|
|
}(server)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
2016-07-15 05:12:32 +00:00
|
|
|
return errChan.Error()
|
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
|
|
|
}
|
|
|
|
|
2016-07-15 05:12:32 +00:00
|
|
|
c, err := as.NewClient(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,
|
2015-10-21 19:05:14 +00:00
|
|
|
}
|
2016-07-19 10:15:09 +00:00
|
|
|
fields := map[string]interface{}{
|
|
|
|
"node_name": n.GetName(),
|
|
|
|
}
|
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 {
|
2016-09-05 13:29:14 +00:00
|
|
|
val, err := parseValue(v)
|
|
|
|
if err == nil {
|
|
|
|
fields[strings.Replace(k, "-", "_", -1)] = val
|
|
|
|
} else {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("I! skipping aerospike field %v with int64 overflow", k)
|
2016-09-05 13:29:14 +00:00
|
|
|
}
|
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,
|
|
|
|
}
|
2016-07-15 05:12:32 +00:00
|
|
|
nTags["namespace"] = namespace
|
2016-07-19 10:15:09 +00:00
|
|
|
nFields := map[string]interface{}{
|
|
|
|
"node_name": n.GetName(),
|
|
|
|
}
|
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
|
|
|
|
}
|
2016-09-05 13:29:14 +00:00
|
|
|
val, err := parseValue(parts[1])
|
|
|
|
if err == nil {
|
|
|
|
nFields[strings.Replace(parts[0], "-", "_", -1)] = val
|
|
|
|
} else {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("I! skipping aerospike field %v with int64 overflow", parts[0])
|
2016-09-05 13:29:14 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2016-09-05 13:29:14 +00:00
|
|
|
func parseValue(v string) (interface{}, error) {
|
2016-07-19 10:15:09 +00:00
|
|
|
if parsed, err := strconv.ParseInt(v, 10, 64); err == nil {
|
2016-09-05 13:29:14 +00:00
|
|
|
return parsed, nil
|
|
|
|
} else if _, err := strconv.ParseUint(v, 10, 64); err == nil {
|
|
|
|
// int64 overflow, yet valid uint64
|
|
|
|
return nil, errors.New("Number is too large")
|
2016-07-19 10:15:09 +00:00
|
|
|
} else if parsed, err := strconv.ParseBool(v); err == nil {
|
2016-09-05 13:29:14 +00:00
|
|
|
return parsed, nil
|
2016-07-19 10:15:09 +00:00
|
|
|
} else {
|
2016-09-05 13:29:14 +00:00
|
|
|
return v, nil
|
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{}
|
|
|
|
})
|
|
|
|
}
|