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 if (fprintf (fh, __VA_ARGS__) < 0) { \
36 char errbuf[1024]; \
37 WARNING ("handle_flush: failed to write to socket #%i: %s", \
38 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
39 return -1; \
40 }
42 static int add_to_array (char ***array, int *array_num, char *value)
43 {
44 char **temp;
46 temp = (char **) realloc (*array, sizeof (char *) * (*array_num + 1));
47 if (temp == NULL)
48 return (-1);
50 *array = temp;
51 (*array)[*array_num] = value;
52 (*array_num)++;
54 return (0);
55 } /* int add_to_array */
57 int handle_flush (FILE *fh, char *buffer)
58 {
59 int success = 0;
60 int error = 0;
62 double timeout = 0.0;
63 char **plugins = NULL;
64 int plugins_num = 0;
65 char **identifiers = NULL;
66 int identifiers_num = 0;
68 int i;
70 if ((fh == NULL) || (buffer == NULL))
71 return (-1);
73 DEBUG ("utils_cmd_flush: handle_flush (fh = %p, buffer = %s);",
74 (void *) fh, buffer);
76 if (strncasecmp ("FLUSH", buffer, strlen ("FLUSH")) != 0)
77 {
78 print_to_socket (fh, "-1 Cannot parse command.\n");
79 return (-1);
80 }
81 buffer += strlen ("FLUSH");
83 while (*buffer != 0)
84 {
85 char *opt_key;
86 char *opt_value;
87 int status;
89 opt_key = NULL;
90 opt_value = NULL;
91 status = parse_option (&buffer, &opt_key, &opt_value);
92 if (status != 0)
93 {
94 print_to_socket (fh, "-1 Parsing options failed.\n");
95 sfree (plugins);
96 sfree (identifiers);
97 return (-1);
98 }
100 if (strcasecmp ("plugin", opt_key) == 0)
101 {
102 add_to_array (&plugins, &plugins_num, opt_value);
103 }
104 else if (strcasecmp ("identifier", opt_key) == 0)
105 {
106 add_to_array (&identifiers, &identifiers_num, opt_value);
107 }
108 else if (strcasecmp ("timeout", opt_key) == 0)
109 {
110 char *endptr;
112 errno = 0;
113 endptr = NULL;
114 timeout = strtod (opt_value, &endptr);
116 if ((endptr == opt_value) || (errno != 0) || (!isfinite (timeout)))
117 {
118 print_to_socket (fh, "-1 Invalid value for option `timeout': "
119 "%s\n", opt_value);
120 sfree (plugins);
121 sfree (identifiers);
122 return (-1);
123 }
124 else if (timeout < 0.0)
125 {
126 timeout = 0.0;
127 }
128 }
129 else
130 {
131 print_to_socket (fh, "-1 Cannot parse option %s\n", opt_key);
132 sfree (plugins);
133 sfree (identifiers);
134 return (-1);
135 }
136 } /* while (*buffer != 0) */
138 /* Add NULL entries for `any plugin' and/or `any value' if nothing was
139 * specified. */
140 if (plugins_num == 0)
141 add_to_array (&plugins, &plugins_num, NULL);
143 if (identifiers_num == 0)
144 add_to_array (&identifiers, &identifiers_num, NULL);
146 for (i = 0; i < plugins_num; i++)
147 {
148 char *plugin;
149 int j;
151 plugin = plugins[i];
153 for (j = 0; j < identifiers_num; j++)
154 {
155 char *identifier;
156 int status;
158 identifier = identifiers[j];
159 status = plugin_flush (plugin,
160 DOUBLE_TO_CDTIME_T (timeout),
161 identifier);
162 if (status == 0)
163 success++;
164 else
165 error++;
166 }
167 }
169 if ((success + error) > 0)
170 {
171 print_to_socket (fh, "0 Done: %i successful, %i errors\n",
172 success, error);
173 }
174 else
175 {
176 plugin_flush (NULL, DOUBLE_TO_CDTIME_T (timeout), NULL);
177 print_to_socket (fh, "0 Done\n");
178 }
180 sfree (plugins);
181 sfree (identifiers);
182 return (0);
183 } /* int handle_flush */
185 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */