2015-10-21 19:05:14 +00:00
|
|
|
package aerospike
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-10-21 19:05:14 +00:00
|
|
|
|
2016-07-18 16:37:21 +00:00
|
|
|
as "github.com/sparrc/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{
|
|
|
|
"node_name": n.GetName(),
|
|
|
|
"aerospike_host": hostport,
|
2015-10-21 19:05:14 +00:00
|
|
|
}
|
2016-07-15 05:12:32 +00:00
|
|
|
fields := make(map[string]interface{})
|
|
|
|
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 {
|
|
|
|
if iv, err := strconv.ParseInt(v, 10, 64); err == nil {
|
|
|
|
fields[strings.Replace(k, "-", "_", -1)] = iv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
nTags := copyTags(tags)
|
|
|
|
nTags["namespace"] = namespace
|
|
|
|
nFields := make(map[string]interface{})
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if iv, err := strconv.ParseInt(parts[1], 10, 64); err == nil {
|
|
|
|
nFields[strings.Replace(parts[0], "-", "_", -1)] = iv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-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{}
|
|
|
|
})
|
|
|
|
}
|