2015-06-22 02:27:46 +00:00
|
|
|
package memcached
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2016-12-23 15:18:27 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins"
|
2016-07-19 13:03:28 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/errchan"
|
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 {
|
2015-12-03 19:08:01 +00:00
|
|
|
Servers []string
|
|
|
|
UnixSockets []string
|
2015-06-22 02:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## An array of address to gather stats about. Specify an ip on hostname
|
|
|
|
## with optional port. ie localhost, 10.0.0.1:11211, etc.
|
2015-12-03 19:08:01 +00:00
|
|
|
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
|
|
|
|
|
2015-12-03 20:53:37 +00:00
|
|
|
// The list of metrics that should be sent
|
|
|
|
var sendMetrics = []string{
|
2015-06-22 02:27:46 +00:00
|
|
|
"get_hits",
|
|
|
|
"get_misses",
|
|
|
|
"evictions",
|
2015-06-23 07:44:39 +00:00
|
|
|
"limit_maxbytes",
|
|
|
|
"bytes",
|
2015-12-02 18:20:31 +00:00
|
|
|
"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",
|
|
|
|
"evictions",
|
|
|
|
"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
|
2016-12-23 15:18:27 +00:00
|
|
|
func (m *Memcached) Gather(acc plugins.Accumulator) error {
|
2015-12-03 19:08:01 +00:00
|
|
|
if len(m.Servers) == 0 && len(m.UnixSockets) == 0 {
|
|
|
|
return m.gatherServer(":11211", false, acc)
|
2015-06-22 02:27:46 +00:00
|
|
|
}
|
|
|
|
|
2016-07-19 13:03:28 +00:00
|
|
|
errChan := errchan.New(len(m.Servers) + len(m.UnixSockets))
|
2015-06-22 02:27:46 +00:00
|
|
|
for _, serverAddress := range m.Servers {
|
2016-07-19 13:03:28 +00:00
|
|
|
errChan.C <- m.gatherServer(serverAddress, false, acc)
|
2015-12-03 19:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, unixAddress := range m.UnixSockets {
|
2016-07-19 13:03:28 +00:00
|
|
|
errChan.C <- m.gatherServer(unixAddress, true, acc)
|
2015-06-22 02:27:46 +00:00
|
|
|
}
|
|
|
|
|
2016-07-19 13:03:28 +00:00
|
|
|
return errChan.Error()
|
2015-06-22 02:27:46 +00:00
|
|
|
}
|
|
|
|
|
2015-12-03 19:08:01 +00:00
|
|
|
func (m *Memcached) gatherServer(
|
|
|
|
address string,
|
|
|
|
unix bool,
|
2016-12-23 15:18:27 +00:00
|
|
|
acc plugins.Accumulator,
|
2015-12-03 19:08:01 +00:00
|
|
|
) error {
|
|
|
|
var conn net.Conn
|
2016-03-21 22:22:01 +00:00
|
|
|
var err error
|
2015-12-03 19:08:01 +00:00
|
|
|
if unix {
|
2016-03-21 22:22:01 +00:00
|
|
|
conn, err = net.DialTimeout("unix", address, defaultTimeout)
|
2015-12-03 19:08:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
} else {
|
2016-03-21 22:22:01 +00:00
|
|
|
_, _, err = net.SplitHostPort(address)
|
2015-12-03 19:08:01 +00:00
|
|
|
if err != nil {
|
|
|
|
address = address + ":11211"
|
|
|
|
}
|
2015-06-22 02:27:46 +00:00
|
|
|
|
2015-12-03 19:08:01 +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
|
|
|
}
|
|
|
|
|
2016-03-21 22:22:01 +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
|
2015-12-03 19:08:01 +00:00
|
|
|
if _, err := fmt.Fprint(rw, "stats\r\n"); err != nil {
|
2015-06-22 02:27:46 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-12-03 19:08:01 +00:00
|
|
|
if err := rw.Flush(); err != nil {
|
2015-06-22 02:27:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-03 20:53:37 +00:00
|
|
|
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{})
|
2015-12-03 20:53:37 +00:00
|
|
|
for _, key := range sendMetrics {
|
|
|
|
if value, ok := values[key]; ok {
|
|
|
|
// Mostly it is the number
|
2016-01-07 00:19:39 +00:00
|
|
|
if iValue, errParse := strconv.ParseInt(value, 10, 64); errParse == nil {
|
2015-12-14 23:17:59 +00:00
|
|
|
fields[key] = iValue
|
2015-12-03 20:53:37 +00:00
|
|
|
} else {
|
2015-12-14 23:17:59 +00:00
|
|
|
fields[key] = value
|
2015-12-03 20:53:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-14 23:17:59 +00:00
|
|
|
acc.AddFields("memcached", fields, tags)
|
2015-12-03 20:53:37 +00:00
|
|
|
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
|
2015-12-03 20:53:37 +00:00
|
|
|
line, _, errRead := r.ReadLine()
|
2015-06-22 02:27:46 +00:00
|
|
|
if errRead != nil {
|
2015-12-03 20:53:37 +00:00
|
|
|
return values, errRead
|
2015-06-22 02:27:46 +00:00
|
|
|
}
|
|
|
|
// Done
|
|
|
|
if bytes.Equal(line, []byte("END")) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Read values
|
2015-08-25 18:17:15 +00:00
|
|
|
s := bytes.SplitN(line, []byte(" "), 3)
|
|
|
|
if len(s) != 3 || !bytes.Equal(s[0], []byte("STAT")) {
|
2015-12-03 20:53:37 +00:00
|
|
|
return values, fmt.Errorf("unexpected line in stats response: %q", line)
|
2015-06-22 02:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save values
|
2015-08-25 18:17:15 +00:00
|
|
|
values[string(s[1])] = string(s[2])
|
2015-06-22 02:27:46 +00:00
|
|
|
}
|
2015-12-03 20:53:37 +00:00
|
|
|
return values, nil
|
2015-06-22 02:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-12-23 15:18:27 +00:00
|
|
|
inputs.Add("memcached", func() plugins.Input {
|
2015-06-22 02:27:46 +00:00
|
|
|
return &Memcached{}
|
|
|
|
})
|
|
|
|
}
|