Ad Widget

Collapse

The problem of Division by zero

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xiaoquwl
    Junior Member
    • Sep 2010
    • 7

    #1

    The problem of Division by zero

    Hi all,

    I have a problem to ask.
    One Item I used in zabbix is calculated, it ‘s formula is last("To_SAA-CSJCZ1_UDP-RTTSum") / last("To_SAA-CSJCZ1_UDP-NumOfRTT")
    But in the log file “zabbix_server.log”
    there is a error like
    “error: Division by zero. Cannot evaluate expression [0/0]”
    How can I avoid this error by adding some judgment, I mean if the last("To_SAA-CSJCZ1_UDP-NumOfRTT") is zero, I just set this formula’s result is “no data”, or if it’s not, then it’s will be calculated. can I ? How can I do that ?
    Is there anybody can help me ?

    That will be very thankful for you information.
  • marcel
    Senior Member
    Zabbix Certified Specialist
    • Oct 2010
    • 112

    #2
    adjust the formula so you never divide by zero:

    last("To_SAA-CSJCZ1_UDP-RTTSum") /( last("To_SAA-CSJCZ1_UDP-NumOfRTT")+0.1)
    Zabbix Certified Specialist for Large Environments since 12/2010

    Comment

    • toks
      Junior Member
      • Apr 2011
      • 9

      #3
      Hi, is there a better answer for this problem? To sum '0.1' doesn't seem an alternative to me. Is there a function that I can use in calculated items that returns zero if there is a division by zero?

      Thanks in advance.

      Comment

      • nelsonab
        Senior Member
        Zabbix Certified SpecialistZabbix Certified Professional
        • Sep 2006
        • 1233

        #4
        Use the Lua patch. :-)
        RHCE, author of zbxapi
        Ansible, the missing piece (Zabconf 2017): https://www.youtube.com/watch?v=R5T9NidjjDE
        Zabbix and SNMP on Linux (Zabconf 2015): https://www.youtube.com/watch?v=98PEHpLFVHM

        Comment

        • rogeriomgatto
          Junior Member
          • Jun 2012
          • 1

          #5
          This formula below works for me, it changes 0 to 1 but doesn't change anything if divisor is not 0.

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

          Comment

          • Pada
            Senior Member
            • Apr 2012
            • 236

            #6
            Originally posted by rogeriomgatto
            This formula below works for me, it changes 0 to 1 but doesn't change anything if divisor is not 0.

            last("X") /( last("Y") + count("Y",#1,0) )
            Thank you! That is a fantastic way of doing it!

            Unfortunately that method does not work when you devide by the sum of 2 items.
            eg. last("X") / ( last("Y") + last("Z") )
            So for that case I'm doing:
            last("X") / ( last("Y") + last("Z") + 0.000001 )
            Last edited by Pada; 10-10-2013, 12:44. Reason: Added sum of 2 item paragraph

            Comment

            • clarkritchie
              Member
              • Aug 2013
              • 46

              #7
              We also get a lot of "Division by zero. Cannot evaluate expression" errors based on a formula that's on an item prototype in a discovery rule. Here's what a coworker came up with:


              100*last(If.32.ifInOctets.["{#SNMPINDEX}"],0)/last(If.32.ifSpeed.["{#SNMPINDEX}"],0)


              If.32.ifSpeed.["{#SNMPINDEX}"] is sometimes 0, I think maybe when the interface is disabled?

              Anyhow, I believe the ",0" is not needed at all as we are not time shifting the results of the last() function, thus it does nothing.

              Based on reply #5 (above), the formula would become:


              100*last(If.32.ifOutOctets.["{#SNMPINDEX}"])/(last(If.32.ifSpeed.["{#SNMPINDEX}"]) + count(If.32.ifSpeed.["{#SNMPINDEX}"],#1,0))


              But I still get div by 0 errors. Advice?

              Comment

              • Zabbix_User_1138
                Junior Member
                • Jul 2014
                • 21

                #8
                Originally posted by clarkritchie
                We also get a lot of "Division by zero. Cannot evaluate expression" errors based on a formula that's on an item prototype in a discovery rule. Here's what a coworker came up with:


                100*last(If.32.ifInOctets.["{#SNMPINDEX}"],0)/last(If.32.ifSpeed.["{#SNMPINDEX}"],0)


                If.32.ifSpeed.["{#SNMPINDEX}"] is sometimes 0, I think maybe when the interface is disabled?

                Anyhow, I believe the ",0" is not needed at all as we are not time shifting the results of the last() function, thus it does nothing.

                Based on reply #5 (above), the formula would become:


                100*last(If.32.ifOutOctets.["{#SNMPINDEX}"])/(last(If.32.ifSpeed.["{#SNMPINDEX}"]) + count(If.32.ifSpeed.["{#SNMPINDEX}"],#1,0))


                But I still get div by 0 errors. Advice?
                Works if you add double quotes around the counted itemkey, like this:
                100*last(If.32.ifOutOctets.["{#SNMPINDEX}"])/(last(If.32.ifSpeed.["{#SNMPINDEX}"]) + count("If.32.ifSpeed.["{#SNMPINDEX}"]",#1,0))

                I haven't tested it, but you might need to escape the double quotes within the quoted itemkey, with backslashes like this:
                100*last(If.32.ifOutOctets.["{#SNMPINDEX}"])/(last(If.32.ifSpeed.["{#SNMPINDEX}"]) + count("If.32.ifSpeed.[\"{#SNMPINDEX}\"]",#1,0))

                I know this is a very late reply, but others with the same issue might benefit.

                Comment

                • wally.hall
                  Junior Member
                  • Aug 2018
                  • 1

                  #9
                  I know this is an old thread, but it helped me today and I successfully "overcame" the issue of division by zero for percentage calculated items by further abusing the limitations of floating point arithmetic...

                  For the example typical percentage equation:

                  READING / MAXIMUM * 100

                  ...I've calculated everything several orders of magnitude larger before shrinking the output back down:

                  (READING * 10000000) / (MAXIMUM * 10000000) / 100000

                  (Note the final division is just me optimizing out the * 100...)

                  This seems to round out to zero in Zabbix - or at worse 0.001.

                  Comment

                  Working...