telegraf/plugins/inputs/memcached/memcached.go

184 lines
3.7 KiB
Go
Raw Permalink Normal View History

2015-06-22 02:27:46 +00:00
package memcached
import (
"bufio"
"bytes"
"fmt"
"net"
"strconv"
"time"
"github.com/influxdata/telegraf"
2016-01-20 18:57:35 +00:00
"github.com/influxdata/telegraf/plugins/inputs"
2015-06-22 02:27:46 +00:00
)
// Memcached is a memcached plugin
type Memcached struct {
Servers []string
UnixSockets []string
2015-06-22 02:27:46 +00:00
}
var sampleConfig = `
## An array of address to gather stats about. Specify an ip on hostname
## with optional port. ie localhost, 10.0.0.1:11211, etc.
servers = ["localhost:11211"]
# unix_sockets = ["/var/run/memcached.sock"]
2015-08-26 15:21:39 +00:00
`
2015-06-22 02:27:46 +00:00
var defaultTimeout = 5 * time.Second
// The list of metrics that should be sent
var sendMetrics = []string{
2015-06-22 02:27:46 +00:00
"get_hits",
"get_misses",
"evictions",
"limit_maxbytes",
"bytes",
"uptime",
"curr_items",
"total_items",
"curr_connections",
"total_connections",
"connection_structures",
"cmd_get",
"cmd_set",
"delete_hits",
"delete_misses",
"incr_hits",
"incr_misses",
"decr_hits",
"decr_misses",
"cas_hits",
"cas_misses",
"bytes_read",
"bytes_written",
"threads",
"conn_yields",
2015-06-22 02:27:46 +00:00
}
// SampleConfig returns sample configuration message
func (m *Memcached) SampleConfig() string {
return sampleConfig
}
// Description returns description of Memcached plugin
func (m *Memcached) Description() string {
return "Read metrics from one or many memcached servers"
}
// Gather reads stats from all configured servers accumulates stats
func (m *Memcached) Gather(acc telegraf.Accumulator) error {
if len(m.Servers) == 0 && len(m.UnixSockets) == 0 {
return m.gatherServer(":11211", false, acc)
2015-06-22 02:27:46 +00:00
}
for _, serverAddress := range m.Servers {
2017-04-24 18:13:26 +00:00
acc.AddError(m.gatherServer(serverAddress, false, acc))
}
for _, unixAddress := range m.UnixSockets {
2017-04-24 18:13:26 +00:00
acc.AddError(m.gatherServer(unixAddress, true, acc))
2015-06-22 02:27:46 +00:00
}
2017-04-24 18:13:26 +00:00
return nil
2015-06-22 02:27:46 +00:00
}
func (m *Memcached) gatherServer(
address string,
unix bool,
acc telegraf.Accumulator,
) error {
var conn net.Conn
var err error
if unix {
conn, err = net.DialTimeout("unix", address, defaultTimeout)
if err != nil {
return err
}
defer conn.Close()
} else {
_, _, err = net.SplitHostPort(address)
if err != nil {
address = address + ":11211"
}
2015-06-22 02:27:46 +00:00
conn, err = net.DialTimeout("tcp", address, defaultTimeout)
if err != nil {
return err
}
defer conn.Close()
2015-06-22 02:27:46 +00:00
}
if conn == nil {
return fmt.Errorf("Failed to create net connection")
}
2015-06-22 02:27:46 +00:00
// Extend connection
conn.SetDeadline(time.Now().Add(defaultTimeout))
// Read and write buffer
rw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
// Send command
if _, err := fmt.Fprint(rw, "stats\r\n"); err != nil {
2015-06-22 02:27:46 +00:00
return err
}
if err := rw.Flush(); err != nil {
2015-06-22 02:27:46 +00:00
return err
}
values, err := parseResponse(rw.Reader)
if err != nil {
return err
}
// Add server address as a tag
tags := map[string]string{"server": address}
// Process values
2015-12-14 23:17:59 +00:00
fields := make(map[string]interface{})
for _, key := range sendMetrics {
if value, ok := values[key]; ok {
// Mostly it is the number
if iValue, errParse := strconv.ParseInt(value, 10, 64); errParse == nil {
2015-12-14 23:17:59 +00:00
fields[key] = iValue
} else {
2015-12-14 23:17:59 +00:00
fields[key] = value
}
}
}
2015-12-14 23:17:59 +00:00
acc.AddFields("memcached", fields, tags)
return nil
}
func parseResponse(r *bufio.Reader) (map[string]string, error) {
2015-06-22 02:27:46 +00:00
values := make(map[string]string)
for {
// Read line
line, _, errRead := r.ReadLine()
2015-06-22 02:27:46 +00:00
if errRead != nil {
return values, errRead
2015-06-22 02:27:46 +00:00
}
// Done
if bytes.Equal(line, []byte("END")) {
break
}
// Read values
s := bytes.SplitN(line, []byte(" "), 3)
if len(s) != 3 || !bytes.Equal(s[0], []byte("STAT")) {
return values, fmt.Errorf("unexpected line in stats response: %q", line)
2015-06-22 02:27:46 +00:00
}
// Save values
values[string(s[1])] = string(s[2])
2015-06-22 02:27:46 +00:00
}
return values, nil
2015-06-22 02:27:46 +00:00
}
func init() {
inputs.Add("memcached", func() telegraf.Input {
2015-06-22 02:27:46 +00:00
return &Memcached{}
})
}