2015-11-12 16:16:49 +00:00
|
|
|
package twemproxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-11-12 16:16:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Twemproxy struct {
|
2015-11-12 16:25:42 +00:00
|
|
|
Addr string
|
|
|
|
Pools []string
|
2015-11-12 16:16:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## Twemproxy stats address and port (no scheme)
|
2015-12-20 03:26:18 +00:00
|
|
|
addr = "localhost:22222"
|
2016-02-18 21:26:51 +00:00
|
|
|
## Monitor pool name
|
2015-12-20 03:26:18 +00:00
|
|
|
pools = ["redis_pool", "mc_pool"]
|
2015-11-12 16:16:49 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (t *Twemproxy) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Twemproxy) Description() string {
|
|
|
|
return "Read Twemproxy stats data"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gather data from all Twemproxy instances
|
2016-01-27 21:21:36 +00:00
|
|
|
func (t *Twemproxy) Gather(acc telegraf.Accumulator) error {
|
2015-12-20 03:26:18 +00:00
|
|
|
conn, err := net.DialTimeout("tcp", t.Addr, 1*time.Second)
|
2015-11-12 16:16:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var stats map[string]interface{}
|
|
|
|
if err = json.Unmarshal(body, &stats); err != nil {
|
|
|
|
return errors.New("Error decoding JSON response")
|
|
|
|
}
|
|
|
|
|
|
|
|
tags := make(map[string]string)
|
2015-12-20 03:26:18 +00:00
|
|
|
tags["twemproxy"] = t.Addr
|
|
|
|
t.processStat(acc, tags, stats)
|
2015-11-12 16:16:49 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process Twemproxy server stats
|
2015-12-20 03:26:18 +00:00
|
|
|
func (t *Twemproxy) processStat(
|
2016-01-27 21:21:36 +00:00
|
|
|
acc telegraf.Accumulator,
|
2015-11-12 16:16:49 +00:00
|
|
|
tags map[string]string,
|
|
|
|
data map[string]interface{},
|
|
|
|
) {
|
|
|
|
if source, ok := data["source"]; ok {
|
|
|
|
if val, ok := source.(string); ok {
|
|
|
|
tags["source"] = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-15 18:36:45 +00:00
|
|
|
fields := make(map[string]interface{})
|
2015-11-12 16:16:49 +00:00
|
|
|
metrics := []string{"total_connections", "curr_connections", "timestamp"}
|
|
|
|
for _, m := range metrics {
|
|
|
|
if value, ok := data[m]; ok {
|
|
|
|
if val, ok := value.(float64); ok {
|
2015-12-15 18:36:45 +00:00
|
|
|
fields[m] = val
|
2015-11-12 16:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-15 18:36:45 +00:00
|
|
|
acc.AddFields("twemproxy", fields, tags)
|
2015-11-12 16:16:49 +00:00
|
|
|
|
2015-12-20 03:26:18 +00:00
|
|
|
for _, pool := range t.Pools {
|
2015-11-12 16:16:49 +00:00
|
|
|
if poolStat, ok := data[pool]; ok {
|
|
|
|
if data, ok := poolStat.(map[string]interface{}); ok {
|
|
|
|
poolTags := copyTags(tags)
|
|
|
|
poolTags["pool"] = pool
|
2015-12-20 03:26:18 +00:00
|
|
|
t.processPool(acc, poolTags, data)
|
2015-11-12 16:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process pool data in Twemproxy stats
|
2015-12-20 03:26:18 +00:00
|
|
|
func (t *Twemproxy) processPool(
|
2016-01-27 21:21:36 +00:00
|
|
|
acc telegraf.Accumulator,
|
2015-11-12 16:16:49 +00:00
|
|
|
tags map[string]string,
|
|
|
|
data map[string]interface{},
|
|
|
|
) {
|
|
|
|
serverTags := make(map[string]map[string]string)
|
|
|
|
|
2015-12-15 18:36:45 +00:00
|
|
|
fields := make(map[string]interface{})
|
2015-11-12 16:16:49 +00:00
|
|
|
for key, value := range data {
|
|
|
|
switch key {
|
|
|
|
case "client_connections", "forward_error", "client_err", "server_ejects", "fragments", "client_eof":
|
|
|
|
if val, ok := value.(float64); ok {
|
2015-12-15 18:36:45 +00:00
|
|
|
fields[key] = val
|
2015-11-12 16:16:49 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
if data, ok := value.(map[string]interface{}); ok {
|
|
|
|
if _, ok := serverTags[key]; !ok {
|
|
|
|
serverTags[key] = copyTags(tags)
|
|
|
|
serverTags[key]["server"] = key
|
|
|
|
}
|
2015-12-20 03:26:18 +00:00
|
|
|
t.processServer(acc, serverTags[key], data)
|
2015-11-12 16:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-15 18:36:45 +00:00
|
|
|
acc.AddFields("twemproxy_pool", fields, tags)
|
2015-11-12 16:16:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process backend server(redis/memcached) stats
|
2015-12-20 03:26:18 +00:00
|
|
|
func (t *Twemproxy) processServer(
|
2016-01-27 21:21:36 +00:00
|
|
|
acc telegraf.Accumulator,
|
2015-11-12 16:16:49 +00:00
|
|
|
tags map[string]string,
|
|
|
|
data map[string]interface{},
|
|
|
|
) {
|
2015-12-15 18:36:45 +00:00
|
|
|
fields := make(map[string]interface{})
|
2015-11-12 16:16:49 +00:00
|
|
|
for key, value := range data {
|
|
|
|
switch key {
|
|
|
|
default:
|
|
|
|
if val, ok := value.(float64); ok {
|
2015-12-15 18:36:45 +00:00
|
|
|
fields[key] = val
|
2015-11-12 16:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-07 05:16:04 +00:00
|
|
|
acc.AddFields("twemproxy_pool_server", fields, tags)
|
2015-11-12 16:16:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tags is not expected to be mutated after passing to Add.
|
|
|
|
func copyTags(tags map[string]string) map[string]string {
|
|
|
|
newTags := make(map[string]string)
|
|
|
|
for k, v := range tags {
|
|
|
|
newTags[k] = v
|
|
|
|
}
|
|
|
|
return newTags
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("twemproxy", func() telegraf.Input {
|
2015-11-12 16:16:49 +00:00
|
|
|
return &Twemproxy{}
|
|
|
|
})
|
|
|
|
}
|