From 0abfebb66bfbc2f4338392d0049f62884da6435c Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Tue, 27 Oct 2020 16:49:19 +0100 Subject: [PATCH] Actually insert NULL into NULLable columns --- configobject/history/history.go | 2 +- .../objecttypes/checkcommand/checkcommand.go | 2 +- configobject/objecttypes/comment/comment.go | 4 +-- configobject/objecttypes/downtime/downtime.go | 6 ++--- .../objecttypes/eventcommand/eventcommand.go | 2 +- configobject/objecttypes/host/host.go | 27 ++++++++++++------- .../objecttypes/host/hoststate/hoststate.go | 2 +- .../objecttypes/hostgroup/hostgroup.go | 2 +- .../objecttypes/notification/notification.go | 6 ++--- .../notificationcommand.go | 2 +- configobject/objecttypes/service/service.go | 14 +++++----- .../service/servicestate/servicestate.go | 2 +- .../objecttypes/servicegroup/servicegroup.go | 2 +- .../objecttypes/timeperiod/timeperiod.go | 2 +- configobject/objecttypes/user/user.go | 4 +-- .../objecttypes/usergroup/usergroup.go | 2 +- configobject/objecttypes/zone/zone.go | 2 +- ha/heartbeat.go | 9 ++----- utils/convert.go | 9 +++++++ 19 files changed, 57 insertions(+), 44 deletions(-) diff --git a/configobject/history/history.go b/configobject/history/history.go index 55a08fc8..aa9bb578 100644 --- a/configobject/history/history.go +++ b/configobject/history/history.go @@ -252,7 +252,7 @@ func downtimeHistoryWorker(super *supervisor.Supervisor) { dataFunctions := []func(values map[string]interface{}) []interface{}{ func(values map[string]interface{}) []interface{} { - var triggeredById []byte + var triggeredById interface{} if values["triggered_by_id"] != nil { triggeredById = utils.EncodeChecksum(values["triggered_by_id"].(string)) } diff --git a/configobject/objecttypes/checkcommand/checkcommand.go b/configobject/objecttypes/checkcommand/checkcommand.go index 709a53c5..ac8f6d82 100644 --- a/configobject/objecttypes/checkcommand/checkcommand.go +++ b/configobject/objecttypes/checkcommand/checkcommand.go @@ -58,7 +58,7 @@ func (c *CheckCommand) UpdateValues() []interface{} { utils.EncodeChecksum(c.PropertiesChecksum), c.Name, c.NameCi, - utils.EncodeChecksum(c.ZoneId), + utils.EncodeChecksumOrNil(c.ZoneId), c.Command, c.Timeout, ) diff --git a/configobject/objecttypes/comment/comment.go b/configobject/objecttypes/comment/comment.go index 3af46f67..5fd31feb 100644 --- a/configobject/objecttypes/comment/comment.go +++ b/configobject/objecttypes/comment/comment.go @@ -70,7 +70,7 @@ func (c *Comment) UpdateValues() []interface{} { utils.EncodeChecksum(c.EnvId), c.ObjectType, utils.EncodeChecksum(c.HostId), - utils.EncodeChecksum(c.ServiceId), + utils.EncodeChecksumOrNil(c.ServiceId), utils.EncodeChecksum(c.NameChecksum), utils.EncodeChecksum(c.PropertiesChecksum), c.Name, @@ -81,7 +81,7 @@ func (c *Comment) UpdateValues() []interface{} { utils.Bool[c.IsPersistent], utils.Bool[c.IsSticky], c.ExpireTime, - utils.EncodeChecksum(c.ZoneId), + utils.EncodeChecksumOrNil(c.ZoneId), ) return v diff --git a/configobject/objecttypes/downtime/downtime.go b/configobject/objecttypes/downtime/downtime.go index 755e9bd3..777b06ea 100644 --- a/configobject/objecttypes/downtime/downtime.go +++ b/configobject/objecttypes/downtime/downtime.go @@ -75,10 +75,10 @@ func (d *Downtime) UpdateValues() []interface{} { v = append( v, utils.EncodeChecksum(d.EnvId), - utils.EncodeChecksum(d.TriggeredById), + utils.EncodeChecksumOrNil(d.TriggeredById), d.ObjectType, utils.EncodeChecksum(d.HostId), - utils.EncodeChecksum(d.ServiceId), + utils.EncodeChecksumOrNil(d.ServiceId), utils.EncodeChecksum(d.NameChecksum), utils.EncodeChecksum(d.PropertiesChecksum), d.Name, @@ -92,7 +92,7 @@ func (d *Downtime) UpdateValues() []interface{} { utils.Bool[d.IsInEffect], d.StartTime, d.EndTime, - utils.EncodeChecksum(d.ZoneId), + utils.EncodeChecksumOrNil(d.ZoneId), ) return v diff --git a/configobject/objecttypes/eventcommand/eventcommand.go b/configobject/objecttypes/eventcommand/eventcommand.go index cfe0d90e..16c3a6d9 100644 --- a/configobject/objecttypes/eventcommand/eventcommand.go +++ b/configobject/objecttypes/eventcommand/eventcommand.go @@ -58,7 +58,7 @@ func (c *EventCommand) UpdateValues() []interface{} { utils.EncodeChecksum(c.PropertiesChecksum), c.Name, c.NameCi, - utils.EncodeChecksum(c.ZoneId), + utils.EncodeChecksumOrNil(c.ZoneId), c.Command, c.Timeout, ) diff --git a/configobject/objecttypes/host/host.go b/configobject/objecttypes/host/host.go index 9f011534..85ade2ce 100644 --- a/configobject/objecttypes/host/host.go +++ b/configobject/objecttypes/host/host.go @@ -107,6 +107,15 @@ func (h *Host) InsertValues() []interface{} { return append([]interface{}{utils.EncodeChecksum(h.Id)}, v...) } +// ipOrNil converts net.IP(nil) to interface{}(nil) – which are not the same. +func ipOrNil(ip net.IP) interface{} { + if ip == nil { + return nil + } + + return ip +} + func (h *Host) UpdateValues() []interface{} { v := make([]interface{}, 0) @@ -120,13 +129,13 @@ func (h *Host) UpdateValues() []interface{} { h.DisplayName, h.Address, h.Address6, - net.ParseIP(h.Address).To4(), - net.ParseIP(h.Address6).To16(), + ipOrNil(net.ParseIP(h.Address).To4()), + ipOrNil(net.ParseIP(h.Address6).To16()), h.Checkcommand, utils.EncodeChecksum(h.CheckcommandId), h.MaxCheckAttempts, h.CheckPeriod, - utils.EncodeChecksum(h.CheckPeriodId), + utils.EncodeChecksumOrNil(h.CheckPeriodId), h.CheckTimeout, h.CheckInterval, h.CheckRetryInterval, @@ -139,17 +148,17 @@ func (h *Host) UpdateValues() []interface{} { h.FlappingThresholdHigh, utils.Bool[h.PerfdataEnabled], h.Eventcommand, - utils.EncodeChecksum(h.EventcommandId), + utils.EncodeChecksumOrNil(h.EventcommandId), utils.Bool[h.IsVolatile], - utils.EncodeChecksum(h.ActionUrlId), - utils.EncodeChecksum(h.NotesUrlId), + utils.EncodeChecksumOrNil(h.ActionUrlId), + utils.EncodeChecksumOrNil(h.NotesUrlId), h.Notes, - utils.EncodeChecksum(h.IconImageId), + utils.EncodeChecksumOrNil(h.IconImageId), h.IconImageAlt, h.Zone, - utils.EncodeChecksum(h.ZoneId), + utils.EncodeChecksumOrNil(h.ZoneId), h.CommandEndpoint, - utils.EncodeChecksum(h.CommandEndpointId), + utils.EncodeChecksumOrNil(h.CommandEndpointId), ) return v diff --git a/configobject/objecttypes/host/hoststate/hoststate.go b/configobject/objecttypes/host/hoststate/hoststate.go index bbb534c1..bce68e2d 100644 --- a/configobject/objecttypes/host/hoststate/hoststate.go +++ b/configobject/objecttypes/host/hoststate/hoststate.go @@ -107,7 +107,7 @@ func (h *HostState) UpdateValues() []interface{} { utils.Bool[h.IsFlapping], utils.Bool[time.Now().After(utils.MillisecsToTime(float64(h.NextUpdate)))], utils.IsAcknowledged[h.Acknowledgement], - utils.EncodeChecksum(h.AcknowledgementCommentId), + utils.EncodeChecksumOrNil(h.AcknowledgementCommentId), utils.Bool[h.InDowntime], h.ExecutionTime, h.Latency, diff --git a/configobject/objecttypes/hostgroup/hostgroup.go b/configobject/objecttypes/hostgroup/hostgroup.go index 51795154..e11754a2 100644 --- a/configobject/objecttypes/hostgroup/hostgroup.go +++ b/configobject/objecttypes/hostgroup/hostgroup.go @@ -57,7 +57,7 @@ func (h *Hostgroup) UpdateValues() []interface{} { h.Name, h.NameCi, h.DisplayName, - utils.EncodeChecksum(h.ZoneId), + utils.EncodeChecksumOrNil(h.ZoneId), ) return v diff --git a/configobject/objecttypes/notification/notification.go b/configobject/objecttypes/notification/notification.go index f1286060..2a870a7b 100644 --- a/configobject/objecttypes/notification/notification.go +++ b/configobject/objecttypes/notification/notification.go @@ -73,15 +73,15 @@ func (n *Notification) UpdateValues() []interface{} { n.Name, n.NameCi, utils.EncodeChecksum(n.HostId), - utils.EncodeChecksum(n.ServiceId), + utils.EncodeChecksumOrNil(n.ServiceId), utils.EncodeChecksum(n.CommandId), n.TimesBegin, n.TimesEnd, n.NotificationInterval, - utils.EncodeChecksum(n.PeriodId), + utils.EncodeChecksumOrNil(n.PeriodId), utils.NotificationStatesToBitMask(n.States), utils.NotificationTypesToBitMask(n.Types), - utils.EncodeChecksum(n.ZoneId), + utils.EncodeChecksumOrNil(n.ZoneId), ) return v diff --git a/configobject/objecttypes/notificationcommand/notificationcommand.go b/configobject/objecttypes/notificationcommand/notificationcommand.go index 1ac13554..90926375 100644 --- a/configobject/objecttypes/notificationcommand/notificationcommand.go +++ b/configobject/objecttypes/notificationcommand/notificationcommand.go @@ -58,7 +58,7 @@ func (c *NotificationCommand) UpdateValues() []interface{} { utils.EncodeChecksum(c.PropertiesChecksum), c.Name, c.NameCi, - utils.EncodeChecksum(c.ZoneId), + utils.EncodeChecksumOrNil(c.ZoneId), c.Command, c.Timeout, ) diff --git a/configobject/objecttypes/service/service.go b/configobject/objecttypes/service/service.go index 0bde534f..27ce13c1 100644 --- a/configobject/objecttypes/service/service.go +++ b/configobject/objecttypes/service/service.go @@ -118,7 +118,7 @@ func (s *Service) UpdateValues() []interface{} { utils.EncodeChecksum(s.CheckcommandId), s.MaxCheckAttempts, s.CheckPeriod, - utils.EncodeChecksum(s.CheckPeriodId), + utils.EncodeChecksumOrNil(s.CheckPeriodId), s.CheckTimeout, s.CheckInterval, s.CheckRetryInterval, @@ -131,17 +131,17 @@ func (s *Service) UpdateValues() []interface{} { s.FlappingThresholdHigh, utils.Bool[s.PerfdataEnabled], s.Eventcommand, - utils.EncodeChecksum(s.EventcommandId), + utils.EncodeChecksumOrNil(s.EventcommandId), utils.Bool[s.IsVolatile], - utils.EncodeChecksum(s.ActionUrlId), - utils.EncodeChecksum(s.NotesUrlId), + utils.EncodeChecksumOrNil(s.ActionUrlId), + utils.EncodeChecksumOrNil(s.NotesUrlId), s.Notes, - utils.EncodeChecksum(s.IconImageId), + utils.EncodeChecksumOrNil(s.IconImageId), s.IconImageAlt, s.Zone, - utils.EncodeChecksum(s.ZoneId), + utils.EncodeChecksumOrNil(s.ZoneId), s.CommandEndpoint, - utils.EncodeChecksum(s.CommandEndpointId), + utils.EncodeChecksumOrNil(s.CommandEndpointId), ) return v diff --git a/configobject/objecttypes/service/servicestate/servicestate.go b/configobject/objecttypes/service/servicestate/servicestate.go index d389ce34..f944d9c2 100644 --- a/configobject/objecttypes/service/servicestate/servicestate.go +++ b/configobject/objecttypes/service/servicestate/servicestate.go @@ -107,7 +107,7 @@ func (s *ServiceState) UpdateValues() []interface{} { utils.Bool[s.IsFlapping], utils.Bool[time.Now().After(utils.MillisecsToTime(float64(s.NextUpdate)))], utils.IsAcknowledged[s.Acknowledgement], - utils.EncodeChecksum(s.AcknowledgementCommentId), + utils.EncodeChecksumOrNil(s.AcknowledgementCommentId), utils.Bool[s.InDowntime], s.ExecutionTime, s.Latency, diff --git a/configobject/objecttypes/servicegroup/servicegroup.go b/configobject/objecttypes/servicegroup/servicegroup.go index e2862f55..9ee5e8a8 100644 --- a/configobject/objecttypes/servicegroup/servicegroup.go +++ b/configobject/objecttypes/servicegroup/servicegroup.go @@ -57,7 +57,7 @@ func (s *Servicegroup) UpdateValues() []interface{} { s.Name, s.NameCi, s.DisplayName, - utils.EncodeChecksum(s.ZoneId), + utils.EncodeChecksumOrNil(s.ZoneId), ) return v diff --git a/configobject/objecttypes/timeperiod/timeperiod.go b/configobject/objecttypes/timeperiod/timeperiod.go index 12301d33..845006e3 100644 --- a/configobject/objecttypes/timeperiod/timeperiod.go +++ b/configobject/objecttypes/timeperiod/timeperiod.go @@ -60,7 +60,7 @@ func (t *Timeperiod) UpdateValues() []interface{} { t.NameCi, t.DisplayName, utils.Bool[t.PreferIncludes], - utils.EncodeChecksum(t.ZoneId), + utils.EncodeChecksumOrNil(t.ZoneId), ) return v diff --git a/configobject/objecttypes/user/user.go b/configobject/objecttypes/user/user.go index be9cc603..34668f8b 100644 --- a/configobject/objecttypes/user/user.go +++ b/configobject/objecttypes/user/user.go @@ -72,10 +72,10 @@ func (u *User) UpdateValues() []interface{} { u.EMail, u.Pager, utils.Bool[u.NotificationsEnabled], - utils.EncodeChecksum(u.PeriodId), + utils.EncodeChecksumOrNil(u.PeriodId), utils.NotificationStatesToBitMask(u.States), utils.NotificationTypesToBitMask(u.Types), - utils.EncodeChecksum(u.ZoneId), + utils.EncodeChecksumOrNil(u.ZoneId), ) return v diff --git a/configobject/objecttypes/usergroup/usergroup.go b/configobject/objecttypes/usergroup/usergroup.go index 4e5f5cc0..4c080f62 100644 --- a/configobject/objecttypes/usergroup/usergroup.go +++ b/configobject/objecttypes/usergroup/usergroup.go @@ -57,7 +57,7 @@ func (u *Usergroup) UpdateValues() []interface{} { u.Name, u.NameCi, u.DisplayName, - utils.EncodeChecksum(u.ZoneId), + utils.EncodeChecksumOrNil(u.ZoneId), ) return v diff --git a/configobject/objecttypes/zone/zone.go b/configobject/objecttypes/zone/zone.go index fb01cc95..6dfbb8af 100644 --- a/configobject/objecttypes/zone/zone.go +++ b/configobject/objecttypes/zone/zone.go @@ -59,7 +59,7 @@ func (z *Zone) UpdateValues() []interface{} { z.Name, z.NameCi, utils.Bool[z.IsGlobal], - utils.EncodeChecksum(z.ParentId), + utils.EncodeChecksumOrNil(z.ParentId), z.Depth, ) diff --git a/ha/heartbeat.go b/ha/heartbeat.go index b78221ab..56260c7e 100644 --- a/ha/heartbeat.go +++ b/ha/heartbeat.go @@ -4,7 +4,6 @@ package ha import ( "crypto/sha1" - "encoding/hex" "encoding/json" "fmt" "github.com/Icinga/icingadb/connection" @@ -24,7 +23,7 @@ type Environment struct { type Icinga2Info struct { Version string ProgramStart float64 - EndpointId []byte + EndpointId interface{} NotificationsEnabled bool ActiveServiceChecksEnabled bool ActiveHostChecksEnabled bool @@ -107,11 +106,7 @@ func IcingaHeartbeatListener(rdb *connection.RDBWrapper, chEnv chan *Environment }, } - if app.EndpointId != "" { - if unHex, errHD := hex.DecodeString(app.EndpointId); errHD == nil { - env.Icinga2.EndpointId = unHex - } - } + env.Icinga2.EndpointId = utils.EncodeChecksumOrNil(app.EndpointId) chEnv <- env } diff --git a/utils/convert.go b/utils/convert.go index 2d6dd357..3bea36e6 100644 --- a/utils/convert.go +++ b/utils/convert.go @@ -73,6 +73,15 @@ func EncodeChecksum(s string) []byte { return c } +// EncodeChecksumOrNil converts a hex string to a byte array – or to nil if empty. +func EncodeChecksumOrNil(s string) interface{} { + if s == "" { + return nil + } + + return EncodeChecksum(s) +} + // DecodeHexIfNotNil converts a hex string to a byte array. func DecodeHexIfNotNil(hexStr interface{}) interface{} { if hexStr == nil {