|
|
@ -138,7 +138,10 @@ func (d *Dog) watch() { |
|
|
|
err := d.check() |
|
|
|
if nil != err { |
|
|
|
failure = true |
|
|
|
} else { |
|
|
|
failure = false |
|
|
|
} |
|
|
|
|
|
|
|
// We should notify if
|
|
|
|
// * We've had success since the last notification
|
|
|
|
// * It's been at least 5 minutes since the last notification
|
|
|
@ -146,7 +149,7 @@ func (d *Dog) watch() { |
|
|
|
if d.lastPassed.After(d.lastNotified) && d.lastNotified.Before(fiveMinutesAgo) { |
|
|
|
d.notify(failure) |
|
|
|
} |
|
|
|
if d.failures >= 5 { |
|
|
|
if !failure || d.failures >= 5 { |
|
|
|
// go back to the main 5-minute loop
|
|
|
|
break |
|
|
|
} |
|
|
@ -213,7 +216,7 @@ func (d *Dog) recover() { |
|
|
|
err = cmd.Wait() |
|
|
|
cancel() |
|
|
|
if nil != err { |
|
|
|
d.logger <- fmt.Sprintf("[Recover] '%s' failed: %s", d.Recover, err) |
|
|
|
d.logger <- fmt.Sprintf("[Recover] '%s' failed for '%s': %s", d.Recover, d.Name, err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -241,26 +244,40 @@ func (d *Dog) notify(hardFail bool) { |
|
|
|
} |
|
|
|
|
|
|
|
var body *strings.Reader |
|
|
|
var err error |
|
|
|
// TODO real templates
|
|
|
|
if 0 != len(h.Form) { |
|
|
|
form := url.Values{} |
|
|
|
for k := range h.Form { |
|
|
|
v := h.Form[k] |
|
|
|
// TODO real templates
|
|
|
|
// because `{{` gets urlencoded
|
|
|
|
//k = strings.Replace(k, "{{ .Name }}", d.Name, -1)
|
|
|
|
v = strings.Replace(v, "{{ .Name }}", d.Name, -1) |
|
|
|
d.logger <- fmt.Sprintf("[HEADER] %s: %s", k, v) |
|
|
|
form.Set(k, v) |
|
|
|
} |
|
|
|
body = strings.NewReader(form.Encode()) |
|
|
|
} else if 0 != len(h.JSON) { |
|
|
|
bodyBuf, err := json.Marshal(h.JSON) |
|
|
|
if nil != err { |
|
|
|
d.logger <- fmt.Sprintf("[Notify] JSON Marshal Error for '%s': %s", h.Name, err) |
|
|
|
continue |
|
|
|
} |
|
|
|
// `{{` should be left alone
|
|
|
|
body = strings.NewReader(strings.Replace(string(bodyBuf), "{{ .Name }}", d.Name, -1)) |
|
|
|
} |
|
|
|
|
|
|
|
client := NewHTTPClient() |
|
|
|
req, err := http.NewRequest(h.Method, h.URL, body) |
|
|
|
if nil != err { |
|
|
|
log.Println("[Notify] HTTP Client Network Error:", err) |
|
|
|
d.logger <- fmt.Sprintf("[Notify] HTTP Client Network Error for '%s': %s", h.Name, err) |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
|
if 0 != len(h.Form) { |
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
|
|
|
} else if 0 != len(h.JSON) { |
|
|
|
req.Header.Set("Content-Type", "application/json") |
|
|
|
} |
|
|
|
|
|
|
|
if 0 != len(h.Auth) { |
|
|
@ -282,12 +299,12 @@ func (d *Dog) notify(hardFail bool) { |
|
|
|
|
|
|
|
resp, err := client.Do(req) |
|
|
|
if nil != err { |
|
|
|
d.logger <- fmt.Sprintf("[Notify] HTTP Client Error: %s", err) |
|
|
|
d.logger <- fmt.Sprintf("[Notify] HTTP Client Error for '%s': %s", h.Name, err) |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
|
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) { |
|
|
|
d.logger <- fmt.Sprintf("[Notify] Response Error: %s", resp.Status) |
|
|
|
d.logger <- fmt.Sprintf("[Notify] Response Error for '%s': %s", h.Name, resp.Status) |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
@ -297,7 +314,7 @@ func (d *Dog) notify(hardFail bool) { |
|
|
|
decoder := json.NewDecoder(resp.Body) |
|
|
|
err = decoder.Decode(&data) |
|
|
|
if err != nil { |
|
|
|
d.logger <- fmt.Sprintf("[Notify] Response Body Error: %s", resp.Status) |
|
|
|
d.logger <- fmt.Sprintf("[Notify] Response Body Error for '%s': %s", h.Name, resp.Status) |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
@ -326,6 +343,7 @@ type ConfigWebhook struct { |
|
|
|
Auth map[string]string `json:"auth"` |
|
|
|
Headers map[string]string `json:"headers"` |
|
|
|
Form map[string]string `json:"form"` |
|
|
|
JSON map[string]string `json:"json"` |
|
|
|
Config map[string]string `json:"config"` |
|
|
|
Configs []map[string]string `json:"configs"` |
|
|
|
} |
|
|
|