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 int handle_flush (FILE *fh, char *buffer)
40 char **plugins = NULL;
41 size_t plugins_num = 0;
42 char **identifiers = NULL;
43 size_t identifiers_num = 0;
47 #define PRINT_TO_SOCK(fh, ...) \
49 if (fprintf (fh, __VA_ARGS__) < 0) { \
51 WARNING ("handle_flush: failed to write to socket #%i: %s", \
52 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
53 strarray_free (plugins, plugins_num); \
54 strarray_free (identifiers, identifiers_num); \
60 if ((fh == NULL) || (buffer == NULL))
63 DEBUG ("utils_cmd_flush: handle_flush (fh = %p, buffer = %s);",
66 if (strncasecmp ("FLUSH", buffer, strlen ("FLUSH")) != 0)
68 PRINT_TO_SOCK (fh, "-1 Cannot parse command.\n");
71 buffer += strlen ("FLUSH");
81 status = parse_option (&buffer, &opt_key, &opt_value);
84 PRINT_TO_SOCK (fh, "-1 Parsing options failed.\n");
85 strarray_free (plugins, plugins_num);
86 strarray_free (identifiers, identifiers_num);
90 if (strcasecmp ("plugin", opt_key) == 0)
91 strarray_add (&plugins, &plugins_num, opt_value);
92 else if (strcasecmp ("identifier", opt_key) == 0)
93 strarray_add (&identifiers, &identifiers_num, opt_value);
94 else if (strcasecmp ("timeout", opt_key) == 0)
100 timeout = strtod (opt_value, &endptr);
102 if ((endptr == opt_value) || (errno != 0) || (!isfinite (timeout)))
104 PRINT_TO_SOCK (fh, "-1 Invalid value for option `timeout': "
106 strarray_free (plugins, plugins_num);
107 strarray_free (identifiers, identifiers_num);
110 else if (timeout < 0.0)
117 PRINT_TO_SOCK (fh, "-1 Cannot parse option %s\n", opt_key);
118 strarray_free (plugins, plugins_num);
119 strarray_free (identifiers, identifiers_num);
122 } /* while (*buffer != 0) */
124 for (i = 0; (i == 0) || (i < plugins_num); i++)
129 if (plugins_num != 0)
132 for (j = 0; (j == 0) || (j < identifiers_num); j++)
134 char *identifier = NULL;
137 if (identifiers_num != 0)
138 identifier = identifiers[j];
140 status = plugin_flush (plugin,
141 DOUBLE_TO_CDTIME_T (timeout),
150 PRINT_TO_SOCK (fh, "0 Done: %i successful, %i errors\n",
153 strarray_free (plugins, plugins_num);
154 strarray_free (identifiers, identifiers_num);
157 } /* int handle_flush */
159 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */