diff --git a/configobject/history/history.go b/configobject/history/history.go index a1ba3af4..b59d054a 100644 --- a/configobject/history/history.go +++ b/configobject/history/history.go @@ -391,9 +391,15 @@ func commentHistoryWorker(super *supervisor.Supervisor) { func flappingHistoryWorker(super *supervisor.Supervisor) { statements := []string{ - `REPLACE INTO flapping_history (id, environment_id, endpoint_id, object_type, host_id, service_id, event_time,` + + `INSERT INTO flapping_history (id, environment_id, endpoint_id, object_type, host_id, service_id, start_time, end_time, ` + `percent_state_change, flapping_threshold_low, flapping_threshold_high)` + - `VALUES (?,?,?,?,?,?,?,?,?,?)`, + `VALUES (?,?,?,?,?,?,?,?,?,?,?)` + + `ON DUPLICATE KEY UPDATE ` + + `start_time=IFNULL(NULLIF(start_time, 0), VALUES(start_time)),` + + `end_time=IFNULL(NULLIF(end_time, 0), VALUES(end_time)),` + + `percent_state_change=IFNULL(NULLIF(percent_state_change, 0), VALUES(percent_state_change)),` + + `flapping_threshold_low=IFNULL(NULLIF(flapping_threshold_low, 0), VALUES(flapping_threshold_low)),` + + `flapping_threshold_high=IFNULL(NULLIF(flapping_threshold_high, 0), VALUES(flapping_threshold_high))`, `REPLACE INTO history (id, environment_id, endpoint_id, object_type, host_id, service_id, notification_history_id,` + `state_history_id, downtime_history_id, comment_history_id, flapping_history_id, event_type, event_time)` + `VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)`, @@ -401,15 +407,15 @@ func flappingHistoryWorker(super *supervisor.Supervisor) { dataFunctions := []func(values map[string]interface{}) []interface{}{ func(values map[string]interface{}) []interface{} { - id := uuid.MustParse(values["id"].(string)) data := []interface{}{ - id[:], + utils.EncodeChecksum(values["id"].(string)), super.EnvId, utils.DecodeHexIfNotNil(values["endpoint_id"]), values["object_type"].(string), utils.EncodeChecksum(values["host_id"].(string)), utils.DecodeHexIfNotNil(values["service_id"]), - values["event_time"], + values["start_time"], + values["end_time"], values["percent_state_change"], values["flapping_threshold_low"], values["flapping_threshold_high"], @@ -419,7 +425,6 @@ func flappingHistoryWorker(super *supervisor.Supervisor) { }, func(values map[string]interface{}) []interface{} { eventId := uuid.MustParse(values["event_id"].(string)) - flappingHistoryId := uuid.MustParse(values["id"].(string)) var eventTime string switch values["event_type"] { @@ -440,7 +445,7 @@ func flappingHistoryWorker(super *supervisor.Supervisor) { nil, nil, nil, - flappingHistoryId[:], + utils.EncodeChecksum(values["id"].(string)), values["event_type"], eventTime, } @@ -454,9 +459,18 @@ func flappingHistoryWorker(super *supervisor.Supervisor) { func acknowledgementHistoryWorker(super *supervisor.Supervisor) { statements := []string{ - `REPLACE INTO acknowledgement_history (id, environment_id, endpoint_id, object_type, host_id, service_id, event_time,` + - `author, comment, expire_time, is_sticky, is_persistent)` + - `VALUES (?,?,?,?,?,?,?,?,?,?,?,?)`, + `INSERT INTO acknowledgement_history (id, environment_id, endpoint_id, object_type, host_id, service_id, set_time, clear_time,` + + `author, cleared_by, comment, expire_time, is_sticky, is_persistent)` + + `VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)` + + `ON DUPLICATE KEY UPDATE ` + + `set_time=IFNULL(NULLIF(set_time, 0), VALUES(set_time)),` + + `clear_time=IFNULL(NULLIF(clear_time, 0), VALUES(clear_time)),` + + `author=IFNULL(NULLIF(author, ''), VALUES(author)),` + + `cleared_by=IFNULL(NULLIF(cleared_by, ''), VALUES(cleared_by)),` + + `comment=IFNULL(NULLIF(comment, ''), VALUES(comment)),` + + `expire_time=IFNULL(NULLIF(expire_time, 0), VALUES(expire_time)),` + + `is_sticky=IFNULL(NULLIF(is_sticky, 'n'), VALUES(is_sticky)),` + + `is_persistent=IFNULL(NULLIF(is_persistent, 'n'), VALUES(is_persistent))`, `REPLACE INTO history (id, environment_id, endpoint_id, object_type, host_id, service_id,` + ` acknowledgement_history_id, event_type, event_time)` + `VALUES (?,?,?,?,?,?,?,?,?)`, @@ -464,17 +478,18 @@ func acknowledgementHistoryWorker(super *supervisor.Supervisor) { dataFunctions := []func(values map[string]interface{}) []interface{}{ func(values map[string]interface{}) []interface{} { - id := uuid.MustParse(values["id"].(string)) data := []interface{}{ - id[:], + utils.EncodeChecksum(values["id"].(string)), super.EnvId, utils.DecodeHexIfNotNil(values["endpoint_id"]), values["object_type"].(string), utils.EncodeChecksum(values["host_id"].(string)), utils.DecodeHexIfNotNil(values["service_id"]), - values["event_time"], - values["author"], - values["comment"], + values["set_time"], + values["clear_time"], + utils.DefaultIfNil(values["author"], ""), + values["cleared_by"], + utils.DefaultIfNil(values["comment"], ""), values["expire_time"], utils.RedisIntToDBBoolean(values["is_sticky"]), utils.RedisIntToDBBoolean(values["is_persistent"]), @@ -483,18 +498,26 @@ func acknowledgementHistoryWorker(super *supervisor.Supervisor) { return data }, func(values map[string]interface{}) []interface{} { - id := uuid.MustParse(values["id"].(string)) + eventId := uuid.MustParse(values["event_id"].(string)) + + var eventTime string + switch values["event_type"] { + case "ack_set": + eventTime = values["set_time"].(string) + case "ack_clear": + eventTime = values["clear_time"].(string) + } data := []interface{}{ - id[:], + eventId[:], super.EnvId, utils.DecodeHexIfNotNil(values["endpoint_id"]), values["object_type"].(string), utils.EncodeChecksum(values["host_id"].(string)), utils.DecodeHexIfNotNil(values["service_id"]), - id[:], + utils.EncodeChecksum(values["id"].(string)), values["event_type"], - values["event_time"], + eventTime, } return data diff --git a/etc/schema/mysql/mysql.schema.sql b/etc/schema/mysql/mysql.schema.sql index 4345133e..d4e93489 100644 --- a/etc/schema/mysql/mysql.schema.sql +++ b/etc/schema/mysql/mysql.schema.sql @@ -912,14 +912,15 @@ CREATE TABLE comment_history ( ) ENGINE=InnoDb ROW_FORMAT=DYNAMIC DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_bin; CREATE TABLE flapping_history ( - id binary(16) NOT NULL COMMENT 'UUID', + id binary(20) NOT NULL COMMENT 'sha1(environment.name + "host"|"service" + host|service.name + start_time)', environment_id binary(20) NOT NULL COMMENT 'environment.id', endpoint_id binary(20) NULL DEFAULT NULL COMMENT 'endpoint.id', object_type enum('host', 'service') NOT NULL, host_id binary(20) NOT NULL COMMENT 'host.id', service_id binary(20) NULL DEFAULT NULL COMMENT 'service.id', - event_time bigint(20) unsigned NOT NULL, + start_time bigint(20) unsigned NOT NULL, + end_time bigint(20) unsigned NULL DEFAULT NULL, percent_state_change float unsigned NOT NULL, flapping_threshold_low float unsigned NOT NULL, flapping_threshold_high float unsigned NOT NULL, @@ -928,25 +929,27 @@ CREATE TABLE flapping_history ( ) ENGINE=InnoDb ROW_FORMAT=DYNAMIC DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_bin; CREATE TABLE acknowledgement_history ( - id binary(16) NOT NULL COMMENT 'UUID', + id binary(20) NOT NULL COMMENT 'sha1(environment.name + "host"|"service" + host|service.name + set_time)', environment_id binary(20) NOT NULL COMMENT 'environment.id', endpoint_id binary(20) NULL DEFAULT NULL COMMENT 'endpoint.id', object_type enum('host', 'service') NOT NULL, host_id binary(20) NOT NULL COMMENT 'host.id', service_id binary(20) NULL DEFAULT NULL COMMENT 'service.id', - event_time bigint(20) unsigned NOT NULL, + set_time bigint(20) unsigned NOT NULL, + clear_time bigint(20) unsigned NULL DEFAULT NULL, author varchar(255) NOT NULL COLLATE utf8mb4_unicode_ci, + cleared_by varchar(255) DEFAULT NULL COLLATE utf8mb4_unicode_ci, comment text DEFAULT NULL, expire_time bigint(20) unsigned DEFAULT NULL, - is_sticky enum('y','n') DEFAULT NULL, - is_persistent enum('y','n') DEFAULT NULL, + is_sticky enum('y','n') NOT NULL, + is_persistent enum('y','n') NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDb ROW_FORMAT=DYNAMIC DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_bin; CREATE TABLE history ( - id binary(16) NOT NULL COMMENT 'notification_history_id, state_history_id, flapping_history_id, acknowledgement_history_id or UUID', + id binary(16) NOT NULL COMMENT 'notification_history_id, state_history_id or UUID', environment_id binary(20) NOT NULL COMMENT 'environment.id', endpoint_id binary(20) NULL DEFAULT NULL COMMENT 'endpoint.id', object_type enum('host', 'service') NOT NULL, @@ -956,8 +959,8 @@ CREATE TABLE history ( state_history_id binary(16) NULL DEFAULT NULL COMMENT 'state_history.id', downtime_history_id binary(20) NULL DEFAULT NULL COMMENT 'downtime_history.downtime_id', comment_history_id binary(20) NULL DEFAULT NULL COMMENT 'comment_history.comment_id', - flapping_history_id binary(16) NULL DEFAULT NULL COMMENT 'flapping_history.id', - acknowledgement_history_id binary(16) NULL DEFAULT NULL COMMENT 'acknowledgement_history.id', + flapping_history_id binary(20) NULL DEFAULT NULL COMMENT 'flapping_history.id', + acknowledgement_history_id binary(20) NULL DEFAULT NULL COMMENT 'acknowledgement_history.id', event_type enum('notification','state_change','downtime_schedule','downtime_start', 'downtime_end','comment_add','comment_remove','flapping_start','flapping_end','ack_set','ack_clear') NOT NULL, event_time bigint(20) unsigned NOT NULL, diff --git a/utils/convert.go b/utils/convert.go index d4ca2eed..ed1de9fe 100644 --- a/utils/convert.go +++ b/utils/convert.go @@ -144,3 +144,12 @@ func TimeToFloat(t time.Time) float64 { secs := t.Unix() return float64(secs) + float64(t.Sub(time.Unix(secs, 0)))/float64(time.Second) } + +// DefaultIfNil returns a defaultValue, if the given value is nil +func DefaultIfNil(value, defaultValue interface{}) interface{} { + if value != nil { + return value + } else { + return defaultValue + } +} \ No newline at end of file