Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ type Receiver struct {
OpsGenieConfigs []*OpsGenieConfig `yaml:"opsgenie_configs,omitempty" json:"opsgenie_configs,omitempty"`
PushoverConfigs []*PushoverConfig `yaml:"pushover_configs,omitempty" json:"pushover_configs,omitempty"`
VictorOpsConfigs []*VictorOpsConfig `yaml:"victorops_configs,omitempty" json:"victorops_configs,omitempty"`
MatrixConfigs []*MatrixConfig `yaml:"matrix_configs,omitempty" json:"matrix_configs,omitempty"`

// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline" json:"-"`
Expand Down
39 changes: 39 additions & 0 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ var (
Retry: duration(1 * time.Minute),
Expire: duration(1 * time.Hour),
}

DefaultMatrixConfig = MatrixConfig{
NotifierConfig: NotifierConfig{
VSendResolved: false,
},
Text: `{{ template "matrix.default.text" . }}`,
HTML: `{{ template "matrix.default.html" . }}`,
}
)

// NotifierConfig contains base options common across all notifier configurations.
Expand Down Expand Up @@ -231,6 +239,37 @@ func (c *SlackConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
return checkOverflow(c.XXX, "slack config")
}

type MatrixConfig struct {
NotifierConfig `yaml:",inline" json:",inline"`

AccessToken string `yaml:"access_token" json:"access_token"`
RoomID string `yaml:"room_id" json:"room_id"`

Text string `yaml:"text" json:"text"`
HTML string `yaml:"html" json:"html"`

// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline" json:"-"`
}

// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (c *MatrixConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = DefaultMatrixConfig
type plain MatrixConfig
if err := unmarshal((*plain)(c)); err != nil {
return err
}
if c.RoomID == "" {
return fmt.Errorf("missing room id in Matrix config")
}

if c.AccessToken == "" {
return fmt.Errorf("missing access token in Matrix config")
}

return checkOverflow(c.XXX, "matrix config")
}

// HipchatConfig configures notifications via Hipchat.
type HipchatConfig struct {
NotifierConfig `yaml:",inline" json:",inline"`
Expand Down
6 changes: 6 additions & 0 deletions doc/examples/simple.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,9 @@ receivers:
room_id: 85
message_format: html
notify: true
- name: 'matrix'
matrix_configs:
- access_token: <access_token>
room_id: !<something>:matrix.org
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to make this not tied to matrix.org homeserver? I would like to use this with my homeserver without having to federate a room to matrix.org

text: "text msg"
html: "html msg"
75 changes: 75 additions & 0 deletions notify/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ func BuildReceiverIntegrations(nc *config.Receiver, tmpl *template.Template) []I
n := NewPushover(c, tmpl)
add("pushover", i, n, c)
}
for i, c := range nc.MatrixConfigs {
n := NewMatrix(c, tmpl)
add("matrix", i, n, c)
}
return integrations
}

Expand Down Expand Up @@ -543,6 +547,77 @@ func (n *Slack) retry(statusCode int) (bool, error) {
return false, nil
}

// matrixEvent is the event for sending a matrix notification.
type matrixEvent struct {
MsgType string `json:"msgtype"`
Body string `json:"body"`
FormattedBody string `json:"formatted_body,omitempty"`
Format string `json:"format,omitempty"`
}

// Matrix implements a Notifier for Matrix notifications.
type Matrix struct {
conf *config.MatrixConfig
tmpl *template.Template
}

// NewMatrix returns a new Matrix notification handler.
func NewMatrix(conf *config.MatrixConfig, tmpl *template.Template) *Matrix {
return &Matrix{
conf: conf,
tmpl: tmpl,
}
}

// Notify implements the Notifier interface.
func (n *Matrix) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
var err error
var (
data = n.tmpl.Data(receiverName(ctx), groupLabels(ctx), as...)
tmplText = tmplText(n.tmpl, data, &err)
tmplHTML = tmplHTML(n.tmpl, data, &err)
url = fmt.Sprintf("https://matrix.org/_matrix/client/r0/rooms/%s/send/m.room.message/m%d.1?access_token=%s", n.conf.RoomID, time.Now().Unix(), n.conf.AccessToken)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with rrix, URL of Matrix Homeserver should be configurable...

)

event := &matrixEvent{
MsgType: "m.text",
Body: tmplText(n.conf.Text),
FormattedBody: tmplHTML(n.conf.HTML),
}
if event.FormattedBody != "" {
event.Format = "org.matrix.custom.html"
}

var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(event); err != nil {
return false, err
}

req, errNew := http.NewRequest("PUT", url, &buf)
if errNew != nil {
return false, errNew
}

resp, err := ctxhttp.Do(ctx, http.DefaultClient, req)
if err != nil {
return true, err
}
defer resp.Body.Close()

return n.retry(resp.StatusCode)
}

func (n *Matrix) retry(statusCode int) (bool, error) {
// Matrix responds with a 200 response code on a successful
// request and 5xx response codes are assumed to be recoverable.
// http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid
if statusCode != 200 {
return (statusCode/100 == 5), fmt.Errorf("unexpected status code %v", statusCode)
}

return false, nil
}

// Hipchat implements a Notifier for Hipchat notifications.
type Hipchat struct {
conf *config.HipchatConfig
Expand Down
5 changes: 5 additions & 0 deletions template/default.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
{{ define "hipchat.default.message" }}{{ template "__subject" . }}{{ end }}


{{ define "matrix.default.text" }}{{ template "__subject" . }}
{{ template "__alertmanagerURL" . }}{{ end }}
{{ define "matrix.default.html" }}<a href="{{ template "__alertmanagerURL" . }}"><font color="{{ if eq .Status "firing" }}red{{ else }}green{{ end }}"><b>{{ template "__subject" . }}</b></font></a>{{ end }}


{{ define "pagerduty.default.description" }}{{ template "__subject" . }}{{ end }}
{{ define "pagerduty.default.client" }}{{ template "__alertmanager" . }}{{ end }}
{{ define "pagerduty.default.clientURL" }}{{ template "__alertmanagerURL" . }}{{ end }}
Expand Down
4 changes: 2 additions & 2 deletions template/internal/deftmpl/bindata.go

Large diffs are not rendered by default.