From d10f67c1e306733b807fb6ca05dad305f54b613f Mon Sep 17 00:00:00 2001 From: Ulric Qin Date: Tue, 26 Apr 2022 14:02:26 +0800 Subject: [PATCH] compute some innodb metrics --- inputs/mysql/engine_innodb_compute.go | 36 +++++++++++++++++++++++++++ inputs/mysql/mysql.go | 17 +------------ 2 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 inputs/mysql/engine_innodb_compute.go diff --git a/inputs/mysql/engine_innodb_compute.go b/inputs/mysql/engine_innodb_compute.go new file mode 100644 index 0000000..f3f6ba7 --- /dev/null +++ b/inputs/mysql/engine_innodb_compute.go @@ -0,0 +1,36 @@ +package mysql + +import ( + "database/sql" + + "flashcat.cloud/categraf/inputs" + "flashcat.cloud/categraf/pkg/tagx" + "github.com/toolkits/pkg/container/list" +) + +func (m *MySQL) gatherEngineInnodbStatusCompute(slist *list.SafeList, ins *Instance, db *sql.DB, globalTags map[string]string, cache map[string]float64) { + if !ins.ExtraInnodbMetrics { + return + } + + tags := tagx.Copy(globalTags) + + pageUsed := cache["innodb_buffer_pool_pages_total"] - cache["innodb_buffer_pool_pages_free"] + byteUsed := pageUsed * cache["innodb_page_size"] + byteData := cache["innodb_buffer_pool_pages_data"] * cache["innodb_page_size"] + byteDirty := cache["innodb_buffer_pool_pages_dirty"] * cache["innodb_page_size"] + byteFree := cache["innodb_buffer_pool_pages_free"] * cache["innodb_page_size"] + byteTotal := cache["innodb_buffer_pool_pages_total"] * cache["innodb_page_size"] + pageUtil := float64(0) + if cache["innodb_buffer_pool_pages_total"] != 0 { + pageUtil = pageUsed / cache["innodb_buffer_pool_pages_total"] + } + + slist.PushFront(inputs.NewSample("global_status_buffer_pool_pages", pageUsed, tags, map[string]string{"state": "used"})) + slist.PushFront(inputs.NewSample("global_status_buffer_pool_pages_bytes", byteUsed, tags, map[string]string{"state": "used"})) + slist.PushFront(inputs.NewSample("global_status_buffer_pool_pages_bytes", byteData, tags, map[string]string{"state": "data"})) + slist.PushFront(inputs.NewSample("global_status_buffer_pool_pages_bytes", byteFree, tags, map[string]string{"state": "free"})) + slist.PushFront(inputs.NewSample("global_status_buffer_pool_pages_bytes", byteTotal, tags, map[string]string{"state": "total"})) + slist.PushFront(inputs.NewSample("global_status_buffer_pool_dirty_pages_bytes", byteDirty, tags)) + slist.PushFront(inputs.NewSample("global_status_buffer_pool_pages_utilization", pageUtil, tags)) +} diff --git a/inputs/mysql/mysql.go b/inputs/mysql/mysql.go index 9a1c1b7..0a2154b 100644 --- a/inputs/mysql/mysql.go +++ b/inputs/mysql/mysql.go @@ -231,20 +231,5 @@ func (m *MySQL) gatherOnce(slist *list.SafeList, ins *Instance) { m.gatherGlobalStatus(slist, ins, db, tags, cache) m.gatherGlobalVariables(slist, ins, db, tags, cache) m.gatherEngineInnodbStatus(slist, ins, db, tags, cache) - - innodbKeys := []string{ - "innodb_page_size", - "innodb_buffer_pool_pages_data", - "innodb_buffer_pool_pages_dirty", - "innodb_buffer_pool_pages_total", - "innodb_buffer_pool_pages_free", - } - - for _, key := range innodbKeys { - if val, has := cache[key]; has { - log.Println("---key:", key, "value:", val) - } else { - log.Println("---key not found:", key) - } - } + m.gatherEngineInnodbStatusCompute(slist, ins, db, tags, cache) }