Skip to content
This repository was archived by the owner on Sep 25, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
5f4baca
initial write of mysqlstat
May 27, 2014
33ccdaa
redone
May 28, 2014
c827549
created mysqlstat
May 28, 2014
bfb257f
minor fixes
May 28, 2014
f1e6940
wrote mysqlstat
May 28, 2014
8f5c18b
get global status started
May 28, 2014
d9e4bed
Merge branch 'master' of https://github.com/square/prodeng into brian…
May 28, 2014
2b749f3
fixes
May 28, 2014
74f757c
update inspect-sql
May 28, 2014
cab8bb9
Merge remote-tracking branch 'upstream/master'
May 28, 2014
d280659
added more metrics
May 28, 2014
0e28292
moved mysqlstat to new directory
May 28, 2014
fd1d461
start writing mysql-table
May 29, 2014
93b6e2e
add mysql tools
May 29, 2014
ac1edd2
enable password from config file
May 29, 2014
0fbdfad
files added
May 29, 2014
11f2c97
more metrics in mysqlstat
May 29, 2014
60a6f6f
Merge branch 'brian-mysql'
May 29, 2014
c90449d
added commentary and go fmt
May 29, 2014
099b677
fix cnf file path
May 29, 2014
5c161e1
added more metrics
May 30, 2014
d1108ba
bug fix in reading query responses
May 30, 2014
bcda50c
metrics added
Jun 3, 2014
c3b90b2
alphabetized lists of metrics
Jun 3, 2014
4a225c1
metrics and error checks
Jun 3, 2014
dd58730
check tables and dbs in mysqlstat-table
Jun 3, 2014
5ccfe54
bug fixes and logging added
Jun 4, 2014
e0c2a71
readme added
Jun 4, 2014
92d00db
minor fixes
Jun 5, 2014
4d783d0
added backups check
Jun 5, 2014
3c5e6a2
issues addressed
Jun 10, 2014
2f8d015
added testing
Jun 11, 2014
599d87c
fixes to tests
Jun 11, 2014
02d8afb
small fix in collect
Jun 11, 2014
6113459
fix path for inndob test files
Jun 11, 2014
0fc8fdd
small fix in mysqlstat test
Jun 11, 2014
803a53c
redirected stderr in _test files
Jun 11, 2014
76cd0dd
Update readme
Jun 11, 2014
1fbe926
Added Testing section to readme
Jun 11, 2014
289020e
fixed innodb tests
Jun 11, 2014
20a3957
Merge branch 'master' of https://github.com/BrianIp/prodeng
Jun 11, 2014
e6b67f4
locks added to mysqlstat-tables
Jun 12, 2014
d3837fa
tests added
Jun 13, 2014
2cb170e
minor fixes
Jun 16, 2014
f4a5951
added longest trx query
Jun 16, 2014
0531a20
Merge remote-tracking branch 'upstream/master' into metrics_fix
Jun 17, 2014
1111b0c
metric renaming to work better with nagios
Jun 19, 2014
61f691b
added new metrics
Jun 19, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions inspect-mysql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#inspect-mysql


inspect-mysql is a collection of libraries for gathering metrics of mysql databases.

inspect command line is a utility that gives a brief overview on the databases: version, uptime, queries made, and database sizes.

inspect gathers the following metrics:
- Version number
- Slave Stats
- Global Stats
- Binlog Stats
- Stacked Query info
- Session Info
- Innodb stats
- Long Run Query info
- Query Response Times

##Installation

1. Get Go
2. `go get -v -u github.com/square/prodeng/inspect-mysql`

##Usage

###Command Line

./bin/inspect-mysql

```
--------------------------
Version: 5.1234
Queries made: 123456
Uptime: 543210
Database sizes:
database_name: 0.54 GB
other_database_name: 12.31 GB

```

###Server

_inspect-mysql_ can be run in server mode to run continuously and expose all metrics via HTTP JSON api

./bin/inspect-mysql -server -address :12345

```
[
{"type": "counter", "name": "mysqlstat.Queries", "value": 9342251, "rate": 31.003152},
{"type": "counter", "name": "mysqltablestat.database_name.table_name.RowsRead", "value": 0, "rate": 0.000000},
{"type": "counter", "name": "mysqltablestat.database_name.table_name.RowsChanged", "value": 0, "rate": 0.000000},
{"type": "counter", "name": "mysqltablestat.database_name.other_table_name.RowsChanged", "value": 0, "rate": 0.000000},
{"type": "counter", "name": "mysqltablestat.database_name.table_name.RowsChangedXIndexes", "value": 0, "rate": 0.000000},
... truncated
{"type": "counter", "name": "mysqlstat.SortMergePasses", "value": 0, "rate": 0.000000}]
```

###Example API Use


```
// Import packages
import "github.com/square/prodeng/inspect-mysql"
import "github.com/square/prodeng/metrics"

// Initialize a metric context
m := metrics.NewMetricContext("system")

// Collect mysql metrics every m.Step seconds
sqlstats := mysqlstat.New(m, time.Millisecond*2000)

// Collects mysql metrics for specific databases and tables
sqltablestats := mysqlstattable.New(m, time.Millisecond*2000)
```

All metrics collected are exported, so any metric may be accessed using Get():
```
// Print the number of queries accessed
fmt.Println(sqlstats.Metrics.Queries.Get())

// Print the size of table t1 in databse db1
fmt.Println(sqltablestats.DBs["db1"].Tables["t1"].Metrics.SizeBytes.Get())
```

##Testing

Packages are tested using Go's testing package.
To test:
1. cd to the directory containing the .go and _test.go files
2. Run `go test`. You can also run with the `-v` option for a verbose output. For these tests, many logs are expected so stderr is redirected to a file `test.log`

Tests for each metric may be added to `mysqlstat_test.go` and `mysqlstat-tables_test.go`. These tests do not connect to a database. Instead, the desired test input is hard coded into each test. Testing for the parser for the Innodb metrics are located in `mysqltools_test.go`.






73 changes: 73 additions & 0 deletions inspect-mysql/inspect-mysql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//Copyright (c) 2014 Square, Inc
//Launches metrics collector for mysql databases

package main

import (
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably run every file through go fmt

"flag"
"fmt"
"log"
"net/http"
"os"
"strconv"
"time"

"github.com/square/prodeng/inspect-mysql/mysqlstat"
"github.com/square/prodeng/inspect-mysql/mysqlstattable"
"github.com/square/prodeng/metrics"
)

func main() {
var user, password, address, conf string
var stepSec int
var servermode, human bool

m := metrics.NewMetricContext("system")

flag.StringVar(&user, "u", "root", "user using database")
flag.StringVar(&password, "p", "", "password for database")
flag.BoolVar(&servermode, "server", false, "Runs continously and exposes metrics as JSON on HTTP")
flag.StringVar(&address, "address", ":12345", "address to listen on for http if running in server mode")
flag.IntVar(&stepSec, "step", 2, "metrics are collected every step seconds")
flag.StringVar(&conf, "conf", "/root/.my.cnf", "configuration file")
flag.BoolVar(&human, "h", false, "Makes output in MB for human readable sizes")
flag.Parse()

if servermode {
go func() {
http.HandleFunc("/metrics.json", m.HttpJsonHandler)
log.Fatal(http.ListenAndServe(address, nil))
}()
}
step := time.Millisecond * time.Duration(stepSec) * 1000

sqlstat, err := mysqlstat.New(m, step, user, password, conf)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
sqlstatTables, err := mysqlstattable.New(m, step, user, password, conf)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
ticker := time.NewTicker(step * 2)
for _ = range ticker.C {
//Print stats here, more stats than printed are actually collected
fmt.Println("--------------------------")
fmt.Println("Version: " + strconv.FormatFloat(sqlstat.Metrics.Version.Get(), 'f', -1, 64))
fmt.Println("Queries made: " + strconv.Itoa(int(sqlstat.Metrics.Queries.Get())))
fmt.Println("Uptime: " + strconv.Itoa(int(sqlstat.Metrics.Uptime.Get())))
fmt.Println("Database sizes: ")
for dbname, db := range sqlstatTables.DBs {
size := db.Metrics.SizeBytes.Get()
units := " B"
if human {
size /= (1024 * 1024)
units = " GB"
}
fmt.Println(" " + dbname + ": " + strconv.FormatFloat(size, 'f', 2, 64) + units)
}
}

}
Loading