|
做过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数据库查询的文章,希望对大家有所帮助。
|
|