From 73dbb86426dc90dfe3a6840294f3556189bb4408 Mon Sep 17 00:00:00 2001 From: Jean Flach Date: Wed, 20 Mar 2019 11:06:31 +0100 Subject: [PATCH 1/4] Add service --- configobject/service/service.go | 174 ++++++++++++++++++++++++++++++++ main.go | 13 +++ 2 files changed, 187 insertions(+) create mode 100644 configobject/service/service.go diff --git a/configobject/service/service.go b/configobject/service/service.go new file mode 100644 index 00000000..355b0a57 --- /dev/null +++ b/configobject/service/service.go @@ -0,0 +1,174 @@ +package service + +import ( + "git.icinga.com/icingadb/icingadb-connection" + "git.icinga.com/icingadb/icingadb-main/configobject" + "git.icinga.com/icingadb/icingadb-utils" +) + +var ( + BulkInsertStmt *icingadb_connection.BulkInsertStmt + BulkDeleteStmt *icingadb_connection.BulkDeleteStmt + BulkUpdateStmt *icingadb_connection.BulkUpdateStmt + Fields = []string{ + "id", + "env_id", + "name_checksum", + "properties_checksum", + "customvars_checksum", + "groups_checksum", + "host_id", + "name", + "name_ci", + "display_name", + "checkcommand", + "checkcommand_id", + "max_check_attempts", + "check_period", + "check_period_id", + "check_timeout", + "check_interval", + "check_retry_interval", + "active_checks_enabled", + "passive_checks_enabled", + "event_handler_enabled", + "notifications_enabled", + "flapping_enabled", + "flapping_threshold_low", + "flapping_threshold_high", + "perfdata_enabled", + "eventcommand", + "eventcommand_id", + "is_volatile", + "action_url_id", + "notes_url_id", + "notes", + "icon_image_id", + "icon_image_alt", + "zone", + "zone_id", + "command_endpoint", + "command_endpoint_id", + } +) + +type Service struct { + Id string `json:"id"` + EnvId string `json:"env_id"` + NameChecksum string `json:"name_checksum"` + PropertiesChecksum string `json:"properties_checksum"` + CustomvarsChecksum string `json:"customvars_checksum"` + GroupsChecksum string `json:"groups_checksum"` + HostId string `json:"host_id"` + Name string `json:"name"` + NameCi *string `json:"name_ci"` + DisplayName string `json:"display_name"` + Checkcommand string `json:"checkcommand"` + CheckcommandId string `json:"checkcommand_id"` + MaxCheckAttempts float32 `json:"max_check_attempts"` + CheckPeriod string `json:"check_period"` + CheckPeriodId string `json:"check_period_id"` + CheckTimeout float32 `json:"check_timeout"` + CheckInterval float32 `json:"check_interval"` + CheckRetryInterval float32 `json:"check_retry_interval"` + ActiveChecksEnabled bool `json:"active_checks_enabled"` + PassiveChecksEnabled bool `json:"passive_checks_enabled"` + EventHandlerEnabled bool `json:"event_handler_enabled"` + NotificationsEnabled bool `json:"notifications_enabled"` + FlappingEnabled bool `json:"flapping_enabled"` + FlappingThresholdLow float32 `json:"flapping_threshold_low"` + FlappingThresholdHigh float32 `json:"flapping_threshold_high"` + PerfdataEnabled bool `json:"perfdata_enabled"` + Eventcommand string `json:"eventcommand"` + EventcommandId string `json:"eventcommand_id"` + IsVolatile bool `json:"is_volatile"` + ActionUrlId string `json:"action_url_id"` + NotesUrlId string `json:"notes_url_id"` + Notes string `json:"notes"` + IconImageId string `json:"icon_image_id"` + IconImageAlt string `json:"icon_image_alt"` + Zone string `json:"zone"` + ZoneId string `json:"zone_id"` + CommandEndpoint string `json:"command_endpoint"` + CommandEndpointId string `json:"command_endpoint_id"` +} + +func NewService() configobject.Row { + s := Service{ + EnvId: icingadb_utils.DecodeChecksum(icingadb_utils.Sha1("default")), + CheckPeriod: "check_period", + CheckPeriodId: icingadb_utils.DecodeChecksum(icingadb_utils.Sha1("check_period")), + Eventcommand: "event_command", + CommandEndpoint: "command_endpoint", + } + s.NameCi = &s.Name + + return &s +} + +func (s *Service) InsertValues() []interface{} { + v := s.UpdateValues() + + return append([]interface{}{icingadb_utils.Checksum(s.Id)}, v...) +} + +func (s *Service) UpdateValues() []interface{} { + v := make([]interface{}, 0) + + v = append( + v, + icingadb_utils.Checksum(s.EnvId), + icingadb_utils.Checksum(s.NameChecksum), + icingadb_utils.Checksum(s.PropertiesChecksum), + icingadb_utils.Checksum(s.CustomvarsChecksum), + icingadb_utils.Checksum(s.GroupsChecksum), + icingadb_utils.Checksum(s.HostId), + s.Name, + s.NameCi, + s.DisplayName, + s.Checkcommand, + icingadb_utils.Checksum(s.CheckcommandId), + s.MaxCheckAttempts, + s.CheckPeriod, + icingadb_utils.Checksum(s.CheckPeriodId), + s.CheckTimeout, + s.CheckInterval, + s.CheckRetryInterval, + icingadb_utils.Bool[s.ActiveChecksEnabled], + icingadb_utils.Bool[s.PassiveChecksEnabled], + icingadb_utils.Bool[s.EventHandlerEnabled], + icingadb_utils.Bool[s.NotificationsEnabled], + icingadb_utils.Bool[s.FlappingEnabled], + s.FlappingThresholdLow, + s.FlappingThresholdHigh, + icingadb_utils.Bool[s.PerfdataEnabled], + s.Eventcommand, + icingadb_utils.Checksum(s.EventcommandId), + icingadb_utils.Bool[s.IsVolatile], + icingadb_utils.Checksum(s.ActionUrlId), + icingadb_utils.Checksum(s.NotesUrlId), + s.Notes, + icingadb_utils.Checksum(s.IconImageId), + s.IconImageAlt, + s.Zone, + icingadb_utils.Checksum(s.ZoneId), + s.CommandEndpoint, + icingadb_utils.Checksum(s.CommandEndpointId), + ) + + return v +} + +func (s *Service) GetId() string { + return s.Id +} + +func (s *Service) SetId(id string) { + s.Id = id +} + +func init() { + BulkInsertStmt = icingadb_connection.NewBulkInsertStmt("service", Fields) + BulkDeleteStmt = icingadb_connection.NewBulkDeleteStmt("service") + BulkUpdateStmt = icingadb_connection.NewBulkUpdateStmt("service", Fields) +} \ No newline at end of file diff --git a/main.go b/main.go index d6be4aa3..918c0d02 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "git.icinga.com/icingadb/icingadb-json-decoder" "git.icinga.com/icingadb/icingadb-main/config" "git.icinga.com/icingadb/icingadb-main/configobject/host" + "git.icinga.com/icingadb/icingadb-main/configobject/service" "git.icinga.com/icingadb/icingadb-main/configobject/sync" "git.icinga.com/icingadb/icingadb-main/prometheus" "git.icinga.com/icingadb/icingadb-main/supervisor" @@ -61,6 +62,18 @@ func main() { }) }() + go func() { + chHA := ha.RegisterNotificationListener() + + super.ChErr <- sync.Operator(&super, chHA, &sync.Context{ + ObjectType: "service", + Factory: service.NewService, + InsertStmt: service.BulkInsertStmt, + DeleteStmt: service.BulkDeleteStmt, + UpdateStmt: service.BulkUpdateStmt, + }) + }() + go prometheus.HandleHttp("0.0.0.0:8080", super.ChErr) for { From 76ae76c367eb979390b2dc73a81d863fa7057f4a Mon Sep 17 00:00:00 2001 From: Jean Flach Date: Wed, 20 Mar 2019 11:31:34 +0100 Subject: [PATCH 2/4] Fix context startup --- main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 918c0d02..439a8207 100644 --- a/main.go +++ b/main.go @@ -51,9 +51,9 @@ func main() { go icingadb_json_decoder.DecodePool(super.ChDecode, super.ChErr, 16) - chHA := ha.RegisterNotificationListener() + chHAHost := ha.RegisterNotificationListener() go func() { - super.ChErr <- sync.Operator(&super, chHA, &sync.Context{ + super.ChErr <- sync.Operator(&super, chHAHost, &sync.Context{ ObjectType: "host", Factory: host.NewHost, InsertStmt: host.BulkInsertStmt, @@ -62,10 +62,10 @@ func main() { }) }() + chHAService := ha.RegisterNotificationListener() go func() { - chHA := ha.RegisterNotificationListener() - super.ChErr <- sync.Operator(&super, chHA, &sync.Context{ + super.ChErr <- sync.Operator(&super, chHAService, &sync.Context{ ObjectType: "service", Factory: service.NewService, InsertStmt: service.BulkInsertStmt, From 0517ea48dfefc6c8bc213525a185fed7eb07063d Mon Sep 17 00:00:00 2001 From: Jean Flach Date: Wed, 20 Mar 2019 15:55:51 +0100 Subject: [PATCH 3/4] Remove defaults --- configobject/host/host.go | 8 +------- configobject/service/service.go | 8 +------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/configobject/host/host.go b/configobject/host/host.go index fc6aea9a..9469569f 100644 --- a/configobject/host/host.go +++ b/configobject/host/host.go @@ -100,13 +100,7 @@ type Host struct { } func NewHost() configobject.Row { - h := Host{ - EnvId: icingadb_utils.DecodeChecksum(icingadb_utils.Sha1("default")), - CheckPeriod: "check_period", - CheckPeriodId: icingadb_utils.DecodeChecksum(icingadb_utils.Sha1("check_period")), - Eventcommand: "event_command", - CommandEndpoint: "command_endpoint", - } + h := Host{} h.NameCi = &h.Name return &h diff --git a/configobject/service/service.go b/configobject/service/service.go index 355b0a57..25839d7c 100644 --- a/configobject/service/service.go +++ b/configobject/service/service.go @@ -94,13 +94,7 @@ type Service struct { } func NewService() configobject.Row { - s := Service{ - EnvId: icingadb_utils.DecodeChecksum(icingadb_utils.Sha1("default")), - CheckPeriod: "check_period", - CheckPeriodId: icingadb_utils.DecodeChecksum(icingadb_utils.Sha1("check_period")), - Eventcommand: "event_command", - CommandEndpoint: "command_endpoint", - } + s := Service{} s.NameCi = &s.Name return &s From 2218652361e4c6f8ff130b58d10668900cd99d4d Mon Sep 17 00:00:00 2001 From: Jean Flach Date: Wed, 20 Mar 2019 15:56:16 +0100 Subject: [PATCH 4/4] Fix logging --- configobject/sync/sync.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/configobject/sync/sync.go b/configobject/sync/sync.go index 5d119c31..aaedf000 100644 --- a/configobject/sync/sync.go +++ b/configobject/sync/sync.go @@ -105,7 +105,7 @@ func Operator(super *supervisor.Supervisor, chHA chan int, ctx *Context) error { benchmarc.Stop() log.WithFields(log.Fields{ "type": ctx.ObjectType, - "count": len(insert), + "count": len(delete), "benchmark": benchmarc.String(), "action": "delete", }).Infof("Deleted %v %ss in %v", len(delete), ctx.ObjectType, benchmarc.String()) @@ -119,7 +119,7 @@ func Operator(super *supervisor.Supervisor, chHA chan int, ctx *Context) error { benchmarc.Stop() log.WithFields(log.Fields{ "type": ctx.ObjectType, - "count": len(insert), + "count": atomic.LoadUint32(updateCounter), "benchmark": benchmarc.String(), "action": "update", }).Infof("Updated %v %ss in %v", atomic.LoadUint32(updateCounter), ctx.ObjectType, benchmarc.String())