diff --git a/conf/input.mysql/mysql.toml b/conf/input.mysql/mysql.toml index 79ee99e..3cf3aad 100644 --- a/conf/input.mysql/mysql.toml +++ b/conf/input.mysql/mysql.toml @@ -6,13 +6,14 @@ address = "127.0.0.1:3306" username = "root" password = "1234" -# # set tls=custom is enable tls +# # set tls=custom to enable tls # parameters = "tls=false" extra_status_metrics = true extra_innodb_metrics = false gather_processlist_processes_by_state = false gather_processlist_processes_by_user = true +gather_schema_size = true # # timeout # timeout_seconds = 3 diff --git a/inputs/mysql/mysql.go b/inputs/mysql/mysql.go index 1d8bd2c..83daa89 100644 --- a/inputs/mysql/mysql.go +++ b/inputs/mysql/mysql.go @@ -33,6 +33,7 @@ type Instance struct { ExtraInnodbMetrics bool `toml:"extra_innodb_metrics"` GatherProcessListProcessByState bool `toml:"gather_processlist_processes_by_state"` GatherProcessListProcessByUser bool `toml:"gather_processlist_processes_by_user"` + GatherSchemaSize bool `toml:"gather_schema_size"` validMetrics map[string]struct{} dsn string @@ -237,4 +238,5 @@ func (m *MySQL) gatherOnce(slist *list.SafeList, ins *Instance) { m.gatherBinlog(slist, ins, db, tags, cache) m.gatherProcesslistByState(slist, ins, db, tags, cache) m.gatherProcesslistByUser(slist, ins, db, tags, cache) + m.gatherSchemaSize(slist, ins, db, tags, cache) } diff --git a/inputs/mysql/queries.go b/inputs/mysql/queries.go index 1c7233a..bd2c616 100644 --- a/inputs/mysql/queries.go +++ b/inputs/mysql/queries.go @@ -31,7 +31,7 @@ ORDER BY percentile ASC LIMIT 1` 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 GROUP BY table_schema` diff --git a/inputs/mysql/schema_size.go b/inputs/mysql/schema_size.go new file mode 100644 index 0000000..a383b85 --- /dev/null +++ b/inputs/mysql/schema_size.go @@ -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})) + } +}