The smallest watchdog on earth. Tiny, monitoring-plugins compatible monitoring with a status page. https://cloud.docker.com/repository/docker/momar/chihuahua/general
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

notify.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package chihuahua
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io/ioutil"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "strings"
  10. "codeberg.org/momar/chihuahua/types"
  11. "codeberg.org/momar/logg"
  12. "codeberg.org/momar/ternary"
  13. "gopkg.in/gomail.v2"
  14. )
  15. var cache map[string]types.CheckStatus
  16. func Notify(settings []map[string]string, servers map[string]*types.Server) {
  17. if cache == nil {
  18. cache = map[string]types.CheckStatus{}
  19. data, _ := ioutil.ReadFile("cache.json")
  20. json.Unmarshal(data, &cache)
  21. }
  22. logg.Tag("notify").Debug("Processing notifications...")
  23. for serverName, server := range servers {
  24. for checkName, check := range server.Checks {
  25. old, ok := cache[serverName+"/"+checkName]
  26. if (!ok && check.Status != types.StatusOk) || check.Status != old {
  27. logg.Tag("notify").Debug("Sending notification for %s", serverName+"/"+checkName)
  28. send(settings, serverName+"/"+checkName, check)
  29. } else {
  30. logg.Tag("notify").Debug("Skipping notification for %s (%s -> %d)", serverName+"/"+checkName, ternary.If(ok, strconv.Itoa(int(old)), "?"), check.Status)
  31. }
  32. cache[serverName+"/"+checkName] = check.Status
  33. }
  34. }
  35. data, _ := json.Marshal(cache)
  36. err := ioutil.WriteFile("cache.json", data, 0644)
  37. if err != nil {
  38. logg.Tag("notify", "cache").Warn("Couldn't save cache: %s", err)
  39. }
  40. }
  41. func send(settings []map[string]string, checkName string, check *types.Check) {
  42. for i, channel := range settings {
  43. logg.Tag("notify", strconv.Itoa(i)).Debug("Processing channel: %+v", channel)
  44. switch channel["provider"] {
  45. case "gotify":
  46. body, err := json.Marshal(map[string]interface{}{
  47. "title": "[" + check.Status.String() + "] " + checkName + " (previously " + cache[checkName].String() + ")",
  48. "message": check.Details + ternary.If(check.Error != "", "\n**"+check.Error+"**", "").(string),
  49. "priority": 8,
  50. })
  51. if err != nil {
  52. logg.Tag("notify", strconv.Itoa(i), "gotify").Error("Couldn't send notification through Gotify: %s", err)
  53. continue
  54. }
  55. res, err := http.Post(strings.TrimSuffix(channel["server"], "/")+"/message?token="+url.QueryEscape(channel["token"]), "application/json", bytes.NewReader(body))
  56. if err != nil || res.StatusCode != 200 {
  57. data, _ := ioutil.ReadAll(res.Body)
  58. logg.Tag("notify", strconv.Itoa(i), "gotify").Error("Couldn't send notification through Gotify: %s (%s %s)", err, res.Status, data)
  59. continue
  60. }
  61. logg.Tag("notify", strconv.Itoa(i), "gotify").Debug("Notification sent.")
  62. case "email":
  63. port, _ := strconv.Atoi(channel["port"])
  64. d := gomail.NewDialer(
  65. ternary.Default(channel["host"], "127.0.0.1").(string),
  66. ternary.Default(port, 25).(int),
  67. channel["username"],
  68. channel["password"])
  69. s, err := d.Dial()
  70. if err != nil {
  71. logg.Tag("notify", strconv.Itoa(i), "email").Error("Couldn't send notification through E-Mail: %s", err)
  72. continue
  73. }
  74. m := gomail.NewMessage(gomail.SetCharset("UTF-8"))
  75. m.SetHeader("From", channel["from"])
  76. m.SetHeader("To", channel["to"])
  77. m.SetHeader("Subject", "["+check.Status.String()+"] "+checkName+" (previously "+cache[checkName].String()+")")
  78. m.SetBody("text/plain", check.Details+ternary.If(check.Error != "", "\n\n"+check.Error, "").(string))
  79. if err := gomail.Send(s, m); err != nil {
  80. logg.Tag("notify", strconv.Itoa(i), "email").Error("Couldn't send notification through E-Mail: %s", err)
  81. continue
  82. }
  83. logg.Tag("notify", strconv.Itoa(i), "email").Debug("Notification sent.")
  84. default:
  85. logg.Tag("notify", strconv.Itoa(i)).Error("No such provider: %s", channel["provider"])
  86. }
  87. }
  88. }