telegraf/plugins/inputs/couchbase/couchbase.go

109 lines
2.8 KiB
Go
Raw Permalink Normal View History

2016-03-16 21:27:59 +00:00
package couchbase
import (
"regexp"
"sync"
2016-03-16 21:27:59 +00:00
couchbase "github.com/couchbase/go-couchbase"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
)
type Couchbase struct {
Servers []string
}
var sampleConfig = `
## specify servers via a url matching:
## [protocol://][:password]@address[:port]
## e.g.
## http://couchbase-0.example.com/
## http://admin:secret@couchbase-0.example.com:8091/
##
## If no servers are specified, then localhost is used as the host.
2017-11-01 00:00:06 +00:00
## If no protocol is specified, HTTP is used.
2016-03-16 21:27:59 +00:00
## If no port is specified, 8091 is used.
servers = ["http://localhost:8091"]
`
var regexpURI = regexp.MustCompile(`(\S+://)?(\S+\:\S+@)`)
2016-03-16 21:27:59 +00:00
func (r *Couchbase) SampleConfig() string {
return sampleConfig
}
func (r *Couchbase) Description() string {
return "Read metrics from one or many couchbase clusters"
}
// Reads stats from all configured clusters. Accumulates stats.
// Returns one of the errors encountered while gathering stats (if any).
func (r *Couchbase) Gather(acc telegraf.Accumulator) error {
if len(r.Servers) == 0 {
2016-03-18 22:37:57 +00:00
r.gatherServer("http://localhost:8091/", acc, nil)
2016-03-16 21:27:59 +00:00
return nil
}
var wg sync.WaitGroup
for _, serv := range r.Servers {
wg.Add(1)
go func(serv string) {
defer wg.Done()
2017-04-24 18:13:26 +00:00
acc.AddError(r.gatherServer(serv, acc, nil))
2016-03-16 21:27:59 +00:00
}(serv)
}
wg.Wait()
2017-04-24 18:13:26 +00:00
return nil
2016-03-16 21:27:59 +00:00
}
2016-03-18 22:37:57 +00:00
func (r *Couchbase) gatherServer(addr string, acc telegraf.Accumulator, pool *couchbase.Pool) error {
if pool == nil {
client, err := couchbase.Connect(addr)
if err != nil {
return err
}
// `default` is the only possible pool name. It's a
// placeholder for a possible future Couchbase feature. See
// http://stackoverflow.com/a/16990911/17498.
p, err := client.GetPool("default")
if err != nil {
return err
}
pool = &p
2016-03-16 21:27:59 +00:00
}
2016-03-16 21:27:59 +00:00
for i := 0; i < len(pool.Nodes); i++ {
node := pool.Nodes[i]
tags := map[string]string{"cluster": regexpURI.ReplaceAllString(addr, "${1}"), "hostname": node.Hostname}
2016-03-16 21:27:59 +00:00
fields := make(map[string]interface{})
fields["memory_free"] = node.MemoryFree
fields["memory_total"] = node.MemoryTotal
acc.AddFields("couchbase_node", fields, tags)
}
for bucketName := range pool.BucketMap {
2016-03-16 21:27:59 +00:00
tags := map[string]string{"cluster": addr, "bucket": bucketName}
bs := pool.BucketMap[bucketName].BasicStats
fields := make(map[string]interface{})
fields["quota_percent_used"] = bs["quotaPercentUsed"]
fields["ops_per_sec"] = bs["opsPerSec"]
fields["disk_fetches"] = bs["diskFetches"]
fields["item_count"] = bs["itemCount"]
fields["disk_used"] = bs["diskUsed"]
fields["data_used"] = bs["dataUsed"]
fields["mem_used"] = bs["memUsed"]
acc.AddFields("couchbase_bucket", fields, tags)
2016-03-16 21:27:59 +00:00
}
return nil
}
func init() {
inputs.Add("couchbase", func() telegraf.Input {
return &Couchbase{}
})
}