Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure recipients based on label #82

Open
FOTempel opened this issue Dec 23, 2020 · 0 comments
Open

Configure recipients based on label #82

FOTempel opened this issue Dec 23, 2020 · 0 comments

Comments

@FOTempel
Copy link

FOTempel commented Dec 23, 2020

I prefer not to have to statically configure recipients for alerts but rather have the alert writer decide how and where to route his alerts to. To this end I configured generic routes in Alertmanager where its recipient(s) is a label attached to said alert. It would be nice if Sachet would also be able to parse these labels into a list of recipients. I dabbled a bit in Go so I've implemented it. These scratching are far from proper code, but it works. I'm not making it a PR because it may be a buggy mess and it's certainly quite ugly. But it works for me.

diff --git a/cmd/sachet/main.go b/cmd/sachet/main.go
index 67d7b2e..c407025 100644
--- a/cmd/sachet/main.go
+++ b/cmd/sachet/main.go
@@ -110,8 +110,16 @@ func main() {
                        }
                }

+                var receivers []string
+
+                if (len(receiverConf.To) == 1 && len(data.GroupLabels[receiverConf.To[0]])>1) {
+                  receivers = strings.Split(data.GroupLabels[receiverConf.To[0]],",")
+                } else {
+                  receivers = receiverConf.To
+                }
+
                message := sachet.Message{
-                       To:   receiverConf.To,
+                       To:   receivers,
                        From: receiverConf.From,
                        Type: receiverConf.Type,
                        Text: text,

How to use this? Configure your sachet receiver as usual, but instead of an actual recipient (number, account or whatever) put in (only!) the label which the receiver should analyze for receivers. E.g.:

providers:
  cm:
    producttoken: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
  telegram:
    token: 'xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

receivers:
  - name: 'sms_to'
    provider: 'cm'
    to:
      - 'sms_to'
    from: '...'
    text: 'This is your extrahandy SMS provider, receiving an alert from Alertmanager!'
  - name: 'telegram_to'
    provider: 'telegram'
    to:
      - 'telegram_to'
    text: 'This is your extrahandy Telegram chat bot, receiving an alert from Alertmanager!'

Boot up Sachet (after building, obviously) and send it a JSON like so (note the added GroupLabels with a comma separated string with recipients) will send in this case an SMS, but a Telegram works the same way. That's the only two providers I've tested because that's what I have access to and I don't need any more than this to be honest. Though MS Teams support would be nice.

$ curl -H "Content-type: application/json" -X POST -d '{"receiver": "sms_to", "status": "firing", "alerts": [{"status": "firing", "labels": {"alertname": "test-123"} }], "commonLabels": {"key": "value"},"GroupLabels":{"sms_to":"<somenumber>,<someothernumber>"}}' http://localhost:9876/alert

It would be great if something like this could be added to this project so I don't need to maintain my own fork and builds anymore. Also, input on how to better implement this is also appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant