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