config: Allow to separate the ntfy topic from the server
This commit is contained in:
parent
9b9d71d648
commit
84a45a909b
4 changed files with 43 additions and 8 deletions
10
config.scfg
10
config.scfg
|
@ -51,7 +51,7 @@ labels {
|
||||||
}
|
}
|
||||||
|
|
||||||
instance "example.com" {
|
instance "example.com" {
|
||||||
topic https://ntfy.sh/homeserver
|
topic homeserver
|
||||||
tags "computer,example"
|
tags "computer,example"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,10 +64,14 @@ resolved {
|
||||||
}
|
}
|
||||||
|
|
||||||
ntfy {
|
ntfy {
|
||||||
# URL of the ntfy topic.
|
# URL of the ntfy server.
|
||||||
|
# Default: "https://ntfy.sh"
|
||||||
|
server https://ntfy.sh
|
||||||
|
# Name of the ntfy topic. For backwards compatibility you can specify the full URL of the
|
||||||
|
# topic (e.g. https://ntfy.sh/alertmanager-alerts) and the server will be parsed from it.
|
||||||
# This setting is required.
|
# This setting is required.
|
||||||
# Default: ""
|
# Default: ""
|
||||||
topic https://ntfy.sh/alertmanager-alerts
|
topic alertmanager-alerts
|
||||||
# ntfy authentication via Basic Auth (https://docs.ntfy.sh/publish/#username-password)
|
# ntfy authentication via Basic Auth (https://docs.ntfy.sh/publish/#username-password)
|
||||||
# Default: ""
|
# Default: ""
|
||||||
user user
|
user user
|
||||||
|
|
|
@ -37,6 +37,7 @@ type Config struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ntfyConfig struct {
|
type ntfyConfig struct {
|
||||||
|
Server string
|
||||||
Topic string
|
Topic string
|
||||||
User string
|
User string
|
||||||
Password string
|
Password string
|
||||||
|
@ -230,6 +231,13 @@ func parseBlock(block scfg.Block, config *Config) error {
|
||||||
|
|
||||||
ntfyDir := block.Get("ntfy")
|
ntfyDir := block.Get("ntfy")
|
||||||
if ntfyDir != nil {
|
if ntfyDir != nil {
|
||||||
|
d = ntfyDir.Children.Get("server")
|
||||||
|
if d != nil {
|
||||||
|
if err := d.ParseParams(&config.Ntfy.Server); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
d = ntfyDir.Children.Get("topic")
|
d = ntfyDir.Children.Get("topic")
|
||||||
if d != nil {
|
if d != nil {
|
||||||
if err := d.ParseParams(&config.Ntfy.Topic); err != nil {
|
if err := d.ParseParams(&config.Ntfy.Topic); err != nil {
|
||||||
|
@ -422,6 +430,8 @@ func Read(path string) (*Config, error) {
|
||||||
config.LogFormat = "text"
|
config.LogFormat = "text"
|
||||||
config.AlertMode = Multi
|
config.AlertMode = Multi
|
||||||
|
|
||||||
|
config.Ntfy.Server = "https://ntfy.sh"
|
||||||
|
|
||||||
config.Cache.Type = "disabled"
|
config.Cache.Type = "disabled"
|
||||||
config.Cache.Duration = time.Hour * 24
|
config.Cache.Duration = time.Hour * 24
|
||||||
// memory
|
// memory
|
||||||
|
|
|
@ -36,7 +36,7 @@ labels {
|
||||||
|
|
||||||
instance "example.com" {
|
instance "example.com" {
|
||||||
tags "computer,example"
|
tags "computer,example"
|
||||||
topic https://ntfy.sh/homeserver
|
topic homeserver
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,6 +78,7 @@ cache {
|
||||||
User: "webhookUser",
|
User: "webhookUser",
|
||||||
Password: "webhookPass",
|
Password: "webhookPass",
|
||||||
Ntfy: ntfyConfig{
|
Ntfy: ntfyConfig{
|
||||||
|
Server: "https://ntfy.sh",
|
||||||
Topic: "https://ntfy.sh/alertmanager-alerts",
|
Topic: "https://ntfy.sh/alertmanager-alerts",
|
||||||
User: "user",
|
User: "user",
|
||||||
Password: "pass",
|
Password: "pass",
|
||||||
|
@ -96,7 +97,7 @@ cache {
|
||||||
"severity:info": {Priority: "1"},
|
"severity:info": {Priority: "1"},
|
||||||
"instance:example.com": {
|
"instance:example.com": {
|
||||||
Tags: []string{"computer", "example"},
|
Tags: []string{"computer", "example"},
|
||||||
Topic: "https://ntfy.sh/homeserver"},
|
Topic: "homeserver"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Cache: CacheConfig{
|
Cache: CacheConfig{
|
||||||
|
|
26
main.go
26
main.go
|
@ -17,6 +17,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"slices"
|
"slices"
|
||||||
|
@ -304,9 +305,9 @@ func (br *bridge) multiAlertNotification(p *payload) *notification {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (br *bridge) publish(n *notification) error {
|
func (br *bridge) publish(n *notification) error {
|
||||||
url := br.cfg.Ntfy.Topic
|
url, err := br.topicURL(n.topic)
|
||||||
if n.topic != "" {
|
if err != nil {
|
||||||
url = n.topic
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(n.body))
|
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(n.body))
|
||||||
|
@ -616,3 +617,22 @@ func main() {
|
||||||
slog.String("error", err.Error()))
|
slog.String("error", err.Error()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (br *bridge) topicURL(topic string) (string, error) {
|
||||||
|
if topic == "" {
|
||||||
|
topic = br.cfg.Ntfy.Topic
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the configured topic name already contains the ntfy server
|
||||||
|
i := strings.Index(topic, "://")
|
||||||
|
if i != -1 {
|
||||||
|
return topic, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
s, err := url.JoinPath(br.cfg.Ntfy.Server, topic)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue