From 2fd84aa66e0ca176b2db5db64136996e98f29fb5 Mon Sep 17 00:00:00 2001 From: Tyler Nisonoff Date: Sat, 17 Oct 2015 21:04:00 -0700 Subject: [PATCH] added connections measurement with user tag --- plugins/mysql/mysql.go | 41 +++++++++++++++++++++++++++++-------- plugins/mysql/mysql_test.go | 1 + 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/plugins/mysql/mysql.go b/plugins/mysql/mysql.go index a4ae7a6da..a2810057f 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,29 @@ 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} + fmt.Println("user: " + user) + fmt.Println(connections) + //fmt.Println("connections: " + string(connections)) + + 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 {