From a716d220b5f236d68ea3a3e8b77fe4d9b6422195 Mon Sep 17 00:00:00 2001 From: Tyler Nisonoff Date: Sat, 17 Oct 2015 23:56:59 -0700 Subject: [PATCH] added connections measurement with user tag --- plugins/mysql/mysql.go | 37 ++++++++++++++++++++++++++++--------- plugins/mysql/mysql_test.go | 1 + 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/plugins/mysql/mysql.go b/plugins/mysql/mysql.go index a4ae7a6da..89c4c9b44 100644 --- a/plugins/mysql/mysql.go +++ b/plugins/mysql/mysql.go @@ -129,6 +129,15 @@ func (m *Mysql) gatherServer(serv string, acc plugins.Accumulator) error { return err } + // Parse out user/password from server address tag if given + var servtag string + if strings.Contains(serv, "@") { + servtag = strings.Split(serv, "@")[1] + } else if serv == "" { + servtag = "localhost" + } else { + servtag = serv + } for rows.Next() { var name string var val interface{} @@ -140,15 +149,6 @@ func (m *Mysql) gatherServer(serv string, acc plugins.Accumulator) error { var found bool - // Parse out user/password from server address tag if given - var servtag string - if strings.Contains(serv, "@") { - servtag = strings.Split(serv, "@")[1] - } else if serv == "" { - servtag = "localhost" - } else { - servtag = serv - } tags := map[string]string{"server": servtag} for _, mapped := range mappings { @@ -181,6 +181,25 @@ func (m *Mysql) gatherServer(serv string, acc plugins.Accumulator) error { } } + conn_rows, err := db.Query("SELECT user, sum(1) FROM INFORMATION_SCHEMA.PROCESSLIST GROUP BY user") + + for conn_rows.Next() { + var user string + var connections int64 + + err = conn_rows.Scan(&user, &connections) + if err != nil { + return err + } + + tags := map[string]string{"server": servtag, "user": user} + + if err != nil { + return err + } + acc.Add("connections", connections, tags) + } + return nil } diff --git a/plugins/mysql/mysql_test.go b/plugins/mysql/mysql_test.go index 8a284ca02..24e7401ce 100644 --- a/plugins/mysql/mysql_test.go +++ b/plugins/mysql/mysql_test.go @@ -45,6 +45,7 @@ func TestMysqlGeneratesMetrics(t *testing.T) { intMetrics := []string{ "queries", "slow_queries", + "connections", } for _, prefix := range prefixes {