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