Improve state sync

This commit is contained in:
Noah Hilverling 2019-09-17 11:18:01 +02:00
parent 041c27dd13
commit 34b75b4f19
7 changed files with 304 additions and 8 deletions

View file

@ -0,0 +1,142 @@
package hoststate
import (
"git.icinga.com/icingadb/icingadb-main/configobject"
"git.icinga.com/icingadb/icingadb-main/connection"
"git.icinga.com/icingadb/icingadb-main/utils"
)
var (
ObjectInformation configobject.ObjectInformation
Fields = []string{
"id",
"env_id",
"state_type",
"soft_state",
"hard_state",
"attempt",
"severity",
"output",
"long_output",
"performance_data",
"check_commandline",
"is_problem",
"is_handled",
"is_reachable",
"is_flapping",
"is_acknowledged",
"acknowledgement_comment_id",
"in_downtime",
"execution_time",
"latency",
"timeout",
"last_update",
"last_state_change",
"last_soft_state",
"last_hard_state",
"next_check",
}
)
type HostState struct {
Id string `json:"id"`
EnvId string `json:"env_id"`
StateType float32 `json:"state_type"`
SoftState float32 `json:"state"`
HardState float32 `json:"last_hard_state"`
Attempt float32 `json:"check_attempt"`
Severity float32 `json:"severity"`
Output string `json:"output"`
LongOutput string `json:"long_output"`
PerformanceData string `json:"performance_data"`
CheckCommandline string `json:"commandline"`
IsProblem bool `json:"is_problem"`
IsHandled bool `json:"is_handled"`
IsReachable bool `json:"is_reachable"`
IsFlapping bool `json:"is_flapping"`
IsAcknowledged bool `json:"is_acknowledged"`
AcknowledgementCommentId string `json:"acknowledgement_comment_id"`
InDowntime bool `json:"in_downtime"`
ExecutionTime float32 `json:"execution_time"`
Latency float32 `json:"latency"`
Timeout float32 `json:"check_timeout"`
LastUpdate float32 `json:"last_update"`
LastStateChange float32 `json:"last_state_change"`
LastSoftState float32 `json:"last_soft_state"`
LastHardState float32 `json:"last_hard_state"`
NextCheck float32 `json:"next_check"`
}
func NewHostState() connection.Row {
h := HostState{}
return &h
}
func (h *HostState) InsertValues() []interface{} {
v := h.UpdateValues()
return append([]interface{}{utils.Checksum(h.Id)}, v...)
}
func (h *HostState) UpdateValues() []interface{} {
v := make([]interface{}, 0)
v = append(
v,
utils.Checksum(h.EnvId),
utils.IcingaStateTypeToString(h.StateType),
h.SoftState,
h.HardState,
h.Attempt,
h.Severity,
h.Output,
h.LongOutput,
h.PerformanceData,
h.CheckCommandline,
utils.Bool[h.IsProblem],
utils.Bool[h.IsHandled],
utils.Bool[h.IsReachable],
utils.Bool[h.IsFlapping],
utils.Bool[h.IsAcknowledged],
utils.Checksum(h.AcknowledgementCommentId),
utils.Bool[h.InDowntime],
h.ExecutionTime,
h.Latency,
h.Timeout,
h.LastUpdate,
h.LastStateChange,
h.LastSoftState,
h.LastHardState,
h.NextCheck,
)
return v
}
func (h *HostState) GetId() string {
return h.Id
}
func (h *HostState) SetId(id string) {
h.Id = id
}
func (h *HostState) GetFinalRows() ([]connection.Row, error) {
return []connection.Row{h}, nil
}
func init() {
name := "host_state"
ObjectInformation = configobject.ObjectInformation{
ObjectType: name,
RedisKey: "state:host",
DeltaMySqlField: "id",
Factory: NewHostState,
HasChecksum: false,
BulkInsertStmt: connection.NewBulkInsertStmt(name, Fields),
BulkDeleteStmt: connection.NewBulkDeleteStmt(name),
BulkUpdateStmt: connection.NewBulkUpdateStmt(name, Fields),
NotificationListenerType: "host",
}
}

View file

@ -0,0 +1,142 @@
package servicestate
import (
"git.icinga.com/icingadb/icingadb-main/configobject"
"git.icinga.com/icingadb/icingadb-main/connection"
"git.icinga.com/icingadb/icingadb-main/utils"
)
var (
ObjectInformation configobject.ObjectInformation
Fields = []string{
"id",
"env_id",
"state_type",
"soft_state",
"hard_state",
"attempt",
"severity",
"output",
"long_output",
"performance_data",
"check_commandline",
"is_problem",
"is_handled",
"is_reachable",
"is_flapping",
"is_acknowledged",
"acknowledgement_comment_id",
"in_downtime",
"execution_time",
"latency",
"timeout",
"last_update",
"last_state_change",
"last_soft_state",
"last_hard_state",
"next_check",
}
)
type ServiceState struct {
Id string `json:"id"`
EnvId string `json:"env_id"`
StateType float32 `json:"state_type"`
SoftState float32 `json:"state"`
HardState float32 `json:"last_hard_state"`
Attempt float32 `json:"check_attempt"`
Severity float32 `json:"severity"`
Output string `json:"output"`
LongOutput string `json:"long_output"`
PerformanceData string `json:"performance_data"`
CheckCommandline string `json:"commandline"`
IsProblem bool `json:"is_problem"`
IsHandled bool `json:"is_handled"`
IsReachable bool `json:"is_reachable"`
IsFlapping bool `json:"is_flapping"`
IsAcknowledged bool `json:"is_acknowledged"`
AcknowledgementCommentId string `json:"acknowledgement_comment_id"`
InDowntime bool `json:"in_downtime"`
ExecutionTime float32 `json:"execution_time"`
Latency float32 `json:"latency"`
Timeout float32 `json:"check_timeout"`
LastUpdate float32 `json:"last_update"`
LastStateChange float32 `json:"last_state_change"`
LastSoftState float32 `json:"last_soft_state"`
LastHardState float32 `json:"last_hard_state"`
NextCheck float32 `json:"next_check"`
}
func NewServiceState() connection.Row {
s := ServiceState{}
return &s
}
func (s *ServiceState) InsertValues() []interface{} {
v := s.UpdateValues()
return append([]interface{}{utils.Checksum(s.Id)}, v...)
}
func (s *ServiceState) UpdateValues() []interface{} {
v := make([]interface{}, 0)
v = append(
v,
utils.Checksum(s.EnvId),
utils.IcingaStateTypeToString(s.StateType),
s.SoftState,
s.HardState,
s.Attempt,
s.Severity,
s.Output,
s.LongOutput,
s.PerformanceData,
s.CheckCommandline,
utils.Bool[s.IsProblem],
utils.Bool[s.IsHandled],
utils.Bool[s.IsReachable],
utils.Bool[s.IsFlapping],
utils.Bool[s.IsAcknowledged],
utils.Checksum(s.AcknowledgementCommentId),
utils.Bool[s.InDowntime],
s.ExecutionTime,
s.Latency,
s.Timeout,
s.LastUpdate,
s.LastStateChange,
s.LastSoftState,
s.LastHardState,
s.NextCheck,
)
return v
}
func (s *ServiceState) GetId() string {
return s.Id
}
func (s *ServiceState) SetId(id string) {
s.Id = id
}
func (s *ServiceState) GetFinalRows() ([]connection.Row, error) {
return []connection.Row{s}, nil
}
func init() {
name := "service_state"
ObjectInformation = configobject.ObjectInformation{
ObjectType: name,
RedisKey: "state:service",
DeltaMySqlField: "id",
Factory: NewServiceState,
HasChecksum: false,
BulkInsertStmt: connection.NewBulkInsertStmt(name, Fields),
BulkDeleteStmt: connection.NewBulkDeleteStmt(name),
BulkUpdateStmt: connection.NewBulkUpdateStmt(name, Fields),
NotificationListenerType: "service",
}
}

View file

