2022-11-02 13:16:19 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
Validate ntfy.sh URL before use
I had a couple of panics due to invalid URL, which could have been an
error message during startup.
Example:
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: 2024/04/01 17:33:49 Received valid hook from 127.0.0.1:43130
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: 2024/04/01 17:33:49 Processing alert: {firing map[alertname:Target Down instance:devkit.local:80 job:esphome severity:high] map[description:The target is not reachable summary:Target is down] 2024->
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: 2024/04/01 17:33:49 http: panic serving 127.0.0.1:43130: runtime error: invalid memory address or nil pointer dereference
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: goroutine 1013 [running]:
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: net/http.(*conn).serve.func1()
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/net/http/server.go:1868 +0xb9
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: panic({0x840200?, 0xd01360?})
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/runtime/panic.go:920 +0x270
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: main.WebhookHandler({0xa1d5f0, 0xc00038e000}, 0xc0001b0000)
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /home/j/src/alertmanager-ntfy/main.go:45 +0xbf2
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: net/http.HandlerFunc.ServeHTTP(0x10?, {0xa1d5f0?, 0xc00038e000?}, 0xc0004020ac?)
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/net/http/server.go:2136 +0x29
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: net/http.(*ServeMux).ServeHTTP(0x411f45?, {0xa1d5f0, 0xc00038e000}, 0xc0001b0000)
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/net/http/server.go:2514 +0x142
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: net/http.serverHandler.ServeHTTP({0xa1c0c0?}, {0xa1d5f0?, 0xc00038e000?}, 0x6?)
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/net/http/server.go:2938 +0x8e
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: net/http.(*conn).serve(0xc0000d21b0, {0xa1e1f0, 0xc00027a600})
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/net/http/server.go:2009 +0x5f4
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: created by net/http.(*Server).Serve in goroutine 1
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/net/http/server.go:3086 +0x5cb
2024-04-01 18:06:40 +00:00
|
|
|
"net/url"
|
2022-11-02 13:16:19 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2023-02-10 19:30:03 +00:00
|
|
|
"github.com/prometheus/alertmanager/template"
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
|
2022-11-02 13:16:19 +00:00
|
|
|
"golang.org/x/exp/maps"
|
|
|
|
)
|
|
|
|
|
|
|
|
func WebhookHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := template.Data{}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
|
|
|
log.Println("Parsing alertmanager JSON failed")
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Received valid hook from %v\n", r.RemoteAddr)
|
|
|
|
|
|
|
|
for _, alert := range payload.Alerts {
|
|
|
|
|
2022-11-04 06:45:37 +00:00
|
|
|
// Skip resolved messages
|
|
|
|
if alert.Status == string(model.AlertResolved) {
|
2022-11-04 07:32:26 +00:00
|
|
|
log.Printf("Skipping notification for alert: %v\n", alert)
|
2022-11-04 06:45:37 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-11-04 07:32:26 +00:00
|
|
|
log.Printf("Processing alert: %v\n", alert)
|
2022-11-02 13:16:19 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", os.Getenv("NTFY_TOPIC"), strings.NewReader(alert.Annotations["description"]))
|
|
|
|
if err != nil {
|
Validate ntfy.sh URL before use
I had a couple of panics due to invalid URL, which could have been an
error message during startup.
Example:
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: 2024/04/01 17:33:49 Received valid hook from 127.0.0.1:43130
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: 2024/04/01 17:33:49 Processing alert: {firing map[alertname:Target Down instance:devkit.local:80 job:esphome severity:high] map[description:The target is not reachable summary:Target is down] 2024->
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: 2024/04/01 17:33:49 http: panic serving 127.0.0.1:43130: runtime error: invalid memory address or nil pointer dereference
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: goroutine 1013 [running]:
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: net/http.(*conn).serve.func1()
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/net/http/server.go:1868 +0xb9
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: panic({0x840200?, 0xd01360?})
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/runtime/panic.go:920 +0x270
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: main.WebhookHandler({0xa1d5f0, 0xc00038e000}, 0xc0001b0000)
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /home/j/src/alertmanager-ntfy/main.go:45 +0xbf2
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: net/http.HandlerFunc.ServeHTTP(0x10?, {0xa1d5f0?, 0xc00038e000?}, 0xc0004020ac?)
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/net/http/server.go:2136 +0x29
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: net/http.(*ServeMux).ServeHTTP(0x411f45?, {0xa1d5f0, 0xc00038e000}, 0xc0001b0000)
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/net/http/server.go:2514 +0x142
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: net/http.serverHandler.ServeHTTP({0xa1c0c0?}, {0xa1d5f0?, 0xc00038e000?}, 0x6?)
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/net/http/server.go:2938 +0x8e
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: net/http.(*conn).serve(0xc0000d21b0, {0xa1e1f0, 0xc00027a600})
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/net/http/server.go:2009 +0x5f4
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: created by net/http.(*Server).Serve in goroutine 1
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/net/http/server.go:3086 +0x5cb
2024-04-01 18:06:40 +00:00
|
|
|
log.Printf("Building request failed: %s", err)
|
2022-11-02 13:16:19 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-03 10:14:36 +00:00
|
|
|
// Title
|
2022-11-02 13:16:19 +00:00
|
|
|
req.Header.Set("Title", fmt.Sprintf("[%s] %s", alert.Labels["instance"], alert.Labels["alertname"]))
|
2022-11-03 10:14:36 +00:00
|
|
|
|
|
|
|
// Priority (if set)
|
|
|
|
if priority := os.Getenv("NTFY_PRIORITY"); len(strings.TrimSpace(os.Getenv(priority))) != 0 {
|
|
|
|
req.Header.Set("Priority", priority)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tags
|
2022-11-02 13:16:19 +00:00
|
|
|
req.Header.Set("Tags", strings.Join(maps.Values(alert.Labels), ","))
|
|
|
|
|
2023-02-10 19:30:03 +00:00
|
|
|
username, password := os.Getenv("NTFY_USER"), os.Getenv("NTFY_PASS")
|
2024-03-24 02:15:31 +00:00
|
|
|
// allow empty `username` (access tokens in Basic Auth leave username empty)
|
|
|
|
// <https://docs.ntfy.sh/publish/#access-tokens>
|
|
|
|
if password != "" {
|
2023-02-10 19:30:03 +00:00
|
|
|
req.SetBasicAuth(username, password)
|
|
|
|
}
|
2022-11-02 13:16:19 +00:00
|
|
|
|
2022-11-04 07:32:26 +00:00
|
|
|
log.Printf("Sending request: %v\n", req)
|
|
|
|
|
2023-02-10 19:30:03 +00:00
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
2022-11-02 13:16:19 +00:00
|
|
|
log.Printf("Sending to %s failed: %s\n", req.RemoteAddr, err)
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-10 19:30:03 +00:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
log.Printf("Failed to send notification: %v\n", resp)
|
|
|
|
}
|
2022-11-02 13:16:19 +00:00
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
for _, v := range []string{"HTTP_ADDRESS", "HTTP_PORT", "NTFY_TOPIC"} {
|
|
|
|
if len(strings.TrimSpace(os.Getenv(v))) == 0 {
|
|
|
|
panic("Environment variable " + v + " not set!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Validate ntfy.sh URL before use
I had a couple of panics due to invalid URL, which could have been an
error message during startup.
Example:
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: 2024/04/01 17:33:49 Received valid hook from 127.0.0.1:43130
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: 2024/04/01 17:33:49 Processing alert: {firing map[alertname:Target Down instance:devkit.local:80 job:esphome severity:high] map[description:The target is not reachable summary:Target is down] 2024->
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: 2024/04/01 17:33:49 http: panic serving 127.0.0.1:43130: runtime error: invalid memory address or nil pointer dereference
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: goroutine 1013 [running]:
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: net/http.(*conn).serve.func1()
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/net/http/server.go:1868 +0xb9
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: panic({0x840200?, 0xd01360?})
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/runtime/panic.go:920 +0x270
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: main.WebhookHandler({0xa1d5f0, 0xc00038e000}, 0xc0001b0000)
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /home/j/src/alertmanager-ntfy/main.go:45 +0xbf2
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: net/http.HandlerFunc.ServeHTTP(0x10?, {0xa1d5f0?, 0xc00038e000?}, 0xc0004020ac?)
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/net/http/server.go:2136 +0x29
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: net/http.(*ServeMux).ServeHTTP(0x411f45?, {0xa1d5f0, 0xc00038e000}, 0xc0001b0000)
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/net/http/server.go:2514 +0x142
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: net/http.serverHandler.ServeHTTP({0xa1c0c0?}, {0xa1d5f0?, 0xc00038e000?}, 0x6?)
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/net/http/server.go:2938 +0x8e
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: net/http.(*conn).serve(0xc0000d21b0, {0xa1e1f0, 0xc00027a600})
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/net/http/server.go:2009 +0x5f4
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: created by net/http.(*Server).Serve in goroutine 1
Apr 01 17:33:49 nyx.jabid.in alertmanager-ntfy[878013]: /usr/lib/go/src/net/http/server.go:3086 +0x5cb
2024-04-01 18:06:40 +00:00
|
|
|
_, err := url.Parse(os.Getenv("NTFY_TOPIC"))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Environment variable NTFY_TOPIC is not a valid URL")
|
|
|
|
}
|
|
|
|
|
2022-11-02 13:16:19 +00:00
|
|
|
http.HandleFunc("/", WebhookHandler)
|
|
|
|
var listenAddr = fmt.Sprintf("%v:%v", os.Getenv("HTTP_ADDRESS"), os.Getenv("HTTP_PORT"))
|
|
|
|
log.Printf("Listening for HTTP requests (webhooks) on %v\n", listenAddr)
|
|
|
|
log.Fatal(http.ListenAndServe(listenAddr, nil))
|
|
|
|
}
|