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"
33 #include "utils_cmd_flush.h"
35 int handle_flush (FILE *fh, char *buffer)
41 char **plugins = NULL;
42 size_t plugins_num = 0;
43 char **identifiers = NULL;
44 size_t identifiers_num = 0;
48 #define PRINT_TO_SOCK(fh, ...) \
50 if (fprintf (fh, __VA_ARGS__) < 0) { \
52 WARNING ("handle_flush: failed to write to socket #%i: %s", \
53 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
54 strarray_free (plugins, plugins_num); \
55 strarray_free (identifiers, identifiers_num); \
61 if ((fh == NULL) || (buffer == NULL))
64 DEBUG ("utils_cmd_flush: handle_flush (fh = %p, buffer = %s);",
67 if (strncasecmp ("FLUSH", buffer, strlen ("FLUSH")) != 0)
69 PRINT_TO_SOCK (fh, "-1 Cannot parse command.\n");
72 buffer += strlen ("FLUSH");
82 status = parse_option (&buffer, &opt_key, &opt_value);
85 PRINT_TO_SOCK (fh, "-1 Parsing options failed.\n");
86 strarray_free (plugins, plugins_num);
87 strarray_free (identifiers, identifiers_num);
91 if (strcasecmp ("plugin", opt_key) == 0)
92 strarray_add (&plugins, &plugins_num, opt_value);
93 else if (strcasecmp ("identifier", opt_key) == 0)
94 strarray_add (&identifiers, &identifiers_num, opt_value);
95 else if (strcasecmp ("timeout", opt_key) == 0)
101 timeout = strtod (opt_value, &endptr);
103 if ((endptr == opt_value) || (errno != 0) || (!isfinite (timeout)))
105 PRINT_TO_SOCK (fh, "-1 Invalid value for option `timeout': "
107 strarray_free (plugins, plugins_num);
108 strarray_free (identifiers, identifiers_num);
111 else if (timeout < 0.0)
118 PRINT_TO_SOCK (fh, "-1 Cannot parse option %s\n", opt_key);
119 strarray_free (plugins, plugins_num);
120 strarray_free (identifiers, identifiers_num);
123 } /* while (*buffer != 0) */
125 for (i = 0; (i == 0) || (i < plugins_num); i++)
130 if (plugins_num != 0)
133 for (j = 0; (j == 0) || (j < identifiers_num); j++)
135 char *identifier = NULL;
138 if (identifiers_num != 0)
139 identifier = identifiers[j];
141 status = plugin_flush (plugin,
142 DOUBLE_TO_CDTIME_T (timeout),
151 PRINT_TO_SOCK (fh, "0 Done: %i successful, %i errors\n",
154 strarray_free (plugins, plugins_num);
155 strarray_free (identifiers, identifiers_num);
158 } /* int handle_flush */
160 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */