Skip to content

Commit c50bf27

Browse files
sukhwinder33445oxzi
authored andcommitted
Format notification subject and message depending on event type
1 parent ad3e679 commit c50bf27

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

notifications/plugin/plugin.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"github.com/icinga/icinga-notifications/internal/event"
78
"github.com/icinga/icinga-notifications/internal/utils"
89
"github.com/icinga/icinga-notifications/pkg/rpc"
910
"github.com/icinga/icingadb/pkg/types"
@@ -194,11 +195,19 @@ func RunPlugin(plugin Plugin) {
194195

195196
// FormatMessage formats a notification message and adds to the given io.Writer
196197
func FormatMessage(writer io.Writer, req *NotificationRequest) {
197-
_, _ = fmt.Fprintf(writer, "Info: %s\n\n", req.Event.Message)
198+
if req.Event.Message != "" {
199+
msgTitle := "Comment"
200+
if req.Event.Type == event.TypeState {
201+
msgTitle = "Output"
202+
}
203+
204+
_, _ = fmt.Fprintf(writer, "%s: %s\n\n", msgTitle, req.Event.Message)
205+
}
206+
198207
_, _ = fmt.Fprintf(writer, "When: %s\n\n", req.Event.Time.Format("2006-01-02 15:04:05 MST"))
199208

200209
if req.Event.Username != "" {
201-
_, _ = fmt.Fprintf(writer, "Commented by %s\n\n", req.Event.Username)
210+
_, _ = fmt.Fprintf(writer, "Author: %s\n\n", req.Event.Username)
202211
}
203212
_, _ = fmt.Fprintf(writer, "Object: %s\n\n", req.Object.Url)
204213
_, _ = writer.Write([]byte("Tags:\n"))
@@ -217,3 +226,15 @@ func FormatMessage(writer io.Writer, req *NotificationRequest) {
217226

218227
_, _ = fmt.Fprintf(writer, "\nIncident: %s", req.Incident.Url)
219228
}
229+
230+
// FormatSubject returns the formatted subject string based on the event type.
231+
func FormatSubject(req *NotificationRequest) string {
232+
switch req.Event.Type {
233+
case event.TypeState:
234+
return fmt.Sprintf("[#%d] %s %s is %s", req.Incident.Id, req.Event.Type, req.Object.Name, req.Incident.Severity)
235+
case event.TypeAcknowledgementCleared, event.TypeDowntimeRemoved:
236+
return fmt.Sprintf("[#%d] %s from %s", req.Incident.Id, req.Event.Type, req.Object.Name)
237+
default:
238+
return fmt.Sprintf("[#%d] %s on %s", req.Incident.Id, req.Event.Type, req.Object.Name)
239+
}
240+
}

0 commit comments

Comments
 (0)