2015-05-18 18:54:11 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
2016-01-20 20:19:03 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-05-18 18:54:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Mysql struct {
|
2015-05-18 22:22:04 +00:00
|
|
|
Servers []string
|
2015-05-18 22:10:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2015-10-15 21:53:29 +00:00
|
|
|
# specify servers via a url matching:
|
|
|
|
# [username[:password]@][protocol[(address)]]/[?tls=[true|false|skip-verify]]
|
2015-10-22 23:11:51 +00:00
|
|
|
# see https://github.com/go-sql-driver/mysql#dsn-data-source-name
|
2015-10-15 21:53:29 +00:00
|
|
|
# e.g.
|
2015-10-22 23:11:51 +00:00
|
|
|
# root:passwd@tcp(127.0.0.1:3306)/?tls=false
|
|
|
|
# root@tcp(127.0.0.1:3306)/?tls=false
|
2015-10-15 21:53:29 +00:00
|
|
|
#
|
|
|
|
# If no servers are specified, then localhost is used as the host.
|
2015-10-22 23:11:51 +00:00
|
|
|
servers = ["tcp(127.0.0.1:3306)/"]
|
2015-08-26 15:21:39 +00:00
|
|
|
`
|
2015-05-18 22:10:11 +00:00
|
|
|
|
|
|
|
func (m *Mysql) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mysql) Description() string {
|
|
|
|
return "Read metrics from one or many mysql servers"
|
2015-05-18 18:54:11 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 22:22:04 +00:00
|
|
|
var localhost = ""
|
2015-05-18 18:54:11 +00:00
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
func (m *Mysql) Gather(acc inputs.Accumulator) error {
|
2015-05-18 18:54:11 +00:00
|
|
|
if len(m.Servers) == 0 {
|
|
|
|
// if we can't get stats in this case, thats fine, don't report
|
|
|
|
// an error.
|
|
|
|
m.gatherServer(localhost, acc)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, serv := range m.Servers {
|
|
|
|
err := m.gatherServer(serv, acc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type mapping struct {
|
|
|
|
onServer string
|
|
|
|
inExport string
|
|
|
|
}
|
|
|
|
|
|
|
|
var mappings = []*mapping{
|
2015-09-01 17:30:56 +00:00
|
|
|
{
|
|
|
|
onServer: "Aborted_",
|
|
|
|
inExport: "aborted_",
|
|
|
|
},
|
2015-05-18 18:54:11 +00:00
|
|
|
{
|
|
|
|
onServer: "Bytes_",
|
2015-05-18 19:15:15 +00:00
|
|
|
inExport: "bytes_",
|
2015-05-18 18:54:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
onServer: "Com_",
|
2015-05-18 19:15:15 +00:00
|
|
|
inExport: "commands_",
|
2015-05-18 18:54:11 +00:00
|
|
|
},
|
2015-09-01 17:30:56 +00:00
|
|
|
{
|
|
|
|
onServer: "Created_",
|
|
|
|
inExport: "created_",
|
|
|
|
},
|
2015-05-18 18:54:11 +00:00
|
|
|
{
|
|
|
|
onServer: "Handler_",
|
2015-05-18 19:15:15 +00:00
|
|
|
inExport: "handler_",
|
2015-05-18 18:54:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
onServer: "Innodb_",
|
2015-05-18 19:15:15 +00:00
|
|
|
inExport: "innodb_",
|
2015-05-18 18:54:11 +00:00
|
|
|
},
|
2015-09-01 17:30:56 +00:00
|
|
|
{
|
|
|
|
onServer: "Key_",
|
|
|
|
inExport: "key_",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
onServer: "Open_",
|
|
|
|
inExport: "open_",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
onServer: "Opened_",
|
|
|
|
inExport: "opened_",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
onServer: "Qcache_",
|
|
|
|
inExport: "qcache_",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
onServer: "Table_",
|
|
|
|
inExport: "table_",
|
|
|
|
},
|
2015-07-19 20:01:45 +00:00
|
|
|
{
|
2015-08-04 22:09:59 +00:00
|
|
|
onServer: "Tokudb_",
|
|
|
|
inExport: "tokudb_",
|
|
|
|
},
|
2015-05-18 18:54:11 +00:00
|
|
|
{
|
|
|
|
onServer: "Threads_",
|
2015-05-18 19:15:15 +00:00
|
|
|
inExport: "threads_",
|
2015-05-18 18:54:11 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
func (m *Mysql) gatherServer(serv string, acc inputs.Accumulator) error {
|
2015-10-22 23:11:51 +00:00
|
|
|
// If user forgot the '/', add it
|
|
|
|
if strings.HasSuffix(serv, ")") {
|
|
|
|
serv = serv + "/"
|
|
|
|
} else if serv == "localhost" {
|
2015-05-18 23:08:22 +00:00
|
|
|
serv = ""
|
|
|
|
}
|
|
|
|
|
2015-05-18 22:22:04 +00:00
|
|
|
db, err := sql.Open("mysql", serv)
|
2015-05-18 18:54:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
rows, err := db.Query(`SHOW /*!50002 GLOBAL */ STATUS`)
|
|
|
|
if err != nil {
|
2015-07-03 12:25:18 +00:00
|
|
|
return err
|
2015-05-18 18:54:11 +00:00
|
|
|
}
|
|
|
|
|
2015-10-18 06:56:59 +00:00
|
|
|
var servtag string
|
2015-10-22 23:11:51 +00:00
|
|
|
servtag, err = parseDSN(serv)
|
|
|
|
if err != nil {
|
2015-10-18 06:56:59 +00:00
|
|
|
servtag = "localhost"
|
|
|
|
}
|
2015-12-15 00:03:33 +00:00
|
|
|
tags := map[string]string{"server": servtag}
|
|
|
|
fields := make(map[string]interface{})
|
2015-05-18 18:54:11 +00:00
|
|
|
for rows.Next() {
|
|
|
|
var name string
|
|
|
|
var val interface{}
|
|
|
|
|
|
|
|
err = rows.Scan(&name, &val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var found bool
|
|
|
|
|
|
|
|
for _, mapped := range mappings {
|
|
|
|
if strings.HasPrefix(name, mapped.onServer) {
|
|
|
|
i, _ := strconv.Atoi(string(val.([]byte)))
|
2015-12-15 00:03:33 +00:00
|
|
|
fields[mapped.inExport+name[len(mapped.onServer):]] = i
|
2015-05-18 18:54:11 +00:00
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch name {
|
|
|
|
case "Queries":
|
|
|
|
i, err := strconv.ParseInt(string(val.([]byte)), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-15 00:03:33 +00:00
|
|
|
fields["queries"] = i
|
2015-05-18 18:54:11 +00:00
|
|
|
case "Slow_queries":
|
|
|
|
i, err := strconv.ParseInt(string(val.([]byte)), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-15 00:03:33 +00:00
|
|
|
fields["slow_queries"] = i
|
2015-05-18 18:54:11 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-15 00:03:33 +00:00
|
|
|
acc.AddFields("mysql", fields, tags)
|
2015-05-18 18:54:11 +00:00
|
|
|
|
2015-10-18 06:56:59 +00:00
|
|
|
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}
|
2015-12-15 00:03:33 +00:00
|
|
|
fields := make(map[string]interface{})
|
2015-10-18 06:56:59 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-15 00:03:33 +00:00
|
|
|
fields["connections"] = connections
|
|
|
|
acc.AddFields("mysql_users", fields, tags)
|
2015-10-18 06:56:59 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 18:54:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-07 20:39:43 +00:00
|
|
|
inputs.Add("mysql", func() inputs.Input {
|
2015-05-18 18:54:11 +00:00
|
|
|
return &Mysql{}
|
|
|
|
})
|
|
|
|
}
|