forked from mackerelio/go-check-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replication.go
60 lines (51 loc) · 1.66 KB
/
replication.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
55
56
57
58
59
60
package main
import (
"fmt"
"os"
"github.com/jessevdk/go-flags"
"github.com/mackerelio/checkers"
)
type replicationOpts struct {
mysqlSetting
Crit int64 `short:"c" long:"critical" default:"250" description:"critical if the seconds behind master is over"`
Warn int64 `short:"w" long:"warning" default:"200" description:"warning if the seconds behind master is over"`
}
func checkReplication(args []string) *checkers.Checker {
opts := replicationOpts{}
psr := flags.NewParser(&opts, flags.Default)
psr.Usage = "replication [OPTIONS]"
_, err := psr.ParseArgs(args)
if err != nil {
os.Exit(1)
}
db := newMySQL(opts.mysqlSetting)
err = db.Connect()
if err != nil {
return checkers.Unknown("couldn't connect DB")
}
defer db.Close()
rows, res, err := db.Query("SHOW SLAVE STATUS")
if err != nil {
return checkers.Unknown("couldn't execute query")
}
if len(rows) == 0 {
return checkers.Ok("MySQL is not slave")
}
idxIoThreadRunning := res.Map("Slave_IO_Running")
idxSQLThreadRunning := res.Map("Slave_SQL_Running")
idxSecondsBehindMaster := res.Map("Seconds_Behind_Master")
ioThreadStatus := rows[0].Str(idxIoThreadRunning)
sqlThreadStatus := rows[0].Str(idxSQLThreadRunning)
secondsBehindMaster := rows[0].Int64(idxSecondsBehindMaster)
if ioThreadStatus == "No" || sqlThreadStatus == "No" {
return checkers.Critical("MySQL replication has been stopped")
}
checkSt := checkers.OK
msg := fmt.Sprintf("MySQL replication behind master %d seconds", secondsBehindMaster)
if secondsBehindMaster > opts.Crit {
checkSt = checkers.CRITICAL
} else if secondsBehindMaster > opts.Warn {
checkSt = checkers.WARNING
}
return checkers.NewChecker(checkSt, msg)
}