@ -75,7 +75,7 @@ func syncStates(super *supervisor.Supervisor, objectType string) {
_, errExec := super.Dbw.SqlExecTx(
tx,
"replace into "+objectType+"_state",
`REPLACE INTO `+objectType+`_state (`+objectType+`_id, env_id, state_type, soft_state, hard_state, attempt, severity, output, long_output, performance_data,`+
`REPLACE INTO `+objectType+`_state (id, env_id, state_type, soft_state, hard_state, attempt, severity, output, long_output, performance_data,`+
`check_commandline, is_problem, is_handled, is_reachable, is_flapping, is_acknowledged, acknowledgement_comment_id,`+
`in_downtime, execution_time, latency, timeout, last_update, last_state_change, last_soft_state,`+
`last_hard_state, next_check) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
@ -100,10 +100,10 @@ func syncStates(super *supervisor.Supervisor, objectType string) {
values["execution_time"],
redisIntToDBInt(values["latency"]),
redisIntToDBInt(values["check_timeout"]),
values["last_state_change"], //TODO: ??? (also see MR) should be last_update
values["last_update"],
values["last_state_change"],
values["state"], //TODO: Should be last_soft_state but is not implemented in core
values["last_hard_state"], //TODO: Should be last_hard_state but is not implemented in core
values["last_soft_state"],
values["last_hard_state"],
values["next_check"],
)

View file

@ -108,7 +108,7 @@ CREATE TABLE hostgroup_customvar (
) ENGINE=InnoDb ROW_FORMAT=DYNAMIC DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_bin;
CREATE TABLE host_state (
host_id binary(20) NOT NULL COMMENT 'host.id',
id binary(20) NOT NULL COMMENT 'host.id',
env_id binary(20) NOT NULL COMMENT 'sha1(environment.name)',
state_type enum('hard', 'soft') NOT NULL,
@ -142,5 +142,5 @@ CREATE TABLE host_state (
last_hard_state tinyint(1) unsigned NOT NULL,
next_check bigint(20) unsigned NOT NULL,
PRIMARY KEY (host_id)
PRIMARY KEY (id)
) ENGINE=InnoDb ROW_FORMAT=DYNAMIC DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_bin;

View file

@ -101,7 +101,7 @@ CREATE TABLE servicegroup_customvar (
) ENGINE=InnoDb ROW_FORMAT=DYNAMIC DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_bin;
CREATE TABLE service_state (
service_id binary(20) NOT NULL COMMENT 'service.id',
id binary(20) NOT NULL COMMENT 'service.id',
env_id binary(20) NOT NULL COMMENT 'sha1(environment.name)',
state_type enum('hard', 'soft') NOT NULL,
@ -135,5 +135,5 @@ CREATE TABLE service_state (
last_hard_state tinyint(1) unsigned NOT NULL,
next_check bigint(20) unsigned NOT NULL,
PRIMARY KEY (service_id)
PRIMARY KEY (id)
) ENGINE=InnoDb ROW_FORMAT=DYNAMIC DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_bin;

View file

@ -21,6 +21,7 @@ import (
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/host/hostcomment"
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/host/hostcustomvar"
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/host/hostdowntime"
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/host/hoststate"
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/hostgroup"
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/hostgroup/hostgroupcustomvar"
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/hostgroup/hostgroupmember"
@ -38,6 +39,7 @@ import (
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/service/servicecomment"
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/service/servicecustomvar"
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/service/servicedowntime"
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/service/servicestate"
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/servicegroup"
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/servicegroup/servicegroupcustomvar"
"git.icinga.com/icingadb/icingadb-main/configobject/objecttypes/servicegroup/servicegroupmember"
@ -128,11 +130,13 @@ func startConfigSyncOperators(super *supervisor.Supervisor, haInstance *ha.HA) {
&hostcustomvar.ObjectInformation,
&hostdowntime.ObjectInformation,
&hostcomment.ObjectInformation,
&hoststate.ObjectInformation,
&service.ObjectInformation,
&servicecustomvar.ObjectInformation,
&servicedowntime.ObjectInformation,
&servicecomment.ObjectInformation,
&servicestate.ObjectInformation,
&hostgroup.ObjectInformation,
&hostgroupcustomvar.ObjectInformation,

View file

@ -70,4 +70,12 @@ func NotificationTypesToBitMask(types []string) int {
mask += NotificationTypes[t]
}
return mask
}
func IcingaStateTypeToString(stateType float32) string {
if stateType == 0 {
return "hard"
} else {
return "soft"
}
}