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

4 History functions

此处列出的所有函数都支持:

下面列出的函数没有附加信息。点击该函数查看完整的详细信息。

函数 说明
change 上一个值和最新值之间的差值。
changecount 在定义的评估期内相邻值之间的变化数值。
count 在定义的评估期内的值计数。
countunique 在定义的评估期内唯一值的数量。
find 在定义的评估期间查找匹配的值。
first 在定义的评估间内的第一个(最早的)值。
fuzzytime 检查被动代理时间与Zabbix server/proxy 时间的差异。
last 最新的值。
logeventid 检查最后一个日志条目的事件ID是否与正则表达式匹配。
logseverity 最后一个日志条目的日志严重性。
logsource 检查最后一个日志条目的日志源是否匹配正则表达式。
monodec 检查值是否单调下降。
monoinc 检查值是否单调增加。
nodata 检查是否未收到数据。
percentile 周期的第P个百分位数,其中P(百分比)由第三个参数指定。
rate 在定义的时间段内,单调递增的计数器每秒的平均递增速率。
常用参数
  • /host/key 是引用主机监控项历史记录函数的常用强制性首选参数
  • (sec|#num)<:time shift> 是引用主机监控项历史记录函数的常用强制性次选参数 ,其中:
    • sec - 以秒为单位的最大 评估周期 (可以使用时间 后缀 ),或者
    • #num - 最新收集值最大 评估范围(如果前面有井号)
    • time shift (可选) 允许将评估点及时移回。参阅有关指定时间偏移 更多详细内容

函数详情

关于函数参数的一般性说明:

  • 函数参数用逗号分隔
  • 可选函数参数(或参数部分)由< >表示
  • 每个函数都描述了特定的函数参数
  • /host/key(sec|#num)<:time shift>参数绝对不能加引号
change(/host/key)

上一个值和最新值之间的差值。
支持的值类型:浮点整数字符串文本日志
对于字符串返回:0 - 值相等; 1 - 值不同。

参数: 参见 [常用参数](https://www.zabbix.com/documentation/current/en/manual/appendix/functions/history#common-parameters)

备注:

  • 计算数值差异, 如以下传入示例值所示 ('上一个值' 和 '最新值' = 差异):
    '1' 和 '5' = +4
    '3' 和 '1' = -2
    '0' 和 '-2.5' = -2.5
  • 另参见: abs 进行比较。

示例:

change(/host/key)>10
changecount(/host/key,(sec|#num)<:time shift>,<mode>)

在定义的评估期内相邻值之间的变化数值。
支持的值类型:浮点整数字符串文本日志

参数:

  • 参见常用参数;
  • mode (必须用双引号括起来) - 可能的值: all - 所有变更项的计数 (默认); dec - 计数减少;inc - 计数增加

对于非数值类型,忽略 mode参数。

示例:

changecount(/host/key,1w) #the number of value changes for the last week until now
       changecount(/host/key,#10,"inc") #the number of value increases (relative to the adjacent value) among the last 10 values
       changecount(/host/key,24h,"dec") #the number of value decreases (relative to the adjacent value) for the last 24 hours until now
count(/host/key,(sec|#num)<:time shift>,<operator>,<pattern>)

在定义的评估期内的值计数。
支持的值类型:浮点整数字符串文本日志

参数:

  • 参见常用参数;
  • operator (必须用双引号引用起来)。 支持的 运算符:
    eq - 等于
    ne - 不等于
    gt - 大于
    ge - 大于或等于
    lt - 小于
    le - 小于或等于
    like - 如果包含模式则匹配 (区分大小写)
    bitand - 按位进行 AND
    regexp - 在pattern中区分大小写匹配给定的正则表达式
    iregexp - 在pattern中不区分大小写匹配给定的正则表达式
  • pattern - 必需的模式(字符串参数必须用双引号引起来)。

备注:

  • 浮点数监控项匹配精度为 2.22e-16;
  • like不支持作为整数值的运算符;
  • 不支持likebitand 作为浮点数值的运算符;
  • 对于字符串、文本和日志值,仅支持eq, ne, like, regexp and iregexp 运算符;
  • 使用bitand 作为运算符,第四个 pattern 参数可以被指定为两个数字, 由 '/'分隔:number_to_compare_with/mask。count()从值和掩码中计算“按位与” ,并将结果与number_to_compare_with进行比较。如果"按位与" 的结果等于 number_to_compare_with,则对该值进行计数。
    如果number_to_compare_with掩码 相等,则只需要指定掩码(不带“/”)。
  • 使用regexp or iregexp 作为运算符,第四个 pattern 参数可以是普通 或者全局 (以 '@'开头)正则表达式。在全局正则表达式的情况下,区分大小写是从全局正则表达式设置继承的。 出于正则表达式匹配的目的,浮点值将始终在 '.'之后用4个十进制数字表示。还要注意的是对于大数字,十进制(存储在数据库中)和二进制(由Zabbix Server使用)表示的差异可能会影响第四个十进制数字。

示例:

count(/host/key,10m) #the values for the last 10 minutes until now
       count(/host/key,10m,"like","error") #the number of values for the last 10 minutes until now that contain 'error'
       count(/host/key,10m,,12) #the number of values for the last 10 minutes until now that equal '12'
       count(/host/key,10m,"gt",12) #the number of values for the last 10 minutes until now that are over '12'
       count(/host/key,#10,"gt",12) #the number of values within the last 10 values until now that are over '12'
       count(/host/key,10m:now-1d,"gt",12) #the number of values between 24 hours and 10 minutes and 24 hours ago from now that were over '12'
       count(/host/key,10m,"bitand","6/7") #the number of values for the last 10 minutes until now having '110' (in binary) in the 3 least significant bits
       count(/host/key,10m:now-1d) #the number of values between 24 hours and 10 minutes and 24 hours ago from now
countunique(/host/key,(sec|#num)<:time shift>,<operator>,<pattern>)

在定义的评估期内唯一值的数量。
支持的值类型:浮点整数字符串文本日志

参数:

  • 参见常用参数;
  • operator (必须用双引号引用起来)。 支持的 运算符:
    eq - 等于
    ne - 不等于
    gt - 大于
    ge - 大于或等于
    lt - 小于
    le - 小于或等于
    like - 如果包含模式则匹配 (区分大小写)
    bitand - 按位进行 AND
    regexp - 在pattern中区分大小写匹配给定的正则表达式
    iregexp - 在pattern中不区分大小写匹配给定的正则表达式
  • pattern - 必需的模式(字符串参数必须用双引号引起来)。

备注:

  • 浮点数监控项匹配精度为 2.22e-16;
  • like不支持作为整数值的运算符;
  • 不支持likebitand 作为浮点数值的运算符;
  • 对于字符串、文本和日志值,仅支持eq, ne, like, regexp and iregexp 运算符;
  • 使用bitand 作为运算符,第四个 pattern 参数可以被指定为两个数字, 由 '/'分隔:number_to_compare_with/mask。count()从值和掩码中计算“按位与” ,并将结果与number_to_compare_with进行比较。如果"按位与" 的结果等于 number_to_compare_with,则对该值进行计数。
    如果number_to_compare_with掩码 相等,则只需要指定掩码(不带“/”)。
  • 使用regexp or iregexp 作为第三参数,第四个 pattern 参数可以是普通 或者全局 (以 '@'开头)正则表达式。在全局正则表达式的情况下,区分大小写是从全局正则表达式设置继承的。 出于正则表达式匹配的目的,浮点值将始终在 '.'之后用4个十进制数字表示。还要注意的是对于大数字,十进制(存储在数据库中)和二进制(由Zabbix Server使用)表示的差异可能会影响第四个十进制数字。

示例:

countunique(/host/key,10m) #the number of unique values for the last 10 minutes until now
       countunique(/host/key,10m,"like","error") #the number of unique values for the last 10 minutes until now that contain 'error'
       countunique(/host/key,10m,,12) #the number of unique values for the last 10 minutes until now that equal '12'
       countunique(/host/key,10m,"gt",12) #the number of unique values for the last 10 minutes until now that are over '12'
       countunique(/host/key,#10,"gt",12) #the number of unique values within the last 10 values until now that are over '12'
       countunique(/host/key,10m:now-1d,"gt",12) #the number of unique values between 24 hours and 10 minutes and 24 hours ago from now that were over '12'
       countunique(/host/key,10m,"bitand","6/7") #the number of unique values for the last 10 minutes until now having '110' (in binary) in the 3 least significant bits
       countunique(/host/key,10m:now-1d) #the number of unique values between 24 hours and 10 minutes and 24 hours ago from now
find(/host/key,(sec|#num)<:time shift>,<operator>,<pattern>)

在定义的评估期间查找匹配的值。
支持的值类型:浮点整数字符串文本日志
返回值: 1 - 找到; 0 - 否则。

参数:

  • 参见 常用参数;
  • sec or #num (可选) - 如果未指定,则默认为最新值
  • operator (必须用双引号引用起来)。 支持的 运算符:
    eq - 等于
    ne - 不等于
    gt - 大于
    ge - 大于或等于
    lt - 小于
    le - 小于或等于
    like - 如果包含模式则匹配 (区分大小写)
    bitand - 按位进行 AND
    regexp - 在pattern中区分大小写匹配给定的正则表达式
    iregexp - 在pattern中不区分大小写匹配给定的正则表达式
  • pattern - 必需的模式(字符串参数必须用双引号引起来);Perl 兼容正则表达式 (PCRE)正则表达式,如果运算符regexp, iregexp

备注:

  • 当处理多个值时, 如果至少有一个匹配值,则返回“1”;

  • like不支持作为整数值的运算符;

  • 不支持likebitand 作为浮点数值的运算符;

  • 对于字符串、文本和日志值,仅支持eq, ne, like, regexp and iregexp 运算符;

  • 使用regexp or iregexp 作为运算符,第四个 pattern 参数可以是普通 或者全局 (以 '@'开头)正则表达式。在全局正则表达式的情况下,区分大小写是从全局正则表达式设置继承的。

  • 示例:

    find(/host/key,10m,"like","error") #find a value that contains 'error' within the last 10 minutes until now

first(/host/key,sec<:time shift>)

在定义的评估间内的第一个(最早的)值。
支持的值类型:浮点整数字符串文本日志

参数:

另参见 last()

示例:

first(/host/key,1h) #retrieve the oldest value within the last hour until now
fuzzytime(/host/key,sec)

检查被动代理时间与Zabbix server/proxy 时间的差异。
支持的值类型:浮点整数
返回值:1 - 被动监控项值(作为时间戳)和Zabbix server/agent 时间戳(值收集的时钟)之间的差异小于或等于T秒; 0 - 否则。

参数:

备注:

  • 通常与 'system.localtime' 监控 项一起使用来检查本地时间是否与 Zabbix server.本地时间同步。请注意 必须将'system.localtime' 配置为被动检查
  • 也可以使用vfs.file.time[/path/file,modify]键来检查文件是否长时间没有更新;
  • 不建议在复杂的触发器表达式(涉及多个监控项)中使用此函数,因为它可能会导致不可预期的结果(时间差将使用最新的度量进行测量),例如 fuzzytime(/Host/system.localtime,60s)=0 或者 last(/Host/trap)<>0

示例:

fuzzytime(/host/key,60s)=0 #detect a problem if the time difference is over 60 seconds

last(/host/key,<#num<:time shift>>)

最新的值。
支持的值类型:浮点整数字符串文本日志

参数:

  • 参见 常用参数;
  • #num (可选) - 第N个最近的值。

备注:

  • 请注意,带哈希标签的时间段 (#N)在这里的工作方式与许多其他函数不同。例如:last() 总是等于 last(#1)last(#3) - 第三个最近的值(不是三个最近的值);
  • Zabbix不保证在历史中一秒中存在两个以上的值的精确顺序;
  • 另参见 See also first()

示例:

last(/host/key) #retrieve the last value
       last(/host/key,#2) #retrieve the previous value
       last(/host/key,#1) <> last(/host/key,#2) #the last and previous values differ
logeventid(/host/key,<#num<:time shift>>,<pattern>)

检查最后一个日志条目的事件ID是否与正则表达式匹配。.
支持的值类型:日志
返回值: 0 - 不匹配; 1 - 匹配。

参数:

  • 参见 常用参数
  • #num (可选) - 第N个最近的值;
  • pattern (可选) - 描述所需模式的正则表达式, Perl 兼容正则表达式 (PCRE) 样式(字符串参数必须用双引号引起来)。
logseverity(/host/key,<#num<:time shift>>)

最后一个日志条目的日志严重性。
支持的值类型:日志
返回值: 0 - 默认严重性; N - 严重性 (整数,适用于Windows事件日志: 1 -信息, 2 - 警告, 4 - 错误, 7 - 失败审计, 8 - 成功审计,9 - 严重, 10 - 详细)。

参数:

  • 参见 常用参数
  • #num (可选) - 第N个最近的值;

Zabbix从Windows事件日志的信息 字段获取日志严重性。

logsource(/host/key,<#num<:time shift>>,<pattern>)

检查最后一个日志条目的日志源是否匹配正则表达式。
支持的值类型:日志
返回值: 0 - 不匹配; 1 - 匹配。

参数:

  • 参见 常用参数
  • #num (可选) - 第N个最近的值;
  • pattern (可选) - 描述所需模式的正则表达式, Perl 兼容正则表达式 (PCRE) 样式(字符串参数必须用双引号引起来)。

通常用于Windows事件日志。

示例:

logsource(/host/key,,"VMware Server")
monodec(/host/key,(sec|#num)<:time shift>,<mode>)

检查值是否单调下降。
支持的值类型: 整数
返回值: 1 - 如果时间段中的所有元素连续减少; 0 - 否则。

参数:

  • 参见 常用参数
  • mode (必须用双引号括起来) - weak (每个值都小于前一个值或与前一个值相同;默认值)或者strict (每个值都减小了)。

示例:

monodec(/Host1/system.swap.size[all,free],60s) + monodec(/Host2/system.swap.size[all,free],60s) + monodec(/Host3/system.swap.size[all,free],60s) #calculate in how many hosts there has been a decrease in free swap size
monoinc(/host/key,(sec|#num)<:time shift>,<mode>)

检查值是否单调增加。
支持的值类型: 整数
返回值: 1 - 如果时间段中的所有元素连续增加; 0 - 否则。

参数:

  • 参见 常用参数
  • mode (必须用双引号括起来) - weak (每个值都大于前一个值或与前一个值相同;默认值)或者strict (每个值都增加了)。

示例:

monoinc(/Host1/system.localtime,#3,"strict")=0 #check if the system local time has been increasing consistently
nodata(/host/key,sec,<mode>)

检查是否未收到数据。
支持的值类型:浮点整数字符串文本日志
返回值: 1 -如果在定义的时间段内没有接收到数据; 0 - 否则。

参数:

  • 参加 常用参数
  • sec - -周期不应小于30秒,因为历史记录同步器进程仅每30秒计算一次此函数;不允许使用 nodata(/host/key,0)
  • mode - 如果设置为strict (双引号),此函数将对代理可用性不敏感(详细信息请参阅注释)。

备注:

  • 默认情况下,代理监视的 'nodata' 触发器对代理可用性敏感- 如果代理变得不可用,则 'nodata' ”触发器不会在恢复连接后立即触发,而是会跳过延迟时间段的数据。请注意,对于被动代理,如果连接恢复超过15秒且不少于2 &代理更新频率时间,则会激活抑制。对于活动代理,如果超过15秒后恢复连接,则激活抑制。要关闭对代理可用性的敏感性,请使用第三个参数,例如:nodata(/host/key,5m,"strict");在这种情况下,该函数将在没有数据的评估期(五分钟)结束后立即启动。
  • 如果在第一个参数的周期内,此函数将显示错误
    - 没有数据且 Zabbix server 已重启
    - 没有数据且维护已完成
    -没有数据且监控项已添加或者重新启用
  • 错误显示在触发器的Info列中配置
  • 如果Zabbix server, proxy 和 agent之间存在时间差, 则此函数可能无法正常工作。另参见: 时间同步要求
percentile(/host/key,(sec|#num)<:time shift>,percentage)

周期的第P个百分位数,其中P(百分比)由第三个参数指定。
支持的值类型:浮点整数

参数:

  • 参见常用参数;
  • percentage - 介于0和100(含)之间的浮点数,小数点后最多4位。
rate(/host/key,sec<:time shift>)

在定义的时间段内,单调递增的计数器每秒的平均递增速率。
支持的值类型:浮点整数

参数:

功能上对应于PromQL的 'rate' 。

示例:

rate(/host/key,30s) #if the monotonic increase over 30 seconds is 20, this function will return 0.67.

参见 所有支持的函数