Exemplo n.º 1
0
func PostNotificationEventToSlackChannel(config config.SlackConfig, event parser.NotificationEvent) (err error) {
	f, err := os.OpenFile("/var/log/redis/sentinel.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		log.Printf("error opening log file: %v", err)
	}
	defer f.Close()

	log.SetOutput(f)
	doTrigger := contains(config.TriggerOn, event.Eventname)
	hostname, err := GetMyFQDN()
	if !doTrigger {
		return nil
	}
	api := slack.New(config.Token)
	channel, err := api.FindChannelByName(config.Channel)
	levelColor := "warning"
	// switch through event to determine attachment color
	switch event.Role {
	case "sentinel":
		levelColor = "danger"
	}
	switch event.Eventname {
	case "+odown", "-failover-abort-no-good-slave":
		levelColor = "danger"
	case "-odown", "-sdown":
		levelColor = "good"
	}
	att := slack.Attachment{Color: levelColor, AuthorName: config.AuthorName}
	att.Title = fmt.Sprintf("Sentinel event")
	eventField := slack.AttachmentField{Title: "Event Name", Value: event.Eventname, Short: true}
	podField := slack.AttachmentField{Title: "Pod Name", Value: event.Podname, Short: true}
	roleField := slack.AttachmentField{Title: "Role", Value: event.Role, Short: true}
	reporterField := slack.AttachmentField{Title: "Reporter", Value: hostname, Short: true}
	att.Fields = []*slack.AttachmentField{&eventField, &podField, &roleField, &reporterField}
	if err != nil {
		return (err)
	}
	var msg string
	switch levelColor {
	case "good":
		msg = "Phew, it has recovered."
	case "danger":
		msg = "UHOH! Something is broken."
	case "warning":
		msg = "Heads up, something isn't looking right."
	}
	atts := []*slack.Attachment{&att}
	msgopt := slack.ChatPostMessageOpt{AsUser: false, Attachments: atts, Username: config.Username}
	err = api.ChatPostMessage(channel.Id, msg, &msgopt)
	return err
}
Exemplo n.º 2
0
// GetAttachments get attachments for message
func GetAttachments(r horenso.Report, items []string) []*slack.Attachment {
	var attachments []*slack.Attachment

	var a slack.Attachment
	a.Fallback = "horenso Reporter"
	a.AuthorName = "horenso Reporter"

	if *r.ExitCode == 0 {
		a.Color = "#32B232"
	} else {
		a.Color = "#FF0000"
	}

	fields := []*slack.AttachmentField{}

	if IsSelectedItem("Result", items) {
		fields = append(fields, &slack.AttachmentField{
			Title: "Result",
			Value: fmt.Sprintf("%v", r.Result),
			Short: false,
		})
	}

	if IsSelectedItem("Output", items) {
		fields = append(fields, &slack.AttachmentField{
			Title: "Output",
			Value: fmt.Sprintf("%v", r.Output),
			Short: false,
		})
	}

	if IsSelectedItem("Stdout", items) {
		fields = append(fields, &slack.AttachmentField{
			Title: "Stdout",
			Value: fmt.Sprintf("%v", r.Stdout),
			Short: false,
		})
	}

	if IsSelectedItem("Stderr", items) {
		fields = append(fields, &slack.AttachmentField{
			Title: "Stderr",
			Value: fmt.Sprintf("%v", r.Stderr),
			Short: false,
		})
	}

	if IsSelectedItem("Command", items) {
		fields = append(fields, &slack.AttachmentField{
			Title: "Command",
			Value: fmt.Sprintf("%v", r.Command),
			Short: true,
		})
	}

	if IsSelectedItem("CommandArgs", items) {
		fields = append(fields, &slack.AttachmentField{
			Title: "CommandArgs",
			Value: fmt.Sprintf("%v", r.CommandArgs),
			Short: true,
		})
	}

	if IsSelectedItem("Pid", items) {
		fields = append(fields, &slack.AttachmentField{
			Title: "Pid",
			Value: fmt.Sprintf("%d", r.Pid),
			Short: true,
		})
	}

	if IsSelectedItem("ExitCode", items) {
		fields = append(fields, &slack.AttachmentField{
			Title: "ExitCode",
			Value: fmt.Sprintf("%d", *r.ExitCode),
			Short: true,
		})
	}

	if IsSelectedItem("StartAt", items) {
		fields = append(fields, &slack.AttachmentField{
			Title: "StartAt",
			Value: fmt.Sprintf("%v", r.StartAt),
			Short: true,
		})
	}
	if IsSelectedItem("EndAt", items) {
		fields = append(fields, &slack.AttachmentField{
			Title: "EndAt",
			Value: fmt.Sprintf("%v", r.EndAt),
			Short: true,
		})
	}
	if IsSelectedItem("Hostname", items) {
		fields = append(fields, &slack.AttachmentField{
			Title: "Hostname",
			Value: fmt.Sprintf("%v", r.Hostname),
			Short: true,
		})
	}
	if IsSelectedItem("SystemTime", items) {
		fields = append(fields, &slack.AttachmentField{
			Title: "SystemTime",
			Value: fmt.Sprintf("%f", *r.SystemTime),
			Short: true,
		})
	}
	if IsSelectedItem("UserTime", items) {
		fields = append(fields, &slack.AttachmentField{
			Title: "UserTime",
			Value: fmt.Sprintf("%f", *r.UserTime),
			Short: true,
		})
	}

	a.Fields = fields

	return append(attachments, &a)
}