2 * collectd - src/utils_cmd_flush.c
3 * Copyright (C) 2008 Sebastian Harl
4 * Copyright (C) 2008 Florian Forster
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Sebastian "tokkee" Harl <sh at tokkee.org>
21 * Florian "octo" Forster <octo at verplant.org>
27 #include "utils_parse_option.h"
29 #define print_to_socket(fh, ...) \
30 if (fprintf (fh, __VA_ARGS__) < 0) { \
32 WARNING ("handle_flush: failed to write to socket #%i: %s", \
33 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
37 static int add_to_array (char ***array, int *array_num, char *value)
41 temp = (char **) realloc (*array, sizeof (char *) * (*array_num + 1));
46 (*array)[*array_num] = value;
50 } /* int add_to_array */
52 int handle_flush (FILE *fh, char *buffer)
58 char **plugins = NULL;
60 char **identifiers = NULL;
61 int identifiers_num = 0;
65 if ((fh == NULL) || (buffer == NULL))
68 DEBUG ("utils_cmd_flush: handle_flush (fh = %p, buffer = %s);",
71 if (strncasecmp ("FLUSH", buffer, strlen ("FLUSH")) != 0)
73 print_to_socket (fh, "-1 Cannot parse command.\n");
76 buffer += strlen ("FLUSH");
86 status = parse_option (&buffer, &opt_key, &opt_value);
89 print_to_socket (fh, "-1 Parsing options failed.\n");
95 if (strcasecmp ("plugin", opt_key) == 0)
97 add_to_array (&plugins, &plugins_num, opt_value);
99 else if (strcasecmp ("identifier", opt_key) == 0)
101 add_to_array (&identifiers, &identifiers_num, opt_value);
103 else if (strcasecmp ("timeout", opt_key) == 0)
109 timeout = strtod (opt_value, &endptr);
111 if ((endptr == opt_value) || (errno != 0) || (!isfinite (timeout)))
113 print_to_socket (fh, "-1 Invalid value for option `timeout': "
119 else if (timeout < 0.0)
126 print_to_socket (fh, "-1 Cannot parse option %s\n", opt_key);
131 } /* while (*buffer != 0) */
133 /* Add NULL entries for `any plugin' and/or `any value' if nothing was
135 if (plugins_num == 0)
136 add_to_array (&plugins, &plugins_num, NULL);
138 if (identifiers_num == 0)
139 add_to_array (&identifiers, &identifiers_num, NULL);
141 for (i = 0; i < plugins_num; i++)
148 for (j = 0; j < identifiers_num; j++)
153 identifier = identifiers[j];
154 status = plugin_flush (plugin,
155 DOUBLE_TO_CDTIME_T (timeout),
164 if ((success + error) > 0)
166 print_to_socket (fh, "0 Done: %i successful, %i errors\n",
171 plugin_flush (NULL, timeout, NULL);
172 print_to_socket (fh, "0 Done\n");
178 } /* int handle_flush */
180 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */