Skip to content
This repository was archived by the owner on Apr 7, 2024. It is now read-only.

Commit 230e1ea

Browse files
authored
Iti 0/feature/sigmasms (#13)
* Sigma notyfiers * Sigma notyfiers * Sigma notyfiers * Sigma notyfiers * Delete .gitignore * Delete alertmanager.iml * Delete modules.xml * Delete vcs.xml * fix pull * fix run * fix string api_key * fix string api_key * fix string api_key * check push * Добавил голосовые сообщения * mini fix * Убрал exclude для получателей
1 parent eed33b2 commit 230e1ea

2 files changed

Lines changed: 54 additions & 18 deletions

File tree

config/notifiers.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,13 @@ func (c *SigmaConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
477477
if c.APIKey == "" {
478478
return fmt.Errorf("api_key must be configured")
479479
}
480-
if c.NotificationType == "" {
480+
switch strings.ToLower(c.NotificationType) {
481+
case "sms", "":
481482
c.NotificationType = "sms"
483+
case "voice":
484+
c.NotificationType = "voice"
485+
default:
486+
return fmt.Errorf("unknown notification type: %s", c.NotificationType)
482487
}
483488
if c.TTS == "" {
484489
c.TTS = "yandex:alena"

notify/sigma/sigma.go

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ import (
2020
"fmt"
2121
"github.com/go-kit/log"
2222
"github.com/pkg/errors"
23+
"github.com/prometheus/alertmanager/config"
2324
"github.com/prometheus/alertmanager/notify"
25+
"github.com/prometheus/alertmanager/template"
2426
"github.com/prometheus/alertmanager/types"
2527
commoncfg "github.com/prometheus/common/config"
2628
"io/ioutil"
2729
"net/http"
28-
29-
"github.com/prometheus/alertmanager/config"
30-
"github.com/prometheus/alertmanager/template"
3130
)
3231

3332
// Notifier implements a Notifier for generic sigma.
@@ -52,8 +51,19 @@ func New(conf *config.SigmaConfig, t *template.Template, l log.Logger, httpOpts
5251
}, nil
5352
}
5453

55-
// Request Message defines the JSON object send to Sigma endpoints.
56-
type Request struct {
54+
// RequestSms Message defines the JSON object send to Sigma endpoints.
55+
type RecipientVoice struct {
56+
Include []string `json:"include"`
57+
Exclude []string `json:"exclude"`
58+
}
59+
60+
type RequestVoice struct {
61+
Recipient RecipientVoice `json:"recipient"`
62+
Type string `json:"type"`
63+
Payload RequestPayload `json:"payload"`
64+
}
65+
66+
type RequestSms struct {
5767
Recipient []string `json:"recipient"`
5868
Type string `json:"type"`
5969
Payload RequestPayload `json:"payload"`
@@ -79,24 +89,44 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
7989
tmplText = notify.TmplText(n.tmpl, data, &err)
8090
)
8191

82-
msg := Request{
83-
Recipient: n.conf.Recipient,
84-
Type: n.conf.NotificationType,
85-
Payload: RequestPayload{
86-
Sender: n.conf.SenderName,
87-
Text: tmplText(n.conf.Text),
88-
},
89-
}
90-
body, err := json.Marshal(msg)
91-
if err != nil {
92-
return false, err
92+
var body []byte
93+
switch n.conf.NotificationType {
94+
case "sms":
95+
msg := RequestSms{
96+
Recipient: n.conf.Recipient,
97+
Type: n.conf.NotificationType,
98+
Payload: RequestPayload{
99+
Sender: n.conf.SenderName,
100+
Text: tmplText(n.conf.Text),
101+
},
102+
}
103+
body, err = json.Marshal(msg)
104+
if err != nil {
105+
return false, err
106+
}
107+
case "voice":
108+
msg := RequestVoice{
109+
Recipient: RecipientVoice{
110+
Include: n.conf.Recipient,
111+
},
112+
Type: n.conf.NotificationType,
113+
Payload: RequestPayload{
114+
Sender: n.conf.SenderName,
115+
Text: tmplText(n.conf.Text),
116+
},
117+
}
118+
body, err = json.Marshal(msg)
119+
if err != nil {
120+
return false, err
121+
}
93122
}
94-
bodyReader := bytes.NewReader(body)
95123

124+
bodyReader := bytes.NewReader(body)
96125
req, err := http.NewRequest("POST", n.conf.URL.String(), bodyReader)
97126
if err != nil {
98127
return false, errors.Wrap(err, "request error")
99128
}
129+
100130
req.Header.Set("Authorization", string(n.conf.APIKey))
101131
req.Header.Set("Content-Type", "application/json")
102132
req.WithContext(ctx)
@@ -122,6 +152,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
122152
}
123153

124154
return false, nil
155+
125156
}
126157

127158
type Error struct {

0 commit comments

Comments
 (0)