Makefile.am: s/INCLUDES/AM_CPPFLAGS/
[collectd.git] / src / utils_cmd_flush.c
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  **/
23
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "utils_parse_option.h"
28
29 #define print_to_socket(fh, ...) \
30         do { \
31                 if (fprintf (fh, __VA_ARGS__) < 0) { \
32                         char errbuf[1024]; \
33                         WARNING ("handle_flush: failed to write to socket #%i: %s", \
34                                         fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
35                         return -1; \
36                 } \
37                 fflush(fh); \
38         } while (0)
39
40 static int add_to_array (char ***array, int *array_num, char *value)
41 {
42         char **temp;
43
44         temp = (char **) realloc (*array, sizeof (char *) * (*array_num + 1));
45         if (temp == NULL)
46                 return (-1);
47
48         *array = temp;
49         (*array)[*array_num] = value;
50         (*array_num)++;
51
52         return (0);
53 } /* int add_to_array */
54
55 int handle_flush (FILE *fh, char *buffer)
56 {
57         int success = 0;
58         int error   = 0;
59
60         double timeout = 0.0;
61         char **plugins = NULL;
62         int plugins_num = 0;
63         char **identifiers = NULL;
64         int identifiers_num = 0;
65
66         int i;
67
68         if ((fh == NULL) || (buffer == NULL))
69                 return (-1);
70
71         DEBUG ("utils_cmd_flush: handle_flush (fh = %p, buffer = %s);",
72                         (void *) fh, buffer);
73
74         if (strncasecmp ("FLUSH", buffer, strlen ("FLUSH")) != 0)
75         {
76                 print_to_socket (fh, "-1 Cannot parse command.\n");
77                 return (-1);
78         }
79         buffer += strlen ("FLUSH");
80
81         while (*buffer != 0)
82         {
83                 char *opt_key;
84                 char *opt_value;
85                 int status;
86
87                 opt_key = NULL;
88                 opt_value = NULL;
89                 status = parse_option (&buffer, &opt_key, &opt_value);
90                 if (status != 0)
91                 {
92                         print_to_socket (fh, "-1 Parsing options failed.\n");
93                         sfree (plugins);
94                         sfree (identifiers);
95                         return (-1);
96                 }
97
98                 if (strcasecmp ("plugin", opt_key) == 0)
99                 {
100                         add_to_array (&plugins, &plugins_num, opt_value);
101                 }
102                 else if (strcasecmp ("identifier", opt_key) == 0)
103                 {
104                         add_to_array (&identifiers, &identifiers_num, opt_value);
105                 }
106                 else if (strcasecmp ("timeout", opt_key) == 0)
107                 {
108                         char *endptr;
109                         
110                         errno = 0;
111                         endptr = NULL;
112                         timeout = strtod (opt_value, &endptr);
113
114                         if ((endptr == opt_value) || (errno != 0) || (!isfinite (timeout)))
115                         {
116                                 print_to_socket (fh, "-1 Invalid value for option `timeout': "
117                                                 "%s\n", opt_value);
118                                 sfree (plugins);
119                                 sfree (identifiers);
120                                 return (-1);
121                         }
122                         else if (timeout < 0.0)
123                         {
124                                 timeout = 0.0;
125                         }
126                 }
127                 else
128                 {
129                         print_to_socket (fh, "-1 Cannot parse option %s\n", opt_key);
130                         sfree (plugins);
131                         sfree (identifiers);
132                         return (-1);
133                 }
134         } /* while (*buffer != 0) */
135
136         /* Add NULL entries for `any plugin' and/or `any value' if nothing was
137          * specified. */
138         if (plugins_num == 0)
139                 add_to_array (&plugins, &plugins_num, NULL);
140
141         if (identifiers_num == 0)
142                 add_to_array (&identifiers, &identifiers_num, NULL);
143
144         for (i = 0; i < plugins_num; i++)
145         {
146                 char *plugin;
147                 int j;
148
149                 plugin = plugins[i];
150
151                 for (j = 0; j < identifiers_num; j++)
152                 {
153                         char *identifier;
154                         int status;
155
156                         identifier = identifiers[j];
157                         status = plugin_flush (plugin,
158                                         DOUBLE_TO_CDTIME_T (timeout),
159                                         identifier);
160                         if (status == 0)
161                                 success++;
162                         else
163                                 error++;
164                 }
165         }
166
167         if ((success + error) > 0)
168         {
169                 print_to_socket (fh, "0 Done: %i successful, %i errors\n",
170                                 success, error);
171         }
172         else
173         {
174                 plugin_flush (NULL, timeout, NULL);
175                 print_to_socket (fh, "0 Done\n");
176         }
177
178         sfree (plugins);
179         sfree (identifiers);
180         return (0);
181 } /* int handle_flush */
182
183 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
184