X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Futils_cmd_flush.c;h=0e7b350f581f02f07ccaf234c1ec0c225011b33d;hb=bc51ef217e871b80152eccd4e5f32659500f75f3;hp=e7737a0c8d01311111c7d74494532ac7469feeed;hpb=a79a29c826b99d9dd2b0214e3bccf7491509d8f5;p=collectd.git diff --git a/src/utils_cmd_flush.c b/src/utils_cmd_flush.c index e7737a0c..0e7b350f 100644 --- a/src/utils_cmd_flush.c +++ b/src/utils_cmd_flush.c @@ -1,6 +1,7 @@ /** * collectd - src/utils_cmd_flush.c * Copyright (C) 2008 Sebastian Harl + * Copyright (C) 2008 Florian Forster * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -15,36 +16,160 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * - * Author: + * Authors: * Sebastian "tokkee" Harl + * Florian "octo" Forster **/ #include "collectd.h" +#include "common.h" #include "plugin.h" +#include "utils_parse_option.h" -int handle_flush (FILE *fh, char **fields, int fields_num) +#define print_to_socket(fh, ...) \ + if (fprintf (fh, __VA_ARGS__) < 0) { \ + char errbuf[1024]; \ + WARNING ("handle_flush: failed to write to socket #%i: %s", \ + fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \ + return -1; \ + } + +static int add_to_array (char ***array, int *array_num, char *value) { + char **temp; + + temp = (char **) realloc (*array, sizeof (char *) * (*array_num + 1)); + if (temp == NULL) + return (-1); + + *array = temp; + (*array)[*array_num] = value; + (*array_num)++; + + return (0); +} /* int add_to_array */ + +int handle_flush (FILE *fh, char *buffer) +{ + int success = 0; + int error = 0; + int timeout = -1; + char **plugins = NULL; + int plugins_num = 0; + char **identifiers = NULL; + int identifiers_num = 0; - if ((fields_num != 1) && (fields_num != 2)) + int i; + + if ((fh == NULL) || (buffer == NULL)) + return (-1); + + DEBUG ("utils_cmd_flush: handle_flush (fh = %p, buffer = %s);", + (void *) fh, buffer); + + if (strncasecmp ("FLUSH", buffer, strlen ("FLUSH")) != 0) { - DEBUG ("unixsock plugin: us_handle_flush: " - "Wrong number of fields: %i", fields_num); - fprintf (fh, "-1 Wrong number of fields: Got %i, expected 1 or 2.\n", - fields_num); - fflush (fh); + print_to_socket (fh, "-1 Cannot parse command.\n"); return (-1); } + buffer += strlen ("FLUSH"); + + while (*buffer != 0) + { + char *opt_key; + char *opt_value; + int status; + + opt_key = NULL; + opt_value = NULL; + status = parse_option (&buffer, &opt_key, &opt_value); + if (status != 0) + { + print_to_socket (fh, "-1 Parsing options failed.\n"); + sfree (plugins); + sfree (identifiers); + return (-1); + } + + if (strcasecmp ("plugin", opt_key) == 0) + { + add_to_array (&plugins, &plugins_num, opt_value); + } + else if (strcasecmp ("identifier", opt_key) == 0) + { + add_to_array (&identifiers, &identifiers_num, opt_value); + } + else if (strcasecmp ("timeout", opt_key) == 0) + { + char *endptr; + + errno = 0; + endptr = NULL; + timeout = strtol (opt_value, &endptr, 0); + + if ((endptr == opt_value) || (errno != 0)) + { + print_to_socket (fh, "-1 Invalid value for option `timeout': " + "%s\n", opt_value); + sfree (plugins); + sfree (identifiers); + return (-1); + } + else if (timeout <= 0) + timeout = -1; + } + else + { + print_to_socket (fh, "-1 Cannot parse option %s\n", opt_key); + sfree (plugins); + sfree (identifiers); + return (-1); + } + } /* while (*buffer != 0) */ + + /* Add NULL entries for `any plugin' and/or `any value' if nothing was + * specified. */ + if (plugins_num == 0) + add_to_array (&plugins, &plugins_num, NULL); + + if (identifiers_num == 0) + add_to_array (&identifiers, &identifiers_num, NULL); + + for (i = 0; i < plugins_num; i++) + { + char *plugin; + int j; - if (fields_num == 2) - timeout = atoi (fields[1]); + plugin = plugins[i]; - INFO ("unixsock plugin: flushing all data"); - plugin_flush_all (timeout); - INFO ("unixsock plugin: finished flushing all data"); + for (j = 0; j < identifiers_num; j++) + { + char *identifier; + int status; + + identifier = identifiers[j]; + status = plugin_flush (plugin, timeout, identifier); + if (status == 0) + success++; + else + error++; + } + } + + if ((success + error) > 0) + { + print_to_socket (fh, "0 Done: %i successful, %i errors\n", + success, error); + } + else + { + plugin_flush (NULL, timeout, NULL); + print_to_socket (fh, "0 Done\n"); + } - fprintf (fh, "0 Done\n"); - fflush (fh); + sfree (plugins); + sfree (identifiers); return (0); } /* int handle_flush */