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

11,440

Solution 1

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,

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

Solution 2

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).

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.

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.

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.

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

Share:
11,440
Michał Kosmulski
Author by

Michał Kosmulski

Senior Software Engineer at Allegro Group (also: editor of our tech blog). Origami folder and designer (check out my flickr stream)

Updated on June 14, 2022

Comments

  • Michał Kosmulski
    Michał Kosmulski almost 2 years

    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.

    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)?

    Both according to the fine manual and 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.

    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.

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

  • Michał Kosmulski
    Michał Kosmulski about 12 years
    Thank you, this looks like a step in the right direction, but this won't trigger if the values are already different at the moment I start monitoring them. A smaller disadvantage is that the event will be triggered at the moment one of the values changes, but after that the trigger will go back to OK (the values are different but none of them is changing).