Hi,
I made some changes to the database, and would love to get some feedback.
I don't think they could cause any issue, but it's always good to have a second opinion.
I've installed zabbix with mariadb, and I have been asked to present a dashboard outside zabbix with one host per row and the items in columns.
Because of the way the history tables are linked to the items table linked to the hosts table, and the impossibility to get the last value based on the clock without another sub join with the history tables again, I came up with this solution:
any thoughts?
I made some changes to the database, and would love to get some feedback.
I don't think they could cause any issue, but it's always good to have a second opinion.
I've installed zabbix with mariadb, and I have been asked to present a dashboard outside zabbix with one host per row and the items in columns.
Because of the way the history tables are linked to the items table linked to the hosts table, and the impossibility to get the last value based on the clock without another sub join with the history tables again, I came up with this solution:
Code:
CREATE TABLE `__items_current_value` (
`itemid` bigint(20) unsigned NOT NULL,
`hostid` bigint(20) unsigned NOT NULL,
`name` varchar(255) NOT NULL DEFAULT '',
`key_` varchar(2048) NOT NULL DEFAULT '',
`clock` int(11) NOT NULL DEFAULT 0,
`value` text NOT NULL,
PRIMARY KEY (`itemid`),
KEY `items_1` (`hostid`,`key_`(764)),
KEY `items_8` (`key_`(768))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
CREATE TRIGGER items_insert_mirror
AFTER INSERT ON items
FOR EACH ROW
INSERT INTO __items_current_value
SET itemid = NEW.itemid,
hostid = NEW.hostid,
name = NEW.name,
key_ = NEW.key_;
CREATE TRIGGER items_update_mirror
AFTER UPDATE ON items
FOR EACH ROW
UPDATE __items_current_value
SET itemid = NEW.itemid,
hostid = NEW.hostid,
name = NEW.name,
key_ = NEW.key_
WHERE itemid = OLD.itemid;
CREATE TRIGGER items_delete_mirror
AFTER DELETE ON items
FOR EACH ROW
DELETE FROM __items_current_value
WHERE itemid = OLD.itemid;
CREATE TRIGGER history_insert_mirror
AFTER INSERT ON history
FOR EACH ROW
UPDATE __items_current_value
SET clock = NEW.clock,
value = NEW.value
WHERE itemid = NEW.itemid;
CREATE TRIGGER history_bin_insert_mirror
AFTER INSERT ON history_bin
FOR EACH ROW
UPDATE __items_current_value
SET clock = NEW.clock,
value = NEW.value
WHERE itemid = NEW.itemid;
CREATE TRIGGER history_log_insert_mirror
AFTER INSERT ON history_log
FOR EACH ROW
UPDATE __items_current_value
SET clock = NEW.clock,
value = NEW.value
WHERE itemid = NEW.itemid;
CREATE TRIGGER history_str_insert_mirror
AFTER INSERT ON history_str
FOR EACH ROW
UPDATE __items_current_value
SET clock = NEW.clock,
value = NEW.value
WHERE itemid = NEW.itemid;
CREATE TRIGGER history_text_insert_mirror
AFTER INSERT ON history_text
FOR EACH ROW
UPDATE __items_current_value
SET clock = NEW.clock,
value = NEW.value
WHERE itemid = NEW.itemid;
CREATE TRIGGER history_uint_insert_mirror
AFTER INSERT ON history_uint
FOR EACH ROW
UPDATE __items_current_value
SET clock = NEW.clock,
value = NEW.value
WHERE itemid = NEW.itemid;