ZABBIX数据库表结构解析

梦想与她 提交于 2019-11-27 08:11:20

############sample 1

https://blog.51cto.com/sfzhang88/1558254

如何从Zabbix数据库中获取监控数据

    做过Zabbix的同学都知道,Zabbix通过专用的Agent或者SNMP收集相关的监控数据,然后存储到数据库里面实时在前台展示。Zabbix监控数据主要分为以下两类:

    历史数据:history相关表,从history_uint表里面可以查询到设备监控项目的最大,最小和平均值,即存储监控数据的原始数据。

    趋势数据:trends相关表,趋势数据是经过Zabbix计算的数据,数据是从history_uint里面汇总的,从trends_uint可以查看到监控数据每小时最大,最小和平均值,即存储监控数据的汇总数据。

    Zabbix可以通过两种方式获取历史数据:

1.通过Zabbix前台获取历史数据

    通过Zabbix前台查看历史数据非常简单,可以通过Monitoring->Lastest data的方式查看。也可以点击右上角的As plain test按钮保存成文本文件。

wKioL1QkDOCjpLoTAAOm77_P1z4367.jpg

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)<'2014-09-21' limit 20;  +---------------------+------------+  | DateTime            | Traffic_in |  +---------------------+------------+  | 2014-09-20 00:00:55 |       0.10 |  | 2014-09-20 00:01:55 |       0.09 |  | 2014-09-20 00:02:55 |       0.07 |  | 2014-09-20 00:03:55 |       0.05 |  | 2014-09-20 00:04:55 |       0.03 |  | 2014-09-20 00:05:55 |       0.06 |  | 2014-09-20 00:06:55 |       0.12 |  | 2014-09-20 00:07:55 |       0.05 |  | 2014-09-20 00:08:55 |       0.10 |  | 2014-09-20 00:09:55 |       0.10 |  | 2014-09-20 00:10:55 |       0.12 |  | 2014-09-20 00:11:55 |       0.12 |  | 2014-09-20 00:12:55 |       0.13 |  | 2014-09-20 00:13:55 |       3.16 |  | 2014-09-20 00:14:55 |       0.23 |  | 2014-09-20 00:15:55 |       0.24 |  | 2014-09-20 00:16:55 |       0.26 |  | 2014-09-20 00:17:55 |       0.23 |  | 2014-09-20 00:18:55 |       0.14 |  | 2014-09-20 00:19:55 |       0.16 |  +---------------------+------------+  20 rows in set (0.82 sec)
 

 

    主机流出流量:

mysql> 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)<'2014-09-21' limit 20;  +---------------------+-------------+  | DateTime            | Traffic_out |  +---------------------+-------------+  | 2014-09-20 00:00:00 |        4.13 |  | 2014-09-20 00:01:00 |        3.21 |  | 2014-09-20 00:02:00 |        2.18 |  | 2014-09-20 00:03:01 |        1.61 |  | 2014-09-20 00:04:00 |        1.07 |  | 2014-09-20 00:05:00 |        0.92 |  | 2014-09-20 00:06:00 |        1.23 |  | 2014-09-20 00:07:00 |        2.76 |  | 2014-09-20 00:08:00 |        1.35 |  | 2014-09-20 00:09:00 |        3.11 |  | 2014-09-20 00:10:00 |        2.99 |  | 2014-09-20 00:11:00 |        2.68 |  | 2014-09-20 00:12:00 |        2.55 |  | 2014-09-20 00:13:00 |        2.89 |  | 2014-09-20 00:14:00 |        4.98 |  | 2014-09-20 00:15:00 |        6.56 |  | 2014-09-20 00:16:00 |        7.34 |  | 2014-09-20 00:17:00 |        6.81 |  | 2014-09-20 00:18:00 |        7.67 |  | 2014-09-20 00:19:00 |        4.11 |  +---------------------+-------------+  20 rows in set (0.74 sec)
 

4)如果是两台设备,汇总流量,假如公司出口有两台设备,可以用下面的SQL语句汇总每天的流量。下面SQL语句是汇总上面主机网卡的进出流量的。

mysql> 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)<'2014-09-21' group by from_unixtime(clock,"%Y-%m-%d %H:%i") limit 20;  +------------------+---------------+  | DateTime         | Traffic_total |  +------------------+---------------+  | 2014-09-20 00:00 |          4.23 |  | 2014-09-20 00:01 |          3.30 |  | 2014-09-20 00:02 |          2.25 |  | 2014-09-20 00:03 |          1.66 |  | 2014-09-20 00:04 |          1.10 |  | 2014-09-20 00:05 |          0.98 |  | 2014-09-20 00:06 |          1.35 |  | 2014-09-20 00:07 |          2.81 |  | 2014-09-20 00:08 |          1.45 |  | 2014-09-20 00:09 |          3.21 |  | 2014-09-20 00:10 |          3.11 |  | 2014-09-20 00:11 |          2.80 |  | 2014-09-20 00:12 |          2.68 |  | 2014-09-20 00:13 |          6.05 |  | 2014-09-20 00:14 |          5.21 |  | 2014-09-20 00:15 |          6.80 |  | 2014-09-20 00:16 |          7.60 |  | 2014-09-20 00:17 |          7.04 |  | 2014-09-20 00:18 |          7.81 |  | 2014-09-20 00:19 |          4.27 |  +------------------+---------------+  20 rows in set (1.52 sec)
 

5)查询一天中主机流量的最大值,最小值和平均值。

mysql> 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)<'2014-09-21' group by from_unixtime(clock,"%Y-%m-%d %H:%i") ) tmp;  +------------+------------+------------+------------+  | DateTime   | TotalMinIN | TotalAvgIN | TotalMaxIN |  +------------+------------+------------+------------+  | 2014-09-20 |       0.01 |       4.63 |     191.30 |  +------------+------------+------------+------------+  1 row in set (1.74 sec)
 

6)查询主机组里面所有主机CPU Idle平均值(原始值)。

mysql> 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)<'2014-09-25' group by h.host,from_unixtime(hi.clock,"%Y-%m-%d %H:%i") limit 10;  +------------------+----------------------------+----------+--------------+  | DateTime         | Group_Name                 | Host     | Cpu_Avg_Idle |  +------------------+----------------------------+----------+--------------+  | 2014-09-24 00:02 | 上海机房--项目测试         | testwb01 |      94.3960 |  | 2014-09-24 00:07 | 上海机房--项目测试         | testwb01 |      95.2086 |  | 2014-09-24 00:12 | 上海机房--项目测试         | testwb01 |      95.4308 |  | 2014-09-24 00:17 | 上海机房--项目测试         | testwe01 |      95.4580 |  | 2014-09-24 00:22 | 上海机房--项目测试         | testwb01 |      95.4611 |  | 2014-09-24 00:27 | 上海机房--项目测试         | testwb01 |      95.2939 |  | 2014-09-24 00:32 | 上海机房--项目测试         | testwb01 |      96.0896 |  | 2014-09-24 00:37 | 上海机房--项目测试         | testwb01 |      96.5286 |  | 2014-09-24 00:42 | 上海机房--项目测试         | testwb01 |      96.8086 |  | 2014-09-24 00:47 | 上海机房--项目测试         | testwb01 |      96.6854 |  +------------------+----------------------------+----------+--------------+  10 rows in set (0.75 sec)
 

7)查询主机组里面所有主机CPU Idle平均值(汇总值)。

mysql> 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)<'2014-09-11' group by h.host,from_unixtime(hi.clock,"%Y-%m-%d %H:%i") limit 10;  +------------------+----------------------------+----------+--------------+  | Date             | Group_Name                 | Host     | Cpu_Avg_Idle |  +------------------+----------------------------+----------+--------------+  | 2014-09-10 00:00 | 上海机房--项目测试         | testwb01 |      99.9826 |  | 2014-09-10 01:00 | 上海机房--项目测试         | testwb01 |      99.9826 |  | 2014-09-10 02:00 | 上海机房--项目测试         | testwb01 |      99.9825 |  | 2014-09-10 03:00 | 上海机房--项目测试         | testwb01 |      99.9751 |  | 2014-09-10 04:00 | 上海机房--项目测试         | testwb01 |      99.9843 |  | 2014-09-10 05:00 | 上海机房--项目测试         | testwb01 |      99.9831 |  | 2014-09-10 06:00 | 上海机房--项目测试         | testwb01 |      99.9829 |  | 2014-09-10 07:00 | 上海机房--项目测试         | testwb01 |      99.9843 |  | 2014-09-10 08:00 | 上海机房--项目测试         | testwb01 |      99.9849 |  | 2014-09-10 09:00 | 上海机房--项目测试         | testwb01 |      99.9849 |  +------------------+----------------------------+----------+--------------+  10 rows in set (0.01 sec)
 

8)其它与Zabbix相关的SQL语句。

    查询主机已经添加但没有开启监控主机:

select host from hosts where status=1;
 

    查询NVPS的值:

mysql> 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.delay<>0;   +--------+  | 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数据库查询的文章,希望对大家有所帮助。

   

 ##################sample 2 (重要)

https://www.cnblogs.com/wumingxiaoyao/p/7412312.html

ZABBIX数据库表结构解析

 

 

 下面开始介绍:

1.添加监控表结构详解

(1)hosts,存储被监控的机器的信息,表结构如下:

 

(2)items

(3)hosts_templates,存储机器和模版或者模版和模版之间的关系

由于模版和机器都存储在hosts表中,所以hosts_templates和hosts 之间可以hostid关联也可以通过templateid关联。

 (4)interface,存储了所有设备的ip和端口的数据。(由于hosts表中不仅保存了设备信息还保存了模版信息,所以统计实际监控的设备,此表更加准确)

 

 2.数据存储表结构详解

 

将clock 转化为人性化时间:

3.报警相关表结构详解

 

(1)triggers

 

 

 

附 functions 表结构:

 (2)events

 

 

例子:

1. 找出某台主机的所有items ,含有某个key_的item , 统计items 总个数
SELECT * FROM HOSTS WHERE hostid=10157;
SELECT * FROM items WHERE hostid=10157 AND key_ LIKE '%agent%';
SELECT COUNT(*) FROM items;

2. 找出触发trigger次数最多的事件,并按trigger 降序排列。
SELECT a.description, COUNT(*) cnt FROM TRIGGERS a , EVENTS b 
WHERE a.triggerid=b.objectid ORDER BY cnt DESC ;
3. 从item记录各找出一个value类型为整形,浮点型的key_。
统计这两个key_ 存储在history或者history_uint 某一个时间段(比如2017/06/12)
的最大值,最小值,平均值,然后与 trends 或者 trends_unint 中相应时间段做对比

整型
SELECT * FROM items WHERE value_type=3 AND hostid=10157 LIMIT 1;
SELECT * FROM history_uint a,trends_uint b WHERE a.clock=b.clock AND a.itemid=b.itemid LIMIT 1;

浮点型
SELECT * FROM items WHERE value_type=0 AND hostid=10157 LIMIT 1;
SELECT * FROM history a,trends b WHERE a.clock=b.clock AND a.itemid=b.itemid LIMIT 1;

 

4.统计Zabbix Dashboard中triggers总数的来源。

SELECT  count(*)
  FROM TRIGGERS
WHERE triggerid IN
       (SELECT triggerid
          FROM functions
         WHERE itemid IN (SELECT itemid
                            FROM items
                           WHERE hostid IN (SELECT hostid FROM interface)
                             AND key_ NOT LIKE '%#%'
                             AND key_ NOT LIKE '%discovery%'
                             AND STATUS != 1));
 
说明:
通过之前对zabbix表结构的学习,我们知道,表triggers和functions相关联,而functions和items相关联,那么,要对triggers做统计,就需要从这三张表下手。
关键就是对items表中的数据做出筛选,key_中带“#”和“discovery”的和status=1(不可用状态)都要排除,这样就统计出来了。
 
 
####sample 3
https://www.cnblogs.com/shhnwangjian/p/5484352.html

想理解zabbix的前端代码、做深入的二次开发,甚至的调优,那就不能不了解数据库的表结构了。

我们这里采用的zabbix1.8、mysql,所以简单的说下我们mysql这边的表结构,其他环境不保证正确。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
mysql> show tables;
+-----------------------+
| Tables_in_zabbix      |
+-----------------------+
| acknowledges          |
| actions               |
| alerts                |
| application_template  |
| applications          |
| auditlog              |
| auditlog_details      |
| autoreg_host          |
| conditions            |
| config                |
| dbversion             |
| dchecks               |
| dhosts                |
| drules                |
| dservices             |
| escalations           |
| events                |
| expressions           |
| functions             |
| globalmacro           |
| globalvars            |
| graph_discovery       |
| graph_theme           |
| graphs                |
| graphs_items          |
| group_discovery       |
| group_prototype       |
groups                |
history               |
| history_log           |
| history_str           |
| history_text          |
| history_uint          |
| host_discovery        |
| host_inventory        |
| hostmacro             |
| hosts                 |
| hosts_groups          |
| hosts_templates       |
| housekeeper           |
| httpstep              |
| httpstepitem          |
| httptest              |
| httptestitem          |
| icon_map              |
| icon_mapping          |
| ids                   |
| images                |
| interface             |
| interface_discovery   |
| item_condition        |
| item_discovery        |
| items                 |
| items_applications    |
| maintenances          |
| maintenances_groups   |
| maintenances_hosts    |
| maintenances_windows  |
| mappings              |
| media                 |
| media_type            |
| opcommand             |
| opcommand_grp         |
| opcommand_hst         |
| opconditions          |
| operations            |
| opgroup               |
| opmessage             |
| opmessage_grp         |
| opmessage_usr         |
| optemplate            |
| profiles              |
| proxy_autoreg_host    |
| proxy_dhistory        |
| proxy_history         |
| regexps               |
| rights                |
| screens               |
| screens_items         |
| scripts               |
| service_alarms        |
| services              |
| services_links        |
| services_times        |
| sessions              |
| slides                |
| slideshows            |
| sysmap_element_url    |
| sysmap_url            |
| sysmaps               |
| sysmaps_elements      |
| sysmaps_link_triggers |
| sysmaps_links         |
| timeperiods           |
| trends                |
| trends_uint           |
| trigger_depends       |
| trigger_discovery     |
| triggers              |
| user_history          |
users                 |
| users_groups          |
| usrgrp                |
| valuemaps             |
+-----------------------+

actions

actions表记录了当触发器触发时,需要采用的动作。

mysql> desc actions;  +---------------+---------------------+------+-----+---------+-------+  | Field         | Type                | Null | Key | Default | Extra |  +---------------+---------------------+------+-----+---------+-------+  | actionid      | bigint(20) unsigned | NO   | PRI | 0       |       |  | name          | varchar(255)        | NO   |     |         |       |  | eventsource   | int(11)             | NO   | MUL | 0       |       |  | evaltype      | int(11)             | NO   |     | 0       |       |  | status        | int(11)             | NO   |     | 0       |       |  | esc_period    | int(11)             | NO   |     | 0       |       |  | def_shortdata | varchar(255)        | NO   |     |         |       |  | def_longdata  | blob                | NO   |     | NULL    |       |  | recovery_msg  | int(11)             | NO   |     | 0       |       |  | r_shortdata   | varchar(255)        | NO   |     |         |       |  | r_longdata    | blob                | NO   |     | NULL    |       |  +---------------+---------------------+------+-----+---------+-------+

alerts

alerts 表保存了历史的告警事件,可以从这个表里面去做一些统计分析,例如某个部门、 
某人、某类时间的告警统计,以及更深入的故障发生、恢复时间,看你想怎么用了。

mysql> desc alerts;  +-------------+---------------------+------+-----+---------+-------+  | Field       | Type                | Null | Key | Default | Extra |  +-------------+---------------------+------+-----+---------+-------+  | alertid     | bigint(20) unsigned | NO   | PRI | 0       |       |  | actionid    | bigint(20) unsigned | NO   | MUL | 0       |       |  | eventid     | bigint(20) unsigned | NO   | MUL | 0       |       |  | userid      | bigint(20) unsigned | NO   | MUL | 0       |       |  | clock       | int(11)             | NO   | PRI | 0       |       |  | mediatypeid | bigint(20) unsigned | NO   | MUL | 0       |       |  | sendto      | varchar(100)        | NO   |     |         |       |  | subject     | varchar(255)        | NO   |     |         |       |  | message     | blob                | NO   |     | NULL    |       |  | status      | int(11)             | NO   | MUL | 0       |       |  | retries     | int(11)             | NO   |     | 0       |       |  | error       | varchar(128)        | NO   |     |         |       |  | nextcheck   | int(11)             | NO   |     | 0       |       |  | esc_step    | int(11)             | NO   |     | 0       |       |  | alerttype   | int(11)             | NO   |     | 0       |       |  +-------------+---------------------+------+-----+---------+-------+

config

config表保存了全局的参数,前端包括后端也是,很多情况下会查询改表的参数的,例如用户的自定义主题、 
登陆认证类型等,非常重要,

不过对我们做数据分析意义不大。

mysql> desc config;  +-------------------------+---------------------+------+-----+-----------------+-------+  | Field                   | Type                | Null | Key | Default         | Extra |  +-------------------------+---------------------+------+-----+-----------------+-------+  | configid                | bigint(20) unsigned | NO   | PRI | 0               |       |  | alert_history           | int(11)             | NO   |     | 0               |       |  | event_history           | int(11)             | NO   |     | 0               |       |  | refresh_unsupported     | int(11)             | NO   |     | 0               |       |  | work_period             | varchar(100)        | NO   |     | 1-5,00:00-24:00 |       |  | alert_usrgrpid          | bigint(20) unsigned | NO   |     | 0               |       |  | event_ack_enable        | int(11)             | NO   |     | 1               |       |  | event_expire            | int(11)             | NO   |     | 7               |       |  | event_show_max          | int(11)             | NO   |     | 100             |       |  | default_theme           | varchar(128)        | NO   |     | default.css     |       |  | authentication_type     | int(11)             | NO   |     | 0               |       |  | ldap_host               | varchar(255)        | NO   |     |                 |       |  | ldap_port               | int(11)             | NO   |     | 389             |       |  | ldap_base_dn            | varchar(255)        | NO   |     |                 |       |  | ldap_bind_dn            | varchar(255)        | NO   |     |                 |       |  | ldap_bind_password      | varchar(128)        | NO   |     |                 |       |  | ldap_search_attribute   | varchar(128)        | NO   |     |                 |       |  | dropdown_first_entry    | int(11)             | NO   |     | 1               |       |  | dropdown_first_remember | int(11)             | NO   |     | 1               |       |  | discovery_groupid       | bigint(20) unsigned | NO   |     | 0               |       |  | max_in_table            | int(11)             | NO   |     | 50              |       |  | search_limit            | int(11)             | NO   |     | 1000            |       |  +-------------------------+---------------------+------+-----+-----------------+-------+

functions

function 表时非常重要的一个表了,记录了trigger中使用的表达式,例如max、last、nodata等函数。

但其实这个表说他重要时因为同时记录了trigger、itemid,那就可以做一些API的开发了,例如根据 
IP 茶香改IP的所有trigger,我记得1.8的版本的API是无法实现我说的这个功能的,那只能利用 
function表去自己查询了。

mysql> desc functions ;  +------------+---------------------+------+-----+---------+-------+  | Field      | Type                | Null | Key | Default | Extra |  +------------+---------------------+------+-----+---------+-------+  | functionid | bigint(20) unsigned | NO   | PRI | 0       |       |  | itemid     | bigint(20) unsigned | NO   | MUL | 0       |       |  | triggerid  | bigint(20) unsigned | NO   | MUL | 0       |       |  | lastvalue  | varchar(255)        | YES  |     | NULL    |       |  | function   | varchar(12)         | NO   |     |         |       |  | parameter  | varchar(255)        | NO   |     | 0       |       |  +------------+---------------------+------+-----+---------+-------+

graphs

graphs 表包含了用户定义的图表信息,同样的玩法可以是根据IP去查询改IP下的所有图表, 
不过似乎是有API的,我只是举例而已。

mysql> desc graphs;  +------------------+---------------------+------+-----+---------+-------+  | Field            | Type                | Null | Key | Default | Extra |  +------------------+---------------------+------+-----+---------+-------+  | graphid          | bigint(20) unsigned | NO   | PRI | 0       |       |  | name             | varchar(128)        | NO   | MUL |         |       |  | width            | int(11)             | NO   |     | 0       |       |  | height           | int(11)             | NO   |     | 0       |       |  | yaxismin         | double(16,4)        | NO   |     | 0.0000  |       |  | yaxismax         | double(16,4)        | NO   |     | 0.0000  |       |  | templateid       | bigint(20) unsigned | NO   |     | 0       |       |  | show_work_period | int(11)             | NO   |     | 1       |       |  | show_triggers    | int(11)             | NO   |     | 1       |       |  | graphtype        | int(11)             | NO   |     | 0       |       |  | show_legend      | int(11)             | NO   |     | 0       |       |  | show_3d          | int(11)             | NO   |     | 0       |       |  | percent_left     | double(16,4)        | NO   |     | 0.0000  |       |  | percent_right    | double(16,4)        | NO   |     | 0.0000  |       |  | ymin_type        | int(11)             | NO   |     | 0       |       |  | ymax_type        | int(11)             | NO   |     | 0       |       |  | ymin_itemid      | bigint(20) unsigned | NO   |     | 0       |       |  | ymax_itemid      | bigint(20) unsigned | NO   |     | 0       |       |  +------------------+---------------------+------+-----+---------+-------+

graphs_items

graphs_items 保存了属于某个图表的所有的监控项信息。

mysql> desc graphs_items;  +-------------+---------------------+------+-----+---------+-------+  | Field       | Type                | Null | Key | Default | Extra |  +-------------+---------------------+------+-----+---------+-------+  | gitemid     | bigint(20) unsigned | NO   | PRI | 0       |       |  | graphid     | bigint(20) unsigned | NO   | MUL | 0       |       |  | itemid      | bigint(20) unsigned | NO   | MUL | 0       |       |  | drawtype    | int(11)             | NO   |     | 0       |       |  | sortorder   | int(11)             | NO   |     | 0       |       |  | color       | varchar(6)          | NO   |     | 009600  |       |  | yaxisside   | int(11)             | NO   |     | 1       |       |  | calc_fnc    | int(11)             | NO   |     | 2       |       |  | type        | int(11)             | NO   |     | 0       |       |  | periods_cnt | int(11)             | NO   |     | 5       |       |  +-------------+---------------------+------+-----+---------+-------+

groups

groups 没啥说的,都懂,就是保存了组名和组的ID 。

mysql> desc groups ;  +----------+---------------------+------+-----+---------+-------+  | Field    | Type                | Null | Key | Default | Extra |  +----------+---------------------+------+-----+---------+-------+  | groupid  | bigint(20) unsigned | NO   | PRI | 0       |       |  | name     | varchar(64)         | NO   | MUL |         |       |  | internal | int(11)             | NO   |     | 0       |       |  +----------+---------------------+------+-----+---------+-------+

history 、history_str、history_log 、history_uint_sync等

这部分表都差不多,唯一不同的是保存的数据类型,history_str保存的数据 
类型就算str即字符类型的。这个是和采集时设置的数据类型一致的。

需要注意的时,因为history表有这么多的类型,那自己写报表系统等去查询 
数据时,就需要判断下数据的采集类型,如果查错了表,那肯定时没有数据的。

mysql> desc history;  +--------+---------------------+------+-----+---------+-------+  | Field  | Type                | Null | Key | Default | Extra |  +--------+---------------------+------+-----+---------+-------+  | itemid | bigint(20) unsigned | NO   | PRI | 0       |       |  | clock  | int(11)             | NO   | PRI | 0       |       |  | value  | double(16,4)        | NO   |     | 0.0000  |       |  +--------+---------------------+------+-----+---------+-------+    mysql> desc history_str;  +--------+---------------------+------+-----+---------+-------+  | Field  | Type                | Null | Key | Default | Extra |  +--------+---------------------+------+-----+---------+-------+  | itemid | bigint(20) unsigned | NO   | MUL | 0       |       |  | clock  | int(11)             | NO   |     | 0       |       |  | value  | varchar(255)        | NO   |     |         |       |  +--------+---------------------+------+-----+---------+-------+

接收item值时的时间值存放在两个字段内,大于1秒的部分存放找clock字段单位是秒(s),小于一秒的部分存放在ns字段单位是纳秒(ns)。

两个字段相加的值才是接收item值时的时间值,一般不用关心小于1秒的部分。

trends、trends_uint

trends 也是保存了历史数据用的,和history不同的时,trends表仅仅保存了 
小时平均的值,即你可以理解为是history表的数据压缩。所以trends表也有 
很多的类型,对应history。

值的注意的trends和history表这两类表数据量都非常大,我们一天大概就要有 
40G 的数据。

所以注意定是去做压缩、删除。

mysql> desc trends;  +-----------+---------------------+------+-----+---------+-------+  | Field     | Type                | Null | Key | Default | Extra |  +-----------+---------------------+------+-----+---------+-------+  | itemid    | bigint(20) unsigned | NO   | PRI | 0       |       |  | clock     | int(11)             | NO   | PRI | 0       |       |  | num       | int(11)             | NO   |     | 0       |       |  | value_min | double(16,4)        | NO   |     | 0.0000  |       |  | value_avg | double(16,4)        | NO   |     | 0.0000  |       |  | value_max | double(16,4)        | NO   |     | 0.0000  |       |  +-----------+---------------------+------+-----+---------+-------+    mysql> desc trends_uint;  +-----------+---------------------+------+-----+---------+-------+  | Field     | Type                | Null | Key | Default | Extra |  +-----------+---------------------+------+-----+---------+-------+  | itemid    | bigint(20) unsigned | NO   | PRI | 0       |       |  | clock     | int(11)             | NO   | PRI | 0       |       |  | num       | int(11)             | NO   |     | 0       |       |  | value_min | bigint(20) unsigned | NO   |     | 0       |       |  | value_avg | bigint(20) unsigned | NO   |     | 0       |       |  | value_max | bigint(20) unsigned | NO   |     | 0       |       |  +-----------+---------------------+------+-----+---------+-------+

hosts

hosts 非常重要,保存了每个agent、proxy等的IP 、hostid、状态、IPMI等信息, 
几乎是记录了一台设备的所有的信息。

当然hostid是当中非常非常重要的信息,其他的表一般都时关联hostid的。

mysql> desc hosts;  +--------------------+---------------------+------+-----+-----------+-------+  | Field              | Type                | Null | Key | Default   | Extra |  +--------------------+---------------------+------+-----+-----------+-------+  | hostid             | bigint(20) unsigned | NO   | PRI | 0         |       |  | proxy_hostid       | bigint(20) unsigned | NO   | MUL | 0         |       |  | host               | varchar(64)         | NO   | MUL |           |       |  | dns                | varchar(64)         | NO   |     |           |       |  | useip              | int(11)             | NO   |     | 1         |       |  | ip                 | varchar(39)         | NO   |     | 127.0.0.1 |       |  | port               | int(11)             | NO   |     | 10050     |       |  | status             | int(11)             | NO   | MUL | 0         |       |  | disable_until      | int(11)             | NO   |     | 0         |       |  | error              | varchar(128)        | NO   |     |           |       |  | available          | int(11)             | NO   |     | 0         |       |  | errors_from        | int(11)             | NO   |     | 0         |       |  | lastaccess         | int(11)             | NO   |     | 0         |       |  | inbytes            | bigint(20) unsigned | NO   |     | 0         |       |  | outbytes           | bigint(20) unsigned | NO   |     | 0         |       |  | useipmi            | int(11)             | NO   |     | 0         |       |  | ipmi_port          | int(11)             | NO   |     | 623       |       |  | ipmi_authtype      | int(11)             | NO   |     | 0         |       |  | ipmi_privilege     | int(11)             | NO   |     | 2         |       |  | ipmi_username      | varchar(16)         | NO   |     |           |       |  | ipmi_password      | varchar(20)         | NO   |     |           |       |  | ipmi_disable_until | int(11)             | NO   |     | 0         |       |  | ipmi_available     | int(11)             | NO   |     | 0         |       |  | snmp_disable_until | int(11)             | NO   |     | 0         |       |  | snmp_available     | int(11)             | NO   |     | 0         |       |  | maintenanceid      | bigint(20) unsigned | NO   |     | 0         |       |  | maintenance_status | int(11)             | NO   |     | 0         |       |  | maintenance_type   | int(11)             | NO   |     | 0         |       |  | maintenance_from   | int(11)             | NO   |     | 0         |       |  | ipmi_ip            | varchar(64)         | NO   |     | 127.0.0.1 |       |  | ipmi_errors_from   | int(11)             | NO   |     | 0         |       |  | snmp_errors_from   | int(11)             | NO   |     | 0         |       |  | ipmi_error         | varchar(128)        | NO   |     |           |       |  | snmp_error         | varchar(128)        | NO   |     |           |       |  +--------------------+---------------------+------+-----+-----------+-------+

其实1.0的版本中,是没有这么多的字段的,好像只有hostid、host、status、disable_until 
等几个字段,但1.8已经如此丰富了。

hosts_groups

hosts_groups 保存了host(主机)与host groups(主机组)的关联关系。

这部分信息可以在我们自己做一些批量查询,例如查询关联到某个主机组的所有 
设备的IP 、存活状态等,进一步去查询该批量设备的load、IO、mem等统计信息。

我之前做的一个简单的报表就是例如了这部分的信息去查询某个业务线下所有设备 
的一周统计信息,当然了是在同一个主机组或者模版组才可以的。

mysql> desc hosts_groups ;  +-------------+---------------------+------+-----+---------+-------+  | Field       | Type                | Null | Key | Default | Extra |  +-------------+---------------------+------+-----+---------+-------+  | hostgroupid | bigint(20) unsigned | NO   | PRI | 0       |       |  | hostid      | bigint(20) unsigned | NO   | MUL | 0       |       |  | groupid     | bigint(20) unsigned | NO   | MUL | 0       |       |  +-------------+---------------------+------+-----+---------+-------+

items

items 表保存了采集项的信息。

mysql> desc items ;  +-----------------------+---------------------+------+-----+---------+-------+  | Field                 | Type                | Null | Key | Default | Extra |  +-----------------------+---------------------+------+-----+---------+-------+  | itemid                | bigint(20) unsigned | NO   | PRI | 0       |       |  | type                  | int(11)             | NO   |     | 0       |       |  | snmp_community        | varchar(64)         | NO   |     |         |       |  | snmp_oid              | varchar(255)        | NO   |     |         |       |  | snmp_port             | int(11)             | NO   |     | 161     |       |  | hostid                | bigint(20) unsigned | NO   | MUL | 0       |       |  | description           | varchar(255)        | NO   |     |         |       |  | key_                  | varchar(255)        | NO   |     |         |       |  | delay                 | int(11)             | NO   |     | 0       |       |  | history               | int(11)             | NO   |     | 90      |       |  | trends                | int(11)             | NO   |     | 365     |       |  | lastvalue             | varchar(255)        | YES  |     | NULL    |       |  | lastclock             | int(11)             | YES  |     | NULL    |       |  | prevvalue             | varchar(255)        | YES  |     | NULL    |       |  | status                | int(11)             | NO   | MUL | 0       |       |  | value_type            | int(11)             | NO   |     | 0       |       |  | trapper_hosts         | varchar(255)        | NO   |     |         |       |  | units                 | varchar(10)         | NO   |     |         |       |  | multiplier            | int(11)             | NO   |     | 0       |       |  | delta                 | int(11)             | NO   |     | 0       |       |  | prevorgvalue          | varchar(255)        | YES  |     | NULL    |       |  | snmpv3_securityname   | varchar(64)         | NO   |     |         |       |  | snmpv3_securitylevel  | int(11)             | NO   |     | 0       |       |  | snmpv3_authpassphrase | varchar(64)         | NO   |     |         |       |  | snmpv3_privpassphrase | varchar(64)         | NO   |     |         |       |  | formula               | varchar(255)        | NO   |     | 1       |       |  | error                 | varchar(128)        | NO   |     |         |       |  | lastlogsize           | int(11)             | NO   |     | 0       |       |  | logtimefmt            | varchar(64)         | NO   |     |         |       |  | templateid            | bigint(20) unsigned | NO   | MUL | 0       |       |  | valuemapid            | bigint(20) unsigned | NO   |     | 0       |       |  | delay_flex            | varchar(255)        | NO   |     |         |       |  | params                | text                | NO   |     | NULL    |       |  | ipmi_sensor           | varchar(128)        | NO   |     |         |       |  | data_type             | int(11)             | NO   |     | 0       |       |  | authtype              | int(11)             | NO   |     | 0       |       |  | username              | varchar(64)         | NO   |     |         |       |  | password              | varchar(64)         | NO   |     |         |       |  | publickey             | varchar(64)         | NO   |     |         |       |  | privatekey            | varchar(64)         | NO   |     |         |       |  | mtime                 | int(11)             | NO   |     | 0       |       |  +-----------------------+---------------------+------+-----+---------+-------+

media

media 保存了某个用户的media配置项,即对应的告警方式。

mysql> desc media;  +-------------+---------------------+------+-----+-----------------+-------+  | Field       | Type                | Null | Key | Default         | Extra |  +-------------+---------------------+------+-----+-----------------+-------+  | mediaid     | bigint(20) unsigned | NO   | PRI | 0               |       |  | userid      | bigint(20) unsigned | NO   | MUL | 0               |       |  | mediatypeid | bigint(20) unsigned | NO   | MUL | 0               |       |  | sendto      | varchar(100)        | NO   |     |                 |       |  | active      | int(11)             | NO   |     | 0               |       |  | severity    | int(11)             | NO   |     | 63              |       |  | period      | varchar(100)        | NO   |     | 1-7,00:00-23:59 |       |  +-------------+---------------------+------+-----+-----------------+-------+

media_type

media_type 表与media 表不同的是media_type 记录了某个告警方式对应的脚步等的存放路径。

mysql> desc media_type;  +-------------+---------------------+------+-----+---------+-------+  | Field       | Type                | Null | Key | Default | Extra |  +-------------+---------------------+------+-----+---------+-------+  | mediatypeid | bigint(20) unsigned | NO   | PRI | 0       |       |  | type        | int(11)             | NO   |     | 0       |       |  | description | varchar(100)        | NO   |     |         |       |  | smtp_server | varchar(255)        | NO   |     |         |       |  | smtp_helo   | varchar(255)        | NO   |     |         |       |  | smtp_email  | varchar(255)        | NO   |     |         |       |  | exec_path   | varchar(255)        | NO   |     |         |       |  | gsm_modem   | varchar(255)        | NO   |     |         |       |  | username    | varchar(255)        | NO   |     |         |       |  | passwd      | varchar(255)        | NO   |     |         |       |  +-------------+---------------------+------+-----+---------+-------+

media 与media_type 通过mediatypeid 键关联。

profiles

profiles 表保存了用户的一些配置项。

mysql> desc profiles ;  +-----------+---------------------+------+-----+---------+-------+  | Field     | Type                | Null | Key | Default | Extra |  +-----------+---------------------+------+-----+---------+-------+  | profileid | bigint(20) unsigned | NO   | PRI | 0       |       |  | userid    | bigint(20) unsigned | NO   | MUL | 0       |       |  | idx       | varchar(96)         | NO   |     |         |       |  | idx2      | bigint(20) unsigned | NO   |     | 0       |       |  | value_id  | bigint(20) unsigned | NO   |     | 0       |       |  | value_int | int(11)             | NO   |     | 0       |       |  | value_str | varchar(255)        | NO   |     |         |       |  | source    | varchar(96)         | NO   |     |         |       |  | type      | int(11)             | NO   |     | 0       |       |  +-----------+---------------------+------+-----+---------+-------+

rights

rights 表保存了用户组的权限信息,zabbix的权限一直也是我理不太清的地方, 
其实这个表里面有详细的记录。

mysql> desc rights;  +------------+---------------------+------+-----+---------+-------+  | Field      | Type                | Null | Key | Default | Extra |  +------------+---------------------+------+-----+---------+-------+  | rightid    | bigint(20) unsigned | NO   | PRI | 0       |       |  | groupid    | bigint(20) unsigned | NO   | MUL | 0       |       |  | permission | int(11)             | NO   |     | 0       |       |  | id         | bigint(20) unsigned | YES  | MUL | NULL    |       |  +------------+---------------------+------+-----+---------+-------+

screens

screens 表保存了用户定义的图片。

