From: Sebastian Harl Date: Mon, 8 Dec 2008 23:33:55 +0000 (+0100) Subject: collectd-nagios: Generalized the "percentage" consolidation function. X-Git-Tag: collectd-4.6.0~127 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=2ca211b327b5e1689937a93ac6a4d0b9754d3627;p=collectd.git collectd-nagios: Generalized the "percentage" consolidation function. Calculation of the percentage is not limited to two values any more but an arbitrary number may be used now. This allows a more flexible usage. Also, the documentation has been updated. --- diff --git a/src/collectd-nagios.c b/src/collectd-nagios.c index 1d989cbd..15252b19 100644 --- a/src/collectd-nagios.c +++ b/src/collectd-nagios.c @@ -447,37 +447,54 @@ static int do_check_con_sum (size_t values_num, return (status_code); } /* int do_check_con_sum */ -static int do_check_con_percentage (int values_num, double *values, char **values_names) +static int do_check_con_percentage (size_t values_num, + double *values, char **values_names) { - int i; + int i; + double sum = 0.0; double percentage; - if (values_num != 2) - return (RET_WARNING); - if (isnan (values[0]) || isnan (values[1])) - return (RET_WARNING); - if ((values[0] + values[1]) == 0) - return (RET_WARNING); + const char *status_str = "UNKNOWN"; + int status_code = RET_UNKNOWN; - percentage = 100 * values[1] / ( values[0] + values[1] ); + if ((values_num < 1) || (isnan (values[0]))) + { + printf ("WARNING: The first value is not defined\n"); + return (RET_WARNING); + } - printf ("%lf percentage |", percentage); for (i = 0; i < values_num; i++) - printf (" %s=%lf;;;;", values_names[i], values[i]); + if (!isnan (values[i])) + sum += values[i]; + + if (sum == 0.0) + { + printf ("WARNING: Values sum up to zero\n"); + return (RET_WARNING); + } + + percentage = 100.0 * values[0] / sum; if (match_range (&range_critical_g, percentage) != 0) { - printf ("CRITICAL: percentage = %lf\n", percentage); - return (RET_CRITICAL); + status_str = "CRITICAL"; + status_code = RET_CRITICAL; } else if (match_range (&range_warning_g, percentage) != 0) { - printf ("WARNING: percentage = %lf\n", percentage); - return (RET_WARNING); - } + status_str = "WARNING"; + status_code = RET_WARNING; + } + else + { + status_str = "OKAY"; + status_code = RET_OKAY; + } - printf ("OKAY: percentage = %lf\n", percentage); - return (RET_OKAY); + printf ("%s: %lf percent |", status_str, percentage); + for (i = 0; i < values_num; i++) + printf (" %s=%lf;;;;", values_names[i], values[i]); + return (status_code); } /* int do_check_con_percentage */ static int do_check (void) diff --git a/src/collectd-nagios.pod b/src/collectd-nagios.pod index 49800255..c6347eac 100644 --- a/src/collectd-nagios.pod +++ b/src/collectd-nagios.pod @@ -63,6 +63,12 @@ The warning and critical ranges are applied to the average of all values. The warning and critical ranges are applied to the sum of all values. +=item B + +The warning and critical ranges are applied to the ratio (in percent) of the +first value and the sum of all values. A warning is returned if the first +value is not defined or if all values sum up to zero. + =back =item B<-c> I @@ -96,7 +102,7 @@ As usual for Nagios plugins, this program writes a short, one line status message to STDOUT and signals success or failure with it's return value. It exits with a return value of B<0> for I, B<1> for I and B<2> for I. If the values are not available or some other error occurred, -it returns B<3> for I. +it returns B<3> for I. =head1 SEE ALSO