X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Futils_cmd_flush.c;h=30da63005b7cf5652562a2c04d97812be4c8afa2;hp=6fa8b7bf00b5d0b68979b4e828e1f7093e32f5dc;hb=633c3966f770e4d46651a2fe219a18d8a9907a9f;hpb=3f2f61c7d74d9204405a4b0c21390451c98665a7 diff --git a/src/utils_cmd_flush.c b/src/utils_cmd_flush.c index 6fa8b7bf..30da6300 100644 --- a/src/utils_cmd_flush.c +++ b/src/utils_cmd_flush.c @@ -1,29 +1,35 @@ /** * collectd - src/utils_cmd_flush.c - * Copyright (C) 2008 Sebastian Harl - * Copyright (C) 2008 Florian Forster + * 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 - * Free Software Foundation; only version 2 of the License is applicable. + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. * * Authors: * Sebastian "tokkee" Harl - * Florian "octo" Forster + * Florian "octo" Forster **/ #include "collectd.h" #include "common.h" #include "plugin.h" +#include "utils_parse_option.h" #define print_to_socket(fh, ...) \ if (fprintf (fh, __VA_ARGS__) < 0) { \ @@ -33,50 +39,131 @@ return -1; \ } -int handle_flush (FILE *fh, char **fields, int fields_num) +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; + double timeout = 0.0; + char **plugins = NULL; + int plugins_num = 0; + char **identifiers = NULL; + int identifiers_num = 0; int i; - for (i = 1; i < fields_num; 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) { - char *option = fields[i]; - int status = 0; + 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; - if (strncasecmp ("plugin=", option, strlen ("plugin=")) == 0) + opt_key = NULL; + opt_value = NULL; + status = parse_option (&buffer, &opt_key, &opt_value); + if (status != 0) { - char *plugin = option + strlen ("plugin="); + print_to_socket (fh, "-1 Parsing options failed.\n"); + sfree (plugins); + sfree (identifiers); + return (-1); + } - if (0 == plugin_flush_one (timeout, plugin)) - ++success; - else - ++error; + if (strcasecmp ("plugin", opt_key) == 0) + { + add_to_array (&plugins, &plugins_num, opt_value); } - else if (strncasecmp ("timeout=", option, strlen ("timeout=")) == 0) + else if (strcasecmp ("identifier", opt_key) == 0) { - char *endptr = NULL; - char *value = option + strlen ("timeout="); - + add_to_array (&identifiers, &identifiers_num, opt_value); + } + else if (strcasecmp ("timeout", opt_key) == 0) + { + char *endptr; + errno = 0; - timeout = strtol (value, &endptr, 0); + endptr = NULL; + timeout = strtod (opt_value, &endptr); - if ((endptr == value) || (0 != errno)) - status = -1; - else if (0 >= timeout) - timeout = -1; + if ((endptr == opt_value) || (errno != 0) || (!isfinite (timeout))) + { + print_to_socket (fh, "-1 Invalid value for option `timeout': " + "%s\n", opt_value); + sfree (plugins); + sfree (identifiers); + return (-1); + } + else if (timeout < 0.0) + { + timeout = 0.0; + } } else - status = -1; - - if (status != 0) { - print_to_socket (fh, "-1 Cannot parse option %s\n", option); + 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; + + plugin = plugins[i]; + + for (j = 0; j < identifiers_num; j++) + { + char *identifier; + int status; + + identifier = identifiers[j]; + status = plugin_flush (plugin, + DOUBLE_TO_CDTIME_T (timeout), + identifier); + if (status == 0) + success++; + else + error++; + } } if ((success + error) > 0) @@ -86,10 +173,12 @@ int handle_flush (FILE *fh, char **fields, int fields_num) } else { - plugin_flush_all (timeout); + plugin_flush (NULL, DOUBLE_TO_CDTIME_T (timeout), NULL); print_to_socket (fh, "0 Done\n"); } + sfree (plugins); + sfree (identifiers); return (0); } /* int handle_flush */