Skip to content

Commit

Permalink
pkg/plugin/plugin.go: Introduce Object struct
Browse files Browse the repository at this point in the history
All object related values (name, url, etc) are moved into this struct and used accordingly
  • Loading branch information
sukhwinder33445 committed Oct 5, 2023
1 parent accd742 commit 57efb76
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cmd/channel/email/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (ch *Email) SendNotification(req *plugin.NotificationRequest) error {

var msg bytes.Buffer
_, _ = fmt.Fprintf(&msg, "To: %s\n", strings.Join(to, ","))
_, _ = fmt.Fprintf(&msg, "Subject: [#%d] %s %s is %s\n\n", req.Incident.Id, req.Event.Type, req.Incident.ObjectDisplayName, req.Event.Severity)
_, _ = fmt.Fprintf(&msg, "Subject: [#%d] %s %s is %s\n\n", req.Incident.Id, req.Event.Type, req.Object.Name, req.Event.Severity)

plugin.FormatMessage(&msg, req)

Expand Down
2 changes: 1 addition & 1 deletion cmd/channel/rocketchat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func main() {

func (ch *RocketChat) SendNotification(req *plugin.NotificationRequest) error {
var output bytes.Buffer
_, _ = fmt.Fprintf(&output, "[#%d] %s %s is %s\n\n", req.Incident.Id, req.Event.Type, req.Incident.ObjectDisplayName, req.Event.Severity)
_, _ = fmt.Fprintf(&output, "[#%d] %s %s is %s\n\n", req.Incident.Id, req.Event.Type, req.Object.Name, req.Event.Severity)

plugin.FormatMessage(&output, req)

Expand Down
17 changes: 13 additions & 4 deletions internal/channel/examples/send-notification.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,26 @@
}
]
},
"object": {
"name": "httpd",
"url": "https://example.com/icingaweb2/icingadb/service?name=httpd&host.name=www1",
"tags": {
"host": "www1",
"service": "httpd"
},
"extra_tags": {
"hostgroup/server": null
}
},
"incident": {
"id": 22,
"object_display_name": "httpd"
"url": "https://example.com/icingaweb2/notifications/incident?id=22"
},
"event": {
"url": "https://example.com/icingaweb2/icingadb/service?name=httpd&host.name=www1",
"type": "state",
"severity": "crit",
"message": "Service is down"
},
"icingaweb2_url": "https://example.com/icingaweb2/"
}
},
"id": 2020
}
20 changes: 16 additions & 4 deletions internal/channel/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/icinga/icinga-notifications/internal/recipient"
"github.com/icinga/icinga-notifications/pkg/plugin"
"github.com/icinga/icinga-notifications/pkg/rpc"
"strings"
)

func (c *Channel) GetInfo() (*plugin.Info, error) {
Expand Down Expand Up @@ -38,18 +39,29 @@ func (c *Channel) SendNotification(contact *recipient.Contact, i contracts.Incid
contactStruct.Addresses = append(contactStruct.Addresses, &plugin.Address{Type: addr.Type, Address: addr.Address})
}

if !strings.HasSuffix(icingaweb2Url, "/") {
icingaweb2Url = icingaweb2Url + "/"
}

req := plugin.NotificationRequest{
Contact: contactStruct,
Incident: &plugin.Incident{Id: i.ID(), ObjectDisplayName: i.ObjectDisplayName()},
Contact: contactStruct,
Object: &plugin.Object{
Name: i.ObjectDisplayName(),
Url: ev.URL,
Tags: ev.Tags,
ExtraTags: ev.ExtraTags,
},
Incident: &plugin.Incident{
Id: i.ID(),
Url: fmt.Sprintf("%snotifications/incident?id=%d\n", icingaweb2Url, i.ID()),
},
Event: &plugin.Event{
Time: ev.Time,
URL: ev.URL,
Type: ev.Type,
Severity: ev.Severity.String(),
Username: ev.Username,
Message: ev.Message,
},
IcingaWeb2Url: icingaweb2Url,
}

params, err := json.Marshal(req)
Expand Down
39 changes: 24 additions & 15 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"log"
"os"
"strings"
"sync"
"time"
)
Expand All @@ -34,25 +33,31 @@ type Address struct {
Address string `json:"address"`
}

type Object struct {
Name string `json:"name"`
Url string `json:"url"`
Tags map[string]string `json:"tags"`
ExtraTags map[string]string `json:"extra_tags"`
}

type Incident struct {
Id int64 `json:"id"`
ObjectDisplayName string `json:"object_display_name"`
Id int64 `json:"id"`
Url string `json:"url"`
}

type Event struct {
Time time.Time `json:"time"`
URL string `json:"url"`
Type string `json:"type"`
Severity string `json:"severity"`
Username string `json:"username"`
Message string `json:"message"`
}

type NotificationRequest struct {
Contact *Contact `json:"contact"`
Incident *Incident `json:"incident"`
Event *Event `json:"event"`
IcingaWeb2Url string `json:"icingaweb2_url"`
Contact *Contact `json:"contact"`
Object *Object `json:"object"`
Incident *Incident `json:"incident"`
Event *Event `json:"event"`
}

type Plugin interface {
Expand Down Expand Up @@ -124,14 +129,18 @@ func FormatMessage(writer io.Writer, req *NotificationRequest) {
if req.Event.Username != "" {
_, _ = fmt.Fprintf(writer, "\nCommented by %s\n\n", req.Event.Username)
}
_, _ = fmt.Fprintf(writer, "Object: %s\n\n", req.Object.Url)
_, _ = writer.Write([]byte("Tags:\n"))
for k, v := range req.Object.Tags {
_, _ = fmt.Fprintf(writer, "%s: %s\n", k, v)
}

_, _ = writer.Write([]byte(req.Event.URL + "\n\n"))
incidentUrl := req.IcingaWeb2Url
if strings.HasSuffix(incidentUrl, "/") {
incidentUrl = fmt.Sprintf("Incident: %snotifications/incident?id=%d\n", req.IcingaWeb2Url, req.Incident.Id)
} else {
incidentUrl = fmt.Sprintf("Incident: %s/notifications/incident?id=%d\n", req.IcingaWeb2Url, req.Incident.Id)
if len(req.Object.ExtraTags) > 0 {
_, _ = writer.Write([]byte("\nExtra Tags:\n"))
for k, v := range req.Object.ExtraTags {
_, _ = fmt.Fprintf(writer, "%s: %s\n", k, v)
}
}

_, _ = writer.Write([]byte(incidentUrl))
_, _ = fmt.Fprintf(writer, "Incident: %s", req.Incident.Url)
}

0 comments on commit 57efb76

Please sign in to comment.