diff --git a/internal/icinga2/api_responses.go b/internal/icinga2/api_responses.go index 54e6edae..cbb9f0c9 100644 --- a/internal/icinga2/api_responses.go +++ b/internal/icinga2/api_responses.go @@ -103,6 +103,11 @@ type Downtime struct { IsFixed bool `json:"fixed"` } +// WasCancelled returns true when the current downtime was cancelled prematurely by a user. +func (d *Downtime) WasCancelled() bool { + return d.RemoveTime.Time().After(time.UnixMilli(0)) +} + // HostServiceRuntimeAttributes are common attributes of both Host and Service objects. // // When catching up potentially missed changes, the following fields are holding relevant changes which, fortunately, diff --git a/internal/icinga2/api_responses_test.go b/internal/icinga2/api_responses_test.go index 4f01ace0..ef817275 100644 --- a/internal/icinga2/api_responses_test.go +++ b/internal/icinga2/api_responses_test.go @@ -33,7 +33,7 @@ func TestUnixFloat_UnmarshalJSON(t *testing.T) { { name: "epoch-time", jsonData: "0.0", - expected: UnixFloat(time.Time{}), + expected: UnixFloat(time.Date(1970, time.January, 1, 0, 0, 0, 0, time.UTC)), }, { name: "example-time", @@ -128,10 +128,11 @@ func TestObjectQueriesResult_UnmarshalJSON(t *testing.T) { Name: "dummy-11!af73f9d9-2ed8-45f8-b541-cce3f3fe0f6c", Type: "Downtime", Attrs: Downtime{ - Host: "dummy-11", - Author: "icingaadmin", - Comment: "turn down for what", - IsFixed: true, + Host: "dummy-11", + Author: "icingaadmin", + Comment: "turn down for what", + RemoveTime: UnixFloat(time.UnixMilli(0)), + IsFixed: true, }, }, }, @@ -144,10 +145,11 @@ func TestObjectQueriesResult_UnmarshalJSON(t *testing.T) { Name: "dummy-7!691d508b-c93f-4565-819c-3e46ffef1555", Type: "Downtime", Attrs: Downtime{ - Host: "dummy-7", - Author: "icingaadmin", - Comment: "Flexible", - IsFixed: false, + Host: "dummy-7", + Author: "icingaadmin", + Comment: "Flexible", + RemoveTime: UnixFloat(time.UnixMilli(0)), + IsFixed: false, }, }, }, @@ -160,11 +162,12 @@ func TestObjectQueriesResult_UnmarshalJSON(t *testing.T) { Name: "docker-master!disk /!97078a44-8902-495a-9f2a-c1f6802bc63d", Type: "Downtime", Attrs: Downtime{ - Host: "docker-master", - Service: "disk /", - Author: "icingaadmin", - Comment: "Flexible", - IsFixed: false, + Host: "docker-master", + Service: "disk /", + Author: "icingaadmin", + Comment: "Flexible", + RemoveTime: UnixFloat(time.UnixMilli(0)), + IsFixed: false, }, }, }, @@ -177,11 +180,12 @@ func TestObjectQueriesResult_UnmarshalJSON(t *testing.T) { Name: "docker-master!load!c27b27c2-e0ab-45ff-8b9b-e95f29851eb0", Type: "Downtime", Attrs: Downtime{ - Host: "docker-master", - Service: "load", - Author: "icingaadmin", - Comment: "Scheduled downtime for backup", - IsFixed: true, + Host: "docker-master", + Service: "load", + Author: "icingaadmin", + Comment: "Scheduled downtime for backup", + RemoveTime: UnixFloat(time.UnixMilli(0)), + IsFixed: true, }, }, }, @@ -208,7 +212,7 @@ func TestObjectQueriesResult_UnmarshalJSON(t *testing.T) { LastStateChange: UnixFloat(time.UnixMicro(1697099900637215)), DowntimeDepth: 0, Acknowledgement: AcknowledgementNone, - AcknowledgementLastChange: UnixFloat(time.Time{}), + AcknowledgementLastChange: UnixFloat(time.UnixMilli(0)), }, }, }, @@ -264,7 +268,7 @@ func TestObjectQueriesResult_UnmarshalJSON(t *testing.T) { LastStateChange: UnixFloat(time.UnixMicro(1697704135756310)), DowntimeDepth: 0, Acknowledgement: AcknowledgementNone, - AcknowledgementLastChange: UnixFloat(time.Time{}), + AcknowledgementLastChange: UnixFloat(time.UnixMilli(0)), }, }, }, @@ -451,10 +455,11 @@ func TestApiResponseUnmarshal(t *testing.T) { expected: &DowntimeAdded{ Timestamp: UnixFloat(time.UnixMicro(1697207050511293)), Downtime: Downtime{ - Host: "dummy-157", - Author: "icingaadmin", - Comment: "updates", - IsFixed: true, + Host: "dummy-157", + Author: "icingaadmin", + Comment: "updates", + RemoveTime: UnixFloat(time.UnixMilli(0)), + IsFixed: true, }, }, }, @@ -464,11 +469,12 @@ func TestApiResponseUnmarshal(t *testing.T) { expected: &DowntimeAdded{ Timestamp: UnixFloat(time.UnixMicro(1697207141217425)), Downtime: Downtime{ - Host: "docker-master", - Service: "http", - Author: "icingaadmin", - Comment: "broken until Monday", - IsFixed: true, + Host: "docker-master", + Service: "http", + Author: "icingaadmin", + Comment: "broken until Monday", + RemoveTime: UnixFloat(time.UnixMilli(0)), + IsFixed: true, }, }, }, @@ -478,10 +484,11 @@ func TestApiResponseUnmarshal(t *testing.T) { expected: &DowntimeStarted{ Timestamp: UnixFloat(time.UnixMicro(1697207050511378)), Downtime: Downtime{ - Host: "dummy-157", - Author: "icingaadmin", - Comment: "updates", - IsFixed: true, + Host: "dummy-157", + Author: "icingaadmin", + Comment: "updates", + RemoveTime: UnixFloat(time.UnixMilli(0)), + IsFixed: true, }, }, }, @@ -491,11 +498,12 @@ func TestApiResponseUnmarshal(t *testing.T) { expected: &DowntimeStarted{ Timestamp: UnixFloat(time.UnixMicro(1697207141217507)), Downtime: Downtime{ - Host: "docker-master", - Service: "http", - Author: "icingaadmin", - Comment: "broken until Monday", - IsFixed: true, + Host: "docker-master", + Service: "http", + Author: "icingaadmin", + Comment: "broken until Monday", + RemoveTime: UnixFloat(time.UnixMilli(0)), + IsFixed: true, }, }, }, @@ -505,10 +513,11 @@ func TestApiResponseUnmarshal(t *testing.T) { expected: &DowntimeTriggered{ Timestamp: UnixFloat(time.UnixMicro(1697207050511608)), Downtime: Downtime{ - Host: "dummy-157", - Author: "icingaadmin", - Comment: "updates", - IsFixed: true, + Host: "dummy-157", + Author: "icingaadmin", + Comment: "updates", + RemoveTime: UnixFloat(time.UnixMilli(0)), + IsFixed: true, }, }, }, @@ -518,10 +527,11 @@ func TestApiResponseUnmarshal(t *testing.T) { expected: &DowntimeTriggered{ Timestamp: UnixFloat(time.UnixMicro(1714040073242575)), Downtime: Downtime{ - Host: "dummy-7", - Author: "icingaadmin", - Comment: "Flexible", - IsFixed: false, + Host: "dummy-7", + Author: "icingaadmin", + Comment: "Flexible", + RemoveTime: UnixFloat(time.UnixMilli(0)), + IsFixed: false, }, }, }, @@ -531,11 +541,12 @@ func TestApiResponseUnmarshal(t *testing.T) { expected: &DowntimeTriggered{ Timestamp: UnixFloat(time.UnixMicro(1697207141217726)), Downtime: Downtime{ - Host: "docker-master", - Service: "http", - Author: "icingaadmin", - Comment: "broken until Monday", - IsFixed: true, + Host: "docker-master", + Service: "http", + Author: "icingaadmin", + Comment: "broken until Monday", + RemoveTime: UnixFloat(time.UnixMilli(0)), + IsFixed: true, }, }, }, @@ -545,11 +556,12 @@ func TestApiResponseUnmarshal(t *testing.T) { expected: &DowntimeTriggered{ Timestamp: UnixFloat(time.UnixMicro(1714039143460918)), Downtime: Downtime{ - Host: "docker-master", - Service: "disk /", - Author: "icingaadmin", - Comment: "Flexible", - IsFixed: false, + Host: "docker-master", + Service: "disk /", + Author: "icingaadmin", + Comment: "Flexible", + RemoveTime: UnixFloat(time.UnixMilli(0)), + IsFixed: false, }, }, }, @@ -562,7 +574,7 @@ func TestApiResponseUnmarshal(t *testing.T) { Host: "dummy-157", Author: "icingaadmin", Comment: "updates", - RemoveTime: UnixFloat(time.Time{}), + RemoveTime: UnixFloat(time.UnixMilli(0)), IsFixed: true, }, }, diff --git a/internal/icinga2/client.go b/internal/icinga2/client.go index 87cdd8fe..b18dbf1a 100644 --- a/internal/icinga2/client.go +++ b/internal/icinga2/client.go @@ -205,7 +205,7 @@ func (client *Client) buildDowntimeEvent(ctx context.Context, d Downtime, startE if startEvent { ev.Type = event.TypeDowntimeStart - } else if d.RemoveTime.Time().IsZero() { + } else if !d.WasCancelled() { ev.Type = event.TypeDowntimeEnd } else { ev.Type = event.TypeDowntimeRemoved