/**
* 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 <sh at tokkee.org>
- * Florian "octo" Forster <octo at verplant.org>
+ * Florian "octo" Forster <octo at collectd.org>
**/
#include "collectd.h"
#include "common.h"
#include "plugin.h"
+#include "utils_parse_option.h"
-struct flush_info_s
+int handle_flush (FILE *fh, char *buffer)
{
- char **plugins;
- int plugins_num;
- int timeout;
-};
-typedef struct flush_info_s flush_info_t;
+ int success = 0;
+ int error = 0;
+
+ double timeout = 0.0;
+ char **plugins = NULL;
+ size_t plugins_num = 0;
+ char **identifiers = NULL;
+ size_t identifiers_num = 0;
+
+ size_t i;
+
+#define PRINT_TO_SOCK(fh, ...) \
+ do { \
+ 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))); \
+ strarray_free (plugins, plugins_num); \
+ strarray_free (identifiers, identifiers_num); \
+ return -1; \
+ } \
+ fflush(fh); \
+ } while (0)
+
+ if ((fh == NULL) || (buffer == NULL))
+ return (-1);
-static int parse_option_plugin (flush_info_t *fi, const char *option)
-{
- char **temp;
+ DEBUG ("utils_cmd_flush: handle_flush (fh = %p, buffer = %s);",
+ (void *) fh, buffer);
- temp = (char **) realloc (fi->plugins,
- (fi->plugins_num + 1) * sizeof (char *));
- if (temp == NULL)
+ if (strncasecmp ("FLUSH", buffer, strlen ("FLUSH")) != 0)
{
- ERROR ("utils_cmd_flush: parse_option_plugin: realloc failed.");
+ PRINT_TO_SOCK (fh, "-1 Cannot parse command.\n");
return (-1);
}
- fi->plugins = temp;
+ buffer += strlen ("FLUSH");
- fi->plugins[fi->plugins_num] = strdup (option + strlen ("plugin="));
- if (fi->plugins[fi->plugins_num] == NULL)
+ while (*buffer != 0)
{
- /* fi->plugins is freed in handle_flush in this case */
- ERROR ("utils_cmd_flush: parse_option_plugin: strdup failed.");
- return (-1);
- }
- fi->plugins_num++;
-
- return (0);
-} /* int parse_option_plugin */
-
-static int parse_option_timeout (flush_info_t *fi, const char *option)
-{
- const char *value_ptr = option + strlen ("timeout=");
- char *endptr = NULL;
- int timeout;
-
- timeout = strtol (value_ptr, &endptr, 0);
- if (value_ptr == endptr)
- return (-1);
-
- fi->timeout = (timeout <= 0) ? (-1) : timeout;
-
- return (0);
-} /* int parse_option_timeout */
-
-static int parse_option (flush_info_t *fi, const char *option)
-{
- if (strncasecmp ("plugin=", option, strlen ("plugin=")) == 0)
- return (parse_option_plugin (fi, option));
- else if (strncasecmp ("timeout=", option, strlen ("timeout=")) == 0)
- return (parse_option_timeout (fi, option));
- else
- return (-1);
-} /* int parse_option */
-
-int handle_flush (FILE *fh, char **fields, int fields_num)
-{
- flush_info_t fi;
- int status;
- int i;
+ char *opt_key;
+ char *opt_value;
+ int status;
- memset (&fi, '\0', sizeof (fi));
- fi.timeout = -1;
-
- for (i = 1; i < fields_num; i++)
- {
- status = parse_option (&fi, fields[i]);
+ opt_key = NULL;
+ opt_value = NULL;
+ status = parse_option (&buffer, &opt_key, &opt_value);
if (status != 0)
{
- fprintf (fh, "-1 Cannot parse option %s\n", fields[i]);
- fflush (fh);
+ PRINT_TO_SOCK (fh, "-1 Parsing options failed.\n");
+ strarray_free (plugins, plugins_num);
+ strarray_free (identifiers, identifiers_num);
return (-1);
}
- }
- if (fi.plugins_num > 0)
+ if (strcasecmp ("plugin", opt_key) == 0)
+ strarray_add (&plugins, &plugins_num, opt_value);
+ else if (strcasecmp ("identifier", opt_key) == 0)
+ strarray_add (&identifiers, &identifiers_num, opt_value);
+ else if (strcasecmp ("timeout", opt_key) == 0)
+ {
+ char *endptr;
+
+ errno = 0;
+ endptr = NULL;
+ timeout = strtod (opt_value, &endptr);
+
+ if ((endptr == opt_value) || (errno != 0) || (!isfinite (timeout)))
+ {
+ PRINT_TO_SOCK (fh, "-1 Invalid value for option `timeout': "
+ "%s\n", opt_value);
+ strarray_free (plugins, plugins_num);
+ strarray_free (identifiers, identifiers_num);
+ return (-1);
+ }
+ else if (timeout < 0.0)
+ {
+ timeout = 0.0;
+ }
+ }
+ else
+ {
+ PRINT_TO_SOCK (fh, "-1 Cannot parse option %s\n", opt_key);
+ strarray_free (plugins, plugins_num);
+ strarray_free (identifiers, identifiers_num);
+ return (-1);
+ }
+ } /* while (*buffer != 0) */
+
+ for (i = 0; (i == 0) || (i < plugins_num); i++)
{
- int success = 0;
- for (i = 0; i < fi.plugins_num; i++)
+ char *plugin = NULL;
+ int j;
+
+ if (plugins_num != 0)
+ plugin = plugins[i];
+
+ for (j = 0; (j == 0) || (j < identifiers_num); j++)
{
- status = plugin_flush_one (fi.timeout, fi.plugins[i]);
+ char *identifier = NULL;
+ int status;
+
+ if (identifiers_num != 0)
+ identifier = identifiers[j];
+
+ status = plugin_flush (plugin,
+ DOUBLE_TO_CDTIME_T (timeout),
+ identifier);
if (status == 0)
success++;
+ else
+ error++;
}
- fprintf (fh, "0 Done: %i successful, %i errors\n",
- success, fi.plugins_num - success);
}
- else
- {
- plugin_flush_all (fi.timeout);
- fprintf (fh, "0 Done");
- }
- fflush (fh);
- for (i = 0; i < fi.plugins_num; i++)
- {
- sfree (fi.plugins[i]);
- }
- sfree (fi.plugins);
+ PRINT_TO_SOCK (fh, "0 Done: %i successful, %i errors\n",
+ success, error);
+ strarray_free (plugins, plugins_num);
+ strarray_free (identifiers, identifiers_num);
return (0);
+#undef PRINT_TO_SOCK
} /* int handle_flush */
/* vim: set sw=4 ts=4 tw=78 noexpandtab : */