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