1 /**
2 * collectd - src/utils_cmd_flush.c
3 * Copyright (C) 2008 Sebastian Harl
4 * Copyright (C) 2008 Florian Forster
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Sebastian "tokkee" Harl <sh at tokkee.org>
21 * Florian "octo" Forster <octo at verplant.org>
22 **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "utils_parse_option.h"
29 #define print_to_socket(fh, ...) \
30 if (fprintf (fh, __VA_ARGS__) < 0) { \
31 char errbuf[1024]; \
32 WARNING ("handle_flush: failed to write to socket #%i: %s", \
33 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
34 return -1; \
35 } \
36 fflush(fh);
38 static int add_to_array (char ***array, int *array_num, char *value)
39 {
40 char **temp;
42 temp = (char **) realloc (*array, sizeof (char *) * (*array_num + 1));
43 if (temp == NULL)
44 return (-1);
46 *array = temp;
47 (*array)[*array_num] = value;
48 (*array_num)++;
50 return (0);
51 } /* int add_to_array */
53 int handle_flush (FILE *fh, char *buffer)
54 {
55 int success = 0;
56 int error = 0;
58 double timeout = 0.0;
59 char **plugins = NULL;
60 int plugins_num = 0;
61 char **identifiers = NULL;
62 int identifiers_num = 0;
64 int i;
66 if ((fh == NULL) || (buffer == NULL))
67 return (-1);
69 DEBUG ("utils_cmd_flush: handle_flush (fh = %p, buffer = %s);",
70 (void *) fh, buffer);
72 if (strncasecmp ("FLUSH", buffer, strlen ("FLUSH")) != 0)
73 {
74 print_to_socket (fh, "-1 Cannot parse command.\n");
75 return (-1);
76 }
77 buffer += strlen ("FLUSH");
79 while (*buffer != 0)
80 {
81 char *opt_key;
82 char *opt_value;
83 int status;
85 opt_key = NULL;
86 opt_value = NULL;
87 status = parse_option (&buffer, &opt_key, &opt_value);
88 if (status != 0)
89 {
90 print_to_socket (fh, "-1 Parsing options failed.\n");
91 sfree (plugins);
92 sfree (identifiers);
93 return (-1);
94 }
96 if (strcasecmp ("plugin", opt_key) == 0)
97 {
98 add_to_array (&plugins, &plugins_num, opt_value);
99 }
100 else if (strcasecmp ("identifier", opt_key) == 0)
101 {
102 add_to_array (&identifiers, &identifiers_num, opt_value);
103 }
104 else if (strcasecmp ("timeout", opt_key) == 0)
105 {
106 char *endptr;
108 errno = 0;
109 endptr = NULL;
110 timeout = strtod (opt_value, &endptr);
112 if ((endptr == opt_value) || (errno != 0) || (!isfinite (timeout)))
113 {
114 print_to_socket (fh, "-1 Invalid value for option `timeout': "
115 "%s\n", opt_value);
116 sfree (plugins);
117 sfree (identifiers);
118 return (-1);
119 }
120 else if (timeout < 0.0)
121 {
122 timeout = 0.0;
123 }
124 }
125 else
126 {
127 print_to_socket (fh, "-1 Cannot parse option %s\n", opt_key);
128 sfree (plugins);
129 sfree (identifiers);
130 return (-1);
131 }
132 } /* while (*buffer != 0) */
134 /* Add NULL entries for `any plugin' and/or `any value' if nothing was
135 * specified. */
136 if (plugins_num == 0)
137 add_to_array (&plugins, &plugins_num, NULL);
139 if (identifiers_num == 0)
140 add_to_array (&identifiers, &identifiers_num, NULL);
142 for (i = 0; i < plugins_num; i++)
143 {
144 char *plugin;
145 int j;
147 plugin = plugins[i];
149 for (j = 0; j < identifiers_num; j++)
150 {
151 char *identifier;
152 int status;
154 identifier = identifiers[j];
155 status = plugin_flush (plugin,
156 DOUBLE_TO_CDTIME_T (timeout),
157 identifier);
158 if (status == 0)
159 success++;
160 else
161 error++;
162 }
163 }
165 if ((success + error) > 0)
166 {
167 print_to_socket (fh, "0 Done: %i successful, %i errors\n",
168 success, error);
169 }
170 else
171 {
172 plugin_flush (NULL, timeout, NULL);
173 print_to_socket (fh, "0 Done\n");
174 }
176 sfree (plugins);
177 sfree (identifiers);
178 return (0);
179 } /* int handle_flush */
181 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */