秦悦明的运维笔记

zabbix获取最大值

1. 目的

zabbix获取某些groups的某个item的最大值。

2. 思路

获取数据无非是两种,一种是通过api,另一张则更暴力,通过database。我之前用api的较多,但是发现这个场景下面直接从database下面获取反而更容易编码。

3. 具体做法

逆向思维,历史数据存在history的几张表里,那个item类型是int,所以放在history_uint下面。现在想想还一部分在trend表里面。
history_uint的数据结构如下:

1
2
3
4
5
6
7
8
+--------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+-------+
| itemid | bigint(20) unsigned | NO | MUL | NULL | |
| clock | int(11) | NO | | 0 | |
| value | bigint(20) unsigned | NO | | 0 | |
| ns | int(11) | NO | | 0 | |
+--------+---------------------+------+-----+---------+-------+

是根据itemid来存放的,那么怎么通过host group来获取itemid, 自然联系到items这个表,

1
2
3
4
5
6
7
8
9
10
11
12
13
+-----------------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+---------------------+------+-----+---------+-------+
| itemid | bigint(20) unsigned | NO | PRI | NULL | |
| type | int(11) | NO | | 0 | |
| snmp_community | varchar(64) | NO | | | |
| snmp_oid | varchar(255) | NO | | | |
| hostid | bigint(20) unsigned | NO | MUL | NULL | |
| name | varchar(255) | NO | | | |
| key_ | varchar(255) | NO | | | |
|
`

select一个row:

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
itemid: 10009
type: 0
snmp_community:
snmp_oid:
hostid: 10001
name: Number of processes
key_: proc.num[]
delay: 60
history: 7
trends: 365
status: 0
value_type: 3
trapper_hosts:
units:
multiplier: 0
delta: 0
snmpv3_securityname:
snmpv3_securitylevel: 0
snmpv3_authpassphrase:
snmpv3_privpassphrase:
formula: 1
error:
lastlogsize: 0
logtimefmt:
templateid: NULL
valuemapid: NULL
delay_flex:
params:
ipmi_sensor:
data_type: 0
authtype: 0
username:
password:
publickey:
privatekey:
mtime: 0
flags: 0
interfaceid: NULL
port:
description: Total number of processes in any state.
inventory_link: 0
lifetime: 0
snmpv3_authprotocol: 0
snmpv3_privprotocol: 0
state: 0
snmpv3_contextname:
evaltype: 0

再看下具体数据,那么大致就了解思路了。可以通过hostid来查itemid 。

然后就是怎么获取hostid了,数据库里面有hosts表,groups表,但是两者并没有直接联系的,关键联系表是hosts_groups,

1
2
3
4
5
6
7
+-------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+---------+-------+
| hostgroupid | bigint(20) unsigned | NO | PRI | NULL | |
| hostid | bigint(20) unsigned | NO | MUL | NULL | |
| groupid | bigint(20) unsigned | NO | MUL | NULL | |
+-------------+---------------------+------+-----+---------+-------+

非常赞,通过groupid能直接找到hostid ,用sql 来表示就是

1
select hostid from hosts_groups where groupid in (a, b ,c);"

因为我是多个group,所以用in来代替等于号。这样我们有了hostid。

再通过查询items表获取itemid, 这里要根据key_来过滤:

1
select itemid from items where key_ like 'emsmob.connect' and hostid=xx

最后有了itemid以后就可以用过history_uint来查最大值:

1
select max(value) from history_uint where itemid=%d and clock>1493598330

这里我加了个clock ,查一个月的数据,不加的话会很慢。加了以后查询一般在10s左右。。还是比较慢。但是比不加要好多了。

clock可以用date命令来找到:

1
date -d "2017-05-01 00:25:30" +%s

把上面所有要素连起来就是完整程序了,再用csv处理一下,输出成csv格式的~