Linux 检查 Zabbix 触发器中两个字符串类型项的值是否相等

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9682771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:08:28  来源:igfitidea点击:

Check if values of two string-type items are equal in a Zabbix trigger

linuxstringmonitoringmirroringzabbix

提问by Micha? Kosmulski

I am monitoring an application using Zabbix and have defined a custom item which returns a string value. Since my item's values are actually checksums, they will only contain the characters [0-9a-f]. Two mirror copies of my application are running on two servers for the sake of redundancy. I would like to create a trigger which would take the item values from both machines and fire if they are not the same.

我正在使用 Zabbix 监视应用程序并定义了一个返回字符串值的自定义项。由于我的项目的值实际上是校验和,因此它们将仅包含字符[0-9a-f]。为了冗余,我的应用程序的两个镜像副本在两台服务器上运行。我想创建一个触发器,它将从两台机器中获取项目值,如果它们不相同则触发。

For a moment, let's forget about the moment when values change (it's not an atomic operation, so the system may see inconsistent state, which is not a real error, for a short time), since I could work around it by looking at several previous values.

暂时,让我们忘记值更改的那一刻(这不是原子操作,因此系统可能会在短时间内看到不一致的状态,这不是真正的错误),因为我可以通过查看几个以前的值。

The crux is: how to write a Zabbix trigger expression which could compare for equality the string values of two items (the same item on two mirror hosts, actually)?

关键是:如何编写一个 Zabbix 触发器表达式来比较两个项目(实际上是两个镜像主机上的相同项目)的字符串值是否相等?

Both according to the fine manualand as I confirmed in praxis, the standard operators =and #only work on numeric values, so I can't just write the natural {host1:myitem[param].last(0)} # {host2:myitem[param].last(0)}. Functions such as change()or diff()can only compare values of the same item at different points in time. Functions such as regexp()can only compare the item's value with a constant string/regular expression, not with another item's value. This is very limiting.

双方根据该精细的手工和我在实践中,标准运营商确认=,并#只对数值的工作,所以我不能只写自然{host1:myitem[param].last(0)} # {host2:myitem[param].last(0)}。诸如change()或之类的函数diff()只能比较同一项目在不同时间点的值。诸如此类的函数regexp()只能将项目的值与常量字符串/正则表达式进行比较,而不能与其他项目的值进行比较。这是非常有限的。

I could move the comparison logic into the script which my custom item executes, but it's a bit messy and not elegant, so if at all possible, I would prefer to have this logic inside my Zabbix trigger.

我可以将比较逻辑移动到我的自定义项执行的脚本中,但它有点凌乱且不优雅,所以如果可能的话,我更愿意在我的 Zabbix 触发器中使用这个逻辑。

Perhaps despite the limitations listed above, someone can come up with a workaround?

也许尽管有上面列出的限制,有人可以想出一个解决方法?

回答by Joao Figueiredo

Workaround:

解决方法:

{host1:myitem[param].change(0)} # {host2:myitem[param].change(0)}

When only one of the servers sees a modification since the previously received value, an event is triggered.

当只有一个服务器看到自先前接收到的值以来发生了修改时,就会触发一个事件。

From the Zabbix Manual,

从 Zabbix 手册中,

change (float, int, str, text, log)
Returns difference between last and previous values. For strings:
0 - values are equal
1 - values differ

change (float, int, str, text, log)
返回上一个值和上一个值之间的差异。对于字符串:
0 - 值相等
1 - 值不同

回答by David Cahill

I believe, and am struggling with this EXACT situation to this myself, that the correct way to do this is via calculated items.

我相信,并且我自己也在为这种确切的情况而苦苦挣扎,这样做的正确方法是通过计算项目。

You want to create a new ITEM, not trigger (yet!), that performs a calculated comparison on multiple item values (Strings Difference, Numbers within range, etc).

您想创建一个新的 ITEM,而不是触发器(还没有!),它对多个项目值(字符串差异、范围内的数字等)执行计算比较。

Once you have that item, have the calculation give you a value you can trigger off of. You can use ANY trigger functions in your calculation along with arrhythmic operations.

拥有该项目后,让计算为您提供一个可以触发的值。您可以在计算中使用任何触发函数以及心律失常操作。

Now to the issue (which I've submitted a feature request for because this is extremely limiting), most trigger expressions evaluate to a number or a 0/1 bool.

现在到这个问题(我已经提交了一个功能请求,因为这是非常有限的),大多数触发器表达式的计算结果为数字或 0/1 布尔值。

I think I have a solution for my problem, which is that I am tracking a version number from a webpage: e.g. v2.0.1, I believe I can use string connotation and regex in calculated items in order to convert my string values into multiple number values. As these would be a breeze to compare.

我想我对我的问题有一个解决方案,那就是我正在跟踪网页中的版本号:例如 v2.0.1,我相信我可以在计算项中使用字符串内涵和正则表达式,以便将我的字符串值转换为多个数字值。因为这些将是轻而易举的比较。

But again, this is convoluted and painful.

但同样,这是令人费解和痛苦的。

If you want my advice, have yourself or a dev look at the code for trigger expressions and see if you can submit a patch add one trigger function for simple string comparison. (Difference, Length, Possible conversion to numerical values (using binary and/or hex combinations) etc.)

如果您需要我的建议,请让您自己或开发人员查看触发器表达式的代码,看看您是否可以提交补丁添加一个触发器函数以进行简单的字符串比较。(差异、长度、可能的数值转换(使用二进制和/或十六进制组合)等)

I'm trying to work on a patch myself, but I don't have time as I have so much monitoring to implement and while zabbix is powerful, it's got several huge flaws. I still believe it's the best monitoring system out there.

我正在尝试自己开发补丁,但我没有时间,因为我有太多的监控要实施,虽然 zabbix 很强大,但它有几个巨大的缺陷。我仍然相信它是最好的监控系统。

Simple answer: Create a UserParameter until someone writes a patch.

简单的回答:创建一个 UserParameter,直到有人编写补丁。

回答by Marián ?erny

You could change your items to return numbers instead of strings. Because your items are checksums that are using only [0-9a-f]characters, they are numbers written in hexadecimal. So you would need to convert the checksum to decimal number.

您可以更改项目以返回数字而不是字符串。因为您的项目是仅使用[0-9a-f]字符的校验和,所以它们是用十六进制写入的数字。因此,您需要将校验和转换为十进制数。

Because the checksum is a big number, you would need to limit the hexadecimal number to 8 characters for Numeric (unsigned) type before conversion. Or if you would want higher precision, you could use float (but that would be more work):

由于校验和是一个大数字,因此您需要在转换前将数字(无符号)类型的十六进制数限制为 8 个字符。或者,如果您想要更高的精度,您可以使用浮点数(但这会更多工作):

Numeric (unsigned) - 64bit unsigned integer
Numeric (float) - floating point number
Negative values can be stored.
Allowed range (for MySQL): -999999999999.9999 to 999999999999.9999 (double(16,4)).

数字(无符号) - 64 位无符号整数
数字(浮点) - 浮点数
负值可以存储。
允许范围(对于 MySQL):-999999999999.9999 到 999999999999.9999(double(16,4))。

I wish Zabbix would have .hashedUnsigned()function that would compute hash of a string and return it as a number. Such a function should be easy to write.

我希望 Zabbix 具有.hashedUnsigned()计算字符串哈希值并将其作为数字返回的函数。这样的函数应该很容易编写。