From: Fabian Linzberger Date: Mon, 3 Nov 2008 19:06:46 +0000 (+0100) Subject: collectd-nagios: add new "percentage" aggregate function. X-Git-Tag: collectd-4.6.0~128 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=d324b71f4a80712261490d2c98bcca22a147c6c8;p=collectd.git collectd-nagios: add new "percentage" aggregate function. 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 --- diff --git a/src/collectd-nagios.c b/src/collectd-nagios.c index f37f78dd..1d989cbd 100644 --- a/src/collectd-nagios.c +++ b/src/collectd-nagios.c @@ -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;