add engine_innodb_status gather

This commit is contained in:
Ulric Qin 2022-04-26 13:33:47 +08:00
parent e8b00fa832
commit f5bcc36e9e
5 changed files with 97 additions and 14 deletions

View File

@ -0,0 +1,58 @@
package mysql
import (
"database/sql"
"log"
"regexp"
"strconv"
"strings"
"flashcat.cloud/categraf/inputs"
"github.com/toolkits/pkg/container/list"
)
func (m *MySQL) gatherEngineInnodbStatus(slist *list.SafeList, ins *Instance, db *sql.DB, globalTags map[string]string, cache map[string]float64) {
rows, err := db.Query(SQL_ENGINE_INNODB_STATUS)
if err != nil {
log.Println("E! failed to query engine innodb status:", err)
return
}
defer rows.Close()
var typeCol, nameCol, statusCol string
// First row should contain the necessary info. If many rows returned then it's unknown case.
if rows.Next() {
if err := rows.Scan(&typeCol, &nameCol, &statusCol); err != nil {
log.Println("E! failed to scan result, sql:", SQL_ENGINE_INNODB_STATUS, "error:", err)
return
}
}
// 0 queries inside InnoDB, 0 queries in queue
// 0 read views open inside InnoDB
rQueries, _ := regexp.Compile(`(\d+) queries inside InnoDB, (\d+) queries in queue`)
rViews, _ := regexp.Compile(`(\d+) read views open inside InnoDB`)
for _, line := range strings.Split(statusCol, "\n") {
if data := rQueries.FindStringSubmatch(line); data != nil {
value, err := strconv.ParseFloat(data[1], 64)
if err != nil {
continue
}
inputs.NewSample("engine_innodb_queries_inside_innodb", value)
value, err = strconv.ParseFloat(data[2], 64)
if err != nil {
continue
}
inputs.NewSample("engine_innodb_queries_in_queue", value)
} else if data := rViews.FindStringSubmatch(line); data != nil {
value, err := strconv.ParseFloat(data[1], 64)
if err != nil {
continue
}
inputs.NewSample("engine_innodb_read_views_open_inside_innodb", value)
}
}
}

View File

@ -16,7 +16,7 @@ import (
// Regexp to match various groups of status vars.
var globalStatusRE = regexp.MustCompile(`^(com|handler|connection_errors|innodb_buffer_pool_pages|innodb_rows|performance_schema)_(.*)$`)
func (m *MySQL) gatherGlobalStatus(slist *list.SafeList, ins *Instance, db *sql.DB, globalTags map[string]string) {
func (m *MySQL) gatherGlobalStatus(slist *list.SafeList, ins *Instance, db *sql.DB, globalTags map[string]string, cache map[string]float64) {
rows, err := db.Query(SQL_GLOBAL_STATUS)
if err != nil {
log.Println("E! failed to query global status:", err)
@ -51,12 +51,14 @@ func (m *MySQL) gatherGlobalStatus(slist *list.SafeList, ins *Instance, db *sql.
continue
}
// collect float fields
if _, has := ins.validMetrics[key]; !has {
continue
}
if floatVal, ok := parseStatus(val); ok {
cache[key] = floatVal
// collect float fields
if _, has := ins.validMetrics[key]; !has {
continue
}
match := globalStatusRE.FindStringSubmatch(key)
if match == nil {
slist.PushFront(inputs.NewSample("global_status_"+key, floatVal, tags))

View File

@ -12,7 +12,7 @@ import (
"github.com/toolkits/pkg/container/list"
)
func (m *MySQL) gatherGlobalVariables(slist *list.SafeList, ins *Instance, db *sql.DB, globalTags map[string]string) {
func (m *MySQL) gatherGlobalVariables(slist *list.SafeList, ins *Instance, db *sql.DB, globalTags map[string]string, cache map[string]float64) {
rows, err := db.Query(SQL_GLOBAL_VARIABLES)
if err != nil {
log.Println("E! failed to query global variables:", err)
@ -50,12 +50,14 @@ func (m *MySQL) gatherGlobalVariables(slist *list.SafeList, ins *Instance, db *s
continue
}
// collect float fields
if _, has := ins.validMetrics[key]; !has {
continue
}
if floatVal, ok := parseStatus(val); ok {
cache[key] = floatVal
// collect float fields
if _, has := ins.validMetrics[key]; !has {
continue
}
slist.PushFront(inputs.NewSample("global_variables_"+key, floatVal, tags))
continue
}

View File

@ -226,6 +226,25 @@ func (m *MySQL) gatherOnce(slist *list.SafeList, ins *Instance) {
slist.PushFront(inputs.NewSample("up", 1, tags))
m.gatherGlobalStatus(slist, ins, db, tags)
m.gatherGlobalVariables(slist, ins, db, tags)
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)
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)
}
}
}

View File

@ -9,6 +9,8 @@ const (
SQL_GLOBAL_VARIABLES = `SHOW GLOBAL VARIABLES`
SQL_ENGINE_INNODB_STATUS = `SHOW /*!50000 ENGINE*/ INNODB STATUS`
SQL_95TH_PERCENTILE = `SELECT avg_us, ro as percentile FROM
(SELECT avg_us, @rownum := @rownum + 1 as ro FROM
(SELECT ROUND(avg_timer_wait / 1000000) as avg_us