Merge pull request #3329 from efuss/fix-3311
[collectd.git] / src / collectd-nagios.c
index 993541d..0629849 100644 (file)
@@ -34,6 +34,7 @@
 
 #include <assert.h>
 #include <errno.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -71,7 +72,7 @@
 #endif
 #endif /* NAN_ZERO_ZERO */
 
-#include "libcollectdclient/collectd/client.h"
+#include "collectd/client.h"
 
 #define RET_OKAY 0
 #define RET_WARNING 1
@@ -93,17 +94,17 @@ typedef struct range_s range_t;
 extern char *optarg;
 extern int optind, opterr, optopt;
 
-static char *socket_file_g = NULL;
-static char *value_string_g = NULL;
-static char *hostname_g = NULL;
+static char *socket_file_g;
+static char *value_string_g;
+static char *hostname_g;
 
 static range_t range_critical_g;
 static range_t range_warning_g;
 static int consolitation_g = CON_NONE;
-static _Bool nan_is_error_g = 0;
+static bool nan_is_error_g;
 
-static char **match_ds_g = NULL;
-static size_t match_ds_num_g = 0;
+static char **match_ds_g;
+static size_t match_ds_num_g;
 
 /* `strdup' is an XSI extension. I don't want to pull in all of XSI just for
  * that, so here's an own implementation.. It's easy enough. The GCC attributes
@@ -115,10 +116,10 @@ cn_strdup(const char *str) /* {{{ */
   char *ret;
 
   strsize = strlen(str) + 1;
-  ret = (char *)malloc(strsize);
+  ret = malloc(strsize);
   if (ret != NULL)
     memcpy(ret, str, strsize);
-  return (ret);
+  return ret;
 } /* }}} char *cn_strdup */
 
 static int filter_ds(size_t *values_num, double **values,
@@ -127,19 +128,19 @@ static int filter_ds(size_t *values_num, double **values,
   char **new_names;
 
   if (match_ds_g == NULL)
-    return (RET_OKAY);
+    return RET_OKAY;
 
-  new_values = (gauge_t *)calloc(match_ds_num_g, sizeof(*new_values));
+  new_values = calloc(match_ds_num_g, sizeof(*new_values));
   if (new_values == NULL) {
     fprintf(stderr, "calloc failed: %s\n", strerror(errno));
-    return (RET_UNKNOWN);
+    return RET_UNKNOWN;
   }
 
-  new_names = (char **)calloc(match_ds_num_g, sizeof(*new_names));
+  new_names = calloc(match_ds_num_g, sizeof(*new_names));
   if (new_names == NULL) {
     fprintf(stderr, "calloc failed: %s\n", strerror(errno));
     free(new_values);
-    return (RET_UNKNOWN);
+    return RET_UNKNOWN;
   }
 
   for (size_t i = 0; i < match_ds_num_g; i++) {
@@ -153,7 +154,7 @@ static int filter_ds(size_t *values_num, double **values,
       for (j = 0; j < i; j++)
         free(new_names[j]);
       free(new_names);
-      return (RET_UNKNOWN);
+      return RET_UNKNOWN;
     }
 
     for (j = 0; j < *values_num; j++)
@@ -166,7 +167,7 @@ static int filter_ds(size_t *values_num, double **values,
       for (j = 0; j <= i; j++)
         free(new_names[j]);
       free(new_names);
-      return (RET_CRITICAL);
+      return RET_CRITICAL;
     }
 
     new_values[i] = (*values)[j];
@@ -180,7 +181,7 @@ static int filter_ds(size_t *values_num, double **values,
   *values = new_values;
   *values_names = new_names;
   *values_num = match_ds_num_g;
-  return (RET_OKAY);
+  return RET_OKAY;
 } /* int filter_ds */
 
 static void parse_range(char *string, range_t *range) {
@@ -227,7 +228,7 @@ static int match_range(range_t *range, double value) {
   if (!isnan(range->max) && (range->max < value))
     ret = 1;
 
-  return (((ret - range->invert) == 0) ? 0 : 1);
+  return ((ret - range->invert) == 0) ? 0 : 1;
 } /* int match_range */
 
 __attribute__((noreturn)) static void usage(const char *name) {
@@ -279,7 +280,7 @@ static int do_listval(lcc_connection_t *connection) {
     printf("UNKNOWN: %s\n", lcc_strerror(connection));
     if (ret_ident != NULL)
       free(ret_ident);
-    return (RET_UNKNOWN);
+    return RET_UNKNOWN;
   }
 
   status = lcc_sort_identifiers(connection, ret_ident, ret_ident_num);
@@ -287,7 +288,7 @@ static int do_listval(lcc_connection_t *connection) {
     printf("UNKNOWN: %s\n", lcc_strerror(connection));
     if (ret_ident != NULL)
       free(ret_ident);
-    return (RET_UNKNOWN);
+    return RET_UNKNOWN;
   }
 
   for (size_t i = 0; i < ret_ident_num; ++i) {
@@ -322,7 +323,7 @@ static int do_listval(lcc_connection_t *connection) {
 
   free(ret_ident);
   free(hostname);
-  return (RET_OKAY);
+  return RET_OKAY;
 } /* int do_listval */
 
 static int do_check_con_none(size_t values_num, double *values,
@@ -349,7 +350,7 @@ static int do_check_con_none(size_t values_num, double *values,
 
   if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0)) {
     printf("WARNING: No defined values found\n");
-    return (RET_WARNING);
+    return RET_WARNING;
   } else if ((num_critical == 0) && (num_warning == 0)) {
     status_str = "OKAY";
     status_code = RET_OKAY;
@@ -370,7 +371,7 @@ static int do_check_con_none(size_t values_num, double *values,
   }
   printf("\n");
 
-  return (status_code);
+  return status_code;
 } /* int do_check_con_none */
 
 static int do_check_con_average(size_t values_num, double *values,
@@ -389,7 +390,7 @@ static int do_check_con_average(size_t values_num, double *values,
         continue;
 
       printf("CRITICAL: Data source \"%s\" is NaN\n", values_names[i]);
-      return (RET_CRITICAL);
+      return RET_CRITICAL;
     }
 
     total += values[i];
@@ -398,7 +399,7 @@ static int do_check_con_average(size_t values_num, double *values,
 
   if (total_num == 0) {
     printf("WARNING: No defined values found\n");
-    return (RET_WARNING);
+    return RET_WARNING;
   }
 
   average = total / total_num;
@@ -419,7 +420,7 @@ static int do_check_con_average(size_t values_num, double *values,
     printf(" %s=%f;;;;", values_names[i], values[i]);
   printf("\n");
 
-  return (status_code);
+  return status_code;
 } /* int do_check_con_average */
 
 static int do_check_con_sum(size_t values_num, double *values,
@@ -437,7 +438,7 @@ static int do_check_con_sum(size_t values_num, double *values,
         continue;
 
       printf("CRITICAL: Data source \"%s\" is NaN\n", values_names[i]);
-      return (RET_CRITICAL);
+      return RET_CRITICAL;
     }
 
     total += values[i];
@@ -446,7 +447,7 @@ static int do_check_con_sum(size_t values_num, double *values,
 
   if (total_num == 0) {
     printf("WARNING: No defined values found\n");
-    return (RET_WARNING);
+    return RET_WARNING;
   }
 
   if (match_range(&range_critical_g, total) != 0) {
@@ -465,7 +466,7 @@ static int do_check_con_sum(size_t values_num, double *values,
     printf(" %s=%f;;;;", values_names[i], values[i]);
   printf("\n");
 
-  return (status_code);
+  return status_code;
 } /* int do_check_con_sum */
 
 static int do_check_con_percentage(size_t values_num, double *values,
@@ -478,7 +479,7 @@ static int do_check_con_percentage(size_t values_num, double *values,
 
   if ((values_num < 1) || (isnan(values[0]))) {
     printf("WARNING: The first value is not defined\n");
-    return (RET_WARNING);
+    return RET_WARNING;
   }
 
   for (size_t i = 0; i < values_num; i++) {
@@ -487,7 +488,7 @@ static int do_check_con_percentage(size_t values_num, double *values,
         continue;
 
       printf("CRITICAL: Data source \"%s\" is NaN\n", values_names[i]);
-      return (RET_CRITICAL);
+      return RET_CRITICAL;
     }
 
     sum += values[i];
@@ -495,7 +496,7 @@ static int do_check_con_percentage(size_t values_num, double *values,
 
   if (sum == 0.0) {
     printf("WARNING: Values sum up to zero\n");
-    return (RET_WARNING);
+    return RET_WARNING;
   }
 
   percentage = 100.0 * values[0] / sum;
@@ -514,7 +515,7 @@ static int do_check_con_percentage(size_t values_num, double *values,
   printf("%s: %lf percent |", status_str, percentage);
   for (size_t i = 0; i < values_num; i++)
     printf(" %s=%lf;;;;", values_names[i], values[i]);
-  return (status_code);
+  return status_code;
 } /* int do_check_con_percentage */
 
 static int do_check(lcc_connection_t *connection) {
@@ -526,14 +527,14 @@ static int do_check(lcc_connection_t *connection) {
   int status;
 
   snprintf(ident_str, sizeof(ident_str), "%s/%s", hostname_g, value_string_g);
-  ident_str[sizeof(ident_str) - 1] = 0;
+  ident_str[sizeof(ident_str) - 1] = '\0';
 
   status = lcc_string_to_identifier(connection, &ident, ident_str);
   if (status != 0) {
     printf("ERROR: Creating an identifier failed: %s.\n",
            lcc_strerror(connection));
     LCC_DESTROY(connection);
-    return (RET_CRITICAL);
+    return RET_CRITICAL;
   }
 
   status = lcc_getval(connection, &ident, &values_num, &values, &values_names);
@@ -541,14 +542,14 @@ static int do_check(lcc_connection_t *connection) {
     printf("ERROR: Retrieving values from the daemon failed: %s.\n",
            lcc_strerror(connection));
     LCC_DESTROY(connection);
-    return (RET_CRITICAL);
+    return RET_CRITICAL;
   }
 
   LCC_DESTROY(connection);
 
   status = filter_ds(&values_num, &values, &values_names);
   if (status != RET_OKAY)
-    return (status);
+    return status;
 
   status = RET_UNKNOWN;
   if (consolitation_g == CON_NONE)
@@ -566,7 +567,7 @@ static int do_check(lcc_connection_t *connection) {
       free(values_names[i]);
   free(values_names);
 
-  return (status);
+  return status;
 } /* int do_check */
 
 int main(int argc, char **argv) {
@@ -625,19 +626,19 @@ int main(int argc, char **argv) {
       tmp = realloc(match_ds_g, (match_ds_num_g + 1) * sizeof(char *));
       if (tmp == NULL) {
         fprintf(stderr, "realloc failed: %s\n", strerror(errno));
-        return (RET_UNKNOWN);
+        return RET_UNKNOWN;
       }
       match_ds_g = tmp;
       match_ds_g[match_ds_num_g] = cn_strdup(optarg);
       if (match_ds_g[match_ds_num_g] == NULL) {
         fprintf(stderr, "cn_strdup failed: %s\n", strerror(errno));
-        return (RET_UNKNOWN);
+        return RET_UNKNOWN;
       }
       match_ds_num_g++;
       break;
     }
     case 'm':
-      nan_is_error_g = 1;
+      nan_is_error_g = true;
       break;
     default:
       usage(argv[0]);
@@ -651,17 +652,17 @@ int main(int argc, char **argv) {
   }
 
   snprintf(address, sizeof(address), "unix:%s", socket_file_g);
-  address[sizeof(address) - 1] = 0;
+  address[sizeof(address) - 1] = '\0';
 
   connection = NULL;
   status = lcc_connect(address, &connection);
   if (status != 0) {
     printf("ERROR: Connecting to daemon at %s failed.\n", socket_file_g);
-    return (RET_CRITICAL);
+    return RET_CRITICAL;
   }
 
   if (0 == strcasecmp(value_string_g, "LIST"))
-    return (do_listval(connection));
+    return do_listval(connection);
 
-  return (do_check(connection));
+  return do_check(connection);
 } /* int main */