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