2015-05-18 18:54:11 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
"github.com/influxdb/tivan/plugins"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Mysql struct {
|
2015-05-18 22:22:04 +00:00
|
|
|
Servers []string
|
2015-05-18 22:10:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
|
|
|
# specify servers via a url matching:
|
|
|
|
# [username[:password]@][protocol[(address)]]/[?tls=[true|false|skip-verify]]
|
|
|
|
#
|
|
|
|
# If no servers are specified, then localhost is used as the host.
|
|
|
|
servers = ["localhost"]`
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
func (m *Mysql) Gather(acc plugins.Accumulator) error {
|
|
|
|
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{
|
|
|
|
{
|
|
|
|
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
|
|
|
},
|
|
|
|
{
|
|
|
|
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
|
|
|
},
|
|
|
|
{
|
|
|
|
onServer: "Threads_",
|
2015-05-18 19:15:15 +00:00
|
|
|
inExport: "threads_",
|
2015-05-18 18:54:11 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-05-18 22:22:04 +00:00
|
|
|
func (m *Mysql) gatherServer(serv string, acc plugins.Accumulator) error {
|
|
|
|
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 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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)))
|
|
|
|
acc.Add(mapped.inExport+name[len(mapped.onServer):], i, nil)
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch name {
|
|
|
|
case "Queries":
|
|
|
|
i, err := strconv.ParseInt(string(val.([]byte)), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-18 19:15:15 +00:00
|
|
|
acc.Add("queries", i, nil)
|
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-05-18 19:15:15 +00:00
|
|
|
acc.Add("slow_queries", i, nil)
|
2015-05-18 18:54:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
plugins.Add("mysql", func() plugins.Plugin {
|
|
|
|
return &Mysql{}
|
|
|
|
})
|
|
|
|
}
|