mysql> desc graphs;  +------------------+---------------------+------+-----+---------+-------+  | Field            | Type                | Null | Key | Default | Extra |  +------------------+---------------------+------+-----+---------+-------+  | graphid          | bigint(20) unsigned | NO   | PRI | 0       |       |  | name             | varchar(128)        | NO   | MUL |         |       |  | width            | int(11)             | NO   |     | 0       |       |  | height           | int(11)             | NO   |     | 0       |       |  | yaxismin         | double(16,4)        | NO   |     | 0.0000  |       |  | yaxismax         | double(16,4)        | NO   |     | 0.0000  |       |  | templateid       | bigint(20) unsigned | NO   |     | 0       |       |  | show_work_period | int(11)             | NO   |     | 1       |       |  | show_triggers    | int(11)             | NO   |     | 1       |       |  | graphtype        | int(11)             | NO   |     | 0       |       |  | show_legend      | int(11)             | NO   |     | 0       |       |  | show_3d          | int(11)             | NO   |     | 0       |       |  | percent_left     | double(16,4)        | NO   |     | 0.0000  |       |  | percent_right    | double(16,4)        | NO   |     | 0.0000  |       |  | ymin_type        | int(11)             | NO   |     | 0       |       |  | ymax_type        | int(11)             | NO   |     | 0       |       |  | ymin_itemid      | bigint(20) unsigned | NO   |     | 0       |       |  | ymax_itemid      | bigint(20) unsigned | NO   |     | 0       |       |  +------------------+---------------------+------+-----+---------+-------+

screens_items

同graphs_items。

mysql> desc screens_items;  +--------------+---------------------+------+-----+---------+-------+  | Field        | Type                | Null | Key | Default | Extra |  +--------------+---------------------+------+-----+---------+-------+  | screenitemid | bigint(20) unsigned | NO   | PRI | 0       |       |  | screenid     | bigint(20) unsigned | NO   |     | 0       |       |  | resourcetype | int(11)             | NO   |     | 0       |       |  | resourceid   | bigint(20) unsigned | NO   |     | 0       |       |  | width        | int(11)             | NO   |     | 320     |       |  | height       | int(11)             | NO   |     | 200     |       |  | x            | int(11)             | NO   |     | 0       |       |  | y            | int(11)             | NO   |     | 0       |       |  | colspan      | int(11)             | NO   |     | 0       |       |  | rowspan      | int(11)             | NO   |     | 0       |       |  | elements     | int(11)             | NO   |     | 25      |       |  | valign       | int(11)             | NO   |     | 0       |       |  | halign       | int(11)             | NO   |     | 0       |       |  | style        | int(11)             | NO   |     | 0       |       |  | url          | varchar(255)        | NO   |     |         |       |  | dynamic      | int(11)             | NO   |     | 0       |       |  +--------------+---------------------+------+-----+---------+-------+

sessions

sessions 表很重要,保存了每个用户的sessions,在登陆、注销的时候均会操作 
该张表的。

做cas等统一认证时,需要了解下该表和相关的登陆、验证流程。有兴趣的看我 
前面的文章吧。

mysql> desc sessions;  +------------+---------------------+------+-----+---------+-------+  | Field      | Type                | Null | Key | Default | Extra |  +------------+---------------------+------+-----+---------+-------+  | sessionid  | varchar(32)         | NO   | PRI |         |       |  | userid     | bigint(20) unsigned | NO   | MUL | 0       |       |  | lastaccess | int(11)             | NO   |     | 0       |       |  | status     | int(11)             | NO   |     | 0       |       |  +------------+---------------------+------+-----+---------+-------+

triggers

triggers 顾名思义保存了trigger的所有信息。

mysql> desc triggers;  +-------------+---------------------+------+-----+---------+-------+  | Field       | Type                | Null | Key | Default | Extra |  +-------------+---------------------+------+-----+---------+-------+  | triggerid   | bigint(20) unsigned | NO   | PRI | 0       |       |  | expression  | varchar(255)        | NO   |     |         |       |  | description | varchar(255)        | NO   |     |         |       |  | url         | varchar(255)        | NO   |     |         |       |  | status      | int(11)             | NO   | MUL | 0       |       |  | value       | int(11)             | NO   | MUL | 0       |       |  | priority    | int(11)             | NO   |     | 0       |       |  | lastchange  | int(11)             | NO   |     | 0       |       |  | dep_level   | int(11)             | NO   |     | 0       |       |  | comments    | blob                | NO   |     | NULL    |       |  | error       | varchar(128)        | NO   |     |         |       |  | templateid  | bigint(20) unsigned | NO   |     | 0       |       |  | type        | int(11)             | NO   |     | 0       |       |  +-------------+---------------------+------+-----+---------+-------+

trigger_depends

trigger_depends 保存了trigger的依赖关系。

mysql> desc trigger_depends;  +----------------+---------------------+------+-----+---------+-------+  | Field          | Type                | Null | Key | Default | Extra |  +----------------+---------------------+------+-----+---------+-------+  | triggerdepid   | bigint(20) unsigned | NO   | PRI | 0       |       |  | triggerid_down | bigint(20) unsigned | NO   | MUL | 0       |       |  | triggerid_up   | bigint(20) unsigned | NO   | MUL | 0       |       |  +----------------+---------------------+------+-----+---------+-------+

users

不需要解释了,值的一提的部分用户配置会在该表中,例如auotlogin、autologout、 
url、theme等信息。

mysql> desc users;  +----------------+---------------------+------+-----+-------------+-------+  | Field          | Type                | Null | Key | Default     | Extra |  +----------------+---------------------+------+-----+-------------+-------+  | userid         | bigint(20) unsigned | NO   | PRI | 0           |       |  | alias          | varchar(100)        | NO   | MUL |             |       |  | name           | varchar(100)        | NO   |     |             |       |  | surname        | varchar(100)        | NO   |     |             |       |  | passwd         | char(32)            | NO   |     |             |       |  | url            | varchar(255)        | NO   |     |             |       |  | autologin      | int(11)             | NO   |     | 0           |       |  | autologout     | int(11)             | NO   |     | 900         |       |  | lang           | varchar(5)          | NO   |     | en_gb       |       |  | refresh        | int(11)             | NO   |     | 30          |       |  | type           | int(11)             | NO   |     | 0           |       |  | theme          | varchar(128)        | NO   |     | default.css |       |  | attempt_failed | int(11)             | NO   |     | 0           |       |  | attempt_ip     | varchar(39)         | NO   |     |             |       |  | attempt_clock  | int(11)             | NO   |     | 0           |       |  | rows_per_page  | int(11)             | NO   |     | 50          |       |  +----------------+---------------------+------+-----+-------------+-------+

转载:http://www.furion.info/623.html

 

使用python获取表中TCP连接数,生成Execl文件\

 

 

############sample 4

https://blog.csdn.net/IREwyz/article/details/50374500

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/IREwyz/article/details/50374500
zabbix是一个强大的网管工具,其功能和用途也随之多变,但万变不离其宗,对于庞大的监控系统性能优化、方案优化是不可缺少的。

以下是我阅读zabbix官方文档,结合两个项目的心得。

一、拓扑优化

zabbix再带proxy功能,因本公司自主研发私有云平台,故可以在个别项目上用虚拟机来作proxy服务器,在确保vm正常运作的前提下,减少了server端的负荷。

二、方案优化

那么有的同学说了,如果硬件条件有限且同一批次中标业务么有私有云,那么建议在agent端采取active checks模式,根据官网定义,虽然agent端有更复杂的流程,且有举例

1. Agent opens a TCP connection
2. Agent asks for the list of checks
3. Server responds with a list of items (item key, delay)4. Agent parses the response
5. TCP connection is closed
6. Agent starts periodical collection of data 

但是相对于被动agent,对于server的压力会大大减小,避免server端宕机,被动agent在此不做赘述,网上有很多部署方法。
若采用active checks模式,首先针对zabbix_agentd.conf文件修改

a)在ServerActive=xxx 填server端ip、

b)Hostname=xx 填agent的hostname 、

c)StartAgents=0 

()

注:如果此处值为0,那么passive checks 将被disable,同学们可以根据实际业务大小来分配主动、被动agent个数)


d)针对模板变更,

1.以任何一个现有模板为例,clone并重命名,假如重命名模板为TEST

2.将模板TEST里所有items和discovery rules里的items都变更type为atvice agent

3.将创建的host关联重命名的模板TEST,注意创建的时候,ip和port都填0,否则会因模式冲突看到一个红色的Z,让人觉得很不舒服,不是么


至此active-checks模式的agent部署完毕,可以在overview中查看模板中的监控项。


三 数据库优化(此部分转载,因时间关系,未能亲自验证,待有时间会把此部分自己试验结果奉上)

分享一个zabbix数据库的优化脚本,适合2.0版本。

对history,hostory_uint按日分区,trends,trends_uint按月分区;

关闭Houserkeeper:

vim zabbix_server.conf

DisableHousekeeper=1

对history,hostory_uint每日建立第二天的分区并删除45天前的分区
对trends,trends_uint每月20号建立下一个月的分区,并删除12个月前的分区

时间可以自己修改

由于events表的大小对仪表盘的展示速度影响很大,2.0以后的版本又对该表做过修改,具有主键约束,不能以clock做分区表优化,所以我每日都清理该表,只保留了3天的数据(根据自己的环境和需求而定,可以自己修改),很简单的一个定期维护脚本,希望能帮到大家

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59
#!/usr/bin/env python
# coding: utf-8
import MySQLdb
import datetime
now_time = datetime.datetime.now()
error_time = now_time.strftime('%Y-%m-%d %H:%M:%S')
theday = (now_time + datetime.timedelta(days=+1)).strftime('%Y%m%d')
next_day = (now_time + datetime.timedelta(days=+2)).strftime('%Y-%m-%d')
themonth = datetime.datetime(now_time.year,(now_time.month+1),now_time.day).strftime('%Y%m')
next_month = datetime.datetime(now_time.year,(now_time.month+2),1).strftime('%Y-%m-%d')
last_time = (now_time - datetime.timedelta(days=30)).strftime('%Y%m%d')
last_time_month = datetime.datetime((now_time.year-1),now_time.month,now_time.day).strftime('%Y%m')
events_time = (now_time - datetime.timedelta(days=1)).strftime('%Y-%m-%d')
history_time = (now_time - datetime.timedelta(days=45)).strftime('%Y%m%d')
trends_time = datetime.datetime((now_time.year-1),now_time.month,now_time.day).strftime('%Y%m')
table_day=['history', 'history_uint']
table_month=['trends', 'trends_uint']
conn=MySQLdb.connect(host='localhost',user='zabbix',passwd='zabbix',db='zabbix',port=3306)
cur=conn.cursor()
for name_d in table_day:
   try:
   ####新增分区#######
      cur.execute('ALTER TABLE `%s` ADD PARTITION (PARTITION p%s VALUES LESS THAN (UNIX_TIMESTAMP("%s 00:00:00")))' % (name_d, theday, next_day))
                                                                                                                                                 
   except MySQLdb.Error,e:
       print "[%s] Mysql Error %d: %s" % (error_time, e.args[0], e.args[1])
       pass
for name_m in table_month:
   try:
      ####新增分区#######
      if now_time.day == 20:
         cur.execute('ALTER TABLE `%s` ADD PARTITION (PARTITION p%s VALUES LESS THAN (UNIX_TIMESTAMP("%s 00:00:00")))' % (name_m, themonth, next_month))
   except MySQLdb.Error,e:
       print "[%s] Mysql Error %d: %s" % (error_time, e.args[0], e.args[1])
       pass
######清除events表1天前的数据######
try:
   cur.execute('DELETE FROM `events` where `clock` < UNIX_TIMESTAMP("%s 00:00:00")'% events_time)
   cur.execute('optimize table events;')
except MySQLdb.Error,e:
   print "[%s] Mysql Error %d: %s" % (error_time, e.args[0], e.args[1])
   pass
######清除history,histroy_uint表45天前的数据######
for name_d in table_day:
    try:
       cur.execute('ALTER TABLE `%s` DROP PARTITION p%s;' % (name_d, history_time))
    except MySQLdb.Error,e:
       print "[%s] Mysql Error %d: %s" % (error_time, e.args[0], e.args[1])
       pass
######清除trends,trends_uint表一年前的数据######
for name_m in table_month:
    try:
       cur.execute('ALTER TABLE `%s` DROP PARTITION p%s;' % (name_m, trends_time))
    except MySQLdb.Error,e:
       print "[%s] Mysql Error %d: %s" % (error_time, e.args[0], e.args[1])
       pass
conn.commit()
cur.close()
conn
---------------------
版权声明:本文为CSDN博主「IRE王一喆」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/IREwyz/article/details/50374500

 

####sample 5

 

http://blog.chinaunix.net/uid-26660567-id-4419434.html

https://www.cnblogs.com/gqdw/archive/2013/01/01/2841221.html

https://blog.csdn.net/ubggze6t/article/details/39119219

https://blog.csdn.net/shuxiang1990/article/details/53416090

 

分类: 系统运维

2014-08-15 15:15:39

 
首先理解housekeeper 是什么,看zabbix_server.conf配置文件

点击(此处)折叠或打开

  1. ### Option: HousekeepingFrequency
  2. # How often Zabbix will perform housekeeping procedure (in hours).
  3. # Housekeeping is removing unnecessary information from history, alert, and alarms tables.
  4. #
  5. # Mandatory: no
  6. # Range: 1-24
  7. # Default:
  8. # HousekeepingFrequency=1
  9. ### Option: MaxHousekeeperDelete
  10. # The table "housekeeper" contains "tasks" for housekeeping procedure in the format:
  11. # [housekeeperid], [tablename], [field], [value].
  12. # No more than 'MaxHousekeeperDelete' rows (corresponding to [tablename], [field], [value])
  13. # will be deleted per one task in one housekeeping cycle.
  14. # SQLite3 does not use this parameter, deletes all corresponding rows without a limit.
  15. # If set to 0 then no limit is used at all. In this case you must know what you are doing!
  16. #
  17. # Mandatory: no
  18. # Range: 0-1000000
  19. # Default:
  20. # MaxHousekeeperDelete=500
大意就是housekeeper就是清理数据库里过期的历史数据神马的。然后MaxHousekeeperDelete就是一个阀值,每次轮询housekeeper这个任务的时候,超过这个阀值的行都会被清理。

怎么查看housekeeper的执行情况?看日志:

点击(此处)折叠或打开

  1. grep housekeeper /var/log/zabbix/zabbix_server.log 
  2.   4850:20140809:175626.071 executing housekeeper
  3.   4850:20140809:181408.036 housekeeper [deleted 279622 hist/trends, 0 items, 0 events, 0 sessions, 0 alarms, 0 audit items in 1061.962644 sec, idle 1 hour(s)]
  4.   4850:20140809:191408.037 executing housekeeper
  5.   4850:20140809:192611.432 housekeeper [deleted 287033 hist/trends, 0 items, 0 events, 0 sessions, 0 alarms, 0 audit items in 723.394480 sec, idle 1 hour(s)]
  6.   4850:20140809:202611.433 executing housekeeper
  7.   4850:20140809:203638.243 housekeeper [deleted 266125 hist/trends, 0 items, 0 events, 0 sessions, 0 alarms, 0 audit items in 626.808964 sec, idle 1 hour(s)]
  8.   4850:20140809:213638.244 executing housekeeper
  9.   4850:20140809:215445.003 housekeeper [deleted 258097 hist/trends, 0 items, 0 events, 0 sessions, 0 alarms, 0 audit items in 1086.756768 sec, idle 1 hour(s)]
  10.   4850:20140809:225445.004 executing housekeeper
  11.   4850:20140809:230601.581 housekeeper [deleted 286602 hist/trends, 0 items, 0 events, 0 sessions, 0 alarms, 0 audit items in 676.576122 sec, idle 1 hour(s)]
  12. ....
  13. ....
