2022 Zabbix中国峰会
2022 Zabbix中国峰会

2 触发器表达式

概述

触发器中使用的表达式是非常灵活的。你可以使用他们去创建关于监控统计的复杂逻辑测试。

一个简单有效的表达式看起来像:

{<server>:<key>.<function>(<parameter>)}<operator><constant>

1 函数

触发器函数允许引用收集的值,当前时间和其他因素。

可以使用的被支持函数的完整列表。

2 函数参数

大多数数字型的函数接受秒数来作为参数。

你可以使用前缀#来指定参数具有不同的含义:

函数内容 含义
sum(600) 600秒内所有值的总和
sum(#5) 最后5个值的总和

函数last当以#作为前缀使用时,值具有不同的含义,它会让她会选择第N个的上一个值,所以给定值3、7、2、6、5(按照时间顺序,第一个值3为最新值),last(#2) 将返回值为7last(#5) 将返回值为5

avg, count, last, min and max 函数支持额外的第二个参数time_shift(时间偏移量)。这个参数允许从过去一段时间内引用数据。例如,avg(1h,1d)将会返回一天前1小时的平均值。

<note tip>触发器需要使用history历史数据来计算。如果历史数据不可用(特别是关于time_shift时间偏移量),则无法使用趋势信息,因此必须至少保持触发器函数所预期这段时间的历史信息。 :::

你可以在触发器表达式中使用支持的单位符号,例如“5m”(分钟)可以被“300”秒代替,“1d”(天)可以被“86400”秒代替。“1k”代表“1024”bytes。

3 运算符

触发器支持的运算符(在执行中优先级递减)

优先级 运算 定义 **[未知值] /manual/config/triggers/expression#expressions_with_unsupported_items_and_unknown_values)的注释**
1 - Unary minus -Unknown → Unknown
2 not 逻辑非(NOT) **no ** Unknown → Unknown
3 * * Unknown → Unknown
(yes, Unknown, not 0 - to not lose
Unknown in arithmetic operations)
1.2 * Unknown → Unknown
/ 相除 U known / 0 → error
Unknown / 1.2 → Unknown
0.0 / Unknown → Unknown
4 + 相加 1 2 + Unknown → Unknown
- 相减 1 2 - Unknown → Unknown
5 < 小于. 该运算符定义:  1.2 **&A<B ⇔ (A<=B-0.000001) t;** Unknown → Unknown
<= 小于等于. Unk own <= Unknown → Unknown
> 大于. 该运算符定义:
A>B ⇔ (A>=B+0.000001)
>= 大于等于.
6 = 相等. 该运算符定义:
A=B ⇔ (A>B-0.000001) and (A<B+0.000001)
<> 不等于. 该运算符定义:
A<>B ⇔ (A<=B-0.000001) or (A>=B+0.000001)
7 and 逻辑与(AND) 0 ** nd** Unknown → 0
1 and Unknown → Unknown
Unknown and Unknown → Unknown
8 or 逻辑或(OR) 1 ** r** Unknown → 1
0 or Unknown → Unknown
Unknown or Unknown → Unknown

not, and and or 运算符区分大小写,而且必须为小写。它们也必须被空格或括号包围。

所有运算符中,除了unary - and not,都有从左到右的关联性。Unary - and not All operators, except unary - and not, have left-to-right associativity. Unary - and not are non-associative (meaning -(-1) and not (not 1) should be used instead of --1 and not not 1).

计算结果:

  • <, <=, >, >=, =, <> 如果指定的关系为真(ture),则运算符将会在触发器表达式中产生“1”;如果指定的关系为假(false),则返回“0”。如果一个运算对象为“Unknown”,那么结果为Unknown;
  • and 对于已知的运算对象,如果两个运算对象的比较不等于“0”,则运算符将会在触发器表达式中产生“1”,否则,它产生“0”;对于未知的运算对象,如果两个运算对象的比较等于“0”,则会产生“0”,否则,则会产生“Unknown”;
  • or 对于已知的运算对象,如果其中任意一个运算对象的比较不等于“0”,则运算符会在触发器表达式中产生“1”,否则,它产生“0”;对于未知的运算对象进行“or”运算,则只有当一个运算对象的比较不等于“0”,才会产生“1”,否则,它会产生“Unknown”;
  • 如果运算单位的值不等于“0”,那么逻辑否定运算符not对于已知运算对象的结果为“0”;
  • The result of the logical negation operator not for a known operand is '0' if the value of its operand compares unequal to '0'; '1' if the value of its operand compares equal to '0'. For unknown operand not yields 'Unknown'.

4 触发器示例

示例 1

触发器名称:Processor load is too high on www.zabbix.com。触发器表达式如下:

{www.zabbix.com:system.cpu.load[all,avg1].last()}>5

“www.zabbix.com:system.cpu.load[all,avg1]” 给出了被监控对象参数的简短名称。它指定了服务器是“www.zabbix.com”,监控项的键值是“system.cpu.load[all,avg1]”。通过使用函数“last()”获取最近一次获取的值。最后,“>5”表示来自主机www.zabbix.com的最后一次获取的负载值大于5时触发器就会进入PROBLEM状态。

示例 2

触发器名称:www.zabbix.com is overloaded。触发器表达式如下:

{www.zabbix.com:system.cpu.load[all,avg1].last()}>5 or {www.zabbix.com:system.cpu.load[all,avg1].min(10m)}>2 

当负载大于5或者最近10分钟内负载大于2,表达式为“TURE”,就会使触发器进入PROBLEM状态。

示例 3

触发器名称:/etc/passwd has been changed。触发器表达式如下:

使用了函数“diff":

{www.zabbix.com:vfs.file.cksum[/etc/passwd].diff()}=1

当文件/etc/passwd检查的checksum值与最近的值不同时,表达式为“TURE”,就会使触发器进入PROBLEM状态。 同样的表达式还可以用于监控重要的文件,比如文件/etc/passwd、/etc/inetd.conf、/kernel等等。

示例 4

触发器名称:Someone is downloading a large file from the Internet。触发器表达式如下:

使用函数“min”:

{www.zabbix.com:net.if.in[eth0,bytes].min(5m)}>100K

当网络适配器“eth0”在5分钟内接收的字节大于100KB,表达式为“TURE”,就会使触发器进入PROBLEM状态。

示例 5

触发器名称:Both nodes of clustered SMTP server are down。触发器表达式如下:

注意:在同一个表达式中使用了两个不同的主机

{smtp1.zabbix.com:net.tcp.service[smtp].last()}=0 and {smtp2.zabbix.com:net.tcp.service[smtp].last()}=0

当SMTP服务器“smtp1.zabbix.com”和“smtp2.zabbix.com”都停止,表达式为“TURE”,就会使触发器进入PROBLEM状态。

示例 6

触发器名称:Zabbix agent needs to be upgraded。触发器表达式如下:

使用函数“str()”:

{zabbix.zabbix.com:agent.version.str("beta8")}=1

如果Zabbix agent有beta8版本(大概为1.0beta8),表达式为“TURE”,就会使触发器进入PROBLEM状态。

示例 7

触发器名称:Server is unreachable。触发器表达式如下:

{zabbix.zabbix.com:icmpping.count(30m,0)}>5

当主机“zabbix.zabbix.com”在30分钟内超过5次不可达,表达式为“TURE”,就会使触发器进入PROBLEM状态。

示例 8

触发器名称:No heartbeats within last 3 minutes。触发器表达式如下:

使用函数“nodata()”:

{zabbix.zabbix.com:tick.nodata(3m)}=1

'tick'必须为'Zabbix trapper'类型。为了使这个触发器工作,监控项'tick'必须要定义,这个主机应使用zabbix_sender定期发送此参数的数据,如果在180秒内还未收到zabbix_sender发送的数据,那么触发器的状态就会变成PROBLEM。

示例9

CPU activity at night time 触发器的名称为:CPU activity at night time

使用了函数time():

{zabbix:system.cpu.load[all,avg1].min(5m)}>2 and {zabbix:system.cpu.load[all,avg1].time()}>000000 and {zabbix:system.cpu.load[all,avg1].time()}<060000

只有在凌晨0点到6点整,最后5分钟内cpu load大于2,触发器的状态才会变更为PROBLEM。

示例 10

触发器名称:Check if client local time is in sync with Zabbix server time 使用了函数fuzzytime():

{MySQL_DB:system.localtime.fuzzytime(10)}=0

当MySQL_DB的本地时间与Zabbix server之间的时间相差10秒以上,就会使触发的状态变更为PROBLEM。

示例 11

触发器名称为:Comparing average load today with average load of the same time yesterday (using a second time_shift parameter).

{server:system.cpu.load.avg(1h)}/{server:system.cpu.load.avg(1h,1d)}>2

This expression will fire if the average load of the last hour tops the average load of the same hour yesterday more than two times. 如果最后一小时的平均cpu load超过前一天的同一小时两倍,就会使触发器的状态变更为PROBLEM。

示例 12

使用了另一个监控项来获得触发器的阈值:

{Template PfSense:hrStorageFree[{#SNMPVALUE}].last()}<{Template PfSense:hrStorageSize[{#SNMPVALUE}].last()}*0.1

如果hrStorageFree低于10%,就会使触发器的状态变更为PROBLEM。

示例 13

使用 evaluation result 来获取触发器的数量超过阈值。

({server1:system.cpu.load[all,avg1].last()}>5) + ({server2:system.cpu.load[all,avg1].last()}>5) + ({server3:system.cpu.load[all,avg1].last()}>5)>=2

如果表达式中的两个触发器表达式的结果大于5,就会使触发器的状态变更为PROBLEM。

5 滞后(Hysteresis)

Sometimes we need an interval between an OK and Problem states, rather than a simple threshold. For example, we would like to define a trigger which becomes Problem when server room temperature goes above 20C and we want it to stay in that state until the temperature drops below 15C. 有时候我们需要一个触发器状态OK和PROBLEM之间的 区间,而不是简单的阈值。例如,我们想定义一个触发器,当机房的室温超过20摄氏度时,我们希望它保持这个状态,直至温度低于15摄氏度,触发器的状态才会变更为OK。

In order to do this, we first define the trigger expression for the problem event. Then select 'Recovery expression' for OK event generation and enter another expression for the OK event. 为了做到这一点,我们首先定义一个PROBLEM事件的触发器表达式,然后为OK event generation选择'Recovery expression',并为OK事件输入不同的表达式。

示例 1

触发器名称:Temperature in server room is too high.

Problem expression:

{server:temp.last()}>20

Recovery expression:

{server:temp.last()}<=15
示例 2

触发器名称:Free disk space is too low.

Problem expression: 在最近5分钟内文件系统/的空闲空间小于10GB。

{server:vfs.fs.size[/,free].max(5m)}<10G

Recovery expression: 在最近10分钟内文件系统/的空闲空间大于40GB。

{server:vfs.fs.size[/,free].min(10m)}>40G

6 具有不受支持的监控项和未知值的表达式

Versions before Zabbix 3.2 are very strict about unsupported items in a trigger expression. Any unsupported item in the expression immediately renders trigger value to Unknown. Zabbix3.2之前的版本对关于触发器表达式中的不受支持的监控项非常严格。表达式中任何不受支持的监控项都将触发器值立即显示为Unknown

Since Zabbix 3.2 there is a more flexible approach to unsupported items by admitting unknown values into expression evaluation: 从Zabbix3.2开始对于不受支持的监控项,通过将它们引入到表达式评估中,产生了一种更加灵活的表达方式:

  • For some functions their values are not affected by whether an item is supported or unsupported. Such functions are now evaluated even if they refer to unsupported items. See the list in functions and unsupported items.
  • 对于一些函数,它们的值不受监控项是否支持与不支持的影响,即使她们引用了不支持的监控项,也会对这些函数进行评估。关于这些函数,请参阅functions and unsupported items
  • Logical expressions with OR and AND can be evaluated to known values in two cases regardless of unknown operands:
  • 具有OR和AND的逻辑表达式可以在两种情况下评估为已知值,不用管未知的运算对象:
    • "1 or Unsuported_item1.some_function() or Unsuported_item2.some_function() or ..." can be evaluated to '1' (True),
    • "1 or Unsuported_item1.some_function() or Unsuported_item2.some_function() or ..."可以被评估为'1' (True),
    • "0 and Unsuported_item1.some_function() and Unsuported_item2.some_function() and ..." can be evaluated to '0' (False).
      Zabbix tries to evaluate logical expressions taking unsupported items as Unknown values. In the two cases mentioned above a known value will be produced; in other cases trigger value will be Unknown.
    • "0 and Unsuported_item1.some_function() and Unsuported_item2.some_function() and ..."可以被评估为'0' (False).\\Zabbix尝试评估将将不受支持的监控项作为Unknown值的逻辑表达式。在上述两种情况下,将产生一个Known值;在其他情况下,触发器将产生Unknown值。
  • If a function evaluation for supported item results in error, the function value is Unknown and it takes part in further expression evaluation.
  • 如果对受支持的监控项的一个函数评估结果为错误,那么这个函数的值为Unknown ,并且它将参与进一步的表达式评估。

Note that unknown values may "disappear" only in logical expressions as described above. In arithmetic expressions unknown values always lead to result Unknown (except division by 0). 备注:未知值只会在上述逻辑表达式中“消失”。在算数表达式中未知值总会导致结果为Unknown(除以0除外)。

If a trigger expression with several unsupported items evaluates to Unknown the error message in the frontend refers to the last unsupported item evaluated. 如果具有多个不受支持的监控项的触发器表达式评估为Unknown,那么在前端中的错误消息值的是最后一个不受支持的项目。