collectd-nagios: add new "percentage" aggregate function.
authorFabian Linzberger <e@lefant.net>
Mon, 3 Nov 2008 19:06:46 +0000 (20:06 +0100)
committerSebastian Harl <sh@tokkee.org>
Tue, 9 Dec 2008 11:39:40 +0000 (12:39 +0100)
the percentage aggregate function takes exactly two values. it
computes the percentage of the first value versus the sum of the first
and the second value. very useful for checking df values like:

sudo collectd-nagios -s /var/run/collectd-unixsock -H localhost -n df/df-root -d used -d free -g percentage -w 20: -c 10:

which will warn if there is less than 20% free space.

Signed-off-by: Sebastian Harl <sh@tokkee.org>
src/collectd-nagios.c

index f37f78d..1d989cb 100644 (file)
@@ -99,6 +99,7 @@
 #define CON_NONE     0
 #define CON_AVERAGE  1
 #define CON_SUM      2
+#define CON_PERCENTAGE  3
 
 struct range_s
 {
@@ -446,6 +447,39 @@ 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)
+{
+        int i;
+       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);
+
+        percentage = 100 * values[1] / ( values[0] + values[1] );
+
+       printf ("%lf percentage |", percentage);
+       for (i = 0; i < values_num; i++)
+               printf (" %s=%lf;;;;", values_names[i], values[i]);
+
+       if (match_range (&range_critical_g, percentage) != 0)
+       {
+               printf ("CRITICAL: percentage = %lf\n", percentage);
+               return (RET_CRITICAL);
+       }
+       else if (match_range (&range_warning_g, percentage) != 0)
+       {
+               printf ("WARNING: percentage = %lf\n", percentage);
+               return (RET_WARNING);
+        }
+
+        printf ("OKAY: percentage = %lf\n", percentage);
+        return (RET_OKAY);
+} /* int do_check_con_percentage */
+
 static int do_check (void)
 {
        lcc_connection_t *connection;
@@ -507,6 +541,8 @@ static int do_check (void)
                status =  do_check_con_average (values_num, values, values_names);
        else if (consolitation_g == CON_SUM)
                status = do_check_con_sum (values_num, values, values_names);
+       else if (consolitation_g == CON_PERCENTAGE)
+               status = do_check_con_percentage (values_num, values, values_names);
 
        free (values);
        if (values_names != NULL)
@@ -559,6 +595,8 @@ int main (int argc, char **argv)
                                        consolitation_g = CON_AVERAGE;
                                else if (strcasecmp (optarg, "sum") == 0)
                                        consolitation_g = CON_SUM;
+                               else if (strcasecmp (optarg, "percentage") == 0)
+                                       consolitation_g = CON_PERCENTAGE;
                                else
                                        usage (argv[0]);
                                break;