forked from signal18/replication-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
323 lines (292 loc) · 11.1 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// replication-manager - Replication Manager Monitoring and CLI for MariaDB and MySQL
// Copyright 2017 Signal 18 SARL
// Authors: Guillaume Lefranc <guillaume@signal18.io>
// Stephane Varoqui <svaroqui@gmail.com>
// This source code is licensed under the GNU General Public License, version 3.
// Redistribution/Reuse of this code is permitted under the GNU v3 license, as
// an additional term, ALL code must carry the original Author(s) credit in comment form.
// See LICENSE in this directory for the integral text.
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
log "github.com/sirupsen/logrus"
"github.com/signal18/replication-manager/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
cfgGroup string
cfgGroupList []string
cfgGroupIndex int
conf config.Config
memprofile string
// Version is the semantic version number, e.g. 1.0.1
Version string
// Provisoning to add flags for compile
WithProvisioning string
WithArbitration string
WithArbitrationClient string
WithProxysql string
WithHaproxy string
WithMaxscale string
WithMariadbshardproxy string
WithMonitoring string
WithMail string
WithHttp string
WithSpider string
WithEnforce string
WithDeprecate string
WithOpenSVC string
WithMultiTiers string
WithTarball string
WithMySQLRouter string
WithSphinx string
WithBackup string
// FullVersion is the semantic version number + git commit hash
FullVersion string
// Build is the build date of replication-manager
Build string
GoOS string
GoArch string
)
var confs = make(map[string]config.Config)
var currentClusterName string
type Settings struct {
Enterprise string `json:"enterprise"`
Interactive string `json:"interactive"`
FailoverCtr string `json:"failoverctr"`
MaxDelay string `json:"maxdelay"`
Faillimit string `json:"faillimit"`
LastFailover string `json:"lastfailover"`
MonHearbeats string `json:"monheartbeats"`
Uptime string `json:"uptime"`
UptimeFailable string `json:"uptimefailable"`
UptimeSemiSync string `json:"uptimesemisync"`
RplChecks string `json:"rplchecks"`
FailSync string `json:"failsync"`
SwitchSync string `json:"switchsync"`
Verbose string `json:"verbose"`
Rejoin string `json:"rejoin"`
RejoinBackupBinlog string `json:"rejoinbackupbinlog"`
RejoinSemiSync string `json:"rejoinsemisync"`
RejoinFlashback string `json:"rejoinflashback"`
RejoinUnsafe string `json:"rejoinunsafe"`
RejoinDump string `json:"rejoindump"`
RejoinPseudoGTID string `json:"rejoinpseudogtid"`
Test string `json:"test"`
Heartbeat string `json:"heartbeat"`
Status string `json:"runstatus"`
IsActive string `json:"isactive"`
ConfGroup string `json:"confgroup"`
MonitoringTicker string `json:"monitoringticker"`
FailResetTime string `json:"failresettime"`
ToSessionEnd string `json:"tosessionend"`
HttpAuth string `json:"httpauth"`
HttpBootstrapButton string `json:"httpbootstrapbutton"`
GraphiteMetrics string `json:"graphitemetrics"`
Clusters []string `json:"clusters"`
RegTests []string `json:"regtests"`
Topology string `json:"topology"`
Version string `json:"version"`
DBTags []string `json:"databasetags"`
ProxyTags []string `json:"proxytags"`
}
type heartbeat struct {
UUID string `json:"uuid"`
Secret string `json:"secret"`
Cluster string `json:"cluster"`
Master string `json:"master"`
UID int `json:"id"`
Status string `json:"status"`
Hosts int `json:"hosts"`
Failed int `json:"failed"`
}
func init() {
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
rootCmd.AddCommand(versionCmd)
rootCmd.PersistentFlags().StringVar(&conf.ConfigFile, "config", "", "Configuration file (default is config.toml)")
rootCmd.PersistentFlags().StringVar(&cfgGroup, "cluster", "", "Configuration group (default is none)")
rootCmd.Flags().StringVar(&conf.KeyPath, "keypath", "/etc/replication-manager/.replication-manager.key", "Encryption key file path")
rootCmd.PersistentFlags().BoolVar(&conf.Verbose, "verbose", false, "Print detailed execution info")
rootCmd.PersistentFlags().StringVar(&memprofile, "memprofile", "/tmp/repmgr.mprof", "Write a memory profile to a file readable by pprof")
viper.BindPFlags(rootCmd.PersistentFlags())
if conf.Verbose == true && conf.LogLevel == 0 {
conf.LogLevel = 1
}
if conf.Verbose == false && conf.LogLevel > 0 {
conf.Verbose = true
}
}
func initConfig() {
// call after init if configuration file is provide
viper.SetConfigType("toml")
if conf.ConfigFile != "" {
viper.SetConfigFile(conf.ConfigFile)
if _, err := os.Stat(conf.ConfigFile); os.IsNotExist(err) {
log.Fatal("No config file " + conf.ConfigFile)
}
} else {
viper.SetConfigName("config")
viper.AddConfigPath("/etc/replication-manager/")
viper.AddConfigPath(".")
if WithTarball == "ON" {
viper.AddConfigPath("/usr/local/replication-manager/etc")
if _, err := os.Stat("/usr/local/replication-manager/etc/config.toml"); os.IsNotExist(err) {
log.Fatal("No config file /usr/local/replication-manager/etc/config.toml ")
}
} else {
if _, err := os.Stat("/etc/replication-manager/config.toml"); os.IsNotExist(err) {
log.Fatal("No config file /etc/replication-manager/config.toml ")
}
}
}
viper.SetEnvPrefix("MRM")
err := viper.ReadInConfig()
if err == nil {
log.WithFields(log.Fields{
"file": viper.ConfigFileUsed(),
}).Debug("Using config file")
}
if _, ok := err.(viper.ConfigParseError); ok {
log.WithError(err).Fatal("Could not parse config file")
}
// Procedd include files
if viper.GetString("default.include") != "" {
if _, err := os.Stat(viper.GetString("default.include")); os.IsNotExist(err) {
log.Fatal("No include config directory " + conf.Include)
}
files, err := ioutil.ReadDir(viper.GetString("default.include"))
if err != nil {
log.Fatal(err)
}
for _, f := range files {
if !f.IsDir() {
viper.SetConfigName(f.Name())
viper.SetConfigFile(viper.GetString("default.include") + "/" + f.Name())
viper.MergeInConfig()
//fmt.Println(f.Name())
}
}
}
// Procedd include files
m := viper.AllKeys()
currentClusterName = cfgGroup
if currentClusterName == "" {
var clusterDiscovery = map[string]string{}
var discoveries []string
for _, k := range m {
if strings.Contains(k, ".") {
mycluster := strings.Split(k, ".")[0]
if mycluster != "default" {
_, ok := clusterDiscovery[mycluster]
if !ok {
clusterDiscovery[mycluster] = mycluster
discoveries = append(discoveries, mycluster)
// log.Println(strings.Split(k, ".")[0])
}
}
}
}
currentClusterName = strings.Join(discoveries, ",")
log.WithField("clusters", currentClusterName).Debug("New clusters discovered")
}
cfgGroupIndex = 0
cf1 := viper.Sub("Default")
if cf1 == nil {
log.Fatal("config.toml has no [Default] configuration group and config group has not been specified")
}
cf1.Unmarshal(&conf)
if currentClusterName != "" {
cfgGroupList = strings.Split(currentClusterName, ",")
for _, gl := range cfgGroupList {
if gl != "" {
clusterconf := conf
cf2 := viper.Sub("Default")
initAlias(cf2)
cf2.Unmarshal(&clusterconf)
currentClusterName = gl
log.WithField("group", gl).Debug("Reading configuration group")
cf2 = viper.Sub(gl)
initAlias(cf2)
if cf2 == nil {
log.WithField("group", gl).Fatal("Could not parse configuration group")
}
cf2.Unmarshal(&clusterconf)
confs[gl] = clusterconf
cfgGroupIndex++
}
}
cfgGroupIndex--
log.WithField("cluster", cfgGroupList[cfgGroupIndex]).Debug("Default Cluster set")
currentClusterName = cfgGroupList[cfgGroupIndex]
} else {
cfgGroupList = append(cfgGroupList, "Default")
log.WithField("cluster", cfgGroupList[cfgGroupIndex]).Debug("Default Cluster set")
confs["Default"] = conf
currentClusterName = "Default"
}
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
var rootCmd = &cobra.Command{
Use: "replication-manager",
Short: "Replication Manager tool for MariaDB and MySQL",
// Copyright 2017 Signal 18 SARL
Long: `replication-manager allows users to monitor interactively MariaDB 10.x and MySQL GTID replication health
and trigger slave to master promotion (aka switchover), or elect a new master in case of failure (aka failover).`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Usage()
},
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the replication manager version number",
Long: `All software has versions. This is ours`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Replication Manager " + Version + " for MariaDB 10.x and MySQL 5.7 Series")
fmt.Println("Full Version: ", FullVersion)
fmt.Println("Build Time: ", Build)
},
}
func initAlias(v *viper.Viper) {
v.RegisterAlias("replication-master-connection", "replication-source-name")
v.RegisterAlias("logfile", "log-file")
v.RegisterAlias("wait-kill", "switchover-wait-kill")
v.RegisterAlias("user", "db-servers-credential")
v.RegisterAlias("hosts", "db-servers-hosts")
v.RegisterAlias("hosts-tls-ca-cert", "db-servers-tls-ca-cert")
v.RegisterAlias("hosts-tls-client-key", "db-servers-tls-client-key")
v.RegisterAlias("hosts-tls-client-cert", "db-servers-tls-client-cert")
v.RegisterAlias("connect-timeout", "db-servers-connect-timeout")
v.RegisterAlias("rpluser", "replication-credential")
v.RegisterAlias("prefmaster", "db-servers-prefered-master")
v.RegisterAlias("ignore-servers", "db-servers-ignored-hosts")
v.RegisterAlias("master-connection", "replication-master-connection")
v.RegisterAlias("master-connect-retry", "replication-master-connection-retry")
v.RegisterAlias("api-user", "api-credential")
v.RegisterAlias("readonly", "failover-readonly-state")
v.RegisterAlias("maxscale-host", "maxscale-servers")
v.RegisterAlias("mdbshardproxy-hosts", "mdbshardproxy-servers")
v.RegisterAlias("multimaster", "replication-multi-master")
v.RegisterAlias("multi-tier-slave", "replication-multi-tier-slave")
v.RegisterAlias("pre-failover-script", "failover-pre-script")
v.RegisterAlias("post-failover-script", "failover-post-script")
v.RegisterAlias("rejoin-script", "autorejoin-script")
v.RegisterAlias("share-directory", "monitoring-sharedir")
v.RegisterAlias("working-directory", "monitoring-datadir")
v.RegisterAlias("interactive", "failover-mode")
v.RegisterAlias("failcount", "failover-falsepositive-ping-counter")
v.RegisterAlias("wait-write-query", "switchover-wait-write-query")
v.RegisterAlias("wait-trx", "switchover-wait-trx")
v.RegisterAlias("gtidcheck", "switchover-at-equal-gtid")
v.RegisterAlias("maxdelay", "failover-max-slave-delay")
v.RegisterAlias("maxscale-host", "maxscale-servers")
v.RegisterAlias("maxscale-pass", "maxscale-password")
}