collectd-nagios: Generalized the "percentage" consolidation function.
authorSebastian Harl <sh@tokkee.org>
Mon, 8 Dec 2008 23:33:55 +0000 (00:33 +0100)
committerSebastian Harl <sh@tokkee.org>
Tue, 9 Dec 2008 11:39:40 +0000 (12:39 +0100)
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.

src/collectd-nagios.c
src/collectd-nagios.pod

index 1d989cb..15252b1 100644 (file)
@@ -447,37 +447,54 @@ static int do_check_con_sum (size_t values_num,
        return (status_code);
 } /* int do_check_con_sum */
 
        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;
 
        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++)
        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)
        {
 
        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)
        {
        }
        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)
 } /* int do_check_con_percentage */
 
 static int do_check (void)
index 4980025..c6347ea 100644 (file)
@@ -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.
 
 
 The warning and critical ranges are applied to the sum of all values.
 
+=item B<percentage>
+
+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<range>
 =back
 
 =item B<-c> I<range>
@@ -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<success>, B<1> for I<warning> and B<2>
 for I<critical>. If the values are not available or some other error occurred,
 message to STDOUT and signals success or failure with it's return value. It
 exits with a return value of B<0> for I<success>, B<1> for I<warning> and B<2>
 for I<critical>. If the values are not available or some other error occurred,
-it returns B<3> for I<unknown>. 
+it returns B<3> for I<unknown>.
 
 =head1 SEE ALSO
 
 
 =head1 SEE ALSO