1 Star 0 Fork 0

sqos/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
stats.go 1.11 KB
一键复制 编辑 原始数据 按行查看 历史
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqos/beats.git
git@gitee.com:sqos/beats.git
sqos
beats
beats
v6.0.0-alpha2

搜索帮助