forked from mackerelio/go-check-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-mysql.go
54 lines (46 loc) · 1.29 KB
/
check-mysql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"fmt"
"os"
"strings"
"github.com/mackerelio/checkers"
"github.com/ziutek/mymysql/mysql"
_ "github.com/ziutek/mymysql/native"
)
type mysqlSetting struct {
Host string `short:"H" long:"host" default:"localhost" description:"Hostname"`
Port string `short:"p" long:"port" default:"3306" description:"Port"`
User string `short:"u" long:"user" default:"root" description:"Username"`
Pass string `short:"P" long:"password" default:"" description:"Password"`
}
var commands = map[string](func([]string) *checkers.Checker){
"replication": checkReplication,
"connection": checkConnection,
"uptime": checkUptime,
}
func separateSub(argv []string) (string, []string) {
if len(argv) == 0 || strings.HasPrefix(argv[0], "-") {
return "", argv
}
return argv[0], argv[1:]
}
func main() {
subCmd, argv := separateSub(os.Args[1:])
fn, ok := commands[subCmd]
if !ok {
fmt.Println(`Usage:
check-mysql [subcommand] [OPTIONS]
SubCommands:`)
for k := range commands {
fmt.Printf(" %s\n", k)
}
os.Exit(1)
}
ckr := fn(argv)
ckr.Name = fmt.Sprintf("MySQL %s", strings.ToUpper(string(subCmd[0]))+subCmd[1:])
ckr.Exit()
}
func newMySQL(m mysqlSetting) mysql.Conn {
target := fmt.Sprintf("%s:%s", m.Host, m.Port)
return mysql.New("tcp", "", target, m.User, m.Pass, "")
}