设为首页 收藏本站
查看: 2815|回复: 0

[经验分享] 如何从Zabbix数据库中获取监控数据

[复制链接]

尚未签到

发表于 2019-1-18 13:44:41 | 显示全部楼层 |阅读模式
      做过Zabbix的同学都知道,Zabbix通过专用的Agent或者SNMP收集相关的监控数据,然后存储到数据库里面实时在前台展示。Zabbix监控数据主要分为以下两类:

      历史数据:history相关表,从history_uint表里面可以查询到设备监控项目的最大,最小和平均值,即存储监控数据的原始数据。
      趋势数据:trends相关表,趋势数据是经过Zabbix计算的数据,数据是从history_uint里面汇总的,从trends_uint可以查看到监控数据每小时最大,最小和平均值,即存储监控数据的汇总数据。
      Zabbix可以通过两种方式获取历史数据:
  1.通过Zabbix前台获取历史数据
      通过Zabbix前台查看历史数据非常简单,可以通过Monitoring->Lastest data的方式查看。也可以点击右上角的As plain test按钮保存成文本文件。

  2.通过前台获取的数据进行处理和二次查询有很多限制,因此可以通过SQL语句直接从后台DB查询数据。   
      首先大家应该熟悉SQL语句Select 常用用法:
  
SELECT [ALL | DISTINCT] Select_List [INTO [New_Table_name]
FROM { Table_name | View_name} [ [,{table2_name | view2_name}
     [,...] ]
[ WHERE Serch_conditions ]
[ GROUP BY Group_by_list ]
[ HAVING Serch_conditions ]
[ ORDER BY Order_list [ASC| DEsC] ]      说明:

  1)SELECT子句指定要查询的特定表中的列,它可以是*,表达式,列表等。
  2)INTO子句指定要生成新的表。
3)FROM子句指定要查询的表或者视图。
4)WHERE子句用来限定查询的范围和条件。
5)GROUP BY子句指定分组查询子句。
6)HAVING子句用于指定分组子句的条件。
7)ORDER BY可以根据一个或者多个列来排序查询结果,在该子句中,既可以使用列名,也可以使用相对列号,ASC表示升序,DESC表示降序。
8)mysql聚合函数:sum(),count(),avg(),max(),avg()等都是聚合函数,当我们在用聚合函数的时候,一般都要用到GROUP BY 先进行分组,然后再进行聚合函数的运算。运算完后就要用到Having子句进行判断了,例如聚合函数的值是否大于某一个值等等。
  从Zabbix数据库中查询监控项目方法,这里已查询主机的网卡流量为例子:
  1)通过hosts表查找host的ID。
mysql> select host,hostid from hosts where host="WWW05";
+-------+--------+
| host  | hostid |
+-------+--------+
| WWW05 |  10534 |
+-------+--------+
1 row in set (0.00 sec)  2)通过items表查找主的监控项和key以及itemid。
mysql> select itemid,name,key_ from items where hostid=10534 and key_="net.if.out[eth0]";
+--------+-----------------+------------------+
| itemid | name            | key_             |
+--------+-----------------+------------------+
|  58860 | 发送流量:      | net.if.out[eth0] |
+--------+-----------------+------------------+
1 row in set (0.00 sec)  3)通过itemid查询主机的监控项目(history_uint或者trends_uint),单位为M。
     主机流入流量:
mysql> select from_unixtime(clock) as DateTime,round(value/1024/1024,2) as Traffic_in from history_uint where itemid="58855" and from_unixtime(clock)>='2014-09-20' and from_unixtime(clock) select from_unixtime(clock) as DateTime,round(value/1024/1024,2) as Traffic_out from history_uint where itemid="58860" and from_unixtime(clock)>='2014-09-20' and from_unixtime(clock) select from_unixtime(clock,"%Y-%m-%d %H:%i") as DateTime,sum(round(value/1024/1024,2)) as Traffic_total from history_uint where itemid in (58855,58860)  and from_unixtime(clock)>='2014-09-20'and from_unixtime(clock) select date as DateTime,round(min(traffic)/2014/1024,2) as TotalMinIN,round(avg(traffic)/1024/1024,2) as TotalAvgIN,round(max(traffic)/1024/1024,2)  as TotalMaxIN from (select from_unixtime(clock,"%Y-%m-%d") as date,sum(value) as traffic from history_uint where itemid in (58855,58860)  and from_unixtime(clock)>='2014-09-20' and from_unixtime(clock) select from_unixtime(hi.clock,"%Y-%m-%d %H:%i") as DateTime,g.name as Group_Name,h.host as Host, hi.value as Cpu_Avg_Idle from hosts_groups as hg join groups g on g.groupid = hg.groupid join items i on hg.hostid = i.hostid join hosts h on h.hostid=i.hostid join history hi on  i.itemid = hi.itemid where g.name='上海机房--项目测试' and i.key_='system.cpu.util[,idle]' and  from_unixtime(clock)>='2014-09-24' and from_unixtime(clock) select from_unixtime(hi.clock,"%Y-%m-%d %H:%i") as Date,g.name as Group_Name,h.host as Host, hi.value_avg as Cpu_Avg_Idle   from hosts_groups as hg join groups g on g.groupid = hg.groupid join items i on hg.hostid = i.hostid join hosts h on h.hostid=i.hostid join trends hi on  i.itemid = hi.itemid     where g.name='上海机房--项目测试' and i.key_='system.cpu.util[,idle]' and  from_unixtime(clock)>='2014-09-10' and from_unixtime(clock) SELECT round(SUM(1.0/i.delay),2) AS qps FROM items i,hosts h WHERE i.status='0' AND i.hostid=h.hostid AND h.status='0' AND i.delay0;
+--------+
| qps    |
+--------+
| 503.40 |
+--------+
1 row in set (0.11 sec)      查询IDC机房的资产信息:
mysql> select name,os,tag,hardware from host_inventory where hostid in (select hostid from  hosts_groups  where groupid=69) limit 2;
+-------+----------------------------+------+-------------------+
| name  | os                         | tag  | hardware          |
+-------+----------------------------+------+-------------------+
| SHDBM | CentOS release 5.2 (Final) | i686 | ProLiant DL360 G5 |
| SHDBS | CentOS release 5.2 (Final) | i686 | ProLiant DL360 G5 |
+-------+----------------------------+------+-------------------+
2 rows in set (0.00 sec)      查询Zabbix interval分布情况:
mysql> select delay,count(*),concat(round(count(*) / (select count(*) from items where status=0)*100,2),"%") as percent from items where status=0 group by delay order by 2 desc;
+-------+----------+---------+
| delay | count(*) | percent |
+-------+----------+---------+
|  3600 |    41168 | 38.92%  |
|   300 |    35443 | 33.51%  |
|   600 |    16035 | 15.16%  |
|    60 |    12178 | 11.51%  |
|     0 |      902 | 0.85%   |
| 36000 |       46 | 0.04%   |
|    30 |        1 | 0.00%   |
+-------+----------+---------+
7 rows in set (0.68 sec)      总结:通过SQL语句可以查询出任何监控项目的数据,并且在SQL语句的末尾通过into outfile '/tmp/zabbix_result.txt'直接把查询的结果保存到系统上面,在通过脚本发送查询结果到指定的用户,实现自动化查询的过程,网上很少有介绍Zabbix数据库查询的文章,希望对大家有所帮助。
     
  

  

  

  

  

  

  

  

  

  

  

  

  

  





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-664873-1-1.html 上篇帖子: zabbix专题:第十二章 zabbix proxy分布式监控配置 下篇帖子: zabbix对主机组中的key进行分组批量添加到screen
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表