-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
583 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ services: | |
volumes: | ||
- ./.data/redis:/data | ||
|
||
build: | ||
dev: | ||
depends_on: | ||
- redis | ||
build: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright © 2019 SIC! Software GmbH | ||
|
||
package handler | ||
|
||
import ( | ||
"bytes" | ||
"text/template" | ||
|
||
sensu "github.com/sensu/sensu-go/types" | ||
) | ||
|
||
func resolveTemplate(templateValue string, event *sensu.Event) (string, error) { | ||
var resolved bytes.Buffer | ||
|
||
tmpl, err := template.New("tmpl").Parse(templateValue) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
err = tmpl.Execute(&resolved, *event) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return resolved.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright © 2019 SIC! Software GmbH | ||
|
||
package handler | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"net/mail" | ||
"net/smtp" | ||
|
||
sensu "github.com/sensu/sensu-go/types" | ||
|
||
"sensu-sic-handler/recipient" | ||
) | ||
|
||
var ( | ||
mailSubjectTemplate = "[Sensu] {{.Entity.Name}}/{{.Check.Name}}: {{.Check.State}}" | ||
mailBodyTemplate = "{{.Check.Output}}" | ||
) | ||
|
||
// HandleMail handles mail recipients (recipient.HandlerTypeMail) | ||
func HandleMail(recipient *recipient.Recipient, event *sensu.Event, config *Config) error { | ||
if len(config.MailFrom) == 0 { | ||
return errors.New("from email is empty") | ||
} | ||
|
||
fromAddress, err := mail.ParseAddress(config.MailFrom) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(recipient.Args["mail"]) == 0 { | ||
return errors.New("to email is empty") | ||
} | ||
|
||
toAddress, err := mail.ParseAddress(recipient.Args["mail"]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
subject, err := resolveTemplate(mailSubjectTemplate, event) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
body, err := resolveTemplate(mailBodyTemplate, event) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
msg := []byte("From: " + fromAddress.String() + "\r\n" + | ||
"To: " + toAddress.String() + "\r\n" + | ||
"Subject: " + subject + "\r\n" + | ||
"\r\n" + | ||
body + "\r\n") | ||
|
||
conn, err := smtp.Dial(config.SMTPAddress) | ||
if err != nil { | ||
return err | ||
} | ||
defer conn.Close() | ||
|
||
conn.Mail(fromAddress.Address) | ||
conn.Rcpt(toAddress.Address) | ||
|
||
data, err := conn.Data() | ||
if err != nil { | ||
return err | ||
} | ||
defer data.Close() | ||
|
||
buffer := bytes.NewBuffer(msg) | ||
if _, err := buffer.WriteTo(data); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright © 2019 SIC! Software GmbH | ||
|
||
package handler | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
sensu "github.com/sensu/sensu-go/types" | ||
|
||
"sensu-sic-handler/recipient" | ||
) | ||
|
||
// Handle handles recipients | ||
func Handle(recipients []*recipient.Recipient, event *sensu.Event, config *Config) error { | ||
recipientMap := make(map[string]bool) | ||
|
||
for _, rcpt := range recipients { | ||
if _, ok := recipientMap[rcpt.ID]; !ok { | ||
var err error | ||
err = nil | ||
|
||
switch rcpt.Type { | ||
case recipient.HandlerTypeNone: | ||
case recipient.HandlerTypeMail: | ||
err = HandleMail(rcpt, event, config) | ||
case recipient.HandlerTypeXMPP: | ||
err = HandleXMPP(rcpt, event, config) | ||
case recipient.HandlerTypeSlack: | ||
err = HandleSlack(rcpt, event, config) | ||
default: | ||
fmt.Fprintln(os.Stderr, fmt.Sprintf("unsupported handler: %s", rcpt)) | ||
} | ||
|
||
if err != nil { | ||
fmt.Fprintln(os.Stderr, fmt.Sprintf("failed to handle: %s", err.Error())) | ||
} | ||
|
||
recipientMap[rcpt.ID] = true | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright © 2019 SIC! Software GmbH | ||
|
||
package handler | ||
|
||
import ( | ||
sensu "github.com/sensu/sensu-go/types" | ||
|
||
"sensu-sic-handler/recipient" | ||
) | ||
|
||
// HandleSlack handles slack recipients (recipient.HandlerTypeSlack) | ||
func HandleSlack(recipient *recipient.Recipient, event *sensu.Event, config *Config) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright © 2019 SIC! Software GmbH | ||
|
||
package handler | ||
|
||
// Config configuration for handlers | ||
type Config struct { | ||
SMTPAddress string | ||
MailFrom string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright © 2019 SIC! Software GmbH | ||
|
||
package handler | ||
|
||
import ( | ||
sensu "github.com/sensu/sensu-go/types" | ||
|
||
"sensu-sic-handler/recipient" | ||
) | ||
|
||
// HandleXMPP handles XMPP recipients (recipient.HandlerTypeXMPP) | ||
func HandleXMPP(recipient *recipient.Recipient, event *sensu.Event, config *Config) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright © 2019 SIC! Software GmbH | ||
|
||
package recipient | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/go-redis/redis" | ||
) | ||
|
||
// ParseMail parse mail recipients (HandlerTypeMail) | ||
func ParseMail(redisClient *redis.Client, value string) []*Recipient { | ||
recipients := make([]*Recipient, 0) | ||
|
||
args := strings.Split(value, ":") | ||
|
||
switch len(args) { | ||
case 1: | ||
recipients = append(recipients, &Recipient{ | ||
Type: HandlerTypeMail, | ||
ID: fmt.Sprintf("mail|%s", args[0]), | ||
Args: map[string]string{"mail": args[0]}, | ||
}) | ||
} | ||
|
||
return recipients | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright © 2019 SIC! Software GmbH | ||
|
||
package recipient | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/go-redis/redis" | ||
) | ||
|
||
// Parse parses recipients from string | ||
func Parse(redisClient *redis.Client, value string) []*Recipient { | ||
recipientDefinitions := strings.Split(value, ",") | ||
recipients := make([]*Recipient, 0) | ||
|
||
for _, recipientDefinition := range recipientDefinitions { | ||
val := strings.SplitN(recipientDefinition, ":", 2) | ||
|
||
switch val[0] { | ||
case "slack": | ||
recipients = append(recipients, ParseSlack(redisClient, val[1])...) | ||
case "xmpp": | ||
recipients = append(recipients, ParseXMPP(redisClient, val[1])...) | ||
case "mail": | ||
recipients = append(recipients, ParseMail(redisClient, val[1])...) | ||
case "project": | ||
recipients = append(recipients, ParseProject(redisClient, val[1])...) | ||
default: | ||
fmt.Fprintln(os.Stderr, fmt.Sprintf("unsupported recipient: %s", recipientDefinition)) | ||
} | ||
} | ||
|
||
return recipients | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright © 2019 SIC! Software GmbH | ||
|
||
package recipient | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/go-redis/redis" | ||
|
||
"sensu-sic-handler/redmine" | ||
) | ||
|
||
// ParseProject parse redmine project recipients | ||
func ParseProject(redisClient *redis.Client, value string) []*Recipient { | ||
recipients := make([]*Recipient, 0) | ||
|
||
args := strings.Split(value, ":") | ||
|
||
switch len(args) { | ||
case 2: | ||
mails := readProjectMailsFromRedis(redisClient, args[0], args[1]) | ||
|
||
for _, m := range mails { | ||
recipients = append(recipients, &Recipient{ | ||
Type: HandlerTypeMail, | ||
ID: fmt.Sprintf("mail|%s", m), | ||
Args: map[string]string{"mail": m}, | ||
}) | ||
} | ||
} | ||
|
||
return recipients | ||
} | ||
|
||
func readProjectMailsFromRedis(client *redis.Client, projectIdentifier string, roleIDStr string) []string { | ||
var mails []string | ||
|
||
roleID, err := strconv.Atoi(roleIDStr) | ||
if err != nil { | ||
return mails | ||
} | ||
|
||
val, err := client.Get(redmine.RedisKey(projectIdentifier, roleID, "mail")).Result() | ||
if err != nil { | ||
return mails | ||
} | ||
|
||
err = json.Unmarshal([]byte(val), &mails) | ||
if err != nil { | ||
return mails | ||
} | ||
|
||
return mails | ||
} |
Oops, something went wrong.