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