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>
复制代码 扫描上面二维码关注我假如你至心以为文章写得不错,而且对你有所资助,那就不妨帮忙“保举"一下,您的“保举”和”打赏“将是我最大的写作动力!本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面显着位置给出原文连接.
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |