add more notifyer #51

Closed
zottelchin wants to merge 2 commits from clockworks into master
  1. 52
      notifiers/sms.go
  2. 57
      notifiers/webhook.go

52
notifiers/sms.go

@ -0,0 +1,52 @@
package notifiers
import (
"codeberg.org/momar/chihuahua"
"fmt"
"github.com/rs/zerolog/log"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type ClockworksNotifier struct {
To string `hcl:"to"`
From string `hcl:"from"`
Key string `hcl:"key"`
}
func init() {
chihuahua.Notifiers["clockworksms"] = &ClockworksNotifier{}
}
func (c *ClockworksNotifier) Notify(cfg *chihuahua.Config, check chihuahua.Check, previous chihuahua.CheckResult) {
content := fmt.Sprintf("Woof!\n%s has status %s! Was previously %s.", strings.Join(check.FullName(), " » " ), check.Result.Status.String(), previous.Status.String())
requestUrl := "https://api.clockworksms.com/http/send.aspx?"
requestUrl += "key=" + url.QueryEscape(c.Key)
requestUrl += "&to=" + url.QueryEscape(c.To)
if c.From != "" { requestUrl += "&from=" + url.QueryEscape(c.From) }
requestUrl += "&content=" + url.QueryEscape(content)
res, err := http.Get(requestUrl)
if err != nil {
log.Error().Str("id", strings.Join(check.FullID(), "/")).Err(err).Msg("couldn't send notification through clockworks. Network error")
return
}
if res != nil {
data := []byte{}
if res.Body != nil {
data, _ = ioutil.ReadAll(res.Body)
}
error := strings.Contains(string(data), "Error")
if error {
errCode := strings.Split(strings.SplitAfter(string(data), "Error ")[1], ":")[0]
id := strings.Split(strings.SplitAfter(string(data), "ID: ")[1], " ")[0]
log.Error().Str("id", strings.Join(check.FullID(), "/")).Str("Message ID", id).Str("Error Code", errCode).Msg("Clockwork responded with an error. See error code documentation for more information.")
}else{
id := strings.Split(strings.SplitAfter(string(data), "ID: ")[1], " ")[0]
log.Trace().Str("id", strings.Join(check.FullID(), "/")).Str("Message ID", id).Msg("send notification through clockworks API")
}
return
}
log.Error().Str("id", strings.Join(check.FullID(), "/")).Msg("No network error and no response body! Undefined behaviour in clockworks api")
}

57
notifiers/webhook.go

@ -0,0 +1,57 @@
package notifiers
import (
"bytes"
"codeberg.org/momar/chihuahua"
"encoding/json"
"github.com/rs/zerolog/log"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
type WebhookNotifier struct {
URL string `hcl:"url"`
}
func init() {
chihuahua.Notifiers["webhook"] = &WebhookNotifier{}
}
func (c *WebhookNotifier) Notify(cfg *chihuahua.Config, check chihuahua.Check, previous chihuahua.CheckResult) {
details := ""
if check.Result.Error == "" { details = check.Result.Details} else { details = check.Result.Error}
jsonContent := map[string]interface{}{
"raw": map[string]interface{}{
"now": check.Result,
"previous_result": previous,
"check_result": check,
},
"name": check.Name,
"status": check.Result.Status.String(),
"details": details,
"previous_status": previous.Status.String(),
"last_update": check.Result.LastUpdate,
}
content, _ := json.Marshal(jsonContent)
res, err := http.Post(c.URL, "application/json", bytes.NewBuffer(content))
if err != nil {
log.Error().Str("id", strings.Join(check.FullID(), "/")).Err(err).Msg("couldn't send notification through clockworks. Network error")
return
}
if res != nil {
if strconv.Itoa(res.StatusCode)[:1] == "2" {
log.Trace().Str("id", strings.Join(check.FullID(), "/")).Int("status", res.StatusCode).Msg("Webhook Executed")
return
}
data := []byte{}
if res.Body != nil {
data, _ = ioutil.ReadAll(res.Body)
}
log.Error().Str("id", strings.Join(check.FullID(), "/")).Int("status", res.StatusCode).Bytes("body", data).Msg("Recived Error Response from Webhook")
return
}
log.Error().Str("id", strings.Join(check.FullID(), "/")).Msg("No network error and no response body! Undefined behaviour in clockworks api")
}
Loading…
Cancel
Save