Code

Don't divide by 0 when doing percentages (e.g. with the swap plugin).
authorCorey Kosak <kosak@google.com>
Fri, 22 Jan 2016 21:56:37 +0000 (16:56 -0500)
committerMarc Fournier <marc.fournier@camptocamp.com>
Tue, 26 Jan 2016 06:15:54 +0000 (07:15 +0100)
Prior to this change, if all the values being sent sum to zero, then the
calculated percentage is a division by 0 which is either Inf or NaN
depending on the numerator.

This is not a theoretical concern: it can easily happen if all the input
gauges are 0.

This change forces the output to be zero if the denominator is zero.

src/daemon/plugin.c

index e7d4e8df2c801b7e6d0a0e6a8bdd4e120cedbf20..29716185db38b7ac59d9dc8a9bfe144812794c04 100644 (file)
@@ -2272,7 +2272,7 @@ int plugin_dispatch_multivalue (value_list_t const *template, /* {{{ */
                case DS_TYPE_GAUGE:
                        vl->values[0].gauge = va_arg (ap, gauge_t);
                        if (store_percentage)
-                               vl->values[0].gauge *= 100.0 / sum;
+                               vl->values[0].gauge *= sum ? (100.0 / sum) : 0;
                        break;
                case DS_TYPE_ABSOLUTE:
                        vl->values[0].absolute = va_arg (ap, absolute_t);