This repository was archived by the owner on Sep 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
inspect-mysql #40
Open
BrianIp
wants to merge
48
commits into
square:master
Choose a base branch
from
BrianIp:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
inspect-mysql #40
Changes from all commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
5f4baca
initial write of mysqlstat
33ccdaa
redone
c827549
created mysqlstat
bfb257f
minor fixes
f1e6940
wrote mysqlstat
8f5c18b
get global status started
d9e4bed
Merge branch 'master' of https://github.com/square/prodeng into brian…
2b749f3
fixes
74f757c
update inspect-sql
cab8bb9
Merge remote-tracking branch 'upstream/master'
d280659
added more metrics
0e28292
moved mysqlstat to new directory
fd1d461
start writing mysql-table
93b6e2e
add mysql tools
ac1edd2
enable password from config file
0fbdfad
files added
11f2c97
more metrics in mysqlstat
60a6f6f
Merge branch 'brian-mysql'
c90449d
added commentary and go fmt
099b677
fix cnf file path
5c161e1
added more metrics
d1108ba
bug fix in reading query responses
bcda50c
metrics added
c3b90b2
alphabetized lists of metrics
4a225c1
metrics and error checks
dd58730
check tables and dbs in mysqlstat-table
5ccfe54
bug fixes and logging added
e0c2a71
readme added
92d00db
minor fixes
4d783d0
added backups check
3c5e6a2
issues addressed
2f8d015
added testing
599d87c
fixes to tests
02d8afb
small fix in collect
6113459
fix path for inndob test files
0fc8fdd
small fix in mysqlstat test
803a53c
redirected stderr in _test files
76cd0dd
Update readme
1fbe926
Added Testing section to readme
289020e
fixed innodb tests
20a3957
Merge branch 'master' of https://github.com/BrianIp/prodeng
e6b67f4
locks added to mysqlstat-tables
d3837fa
tests added
2cb170e
minor fixes
f4a5951
added longest trx query
0531a20
Merge remote-tracking branch 'upstream/master' into metrics_fix
1111b0c
metric renaming to work better with nagios
61f691b
added new metrics
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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`. | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 ( | ||
| "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) | ||
| } | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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