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"
28 #include "utils_cmd_flush.h"
30 int handle_flush (FILE *fh, char *buffer)
31 {
32 int success = 0;
33 int error = 0;
35 double timeout = 0.0;
36 char **plugins = NULL;
37 size_t plugins_num = 0;
38 char **identifiers = NULL;
39 size_t identifiers_num = 0;
41 size_t i;
43 #define PRINT_TO_SOCK(fh, ...) \
44 do { \
45 if (fprintf (fh, __VA_ARGS__) < 0) { \
46 char errbuf[1024]; \
47 WARNING ("handle_flush: failed to write to socket #%i: %s", \
48 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
49 strarray_free (plugins, plugins_num); \
50 strarray_free (identifiers, identifiers_num); \
51 return -1; \
52 } \
53 fflush(fh); \
54 } while (0)
56 if ((fh == NULL) || (buffer == NULL))
57 return (-1);
59 DEBUG ("utils_cmd_flush: handle_flush (fh = %p, buffer = %s);",
60 (void *) fh, buffer);
62 if (strncasecmp ("FLUSH", buffer, strlen ("FLUSH")) != 0)
63 {
64 PRINT_TO_SOCK (fh, "-1 Cannot parse command.\n");
65 return (-1);
66 }
67 buffer += strlen ("FLUSH");
69 while (*buffer != 0)
70 {
71 char *opt_key;
72 char *opt_value;
73 int status;
75 opt_key = NULL;
76 opt_value = NULL;
77 status = parse_option (&buffer, &opt_key, &opt_value);
78 if (status != 0)
79 {
80 PRINT_TO_SOCK (fh, "-1 Parsing options failed.\n");
81 strarray_free (plugins, plugins_num);
82 strarray_free (identifiers, identifiers_num);
83 return (-1);
84 }
86 if (strcasecmp ("plugin", opt_key) == 0)
87 strarray_add (&plugins, &plugins_num, opt_value);
88 else if (strcasecmp ("identifier", opt_key) == 0)
89 strarray_add (&identifiers, &identifiers_num, opt_value);
90 else if (strcasecmp ("timeout", opt_key) == 0)
91 {
92 char *endptr;
94 errno = 0;
95 endptr = NULL;
96 timeout = strtod (opt_value, &endptr);
98 if ((endptr == opt_value) || (errno != 0) || (!isfinite (timeout)))
99 {
100 PRINT_TO_SOCK (fh, "-1 Invalid value for option `timeout': "
101 "%s\n", opt_value);
102 strarray_free (plugins, plugins_num);
103 strarray_free (identifiers, identifiers_num);
104 return (-1);
105 }
106 else if (timeout < 0.0)
107 {
108 timeout = 0.0;
109 }
110 }
111 else
112 {
113 PRINT_TO_SOCK (fh, "-1 Cannot parse option %s\n", opt_key);
114 strarray_free (plugins, plugins_num);
115 strarray_free (identifiers, identifiers_num);
116 return (-1);
117 }
118 } /* while (*buffer != 0) */
120 for (i = 0; (i == 0) || (i < plugins_num); i++)
121 {
122 char *plugin = NULL;
123 int j;
125 if (plugins_num != 0)
126 plugin = plugins[i];
128 for (j = 0; (j == 0) || (j < identifiers_num); j++)
129 {
130 char *identifier = NULL;
131 int status;
133 if (identifiers_num != 0)
134 identifier = identifiers[j];
136 status = plugin_flush (plugin,
137 DOUBLE_TO_CDTIME_T (timeout),
138 identifier);
139 if (status == 0)
140 success++;
141 else
142 error++;
143 }
144 }
146 PRINT_TO_SOCK (fh, "0 Done: %i successful, %i errors\n",
147 success, error);
149 strarray_free (plugins, plugins_num);
150 strarray_free (identifiers, identifiers_num);
151 return (0);
152 #undef PRINT_TO_SOCK
153 } /* int handle_flush */
155 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */