从zabbix数据表获取服务器信息
创始人
2024-09-25 01:25:40
0

本次主要使用的数据表

  • hosts:主机,所有服务器都存在这张表里面。(关键字段:hostid,也就是主机id)
  • items:监控项目。(关键字段:itemid、hostid。通过hostid与主机表关联)
  • history:监控记录历史表。想要取最新的数据,只需按时间排序,取第一条数据即可。
  • history_uint:这表也是存储历史数据,通过表名可以推测出,这种表只存储无符号整型的数据(说人话就是不存字符串,只存数值),例如:CPU的核心数、硬盘总容量(字节)。
  • history_str:历史数据(字符串),从表名可以推测出这张表只存字符串数值,例如:操作系统名称。
  • events:事件,存储一些告警事件,比如磁盘读写速度太高等。(非常坑的设计,需要关联好几张表才能取到服务器名称)
  • triggers、functions表:这里就不多说了,主要是事件表需要关联这两张表、再关联监控项目获取服务器名称。

脚本示例

此处我有一台服务器,hostid为:10613

select * from hosts where hostid = '10613'

查询结果

查询服务器最近一次的CPU使用率情况

-- 查询CPU使用率 select * from history where itemid in ( 	select itemid from items where name = 'CPU utilization' and hostid = '10613' ) order by clock desc limit 1

查询结果:

查询最近的一次内存使用率

-- 查询内存使用率 select * from history where itemid in ( 	select itemid from items where name = 'Memory utilization' and hostid = '10613' ) order by clock desc limit 1 

查询cpu核数
 

-- 查询cpu核数 select * from history_uint a where itemid in  ( 	select itemid from items where name = 'Number of CPUs' and hostid = '10613' ) order by clock desc limit 1

 查询oracle 总存储空间

这个SQL语句其实就是查询linux服务器oracle硬盘分区的总空间,如果你的oracle分区不叫oralce,则改成对应的分区名称即可。

-- 查询oracle 总存储空间 select *  from history_uint  where itemid in  ( 	select itemid from items where name = '/oracle: Total space' and hostid = '10613' ) order by clock desc limit 1

 查询多个硬盘分区的空间大小


此处需要注意的是,zabbix只识别到了三个硬盘分区,boot、home、oracle,其他分区并未识别到。

-- 查询多个硬盘分区的空间大小 SELECT 	*  FROM 	( 	SELECT 		hu.*, 		it.NAME, 		ROW_NUMBER() OVER ( PARTITION BY it.NAME ORDER BY clock DESC ) AS row_num  	FROM 		history_uint hu 		LEFT JOIN items it ON it.itemid = hu.itemid  	WHERE 		it.NAME LIKE '% Total space'  		AND it.hostid = '10613'  	) tmp  WHERE 	row_num = 1

查询多个硬盘分区已用空间大小
 

-- 查询多个硬盘分区已用空间大小 select * from ( select hu.*,it.name,ROW_NUMBER() OVER(PARTITION BY it.name ORDER BY clock desc) AS row_num from history_uint hu  left join items it on it.itemid = hu.itemid where it.name like '% Used space' and it.hostid = '10613' ) tmp where row_num = 1

查询内存大小

-- 查询内存大小 select *  from history_uint  where itemid in  ( 	select itemid from items where name = 'Total memory' and hostid = '10613' ) order by clock desc limit 1

查询下载网速


此处需要注意的是,可能存在多张网卡了。如果有多个网卡,则需要根据zabbix中监控到的网卡名称,修改like条件。

-- 查询下载网速 select *  from history_uint  where itemid in  ( 	select itemid from items where name like '%Bits received%' and hostid = '10613' ) order by clock desc limit 1

查询操作系统
 

-- 查询操作系统  select * from history_str a where itemid in ( 		select itemid from items where name = 'Operating system' and hostid = '10613' ) order by clock desc limit 1

查询操作系统架构
 

-- 查询操作系统架构  select * from history_str a where itemid in ( 		select itemid from items where name = 'Operating system architecture' and hostid = '10613' ) order by clock desc limit 1

 查询事件记录,关联主机


这表events需要拐弯抹角关联好几次,才能获取到服务器名称。

-- 查询事件记录,关联主机 select   a.*,h.name  as "hosts.name"  from events a   LEFT JOIN triggers tri on a.objectid = tri.triggerid left join (select * from ( SELECT     *,     ROW_NUMBER() OVER(PARTITION BY triggerid ORDER BY functionid asc) AS row_num FROM functions ) f where row_num = 1  ) fun on fun.triggerid = a.objectid left join items it on it.itemid = fun.itemid left join hosts h on h.hostid = it.hostid 

注意事项

此示例为获取linux操作系统服务器信息,若是windows系统,监控项的名称会略有不同,但原理是一样的。

zabbix的模式是定时上报服务器状态,所有数据大约都会有1分钟的延迟,所以不可能获取到实时数据(个人见解,如有错误请指正)。

相关内容

热门资讯

第五分钟了解!新青鸟必胜(辅助... 第五分钟了解!新青鸟必胜(辅助)决胜山西麻将开挂辅助工具-总是存在有修改器1、超多福利:超高返利,海...
第十分钟了解!朱雀开心罗松开挂... 第十分钟了解!朱雀开心罗松开挂(辅助)大神棋牌开挂辅助软件-总是是有修改器1、每一步都需要思考,不同...
第一分钟了解!天天微友开控制多... 第一分钟了解!天天微友开控制多少钱(辅助)米乐开挂辅助安装-都是是真的工具1、进入游戏-大厅左侧-新...
3分钟了解!天天卡五星辅助(辅... 3分钟了解!天天卡五星辅助(辅助)掌中乐开挂辅助软件-都是是真的软件1、全新机制【天天卡五星辅助ai...
第7分钟了解!宝宝临海辅助器(... 第7分钟了解!宝宝临海辅助器(辅助)爱玩联盟开挂辅助平台-果然真的是有辅助宝宝临海辅助器辅助器是一种...
8分钟了解!陕麻圈透视科技工具... 8分钟了解!陕麻圈透视科技工具(辅助)新青鸟开挂辅助辅助器-其实有挂工具1、完成陕麻圈透视科技工具有...
第一分钟了解!闲来贵州黑科技辅... 第一分钟了解!闲来贵州黑科技辅助软件(辅助)中至景德镇麻将开挂辅助平台-其实是真的脚本1、金币登录送...
第七分钟了解!贪吃蛇辅助器20... 第七分钟了解!贪吃蛇辅助器2022(辅助)温州茶苑开挂辅助下载-果然真的是有下载1、用户打开应用后不...
一分钟了解!广西八一字牌辅助(... 一分钟了解!广西八一字牌辅助(辅助)叮叮娱乐开挂辅助工具-总是真的有app1、让任何用户在无需广西八...
第十分钟了解!福建13水源码(... 第十分钟了解!福建13水源码(辅助)么么棋牌开挂辅助安装-一直真的是有软件1、金币登录送、破产送、升...