forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
62 lines (49 loc) · 1.11 KB
/
stats.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
package stats
import (
"bufio"
"net"
"strings"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/mb"
)
func init() {
if err := mb.Registry.AddMetricSet("memcached", "stats", New); err != nil {
panic(err)
}
}
type MetricSet struct {
mb.BaseMetricSet
}
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
logp.Beta("The memcached stats metricset is beta")
return &MetricSet{
BaseMetricSet: base,
}, nil
}
func (m *MetricSet) Fetch() (common.MapStr, error) {
conn, err := net.DialTimeout("tcp", m.Host(), m.Module().Config().Timeout)
if err != nil {
return nil, err
}
defer conn.Close()
_, err = conn.Write([]byte("stats\n"))
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(conn)
data := map[string]interface{}{}
for scanner.Scan() {
text := scanner.Text()
if text == "END" {
break
}
// Split entries which look like: STAT time 1488291730
entries := strings.Split(text, " ")
if len(entries) == 3 {
data[entries[1]] = entries[2]
}
}
event, _ := schema.Apply(data)
return event, nil
}