Zabbix模板數(shù)據(jù)存儲(chǔ)在哪里?
2024-10-18 10:33 瀟湘隱者 閱讀(408) 評(píng)論(0) 收藏 舉報(bào)Zabbix的模板數(shù)據(jù)存儲(chǔ)在數(shù)據(jù)庫的哪一個(gè)表里面?以MySQL數(shù)據(jù)庫為例,在數(shù)據(jù)庫zabbix中,其實(shí)模板數(shù)據(jù)存儲(chǔ)在hosts這個(gè)表里面,而不是存在hosts_templates表里面。很多人一看到templates關(guān)鍵字,容易先入為主的以為這個(gè)表會(huì)存儲(chǔ)模板的相關(guān)數(shù)據(jù)。但是實(shí)際上,hosts_templates表用于存儲(chǔ)主機(jī)和模板之間的關(guān)系。這個(gè)表允許一個(gè)主機(jī)與多個(gè)模板關(guān)聯(lián),對(duì)應(yīng)實(shí)際情況中的主機(jī)配置多個(gè)模板,從而實(shí)現(xiàn)監(jiān)控項(xiàng)、觸發(fā)器和圖形的繼承。以下是hosts_templates表的一些重要字段及其描述:
mysql> desc hosts_templates;
+----------------+-----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-----------------+------+-----+---------+-------+
| hosttemplateid | bigint unsigned | NO | PRI | NULL | |
| hostid | bigint unsigned | NO | MUL | NULL | |
| templateid | bigint unsigned | NO | MUL | NULL | |
| link_type | int | NO | | 0 | |
+----------------+-----------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>
mysql> show create table hosts_templates\G
*************************** 1. row ***************************
Table: hosts_templates
Create Table: CREATE TABLE `hosts_templates` (
`hosttemplateid` bigint unsigned NOT NULL,
`hostid` bigint unsigned NOT NULL,
`templateid` bigint unsigned NOT NULL,
`link_type` int NOT NULL DEFAULT '0',
PRIMARY KEY (`hosttemplateid`),
UNIQUE KEY `hosts_templates_1` (`hostid`,`templateid`),
KEY `hosts_templates_2` (`templateid`),
CONSTRAINT `c_hosts_templates_1` FOREIGN KEY (`hostid`) REFERENCES `hosts` (`hostid`) ON DELETE CASCADE,
CONSTRAINT `c_hosts_templates_2` FOREIGN KEY (`templateid`) REFERENCES `hosts` (`hostid`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
1 row in set (0.00 sec)
mysql>

由于模板和主機(jī)都存儲(chǔ)在hosts表中,hosts_templates表可以通過hostid或templateid與hosts表進(jìn)行關(guān)聯(lián)。這種設(shè)計(jì)允許靈活地管理主機(jī)和模板之間的關(guān)系,包括創(chuàng)建層次化的模板結(jié)構(gòu),其中父模板的設(shè)置可以被子模板繼承。
下面我們來看一個(gè)簡單的例子,服務(wù)器mysqlu01映射了兩個(gè)模板,如下所示:

mysql> select hostid from hosts where name='mysqlu01';
+--------+
| hostid |
+--------+
| 10556 |
+--------+
1 row in set (0.00 sec)
mysql> select * from hosts_templates where hostid=10556;
+----------------+--------+------------+-----------+
| hosttemplateid | hostid | templateid | link_type |
+----------------+--------+------------+-----------+
| 460 | 10556 | 10001 | 0 |
| 461 | 10556 | 10316 | 0 |
+----------------+--------+------------+-----------+
2 rows in set (0.00 sec)
mysql> select name,host from hosts where hostid in(10001,10316);
+-----------------------+-----------------------+
| name | host |
+-----------------------+-----------------------+
| Linux by Zabbix agent | Linux by Zabbix agent |
| MySQL by Zabbix agent | MySQL by Zabbix agent |
+-----------------------+-----------------------+
2 rows in set (0.00 sec)
mysql>
上面例子可以很清楚的展示了模板與主機(jī)的關(guān)聯(lián)關(guān)系。這里就不做過多闡述了。
另外一個(gè)問題,既然hosts中保存了主機(jī)和模板的數(shù)據(jù),那么它通過哪一個(gè)字段來表示區(qū)分?jǐn)?shù)據(jù)是模板數(shù)據(jù)還是主機(jī)數(shù)據(jù)呢? 其實(shí)它是通過hosts中的字段status來區(qū)分的。其中status 代表主機(jī)的狀態(tài):它有三個(gè)值,0 表示正常監(jiān)控,1表示未被監(jiān)控(disable狀態(tài)), 3表示該主機(jī)是模板。
備注:不清楚是否有狀態(tài)為2的記錄。
我們可以驗(yàn)證一下,例如,在Zabbix的主頁,它提示模板數(shù)量有315個(gè),如下所示,那么

mysql> select count(*) from hosts where status=3;
+----------+
| count(*) |
+----------+
| 315 |
+----------+
1 row in set (0.00 sec)
mysql>
浙公網(wǎng)安備 33010602011771號(hào)