发表于 2019-1-17 13:20:58

zabbix从入门到精通之zabbix历史数据

                  zabbix从入门到精通之zabbix历史数据
1.1 通过zabbix前台获取历史数据。
          通过zabbix的前台也就是登陆web页面的方式来查看监控项item的历史数据是件非常简单的事情,我们可以通过Monitoring—>Lastest data的方式直接进行查看。
http://blog.运维网.com/attachment/201211/173402858.png
通过Monitoring—>Lastest data选择相关主机的item,然后选择右边的grahp按钮后入下图所示。会得到相关item的数据图。
http://blog.运维网.com/attachment/201211/173853530.png
  双击右上角的Values按钮。就可以看到相关的时间点对着的历史数据了。同时我们可以通过时间滚动条来选择具体的时间范围的历史数据。
http://blog.运维网.com/attachment/201211/174130814.png
  通过右上角的As Plain text按钮可以转成成方便的text的方式显示方便我们copy数据。
http://blog.运维网.com/attachment/201211/174325400.png
右上角还有一个选项是500 latest values则是获取默认的500个历史数据进行查看。 然后通过zabbix的前台获取历史数据固然很方便但是有时候对数据的处理查询和二次开发往往有很多限制,如果可以通过数据库的方式对历史数据进行查询修改过滤是件很好的事情。
1.1 通过mysql获取历史数据
Zabbix中存储历史数据的表是以history开头的,目前zabbix 2.03 中主要有一下几张表。
>>>>>history            Numeric(float)
>>>>>history_log         -log
>>>>>history_str         -Character
>>>>>history_text          -text
>>>>>history_uint          -Numeric(unsigned)

Use zabbix;
Select * from history;
mysql> select * from history limit 3;
+--------+------------+--------+-----------+
| itemid | clock      | value |
+--------+------------+--------+-----------+
| 23378 | 1349976338 | 1.0000
| 23378 | 1349976368 | 1.0000
| 23378 | 1349976428 | 1.0000
+--------+------------+--------+-----------+
3 rows in set (0.00 sec)

Itemid
Item所对应的id号
Clock
Item的值所对应的时间
value
Item对应的值


       问题:大家发现2个问题没有,1是时间的问题,其中clock为uninx time,我们读起来不太方便,第二个是item只有itemid,而不是具体的item的名字,我们查找起来不太方便。
Zabbix server.solutionware.com.cn: Processor load (5 min average per core)
2012-11-02 16:39:17    1351845557   0.05
2012-11-02 16:38:17    1351845497   0.07
2012-11-02 16:37:17    1351845437   0.05
2012-11-02 16:36:17    1351845377   0.06
2012-11-02 16:35:17    1351845317   0.08
2012-11-02 16:34:17    1351845257   0.07
2012-11-02 16:33:18    1351845198   0.08
2012-11-02 16:32:17    1351845137   0.03
2012-11-02 16:31:17    1351845077   0.04
2012-11-02 16:30:17    1351845017   0.03
2012-11-02 16:29:17    1351844957   0.04
         首先看一段安装上节As Plain text方式获取的一段主机Zabbix server.solutionware.com.cn,每5分钟cpu的负的值。其中左边第一行为item对应的时间, 中间位为进过出来的时间,也就是mysql数据库对应的clock,最后一列则为对应的cpu负载的值。
我想获得主机Zabbix server.solutionware.com.cn cpu的负载怎么获得呢通过mysql的方式。因为mysql里的history只存储itemid的值,而没有具体某个主机的item的名字的值,所以我们必须通过itemid的方式获取。
获取itemid:其实方法很简单。我们只要在As Plain tex页面双击页面属性

http://blog.运维网.com/attachment/201211/174641606.png
找到itemid后面=号的值就是我们想要的itemid的值。这里为23297。
有了itemid获取其对应的值就很简单了。

mysql> select * from history where itemid=23297 limit 5;
+--------+------------+--------+-----------+
| itemid | clock      | value | ns      |
+--------+------------+--------+-----------+
| 23297 | 1351263137 | 1.0300 |92325659 |
| 23297 | 1351263197 | 1.1700 | 239910161 |
| 23297 | 1351263257 | 0.9600 | 87896295 |
| 23297 | 1351263317 | 0.7800 | 215842701 |
| 23297 | 1351263377 | 0.6400 | 69620999 |
+--------+------------+--------+-----------+
5 rows in set (0.00 sec)
下面进行下验证。这个值为上面例子取出的一部分的值。
2012-11-02 16:29:17    1351844957   0.04

获取主机Zabbix server.solutionware.com.cn 2012-11-02 16:29:17这一时间对应的值。

mysql> select * from history where itemid=23297 and clock=1351844957;
+--------+------------+--------+-----------+
| itemid | clock      | value |         |
+--------+------------+--------+-----------+
| 23297 | 1351844957 | 0.0400 |
+--------+------------+--------+-----------+
1 row in set (0.00 sec)
      看到吗其中value为0.0400和我们zabbix前台显示的值是一样的。
获取某一时间段的值。
2012-11-02 16:39:17    1351845557   0.05
2012-11-02 16:38:17    1351845497   0.07
2012-11-02 16:37:17    1351845437   0.05
2012-11-02 16:36:17    1351845377   0.06
2012-11-02 16:35:17    1351845317   0.08
2012-11-02 16:34:17    1351845257   0.07
2012-11-02 16:33:18    1351845198   0.08
2012-11-02 16:32:17    1351845137   0.03
2012-11-02 16:31:17    1351845077   0.04
2012-11-02 16:30:17    1351845017   0.03
2012-11-02 16:29:17    1351844957   0.04

比如获取2012-11-02 16:29:17到2012-11-02 16:39:17这一时间段的值。其实也很简单
mysql> select * from history where itemid=23297 and clock>=1351844957 and clock
页: [1]
查看完整版本: zabbix从入门到精通之zabbix历史数据