Zabbix模板数据存储在哪里?
2024-10-18 10:33 潇湘隐者 阅读(174) 评论(0) 编辑 收藏 举报Zabbix的模板数据存储在数据库的哪一个表里面?以MySQL数据库为例,在数据库zabbix中,其实模板数据存储在hosts这个表里面,而不是存在hosts_templates表里面。很多人一看到templates关键字,容易先入为主的以为这个表会存储模板的相关数据。但是实际上,hosts_templates表用于存储主机和模板之间的关系。这个表允许一个主机与多个模板关联,对应实际情况中的主机配置多个模板,从而实现监控项、触发器和图形的继承。以下是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>

由于模板和主机都存储在hosts表中,hosts_templates表可以通过hostid或templateid与hosts表进行关联。这种设计允许灵活地管理主机和模板之间的关系,包括创建层次化的模板结构,其中父模板的设置可以被子模板继承。
下面我们来看一个简单的例子,服务器mysqlu01映射了两个模板,如下所示:

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>
上面例子可以很清楚的展示了模板与主机的关联关系。这里就不做过多阐述了。
另外一个问题,既然hosts中保存了主机和模板的数据,那么它通过哪一个字段来表示区分数据是模板数据还是主机数据呢? 其实它是通过hosts中的字段status来区分的。其中status 代表主机的状态:它有三个值,0 表示正常监控,1表示未被监控(disable状态), 3表示该主机是模板。
备注:不清楚是否有状态为2的记录。
我们可以验证一下,例如,在Zabbix的主页,它提示模板数量有315个,如下所示,那么

mysql> select count(*) from hosts where status=3;
+----------+
| count(*) |
+----------+
| 315 |
+----------+
1 row in set (0.00 sec)
mysql>

· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
2023-10-18 Linux下非oracle用户如何使用系统认证方式登陆数据
2023-10-18 TNS-12533: TNS:illegal ADDRESS parameters
2017-10-18 AutoAudit研究学习
2016-10-18 SQL Challenge ——快速找到1-100之间缺失的数