-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
137 lines (111 loc) · 2.97 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"flag"
"fmt"
"os"
"time"
"mysql-diag/canaryclient"
"mysql-diag/config"
"mysql-diag/database"
"mysql-diag/disk"
"mysql-diag/msg"
"mysql-diag/ui"
)
const (
defaultConfigPath = "/var/vcap/jobs/mysql-diag/config/mysql-diag-config.yml"
)
var configFilepath = flag.String("c", defaultConfigPath, "location of config file")
// returns true if the cluster needs bootstrap
func checkClusterStatus(mysqlConfig config.MysqlConfig) bool {
clusterStateTable := ui.NewClusterStateTable(os.Stdout)
rows := getClusterStatus(mysqlConfig)
for _, row := range rows {
n := row.node
clusterStateTable.Add(n.Host, n.Name, n.UUID, row.status)
}
clusterStateTable.Render()
statuses := make([]*database.GaleraStatus, len(rows))
for i, row := range rows {
statuses[i] = row.status
}
if database.DoWeNeedBootstrap(statuses) {
return true
} else {
fmt.Println("I don't think bootstrap is necessary")
return false
}
}
type nodeStatus struct {
node config.MysqlNode
status *database.GaleraStatus
}
func getClusterStatus(mysqlConfig config.MysqlConfig) []*nodeStatus {
channel := make(chan nodeStatus, len(mysqlConfig.Nodes))
for _, n := range mysqlConfig.Nodes {
n := n
go func() {
intro := fmt.Sprintf("Checking cluster status of %s/%s at %s... ", n.Name, n.UUID, n.Host)
fmt.Println(intro)
ac := database.NewDatabaseClient(mysqlConfig.Connection(n))
galeraStatus, err := ac.Status()
if err != nil {
msg.PrintfErrorIntro(intro, "%v", err)
} else {
fmt.Println(intro + "done")
}
channel <- nodeStatus{node: n, status: galeraStatus}
}()
}
var nodeStatuses []*nodeStatus
for i := 0; i < len(mysqlConfig.Nodes); i++ {
ns := <-channel
nodeStatuses = append(nodeStatuses, &ns)
}
return nodeStatuses
}
// Returns true if the canary is unhealthy. Otherwise it's either healthy or unknown.
func checkCanary(config *config.CanaryConfig) bool {
if config == nil {
fmt.Println("Canary not configured, skipping health check")
return false
}
intro := "Checking canary status... "
fmt.Println(intro)
client := canaryclient.NewCanaryClient("127.0.0.1", config.ApiPort, *config)
healthy, err := client.Status()
if err != nil {
msg.PrintfErrorIntro(intro, "%v", err)
return false
} else {
if healthy {
fmt.Println(intro + msg.Happy("healthy"))
return false
} else {
fmt.Println(intro + msg.Alert("unhealthy"))
return true
}
}
}
func printCurrentTime() {
fmt.Println(time.Now().UTC().Format(time.UnixDate))
}
func main() {
flag.Parse()
c, err := config.LoadFromFile(*configFilepath)
if err != nil {
msg.PrintfError("%v", err)
os.Exit(1)
}
printCurrentTime()
unhealthy := checkCanary(c.Canary)
needsBootstrap := checkClusterStatus(c.Mysql)
diskSpaceIssues := disk.CheckDiskStatus(c.Mysql)
messages := ui.Report(ui.ReporterParams{
IsCanaryHealthy: !unhealthy,
NeedsBootstrap: needsBootstrap,
DiskSpaceIssues: diskSpaceIssues,
}, c)
for _, message := range messages {
fmt.Println(message)
}
}