1 /**
2 * collectd - src/utils_cmd_flush.c
3 * Copyright (C) 2008 Sebastian Harl
4 * Copyright (C) 2008 Florian Forster
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 *
24 * Authors:
25 * Sebastian "tokkee" Harl <sh at tokkee.org>
26 * Florian "octo" Forster <octo at collectd.org>
27 **/
29 #include "collectd.h"
30 #include "common.h"
31 #include "plugin.h"
32 #include "utils_parse_option.h"
34 #define print_to_socket(fh, ...) \
35 do { \
36 if (fprintf (fh, __VA_ARGS__) < 0) { \
37 char errbuf[1024]; \
38 WARNING ("handle_flush: failed to write to socket #%i: %s", \
39 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
40 return -1; \
41 } \
42 fflush(fh); \
43 } while (0)
45 static int add_to_array (char ***array, int *array_num, char *value)
46 {
47 char **temp;
49 temp = (char **) realloc (*array, sizeof (char *) * (*array_num + 1));
50 if (temp == NULL)
51 return (-1);
53 *array = temp;
54 (*array)[*array_num] = value;
55 (*array_num)++;
57 return (0);
58 } /* int add_to_array */
60 int handle_flush (FILE *fh, char *buffer)
61 {
62 int success = 0;
63 int error = 0;
65 double timeout = 0.0;
66 char **plugins = NULL;
67 int plugins_num = 0;
68 char **identifiers = NULL;
69 int identifiers_num = 0;
71 int i;
73 if ((fh == NULL) || (buffer == NULL))
74 return (-1);
76 DEBUG ("utils_cmd_flush: handle_flush (fh = %p, buffer = %s);",
77 (void *) fh, buffer);
79 if (strncasecmp ("FLUSH", buffer, strlen ("FLUSH")) != 0)
80 {
81 print_to_socket (fh, "-1 Cannot parse command.\n");
82 return (-1);
83 }
84 buffer += strlen ("FLUSH");
86 while (*buffer != 0)
87 {
88 char *opt_key;
89 char *opt_value;
90 int status;
92 opt_key = NULL;
93 opt_value = NULL;
94 status = parse_option (&buffer, &opt_key, &opt_value);
95 if (status != 0)
96 {
97 print_to_socket (fh, "-1 Parsing options failed.\n");
98 sfree (plugins);
99 sfree (identifiers);
100 return (-1);
101 }
103 if (strcasecmp ("plugin", opt_key) == 0)
104 {
105 add_to_array (&plugins, &plugins_num, opt_value);
106 }
107 else if (strcasecmp ("identifier", opt_key) == 0)
108 {
109 add_to_array (&identifiers, &identifiers_num, opt_value);
110 }
111 else if (strcasecmp ("timeout", opt_key) == 0)
112 {
113 char *endptr;
115 errno = 0;
116 endptr = NULL;
117 timeout = strtod (opt_value, &endptr);
119 if ((endptr == opt_value) || (errno != 0) || (!isfinite (timeout)))
120 {
121 print_to_socket (fh, "-1 Invalid value for option `timeout': "
122 "%s\n", opt_value);
123 sfree (plugins);
124 sfree (identifiers);
125 return (-1);
126 }
127 else if (timeout < 0.0)
128 {
129 timeout = 0.0;
130 }
131 }
132 else
133 {
134 print_to_socket (fh, "-1 Cannot parse option %s\n", opt_key);
135 sfree (plugins);
136 sfree (identifiers);
137 return (-1);
138 }
139 } /* while (*buffer != 0) */
141 /* Add NULL entries for `any plugin' and/or `any value' if nothing was
142 * specified. */
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++)
150 {
151 char *plugin;
152 int j;
154 plugin = plugins[i];
156 for (j = 0; j < identifiers_num; j++)
157 {
158 char *identifier;
159 int status;
161 identifier = identifiers[j];
162 status = plugin_flush (plugin,
163 DOUBLE_TO_CDTIME_T (timeout),
164 identifier);
165 if (status == 0)
166 success++;
167 else
168 error++;
169 }
170 }
172 if ((success + error) > 0)
173 {
174 print_to_socket (fh, "0 Done: %i successful, %i errors\n",
175 success, error);
176 }
177 else
178 {
179 plugin_flush (NULL, DOUBLE_TO_CDTIME_T (timeout), NULL);
180 print_to_socket (fh, "0 Done\n");
181 }
183 sfree (plugins);
184 sfree (identifiers);
185 return (0);
186 } /* int handle_flush */
188 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */