collect binlog

This commit is contained in:
Ulric Qin 2022-04-26 17:04:51 +08:00
parent c931747a58
commit 3ca8f7d5ab
3 changed files with 79 additions and 2 deletions

View File

@ -1,5 +1,5 @@
# # collect interval
# interval = 15
interval = 3
[[instances]]
address = "127.0.0.1:3306"
@ -9,7 +9,7 @@ password = "1234"
# # set tls=custom is enable tls
# parameters = "tls=false"
extra_status_metrics = false
extra_status_metrics = true
extra_innodb_metrics = false
# # timeout

76
inputs/mysql/binlog.go Normal file
View File

@ -0,0 +1,76 @@
package mysql
import (
"database/sql"
"log"
"strconv"
"strings"
"flashcat.cloud/categraf/inputs"
"flashcat.cloud/categraf/pkg/tagx"
"github.com/toolkits/pkg/container/list"
)
func (m *MySQL) gatherBinlog(slist *list.SafeList, ins *Instance, db *sql.DB, globalTags map[string]string, cache map[string]float64) {
var logBin uint8
err := db.QueryRow(`SELECT @@log_bin`).Scan(&logBin)
if err != nil {
log.Println("E! failed to query SELECT @@log_bin:", err)
return
}
// If log_bin is OFF, do not run SHOW BINARY LOGS which explicitly produces MySQL error
if logBin == 0 {
return
}
rows, err := db.Query(`SHOW BINARY LOGS`)
if err != nil {
log.Println("E! failed to query SHOW BINARY LOGS:", err)
return
}
defer rows.Close()
columns, err := rows.Columns()
if err != nil {
log.Println("E! failed to get columns:", err)
return
}
var (
size uint64 = 0
count uint64 = 0
filename string
filesize uint64
encrypted string
columnCount int = len(columns)
)
for rows.Next() {
switch columnCount {
case 2:
if err := rows.Scan(&filename, &filesize); err != nil {
return
}
case 3:
if err := rows.Scan(&filename, &filesize, &encrypted); err != nil {
return
}
default:
log.Println("E! invalid number of columns:", columnCount)
}
size += filesize
count++
}
tags := tagx.Copy(globalTags)
slist.PushFront(inputs.NewSample("binlog_size_bytes", size, tags))
slist.PushFront(inputs.NewSample("binlog_file_count", count, tags))
value, err := strconv.ParseFloat(strings.Split(filename, ".")[1], 64)
if err == nil {
slist.PushFront(inputs.NewSample("binlog_file_number", value, tags))
}
}

View File

@ -232,4 +232,5 @@ func (m *MySQL) gatherOnce(slist *list.SafeList, ins *Instance) {
m.gatherGlobalVariables(slist, ins, db, tags, cache)
m.gatherEngineInnodbStatus(slist, ins, db, tags, cache)
m.gatherEngineInnodbStatusCompute(slist, ins, db, tags, cache)
m.gatherBinlog(slist, ins, db, tags, cache)
}