@@ -7,15 +7,16 @@ import (
7
7
"github.com/icinga/icinga-go-library/database"
8
8
"github.com/icinga/icinga-go-library/types"
9
9
"github.com/icinga/icinga-notifications/internal/config"
10
- "github.com/icinga/icinga-notifications/internal/contracts"
11
10
"github.com/icinga/icinga-notifications/internal/daemon"
12
11
"github.com/icinga/icinga-notifications/internal/event"
13
12
"github.com/icinga/icinga-notifications/internal/object"
14
13
"github.com/icinga/icinga-notifications/internal/recipient"
15
14
"github.com/icinga/icinga-notifications/internal/rule"
16
15
"github.com/icinga/icinga-notifications/internal/utils"
16
+ "github.com/icinga/icinga-notifications/pkg/plugin"
17
17
"github.com/jmoiron/sqlx"
18
18
"go.uber.org/zap"
19
+ "net/url"
19
20
"sync"
20
21
"time"
21
22
)
@@ -75,14 +76,6 @@ func NewIncident(
75
76
return i
76
77
}
77
78
78
- func (i * Incident ) IncidentObject () * object.Object {
79
- return i .Object
80
- }
81
-
82
- func (i * Incident ) SeverityString () string {
83
- return i .Severity .String ()
84
- }
85
-
86
79
func (i * Incident ) String () string {
87
80
return fmt .Sprintf ("#%d" , i .Id )
88
81
}
@@ -559,14 +552,43 @@ func (i *Incident) triggerEscalations(ctx context.Context, tx *sqlx.Tx, ev *even
559
552
// notifyContacts executes all the given pending notifications of the current incident.
560
553
// Returns error on database failure or if the provided context is cancelled.
561
554
func (i * Incident ) notifyContacts (ctx context.Context , ev * event.Event , notifications []* NotificationEntry ) error {
555
+ baseUrl , err := url .Parse (daemon .Config ().Icingaweb2URL )
556
+ if err != nil {
557
+ i .logger .Errorw ("Failed to parse Icinga Web 2 URL" , zap .String ("url" , daemon .Config ().Icingaweb2URL ), zap .Error (err ))
558
+ return err
559
+ }
560
+
561
+ incidentUrl := baseUrl .JoinPath ("/notifications/incident" )
562
+ incidentUrl .RawQuery = fmt .Sprintf ("id=%d" , i .ID ())
563
+
564
+ req := & plugin.NotificationRequest {
565
+ Object : & plugin.Object {
566
+ Name : i .Object .DisplayName (),
567
+ Url : ev .URL ,
568
+ Tags : i .Object .Tags ,
569
+ ExtraTags : i .Object .ExtraTags ,
570
+ },
571
+ Incident : & plugin.Incident {
572
+ Id : i .Id ,
573
+ Url : incidentUrl .String (),
574
+ Severity : i .Severity .String (),
575
+ },
576
+ Event : & plugin.Event {
577
+ Time : ev .Time ,
578
+ Type : ev .Type ,
579
+ Username : ev .Username ,
580
+ Message : ev .Message ,
581
+ },
582
+ }
583
+
562
584
for _ , notification := range notifications {
563
585
contact := i .runtimeConfig .Contacts [notification .ContactID ]
564
586
if contact == nil {
565
587
i .logger .Debugw ("Incident refers unknown contact, might got deleted" , zap .Int64 ("contact_id" , notification .ContactID ))
566
588
continue
567
589
}
568
590
569
- if i .notifyContact (contact , ev , notification .ChannelID ) != nil {
591
+ if i .notifyContact (contact , req , notification .ChannelID ) != nil {
570
592
notification .State = NotificationStateFailed
571
593
} else {
572
594
notification .State = NotificationStateSent
@@ -590,7 +612,7 @@ func (i *Incident) notifyContacts(ctx context.Context, ev *event.Event, notifica
590
612
}
591
613
592
614
// notifyContact notifies the given recipient via a channel matching the given ID.
593
- func (i * Incident ) notifyContact (contact * recipient.Contact , ev * event. Event , chID int64 ) error {
615
+ func (i * Incident ) notifyContact (contact * recipient.Contact , req * plugin. NotificationRequest , chID int64 ) error {
594
616
ch := i .runtimeConfig .Channels [chID ]
595
617
if ch == nil {
596
618
i .logger .Errorw ("Could not find config for channel" , zap .Int64 ("channel_id" , chID ))
@@ -599,16 +621,21 @@ func (i *Incident) notifyContact(contact *recipient.Contact, ev *event.Event, ch
599
621
}
600
622
601
623
i .logger .Infow (fmt .Sprintf ("Notify contact %q via %q of type %q" , contact .FullName , ch .Name , ch .Type ),
602
- zap .Int64 ("channel_id" , chID ), zap .String ("event_type" , ev .Type ))
624
+ zap .Int64 ("channel_id" , chID ), zap .String ("event_type" , req . Event .Type ))
603
625
604
- err := ch .Notify (contact , i , ev , daemon .Config ().Icingaweb2URL )
605
- if err != nil {
626
+ contactStruct := & plugin.Contact {FullName : contact .FullName }
627
+ for _ , addr := range contact .Addresses {
628
+ contactStruct .Addresses = append (contactStruct .Addresses , & plugin.Address {Type : addr .Type , Address : addr .Address })
629
+ }
630
+ req .Contact = contactStruct
631
+
632
+ if err := ch .Notify (req ); err != nil {
606
633
i .logger .Errorw ("Failed to send notification via channel plugin" , zap .String ("type" , ch .Type ), zap .Error (err ))
607
634
return err
608
635
}
609
636
610
637
i .logger .Infow ("Successfully sent a notification via channel plugin" , zap .String ("type" , ch .Type ),
611
- zap .String ("contact" , contact .FullName ), zap .String ("event_type" , ev .Type ))
638
+ zap .String ("contact" , contact .FullName ), zap .String ("event_type" , req . Event .Type ))
612
639
613
640
return nil
614
641
}
@@ -767,7 +794,3 @@ func (e *EscalationState) TableName() string {
767
794
type RecipientState struct {
768
795
Role ContactRole
769
796
}
770
-
771
- var (
772
- _ contracts.Incident = (* Incident )(nil )
773
- )
0 commit comments