Skip to content

Instantly share code, notes, and snippets.

@limitusus
Last active June 28, 2023 06:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save limitusus/449816251f0a9860b7d4155cc6326474 to your computer and use it in GitHub Desktop.
Save limitusus/449816251f0a9860b7d4155cc6326474 to your computer and use it in GitHub Desktop.
Zabbix: handle calculated item with zero division

Problem

Want to create a calculated item with last("X") / last("Y"). When Y = 0, the result should be 0.

Answer

last("X") * (1 - count("Y",#1,0)) / (last("Y") + count("Y",#1,0))

Reason

For simplicity, let's write as:

x := last("X")
y := last("Y")
c := count(Y,#1,0)

The formula is now written

x * (1 - c) / (y + c)

c is the key of this trick, which is 0 when Y != 0 and 1 when Y = 0.

y = 0

apply c = 1, y = 0

x * (1 - c) / (y + c)
=
x * (1 - 1) / (0 + 1) = 0

Zero division is avoided and the answer gets always 0.

y != 0

apply c = 0

x * (1 - c) / (y + c)
=
x * (1 - 0) / (y + 0)
=
x / y

this is expected answer.

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment