/
monit_client.go
275 lines (230 loc) · 6.95 KB
/
monit_client.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
package monit_client
import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"code.cloudfoundry.org/lager"
"github.com/cloudfoundry-incubator/galera-healthcheck/config"
"github.com/cloudfoundry-incubator/galera-healthcheck/mysql_start_mode"
)
type MonitClient struct {
monitConfig config.MonitConfig
logger lager.Logger
}
func New(monitConfig config.MonitConfig, logger lager.Logger) *MonitClient {
return &MonitClient{
monitConfig: monitConfig,
logger: logger,
}
}
func (m *MonitClient) GetLogger(req *http.Request) lager.Logger {
return m.logger
}
func (m *MonitClient) StartServiceBootstrap(req *http.Request) (string, error) {
if m.monitConfig.ServiceName == "mariadb_ctrl" {
return m.startService("bootstrap", true)
} else {
return "", errors.New("bootstrapping arbitrator not allowed")
}
}
func (m *MonitClient) StartServiceJoin(req *http.Request) (string, error) {
urlParams := req.URL.Query()
sstDisabled := !(urlParams.Get("sst") == "true")
return m.startService("join", sstDisabled)
}
func (m *MonitClient) StartServiceSingleNode(req *http.Request) (string, error) {
return m.startService("singleNode", true)
}
func (m *MonitClient) startService(startMode string, sstDisabled bool) (string, error) {
if m.monitConfig.ServiceName == "mariadb_ctrl" {
mySqlStartMode := mysql_start_mode.NewMysqlStartMode(m.monitConfig.MysqlStateFilePath, startMode)
err := mySqlStartMode.Start()
if err != nil {
m.logger.Error("Failed to start mysql node", err)
return "", err
}
if m.monitConfig.BootstrapFilePath != "" {
prestartCmd := exec.Command(
"/bin/bash",
m.monitConfig.BootstrapFilePath,
)
env := os.Environ()
env = append(env, fmt.Sprintf("LOG_FILE=%s", m.monitConfig.BootstrapLogFilePath))
if sstDisabled {
env = append(env, "DISABLE_SST=1")
env = append(env, fmt.Sprintf("INTERRUPT_NOTIFY_CMD=%s", m.monitConfig.SstInterruptNotifyCmd))
}
prestartCmd.Env = env
stdoutDest, err := os.OpenFile(m.monitConfig.BootstrapLogFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
m.logger.Error(fmt.Sprintf("Failed to open pre-start-unprivileged log file: %s", m.monitConfig.BootstrapLogFilePath), err)
return "", err
}
defer stdoutDest.Close()
prestartCmd.Stdout = stdoutDest
prestartCmd.Stderr = stdoutDest
err = prestartCmd.Run()
if err != nil {
m.logger.Error("Failed to pre-start mysql node", err)
return "", err
}
}
// Used when mariadb_ctrl does not support a bootstrap file
if m.monitConfig.EnableSstMarkerFilePath != "" {
if sstDisabled {
os.Remove(m.monitConfig.EnableSstMarkerFilePath)
} else {
os.Create(m.monitConfig.EnableSstMarkerFilePath)
}
}
}
err := m.runServiceCmd("start")
msg := ""
if err == nil {
msg = fmt.Sprintf("Successfully sent start request in %s mode", startMode)
}
return msg, err
}
func (m *MonitClient) StopService(req *http.Request) (string, error) {
err := m.runServiceCmd("stop")
msg := ""
if err == nil {
msg = "Successfully sent stop request"
}
return msg, err
}
func (m *MonitClient) statusLookup(s MonitStatus) (string, error) {
var tagForService ServiceTag
foundService := false
for _, serviceTag := range s.Services {
if serviceTag.Name == m.monitConfig.ServiceName {
tagForService = serviceTag
foundService = true
break
}
}
if foundService == false {
return "", fmt.Errorf("Could not find process %s", m.monitConfig.ServiceName)
}
switch {
case tagForService.PendingAction != 0:
return "pending", nil
case tagForService.Monitor == 0:
return "stopped", nil
case tagForService.Monitor == 2:
return "starting", nil
case tagForService.Status == 0:
return "running", nil
default:
return "failing", nil
}
}
func (m *MonitClient) GetStatus(req *http.Request) (string, error) {
statusResponse, err := m.runStatusCmd()
if err != nil {
return "", err
}
monitStatus, err := ParseXML(statusResponse)
if err != nil {
return "", err
}
status, err := m.statusLookup(monitStatus)
if err != nil {
return "", err
}
return status, nil
}
func (m *MonitClient) newUrl(endpoint string, queryParams ...url.Values) (*url.URL, error) {
config := m.monitConfig
statusURL, err := url.Parse(fmt.Sprintf("http://%s:%d/%s", config.Host, config.Port, endpoint))
if err != nil {
m.logger.Error("Failed to parse URL", err)
m.logger.Info("URL info", lager.Data{
"URL": statusURL,
})
return nil, err
}
if len(queryParams) > 0 {
statusURL.RawQuery = queryParams[0].Encode()
}
return statusURL, nil
}
func (m *MonitClient) runStatusCmd() (io.Reader, error) {
statusURL, err := m.newUrl("_status", url.Values{
"format": []string{"xml"},
})
resp, err := m.sendRequest(statusURL, "GET")
if err != nil {
return nil, err
}
return resp, err
}
func (m *MonitClient) runServiceCmd(command string) error {
serviceAction := fmt.Sprintf("action=%s", command)
pendingStatusMsg := fmt.Sprintf("%s pending", command)
statusURL, err := m.newUrl(m.monitConfig.ServiceName)
respBody, err := m.sendRequest(statusURL, "POST", serviceAction)
if err != nil {
return err
}
responseBytes, _ := ioutil.ReadAll(respBody)
responseStr := string(responseBytes)
if !strings.Contains(responseStr, pendingStatusMsg) {
monitFailure := fmt.Errorf("Monit failed to %s %s successfully", command, m.monitConfig.ServiceName)
m.logger.Error("Monit failure:", monitFailure)
m.logger.Info("request info", lager.Data{
"response_body": string(responseBytes),
})
return monitFailure
}
return nil
}
func (m *MonitClient) sendRequest(statusURL *url.URL, reqMethod string, params ...string) (io.Reader, error) {
config := m.monitConfig
client := &http.Client{}
var err error
var req *http.Request
if len(params) > 0 {
req, err = http.NewRequest(reqMethod, statusURL.String(), strings.NewReader(params[0])) //bytes.NewBufferString(params[0]))
} else {
req, err = http.NewRequest(reqMethod, statusURL.String(), nil)
}
if err != nil {
m.logger.Error("Failed to create http request", err)
return nil, err
}
if reqMethod == "POST" || reqMethod == "PUT" {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
m.logger.Info("Forwarding request to monit API", lager.Data{
"url": req.URL,
})
req.SetBasicAuth(config.User, config.Password)
resp, err := client.Do(req)
if err != nil {
errMsg := fmt.Errorf("Error sending http request: %s", err.Error())
m.logger.Error(errMsg.Error(), err)
m.logger.Info("request info", lager.Data{
"request": req.URL,
})
return nil, errMsg
}
if resp.StatusCode != 200 {
responseBytes, _ := ioutil.ReadAll(resp.Body)
non200Error := fmt.Errorf("Received %d response from monit: %s", resp.StatusCode, responseBytes)
m.logger.Error("Failed with non-200 response", non200Error)
m.logger.Info("", lager.Data{
"status_code": resp.StatusCode,
"response_body": string(responseBytes),
})
return nil, non200Error
}
m.logger.Info("Made successful request to monit API")
return resp.Body, nil
}