-
Notifications
You must be signed in to change notification settings - Fork 13
/
memcached.go
245 lines (220 loc) · 5.33 KB
/
memcached.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package memcached
import (
"bufio"
"bytes"
"fmt"
"net"
"strconv"
"time"
"github.com/circonus-labs/circonus-unified-agent/cua"
"github.com/circonus-labs/circonus-unified-agent/plugins/inputs"
)
// Memcached is a memcached plugin
type Memcached struct {
Servers []string
UnixSockets []string
lastVerSend *time.Time
}
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"]
`
var defaultTimeout = 5 * time.Second
// The list of metrics that should be sent
// https://github.com/memcached/memcached/blob/master/doc/protocol.txt#L1325 (the "Settings statistics" section)
var sendMetrics = []string{
"uptime",
"version",
"curr_items",
"total_items",
"bytes",
"max_connections",
"curr_connections",
"total_connections",
"rejected_connecctions",
"connection_structures",
"response_obj_oom",
"response_obj_count",
"response_obj_bytes",
"read_buf_count",
"read_buf_bytes",
"read_buf_bytes_free",
"read_buf_oom",
"cmd_get",
"cmd_set",
"cmd_flush",
"cmd_touch",
"get_hits",
"get_misses",
"get_expired",
"get_flushed",
"delete_misses",
"delete_hits",
"incr_misses",
"incr_hits",
"decr_misses",
"decr_hits",
"cas_hits",
"cas_misses",
"cas_badval",
"touch_hits",
"touch_misses",
"auth_cmds",
"auth_errors",
"idle_kicks",
"evictions",
"reclaimed",
"bytes_read",
"bytes_written",
"limit_maxbytes",
"accepting_conns",
"listen_disabled_num",
"time_in_listen_disabled",
"threads",
"conn_yields",
"hash_power_level",
"hash_bytes",
"hash_is_expanding",
"expired_unfetched",
"evicted_unfetched",
"evicted_active",
"slabs_reassign_running",
"slabs_moved",
"crawler_reclaimed",
"crawler_items_checked",
"lrutail_reflocked",
"moves_to_cold",
"moves_to_warm",
"moves_within_lru",
"direct_reclaims",
"lru_crawler_starts",
"lru_maintainer_juggles",
"slab_global_page_pool",
"slab_reassign_rescues",
"slab_reassign_evictions_nomem",
"slab_reassign_chunk_rescues",
"slab_reassign_inline_reclaim",
"slab_reassign_busy_deletes",
"log_worker_dropped",
"log_worker_written",
"log_watcher_skipped",
"log_watcher_sent",
"unexpected_napi_ids",
"round_robin_fallback",
}
// 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 cua.Accumulator) error {
if len(m.Servers) == 0 && len(m.UnixSockets) == 0 {
return m.gatherServer(":11211", false, acc)
}
for _, serverAddress := range m.Servers {
acc.AddError(m.gatherServer(serverAddress, false, acc))
}
for _, unixAddress := range m.UnixSockets {
acc.AddError(m.gatherServer(unixAddress, true, acc))
}
return nil
}
func (m *Memcached) gatherServer(
address string,
unix bool,
acc cua.Accumulator,
) error {
var conn net.Conn
var err error
if unix {
conn, err = net.DialTimeout("unix", address, defaultTimeout)
if err != nil {
return fmt.Errorf("dial timeout unix (%s): %w", address, err)
}
defer conn.Close()
} else {
_, _, err = net.SplitHostPort(address)
if err != nil {
address += ":11211"
}
conn, err = net.DialTimeout("tcp", address, defaultTimeout)
if err != nil {
return fmt.Errorf("dial timeout tcp (%s): %w", address, err)
}
defer conn.Close()
}
if conn == nil {
return fmt.Errorf("Failed to create net connection")
}
// 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 {
return fmt.Errorf("send cmd: %w", err)
}
if err := rw.Flush(); err != nil {
return fmt.Errorf("bufio flush: %w", 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
fields := make(map[string]interface{})
for _, key := range sendMetrics {
if value, ok := values[key]; ok {
if key == "version" {
if m.lastVerSend == nil || time.Since(*m.lastVerSend) > 5*time.Minute {
fields[key] = value
t := time.Now().UTC()
m.lastVerSend = &t
}
}
// Mostly it is the number
if iValue, errParse := strconv.ParseInt(value, 10, 64); errParse == nil {
fields[key] = iValue
} else {
fields[key] = value
}
}
}
acc.AddFields("memcached", fields, tags)
return nil
}
func parseResponse(r *bufio.Reader) (map[string]string, error) {
values := make(map[string]string)
for {
// Read line
line, _, errRead := r.ReadLine()
if errRead != nil {
return values, fmt.Errorf("bufio read: %w", errRead)
}
// 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)
}
// Save values
values[string(s[1])] = string(s[2])
}
return values, nil
}
func init() {
inputs.Add("memcached", func() cua.Input {
return &Memcached{}
})
}