关于housekeeper的执行过程,摘抄了zabbix论坛上的:

 

点击(此处)折叠或打开

  1. That is fine.
  2. Zabbix server housekeeper is doing all deletes in few stages:
  3. - in first is deleting from history* and trends* tables using clock key and it deletes ALL data from items older than specified in "Keep history" param,
  4. - in second stage is deleting rows of items of deleted items and deleted hosts (zabbix does not deletes all these data just when you click on delete but it adds all these items ids to 'housekeeper' table).
  5. - at the end it deletes items from events, acknowledgements, alarms tables
另外,在Monitor-Dashboard-Graphs里面也可以查看到Zabbix server的一些性能情况。
比如Zabbix internal process busy % 


到zabbix server上查看系统性能情况,发现io很高:

点击(此处)折叠或打开

  1. [root@zabbix ~]# iostat -xm 1
  2. Linux 2.6.32-431.5.1.el6.x86_64 (zabbix)     08/14/2014     _x86_64_    (2 CPU)
  3. avg-cpu: %user %nice %system %iowait %steal %idle
  4.            8.57 0.00 4.23 11.05 0.13 76.02
  5. Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await svctm %util
  6. xvda 0.00 588.40 0.56 275.92 0.01 3.38 25.11 0.19 0.69 0.84 23.15
  7. xvdb 0.00 76.12 0.29 59.23 0.01 0.53 18.46 1.12 18.87 1.88 11.17
  8. dm-0 0.00 0.00 0.86 999.67 0.02 3.90 8.04 0.86 0.86 0.26 26.15
  9. dm-1 0.00 0.00 0.00 0.00 0.00 0.00 8.00 0.00 67.83 6.77 0.00
  10. avg-cpu: %user %nice %system %iowait %steal %idle
  11.           23.98 0.00 11.73 42.35 0.51 21.43
  12. Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await svctm %util
  13. xvda 0.00 2964.00 20.00 1292.00 0.31 16.39 26.07 13.19 9.43 0.60 78.90
  14. xvdb 0.00 37.00 0.00 36.00 0.00 0.14 8.00 4.02 59.64 6.64 23.90
  15. dm-0 0.00 0.00 22.00 4385.00 0.42 17.13 8.16 57.78 11.69 0.18 81.30
  16. dm-1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
  17. avg-cpu: %user %nice %system %iowait %steal %idle
  18.           18.46 0.00 6.67 34.36 0.00 40.51
  19. Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await svctm %util
  20. xvda 0.00 1849.00 4.00 826.00 0.14 10.69 26.72 11.90 15.43 1.01 83.90
  21. xvdb 0.00 0.00 0.00 83.00 0.00 0.47 11.57 6.01 94.92 3.66 30.40
  22. dm-0 0.00 0.00 1.00 2700.00 0.02 10.55 8.01 57.89 23.79 0.32 87.10
  23. dm-1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
感谢linux群友提供的实际经验:

点击(此处)折叠或打开

  1. “建议你照着群上的那个文档去优化,基本10w的items不会有太大问题”
  2. 广州-小王 15:44:26
  3. 做分区表,直接删分区就好了
  4. 广州-小王 15:44:43
  5. 文档上有说,很简单,用脚本定时去删
  6. 广州-小王 15:45:09
  7. history开头的都能做分区,events表貌似2.0后就不能直接做了,有外键约束
  8. 广州-小王 15:48:53
  9. housekeeper有点废,当你量到达一定程度的时候删的速度没你增加的快..
  10. 广州-Samma 15:38:33
  11. 看housekeeper具体做了什么事。如果写对象是小一些的表,可以放到内存。
  12. 广州-Samma 15:39:21
  13. 或者把housekeeper的频度调大一些。 间隔N小时才执行一次
先写到这里吧,搞优化去了
优化参考:
先配置独立数据库,使用独立表空间
http://junqili.com/zabbix/zabbix-performance-tunning/
然后按照官网的这个文档对mysql 做分区
https://www.zabbix.org/wiki/Docs/howto/mysql_partition

参考资料:https://www.zabbix.com/forum/showthread.php?t=43311
https://www.zabbix.com/forum/showthread.php?t=43311&page=2

 

 

 下面开始介绍:

1.添加监控表结构详解

(1)hosts,存储被监控的机器的信息,表结构如下:

 

(2)items

(3)hosts_templates,存储机器和模版或者模版和模版之间的关系

由于模版和机器都存储在hosts表中,所以hosts_templates和hosts 之间可以hostid关联也可以通过templateid关联。

 (4)interface,存储了所有设备的ip和端口的数据。(由于hosts表中不仅保存了设备信息还保存了模版信息,所以统计实际监控的设备,此表更加准确)

 

 2.数据存储表结构详解

 

将clock 转化为人性化时间:

3.报警相关表结构详解

 

(1)triggers

 

 

 

附 functions 表结构:

 (2)events

 

 

例子:

1. 找出某台主机的所有items ,含有某个key_的item , 统计items 总个数
SELECT * FROM HOSTS WHERE hostid=10157;
SELECT * FROM items WHERE hostid=10157 AND key_ LIKE '%agent%';
SELECT COUNT(*) FROM items;

2. 找出触发trigger次数最多的事件,并按trigger 降序排列。
SELECT a.description, COUNT(*) cnt FROM TRIGGERS a , EVENTS b 
WHERE a.triggerid=b.objectid ORDER BY cnt DESC ;
3. 从item记录各找出一个value类型为整形,浮点型的key_。
统计这两个key_ 存储在history或者history_uint 某一个时间段(比如2017/06/12)
的最大值,最小值,平均值,然后与 trends 或者 trends_unint 中相应时间段做对比

整型
SELECT * FROM items WHERE value_type=3 AND hostid=10157 LIMIT 1;
SELECT * FROM history_uint a,trends_uint b WHERE a.clock=b.clock AND a.itemid=b.itemid LIMIT 1;

浮点型
SELECT * FROM items WHERE value_type=0 AND hostid=10157 LIMIT 1;
SELECT * FROM history a,trends b WHERE a.clock=b.clock AND a.itemid=b.itemid LIMIT 1;

 

4.统计Zabbix Dashboard中triggers总数的来源。

SELECT  count(*)
  FROM TRIGGERS
WHERE triggerid IN
       (SELECT triggerid
          FROM functions
         WHERE itemid IN (SELECT itemid
                            FROM items
                           WHERE hostid IN (SELECT hostid FROM interface)
                             AND key_ NOT LIKE '%#%'
                             AND key_ NOT LIKE '%discovery%'
                             AND STATUS != 1));
 
说明:
通过之前对zabbix表结构的学习,我们知道,表triggers和functions相关联,而functions和items相关联,那么,要对triggers做统计,就需要从这三张表下手。
关键就是对items表中的数据做出筛选,key_中带“#”和“discovery”的和status=1(不可用状态)都要排除,这样就统计出来了。
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!