gather schema size

This commit is contained in:
Ulric Qin 2022-04-27 12:37:02 +08:00
parent 9dbbc1c908
commit 59674d645d
4 changed files with 44 additions and 2 deletions

View File

@ -6,13 +6,14 @@ address = "127.0.0.1:3306"
username = "root" username = "root"
password = "1234" password = "1234"
# # set tls=custom is enable tls # # set tls=custom to enable tls
# parameters = "tls=false" # parameters = "tls=false"
extra_status_metrics = true extra_status_metrics = true
extra_innodb_metrics = false extra_innodb_metrics = false
gather_processlist_processes_by_state = false gather_processlist_processes_by_state = false
gather_processlist_processes_by_user = true gather_processlist_processes_by_user = true
gather_schema_size = true
# # timeout # # timeout
# timeout_seconds = 3 # timeout_seconds = 3

View File

@ -33,6 +33,7 @@ type Instance struct {
ExtraInnodbMetrics bool `toml:"extra_innodb_metrics"` ExtraInnodbMetrics bool `toml:"extra_innodb_metrics"`
GatherProcessListProcessByState bool `toml:"gather_processlist_processes_by_state"` GatherProcessListProcessByState bool `toml:"gather_processlist_processes_by_state"`
GatherProcessListProcessByUser bool `toml:"gather_processlist_processes_by_user"` GatherProcessListProcessByUser bool `toml:"gather_processlist_processes_by_user"`
GatherSchemaSize bool `toml:"gather_schema_size"`
validMetrics map[string]struct{} validMetrics map[string]struct{}
dsn string dsn string
@ -237,4 +238,5 @@ func (m *MySQL) gatherOnce(slist *list.SafeList, ins *Instance) {
m.gatherBinlog(slist, ins, db, tags, cache) m.gatherBinlog(slist, ins, db, tags, cache)
m.gatherProcesslistByState(slist, ins, db, tags, cache) m.gatherProcesslistByState(slist, ins, db, tags, cache)
m.gatherProcesslistByUser(slist, ins, db, tags, cache) m.gatherProcesslistByUser(slist, ins, db, tags, cache)
m.gatherSchemaSize(slist, ins, db, tags, cache)
} }

View File

@ -31,7 +31,7 @@ ORDER BY percentile ASC
LIMIT 1` LIMIT 1`
SQL_QUERY_SCHEMA_SIZE = ` SQL_QUERY_SCHEMA_SIZE = `
SELECT table_schema, IFNULL(SUM(data_length+index_length)/1024/1024,0) AS total_mb SELECT table_schema, IFNULL(SUM(data_length+index_length),0) AS total_bytes
FROM information_schema.tables FROM information_schema.tables
GROUP BY table_schema` GROUP BY table_schema`

View File

@ -0,0 +1,39 @@
package mysql
import (
"database/sql"
"log"
"flashcat.cloud/categraf/inputs"
"flashcat.cloud/categraf/pkg/tagx"
"github.com/toolkits/pkg/container/list"
)
func (m *MySQL) gatherSchemaSize(slist *list.SafeList, ins *Instance, db *sql.DB, globalTags map[string]string, cache map[string]float64) {
if !ins.GatherSchemaSize {
return
}
rows, err := db.Query(SQL_QUERY_SCHEMA_SIZE)
if err != nil {
log.Println("E! failed to get schema size:", err)
return
}
defer rows.Close()
labels := tagx.Copy(globalTags)
for rows.Next() {
var schema string
var size int64
err = rows.Scan(&schema, &size)
if err != nil {
log.Println("E! failed to scan rows:", err)
return
}
slist.PushFront(inputs.NewSample("schema_size", size, labels, map[string]string{"schema": schema}))
}
}