forked from ngaut/codis-ha
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
executable file
·188 lines (170 loc) · 4.74 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
"time"
"github.com/juju/errors"
log "github.com/ngaut/logging"
)
var (
defaultConfigFile string = "./codis-ha.json"
defaultDashboardAddr string = "127.0.0.1:18087"
defaultProductName string = "db_test"
defaultLogFile string = "./codis-ha.log"
defaultLogLevel string = "info"
defaultCheckInterval int = 5
defaultMaxTryTimes int = 10
defaultEmailAddr string = ""
defaultEmailPwd string = ""
defaultSmtpAddr string = ""
defaultToAddr string = ""
defaultSendInterval int64 = 300
defaultMasterSave string = ""
defaultSlaveSave string = "600 1"
)
type CodisHAConf struct {
DashboadAddr string `json:"dashboard_addr"`
ProductName string `json:"product_name"`
LogFile string `json:"log_file"`
LogLevel string `json:"log_level"`
CheckInterval int `json:"check_interval"`
MaxTryTimes int `json:"max_try_times"`
EmailAddr string `json:"email_addr"`
EmailPwd string `json:"email_pwd"`
SmtpAddr string `json:"smtp_addr"`
ToAddr string `json:"to_addr"`
SendInterval int64 `json:"send_interval"`
MasterSave string `json:"master_save"`
SlaveSave string `json:"slave_save"`
}
var HAConf CodisHAConf = CodisHAConf{
DashboadAddr: defaultDashboardAddr,
ProductName: defaultProductName,
LogFile: defaultLogFile,
LogLevel: defaultLogLevel,
CheckInterval: defaultCheckInterval,
MaxTryTimes: defaultMaxTryTimes,
EmailAddr: defaultEmailAddr,
EmailPwd: defaultEmailPwd,
SmtpAddr: defaultSmtpAddr,
ToAddr: defaultToAddr,
SendInterval: defaultSendInterval,
MasterSave: defaultMasterSave,
SlaveSave: defaultSlaveSave,
}
func LoadConf(fileName string, v interface{}) error {
data, err := ioutil.ReadFile(fileName)
if err != nil {
return err
}
err = json.Unmarshal([]byte(data), v)
if err != nil {
return err
}
return nil
}
func PrintCodisHAConf(conf CodisHAConf) {
fmt.Printf("dashboard_addr:%s\n", conf.DashboadAddr)
fmt.Printf("product_name:%s\n", conf.ProductName)
fmt.Printf("LogFile:%s\n", conf.LogFile)
fmt.Printf("LogLevel:%s\n", conf.LogLevel)
fmt.Printf("CheckInterval:%d\n", conf.CheckInterval)
fmt.Printf("MaxTryTimes:%d\n", conf.MaxTryTimes)
fmt.Printf("EmailAddr:%s\n", conf.EmailAddr)
fmt.Printf("EmailPwd:%s\n", conf.EmailPwd)
fmt.Printf("SmtpAddr:%s\n", conf.SmtpAddr)
fmt.Printf("ToAddr:%s\n", conf.ToAddr)
fmt.Printf("SendInterval:%d\n", conf.SendInterval)
fmt.Printf("MasterSave:%d\n", conf.MasterSave)
fmt.Printf("SlaveSave:%d\n", conf.SlaveSave)
}
type fnHttpCall func(objPtr interface{}, api string, method string, arg interface{}) error
type aliveCheckerFactory func(addr, role string, defaultTimeout time.Duration) AliveChecker
var (
callHttp fnHttpCall = httpCall
acf aliveCheckerFactory = func(addr, role string, timeout time.Duration) AliveChecker {
return &redisChecker{
addr: addr,
role: role,
defaultTimeout: timeout,
}
}
)
func genUrl(args ...interface{}) string {
url := "http://"
for _, v := range args {
switch v.(type) {
case string:
url += v.(string)
case int:
url += strconv.Itoa(v.(int))
default:
log.Errorf("unsupported type %T", v)
}
}
return url
}
func Usage(progName string) {
fmt.Printf("Usage: %s [xxx.json]\n", progName)
os.Exit(0)
}
var JsonExample string = `
{
"dashboard_addr":"127.0.0.1:18087",
"product_name":"db_test",
"log_file":"./codis-ha.log",
"log_level":"info",
"check_interval":5,
"max_try_times":10,
"email_addr":"xxx@letv.com",
"email_pwd":"xxx",
"smtp_addr":"mail.letv.com:25",
"to_addr":"xxx@126.com;xxx@163.com",
"send_interval":300,
"master_save":"",
"slave_save":"600 1"
}
`
func ShowJsonExample(config string) {
fmt.Printf("%s should like this:\n%s\n", config, JsonExample)
os.Exit(0)
}
func main() {
var argNum int = len(os.Args)
var confile string
if argNum != 1 && argNum != 2 {
Usage(os.Args[0])
}
if argNum == 1 {
confile = defaultConfigFile
} else if argNum == 2 {
confile = os.Args[1]
}
err := LoadConf(confile, &HAConf)
if err != nil {
fmt.Printf("Load config [%s] failed,err:%s\n", confile, err.Error())
ShowJsonExample(confile)
}
if len(HAConf.LogFile) > 0 {
log.SetOutputByName(HAConf.LogFile)
}
if len(HAConf.LogLevel) > 0 {
log.SetLevelByString(HAConf.LogLevel)
}
//PrintCodisHAConf(HAConf)
log.Infof("program [%s] start...", os.Args[0])
for {
groups, err := GetServerGroups()
if err != nil {
log.Errorf("GetServerGroups failed,will sleep 30 seconds,err:%s", errors.ErrorStack(err))
time.Sleep(30 * time.Second)
continue
}
CheckAliveAndPromote(groups)
CheckOfflineAndPromoteSlave(groups)
time.Sleep(time.Duration(HAConf.CheckInterval) * time.Second)
}
}