1 Star 0 Fork 0

sqos/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
status.go 1.83 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Package status fetches MySQL server status metrics.
For more information on the query it uses, see:
http://dev.mysql.com/doc/refman/5.7/en/show-status.html
*/
package status
import (
"database/sql"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/module/mysql"
"github.com/pkg/errors"
)
var (
debugf = logp.MakeDebug("mysql-status")
)
func init() {
if err := mb.Registry.AddMetricSet("mysql", "status", New, mysql.ParseDSN); err != nil {
panic(err)
}
}
// MetricSet for fetching MySQL server status.
type MetricSet struct {
mb.BaseMetricSet
db *sql.DB
}
// New creates and returns a new MetricSet instance.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
return &MetricSet{BaseMetricSet: base}, nil
}
// Fetch fetches status messages from a mysql host.
func (m *MetricSet) Fetch() (common.MapStr, error) {
if m.db == nil {
var err error
m.db, err = mysql.NewDB(m.HostData().URI)
if err != nil {
return nil, errors.Wrap(err, "mysql-status fetch failed")
}
}
status, err := m.loadStatus(m.db)
if err != nil {
return nil, err
}
event := eventMapping(status)
if m.Module().Config().Raw {
event["raw"] = rawEventMapping(status)
}
return event, nil
}
// loadStatus loads all status entries from the given database into an array.
func (m *MetricSet) loadStatus(db *sql.DB) (map[string]string, error) {
// Returns the global status, also for versions previous 5.0.2
rows, err := db.Query("SHOW /*!50002 GLOBAL */ STATUS;")
if err != nil {
return nil, err
}
defer rows.Close()
mysqlStatus := map[string]string{}
for rows.Next() {
var name string
var value string
err = rows.Scan(&name, &value)
if err != nil {
return nil, err
}
mysqlStatus[name] = value
}
return mysqlStatus, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqos/beats.git
git@gitee.com:sqos/beats.git
sqos
beats
beats
v5.2.2

搜索帮助