2 * collectd - src/utils_cmd_flush.c
3 * Copyright (C) 2008 Sebastian Harl
4 * Copyright (C) 2008 Florian Forster
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Sebastian "tokkee" Harl <sh at tokkee.org>
26 * Florian "octo" Forster <octo at collectd.org>
32 #include "utils_parse_option.h"
34 #define print_to_socket(fh, ...) \
36 if (fprintf (fh, __VA_ARGS__) < 0) { \
38 WARNING ("handle_flush: failed to write to socket #%i: %s", \
39 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
45 static int add_to_array (char ***array, int *array_num, char *value)
49 temp = (char **) realloc (*array, sizeof (char *) * (*array_num + 1));
54 (*array)[*array_num] = value;
58 } /* int add_to_array */
60 int handle_flush (FILE *fh, char *buffer)
66 char **plugins = NULL;
68 char **identifiers = NULL;
69 int identifiers_num = 0;
73 if ((fh == NULL) || (buffer == NULL))
76 DEBUG ("utils_cmd_flush: handle_flush (fh = %p, buffer = %s);",
79 if (strncasecmp ("FLUSH", buffer, strlen ("FLUSH")) != 0)
81 print_to_socket (fh, "-1 Cannot parse command.\n");
84 buffer += strlen ("FLUSH");
94 status = parse_option (&buffer, &opt_key, &opt_value);
97 print_to_socket (fh, "-1 Parsing options failed.\n");
103 if (strcasecmp ("plugin", opt_key) == 0)
105 add_to_array (&plugins, &plugins_num, opt_value);
107 else if (strcasecmp ("identifier", opt_key) == 0)
109 add_to_array (&identifiers, &identifiers_num, opt_value);
111 else if (strcasecmp ("timeout", opt_key) == 0)
117 timeout = strtod (opt_value, &endptr);
119 if ((endptr == opt_value) || (errno != 0) || (!isfinite (timeout)))
121 print_to_socket (fh, "-1 Invalid value for option `timeout': "
127 else if (timeout < 0.0)
134 print_to_socket (fh, "-1 Cannot parse option %s\n", opt_key);
139 } /* while (*buffer != 0) */
141 /* Add NULL entries for `any plugin' and/or `any value' if nothing was
143 if (plugins_num == 0)
144 add_to_array (&plugins, &plugins_num, NULL);
146 if (identifiers_num == 0)
147 add_to_array (&identifiers, &identifiers_num, NULL);
149 for (i = 0; i < plugins_num; i++)
156 for (j = 0; j < identifiers_num; j++)
161 identifier = identifiers[j];
162 status = plugin_flush (plugin,
163 DOUBLE_TO_CDTIME_T (timeout),
172 if ((success + error) > 0)
174 print_to_socket (fh, "0 Done: %i successful, %i errors\n",
179 plugin_flush (NULL, DOUBLE_TO_CDTIME_T (timeout), NULL);
180 print_to_socket (fh, "0 Done\n");
186 } /* int handle_flush */
188 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */