-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
90 lines (75 loc) · 2.16 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"database/sql"
"flag"
"fmt"
"os"
"strings"
// 🐒 patching of "database/sql".
_ "github.com/go-sql-driver/mysql"
)
const (
query = "SELECT * FROM sys.gr_member_routing_candidate_status"
)
var (
// Version version.
Version = "DEV"
// We pass credentials in this env var as there is no better way of doing this from haproxy.
mysqlCredentials = os.Getenv("PATH")
mysqlAddress = os.Getenv("HAPROXY_SERVER_ADDR") + ":" + os.Getenv("HAPROXY_SERVER_PORT")
haproxyBackendName = os.Getenv("HAPROXY_PROXY_NAME")
)
type eventRow struct {
ViableCandidate string
ReadOnly string
TransactionsBehind string
TransactionsToCert string
}
func debugMsg(isDebug bool, msg string) {
if isDebug {
fmt.Println(msg)
}
}
func main() {
var versionFlag, debugFlag bool
flag.BoolVar(&versionFlag, "v", false, "show version")
flag.BoolVar(&debugFlag, "d", false, "enable debug output")
flag.Parse()
if versionFlag {
fmt.Println("Version", Version)
os.Exit(0)
}
if !strings.HasSuffix(haproxyBackendName, "_primary") && !strings.HasSuffix(haproxyBackendName, "_secondary") {
debugMsg(debugFlag, "Haproxy backend name does not end with either _primary or _secondary.")
os.Exit(1)
}
db, err := sql.Open("mysql", fmt.Sprintf("%s@tcp(%s)/", mysqlCredentials, mysqlAddress))
if err != nil {
fmt.Println("Error connecting to MySQL", err)
os.Exit(1)
}
rows, err := db.Query(query)
if err != nil {
fmt.Println("Error selecting from MySQL table:", err)
os.Exit(1)
}
var row eventRow
for rows.Next() {
rows.Scan(&row.ViableCandidate, &row.ReadOnly, &row.TransactionsBehind, &row.TransactionsToCert)
debugMsg(debugFlag, fmt.Sprintf("MySQL query result: %+v\n", row))
break
}
if row.ViableCandidate != "YES" {
debugMsg(debugFlag, "GR member is not viable candidate.")
os.Exit(1)
}
if strings.HasSuffix(haproxyBackendName, "_primary") && row.ReadOnly == "NO" {
debugMsg(debugFlag, "HEALTHCHECK PRIMARY - OK")
return
} else if strings.HasSuffix(haproxyBackendName, "_secondary") && row.ReadOnly == "YES" {
debugMsg(debugFlag, "HEALTHCHECK SECONDARY - OK")
return
}
debugMsg(debugFlag, "HEALTHCHECK - NOT OK")
os.Exit(1)
}