2022-04-25 12:08:56 +08:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2022-04-25 15:34:15 +08:00
|
|
|
"database/sql"
|
2022-04-25 12:08:56 +08:00
|
|
|
"errors"
|
2022-04-25 15:34:15 +08:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2022-04-25 12:08:56 +08:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"flashcat.cloud/categraf/config"
|
|
|
|
"flashcat.cloud/categraf/inputs"
|
|
|
|
"flashcat.cloud/categraf/pkg/tls"
|
|
|
|
"flashcat.cloud/categraf/types"
|
2022-04-25 15:34:15 +08:00
|
|
|
"github.com/go-sql-driver/mysql"
|
2022-04-25 12:08:56 +08:00
|
|
|
"github.com/toolkits/pkg/container/list"
|
|
|
|
)
|
|
|
|
|
|
|
|
const inputName = "mysql"
|
|
|
|
|
2022-04-27 17:22:21 +08:00
|
|
|
type QueryConfig struct {
|
|
|
|
Mesurement string `toml:"mesurement"`
|
|
|
|
LabelFields []string `toml:"label_fields"`
|
|
|
|
MetricFields []string `toml:"metric_fields"`
|
|
|
|
FieldToAppend string `toml:"field_to_append"`
|
|
|
|
Timeout config.Duration `toml:"timeout"`
|
|
|
|
Request string `toml:"request"`
|
|
|
|
}
|
|
|
|
|
2022-04-25 12:08:56 +08:00
|
|
|
type Instance struct {
|
2022-04-25 15:34:15 +08:00
|
|
|
Address string `toml:"address"`
|
|
|
|
Username string `toml:"username"`
|
|
|
|
Password string `toml:"password"`
|
2022-04-25 16:36:13 +08:00
|
|
|
Parameters string `toml:"parameters"`
|
2022-04-25 15:34:15 +08:00
|
|
|
TimeoutSeconds int64 `toml:"timeout_seconds"`
|
|
|
|
|
2022-04-25 12:08:56 +08:00
|
|
|
Labels map[string]string `toml:"labels"`
|
|
|
|
IntervalTimes int64 `toml:"interval_times"`
|
2022-04-27 17:22:21 +08:00
|
|
|
Queries []QueryConfig `toml:"queries"`
|
2022-04-25 12:08:56 +08:00
|
|
|
|
2022-04-27 11:48:57 +08:00
|
|
|
ExtraStatusMetrics bool `toml:"extra_status_metrics"`
|
|
|
|
ExtraInnodbMetrics bool `toml:"extra_innodb_metrics"`
|
|
|
|
GatherProcessListProcessByState bool `toml:"gather_processlist_processes_by_state"`
|
|
|
|
GatherProcessListProcessByUser bool `toml:"gather_processlist_processes_by_user"`
|
2022-04-27 12:37:02 +08:00
|
|
|
GatherSchemaSize bool `toml:"gather_schema_size"`
|
2022-04-27 13:28:35 +08:00
|
|
|
GatherTableSize bool `toml:"gather_table_size"`
|
|
|
|
GatherSystemTableSize bool `toml:"gather_system_table_size"`
|
2022-04-27 15:37:41 +08:00
|
|
|
GatherSlaveStatus bool `toml:"gather_slave_status"`
|
2022-04-26 11:00:26 +08:00
|
|
|
|
|
|
|
validMetrics map[string]struct{}
|
|
|
|
dsn string
|
2022-04-25 12:08:56 +08:00
|
|
|
tls.ClientConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ins *Instance) Init() error {
|
|
|
|
if ins.Address == "" {
|
|
|
|
return errors.New("address is blank")
|
|
|
|
}
|
2022-04-25 15:34:15 +08:00
|
|
|
|
2022-04-25 16:36:13 +08:00
|
|
|
if ins.UseTLS {
|
|
|
|
tlsConfig, err := ins.ClientConfig.TLSConfig()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to register tls config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mysql.RegisterTLSConfig("custom", tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to register tls config: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ins.dsn = fmt.Sprintf("%s:%s@tcp(%s)/?%s", ins.Username, ins.Password, ins.Address, ins.Parameters)
|
2022-04-25 15:34:15 +08:00
|
|
|
|
|
|
|
conf, err := mysql.ParseDSN(ins.dsn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.Timeout == 0 {
|
|
|
|
if ins.TimeoutSeconds == 0 {
|
|
|
|
ins.TimeoutSeconds = 3
|
|
|
|
}
|
|
|
|
conf.Timeout = time.Second * time.Duration(ins.TimeoutSeconds)
|
|
|
|
}
|
|
|
|
|
|
|
|
ins.dsn = conf.FormatDSN()
|
|
|
|
|
2022-04-26 11:00:26 +08:00
|
|
|
ins.InitValidMetrics()
|
|
|
|
|
2022-04-25 12:08:56 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-26 11:00:26 +08:00
|
|
|
func (ins *Instance) InitValidMetrics() {
|
|
|
|
ins.validMetrics = make(map[string]struct{})
|
|
|
|
|
|
|
|
for key := range STATUS_VARS {
|
|
|
|
ins.validMetrics[key] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key := range VARIABLES_VARS {
|
|
|
|
ins.validMetrics[key] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key := range INNODB_VARS {
|
|
|
|
ins.validMetrics[key] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key := range BINLOG_VARS {
|
|
|
|
ins.validMetrics[key] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key := range GALERA_VARS {
|
|
|
|
ins.validMetrics[key] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key := range PERFORMANCE_VARS {
|
|
|
|
ins.validMetrics[key] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key := range SCHEMA_VARS {
|
|
|
|
ins.validMetrics[key] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key := range TABLE_VARS {
|
|
|
|
ins.validMetrics[key] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key := range REPLICA_VARS {
|
|
|
|
ins.validMetrics[key] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key := range GROUP_REPLICATION_VARS {
|
|
|
|
ins.validMetrics[key] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key := range SYNTHETIC_VARS {
|
|
|
|
ins.validMetrics[key] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ins.ExtraStatusMetrics {
|
|
|
|
for key := range OPTIONAL_STATUS_VARS {
|
|
|
|
ins.validMetrics[key] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ins.ExtraInnodbMetrics {
|
|
|
|
for key := range OPTIONAL_INNODB_VARS {
|
|
|
|
ins.validMetrics[key] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 12:08:56 +08:00
|
|
|
type MySQL struct {
|
2022-04-29 00:02:20 +08:00
|
|
|
config.Interval
|
|
|
|
Instances []*Instance `toml:"instances"`
|
2022-04-25 12:08:56 +08:00
|
|
|
|
|
|
|
Counter uint64
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
inputs.Add(inputName, func() inputs.Input {
|
|
|
|
return &MySQL{}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-28 15:24:48 +08:00
|
|
|
func (m *MySQL) Prefix() string {
|
2022-04-25 12:08:56 +08:00
|
|
|
return inputName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MySQL) Init() error {
|
|
|
|
if len(m.Instances) == 0 {
|
|
|
|
return types.ErrInstancesEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(m.Instances); i++ {
|
|
|
|
if err := m.Instances[i].Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MySQL) Drop() {}
|
|
|
|
|
2022-04-25 15:34:15 +08:00
|
|
|
func (m *MySQL) Gather(slist *list.SafeList) {
|
2022-04-25 12:08:56 +08:00
|
|
|
atomic.AddUint64(&m.Counter, 1)
|
|
|
|
for i := range m.Instances {
|
|
|
|
ins := m.Instances[i]
|
|
|
|
m.wg.Add(1)
|
|
|
|
go m.gatherOnce(slist, ins)
|
|
|
|
}
|
|
|
|
m.wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MySQL) gatherOnce(slist *list.SafeList, ins *Instance) {
|
|
|
|
defer m.wg.Done()
|
|
|
|
|
|
|
|
if ins.IntervalTimes > 0 {
|
|
|
|
counter := atomic.LoadUint64(&m.Counter)
|
|
|
|
if counter%uint64(ins.IntervalTimes) != 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tags := map[string]string{"address": ins.Address}
|
|
|
|
for k, v := range ins.Labels {
|
|
|
|
tags[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
begun := time.Now()
|
|
|
|
|
|
|
|
// scrape use seconds
|
|
|
|
defer func(begun time.Time) {
|
|
|
|
use := time.Since(begun).Seconds()
|
|
|
|
slist.PushFront(inputs.NewSample("scrape_use_seconds", use, tags))
|
|
|
|
}(begun)
|
|
|
|
|
2022-04-25 15:34:15 +08:00
|
|
|
db, err := sql.Open("mysql", ins.dsn)
|
|
|
|
if err != nil {
|
|
|
|
slist.PushFront(inputs.NewSample("up", 0, tags))
|
|
|
|
log.Println("E! failed to open mysql:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer db.Close()
|
|
|
|
|
2022-04-25 16:49:37 +08:00
|
|
|
db.SetMaxOpenConns(1)
|
|
|
|
db.SetMaxIdleConns(1)
|
|
|
|
db.SetConnMaxLifetime(time.Minute)
|
2022-04-25 16:36:13 +08:00
|
|
|
|
2022-04-25 16:49:37 +08:00
|
|
|
if err = db.Ping(); err != nil {
|
|
|
|
slist.PushFront(inputs.NewSample("up", 0, tags))
|
|
|
|
log.Println("E! failed to ping mysql:", err)
|
2022-04-27 17:22:21 +08:00
|
|
|
return
|
2022-04-25 16:49:37 +08:00
|
|
|
}
|
2022-04-25 16:36:13 +08:00
|
|
|
|
2022-04-25 16:49:37 +08:00
|
|
|
slist.PushFront(inputs.NewSample("up", 1, tags))
|
2022-04-25 16:36:13 +08:00
|
|
|
|
2022-04-26 13:33:47 +08:00
|
|
|
cache := make(map[string]float64)
|
|
|
|
|
|
|
|
m.gatherGlobalStatus(slist, ins, db, tags, cache)
|
|
|
|
m.gatherGlobalVariables(slist, ins, db, tags, cache)
|
|
|
|
m.gatherEngineInnodbStatus(slist, ins, db, tags, cache)
|
2022-04-26 14:02:26 +08:00
|
|
|
m.gatherEngineInnodbStatusCompute(slist, ins, db, tags, cache)
|
2022-04-27 15:55:35 +08:00
|
|
|
m.gatherBinlog(slist, ins, db, tags)
|
|
|
|
m.gatherProcesslistByState(slist, ins, db, tags)
|
|
|
|
m.gatherProcesslistByUser(slist, ins, db, tags)
|
|
|
|
m.gatherSchemaSize(slist, ins, db, tags)
|
|
|
|
m.gatherTableSize(slist, ins, db, tags, false)
|
|
|
|
m.gatherTableSize(slist, ins, db, tags, true)
|
|
|
|
m.gatherSlaveStatus(slist, ins, db, tags)
|
2022-04-27 17:22:21 +08:00
|
|
|
m.gatherCustomQueries(slist, ins, db, tags)
|
2022-04-25 12:08:56 +08:00
|
|
|
}
|