X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcollectd-nagios.c;h=1bf5b3f3dc6061be372aa725d5766dbc7283fe64;hb=654783aed1fab02766a9cc036e859799e01f6f9e;hp=63effd5528bd706a30426d0b22cb557fd91faee9;hpb=c8ec3924c92ad3ca15229e8e923eaece64221337;p=collectd.git diff --git a/src/collectd-nagios.c b/src/collectd-nagios.c index 63effd55..1bf5b3f3 100644 --- a/src/collectd-nagios.c +++ b/src/collectd-nagios.c @@ -142,7 +142,7 @@ static int get_values (int *ret_values_num, double **ret_values, struct sockaddr_un sa; int status; int fd; - FILE *fh; + FILE *fh_in, *fh_out; char buffer[4096]; int values_num; @@ -172,8 +172,8 @@ static int get_values (int *ret_values_num, double **ret_values, return (-1); } - fh = fdopen (fd, "r+"); - if (fh == NULL) + fh_in = fdopen (fd, "r"); + if (fh_in == NULL) { fprintf (stderr, "fdopen failed: %s\n", strerror (errno)); @@ -181,21 +181,37 @@ static int get_values (int *ret_values_num, double **ret_values, return (-1); } - fprintf (fh, "GETVAL %s/%s\n", hostname_g, value_string_g); - fflush (fh); + fh_out = fdopen (fd, "w"); + if (fh_out == NULL) + { + fprintf (stderr, "fdopen failed: %s\n", + strerror (errno)); + fclose (fh_in); + return (-1); + } - if (fgets (buffer, sizeof (buffer), fh) == NULL) + fprintf (fh_out, "GETVAL %s/%s\n", hostname_g, value_string_g); + fflush (fh_out); + + if (fgets (buffer, sizeof (buffer), fh_in) == NULL) { fprintf (stderr, "fgets failed: %s\n", strerror (errno)); - close (fd); + fclose (fh_in); + fclose (fh_out); return (-1); } - close (fd); fd = -1; - values_num = atoi (buffer); - if (values_num < 1) - return (-1); + { + char *ptr = strchr (buffer, ' '); + + if (ptr != NULL) + *ptr = '\0'; + + values_num = atoi (buffer); + if (values_num < 1) + return (-1); + } values = (double *) malloc (values_num * sizeof (double)); if (values == NULL) @@ -214,32 +230,33 @@ static int get_values (int *ret_values_num, double **ret_values, return (-1); } + i = 0; + while (fgets (buffer, sizeof (buffer), fh_in) != NULL) { - char *ptr = strchr (buffer, ' ') + 1; char *key; char *value; - i = 0; - while ((key = strtok (ptr, " \t")) != NULL) - { - ptr = NULL; - value = strchr (key, '='); - if (value == NULL) - continue; - *value = '\0'; value++; + key = buffer; - if (ignore_ds (key) != 0) - continue; + value = strchr (key, '='); + if (value == NULL) + continue; + *value = '\0'; value++; - values_names[i] = strdup (key); - values[i] = atof (value); + if (ignore_ds (key) != 0) + continue; - i++; - if (i >= values_num) - break; - } - values_num = i; + values_names[i] = strdup (key); + values[i] = atof (value); + + i++; + if (i >= values_num) + break; } + values_num = i; + + fclose (fh_in); fh_in = NULL; fd = -1; + fclose (fh_out); fh_out = NULL; *ret_values_num = values_num; *ret_values = values;