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"
28 #define print_to_socket(fh, ...) \
29 if (fprintf (fh, __VA_ARGS__) < 0) { \
30 char errbuf[1024]; \
31 WARNING ("handle_flush: failed to write to socket #%i: %s", \
32 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
33 return -1; \
34 }
36 static int add_to_array (char ***array, int *array_num, char *value)
37 {
38 char **temp;
40 temp = (char **) realloc (*array, sizeof (char *) * (*array_num + 1));
41 if (temp == NULL)
42 return (-1);
44 *array = temp;
45 (*array)[*array_num] = value;
46 (*array_num)++;
48 return (0);
49 } /* int add_to_array */
51 int handle_flush (FILE *fh, char **fields, int fields_num)
52 {
53 int success = 0;
54 int error = 0;
56 int timeout = -1;
57 char **plugins = NULL;
58 int plugins_num = 0;
59 char **identifiers = NULL;
60 int identifiers_num = 0;
62 int i;
64 for (i = 1; i < fields_num; i++)
65 {
66 char *option = fields[i];
67 int status = 0;
69 if (strncasecmp ("plugin=", option, strlen ("plugin=")) == 0)
70 {
71 char *plugin;
73 plugin = option + strlen ("plugin=");
74 add_to_array (&plugins, &plugins_num, plugin);
75 }
76 else if (strncasecmp ("identifier=", option, strlen ("identifier=")) == 0)
77 {
78 char *identifier;
80 identifier = option + strlen ("identifier=");
81 add_to_array (&identifiers, &identifiers_num, identifier);
82 }
83 else if (strncasecmp ("timeout=", option, strlen ("timeout=")) == 0)
84 {
85 char *endptr = NULL;
86 char *value = option + strlen ("timeout=");
88 errno = 0;
89 timeout = strtol (value, &endptr, 0);
91 if ((endptr == value) || (0 != errno))
92 status = -1;
93 else if (0 >= timeout)
94 timeout = -1;
95 }
96 else
97 status = -1;
99 if (status != 0)
100 {
101 print_to_socket (fh, "-1 Cannot parse option %s\n", option);
102 return (-1);
103 }
104 }
106 /* Add NULL entries for `any plugin' and/or `any value' if nothing was
107 * specified. */
108 if (plugins_num == 0)
109 add_to_array (&plugins, &plugins_num, NULL);
111 if (identifiers_num == 0)
112 add_to_array (&identifiers, &identifiers_num, NULL);
114 for (i = 0; i < plugins_num; i++)
115 {
116 char *plugin;
117 int j;
119 plugin = plugins[i];
121 for (j = 0; j < identifiers_num; j++)
122 {
123 char *identifier;
124 int status;
126 identifier = identifiers[j];
127 status = plugin_flush (plugin, timeout, identifier);
128 if (status == 0)
129 success++;
130 else
131 error++;
132 }
133 }
135 if ((success + error) > 0)
136 {
137 print_to_socket (fh, "0 Done: %i successful, %i errors\n",
138 success, error);
139 }
140 else
141 {
142 plugin_flush_all (timeout);
143 print_to_socket (fh, "0 Done\n");
144 }
146 return (0);
147 } /* int handle_flush */
149 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */