320b0632215b24d5a88c846e56d7a77fe9eee8ab
[collectd.git] / src / utils_cmd_flush.c
1 /**
2  * collectd - src/utils_cmd_flush.c
3  * Copyright (C) 2008, 2016 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 cmd_status_t cmd_parse_flush (size_t argc, char **argv,
37                 cmd_flush_t *ret_flush, const cmd_options_t *opts,
38                 cmd_error_handler_t *err)
39 {
40
41         if ((ret_flush == NULL) || (opts == NULL))
42         {
43                 errno = EINVAL;
44                 cmd_error (CMD_ERROR, err, "Invalid arguments to cmd_parse_flush.");
45                 return (CMD_ERROR);
46         }
47
48         for (size_t i = 0; i < argc; i++)
49         {
50                 char *opt_key;
51                 char *opt_value;
52                 int status;
53
54                 opt_key = NULL;
55                 opt_value = NULL;
56                 status = cmd_parse_option (argv[i], &opt_key, &opt_value, err);
57                 if (status != 0)
58                 {
59                         if (status == CMD_NO_OPTION)
60                                 cmd_error (CMD_PARSE_ERROR, err,
61                                                 "Invalid option string `%s'.", argv[i]);
62                         cmd_destroy_flush (ret_flush);
63                         return (CMD_PARSE_ERROR);
64                 }
65
66                 if (strcasecmp ("plugin", opt_key) == 0)
67                 {
68                         strarray_add (&ret_flush->plugins, &ret_flush->plugins_num,
69                                         opt_value);
70                 }
71                 else if (strcasecmp ("identifier", opt_key) == 0)
72                 {
73                         identifier_t *id = realloc (ret_flush->identifiers,
74                                         (ret_flush->identifiers_num + 1) * sizeof (*id));
75                         if (id == NULL)
76                         {
77                                 cmd_error (CMD_ERROR, err, "realloc failed.");
78                                 cmd_destroy_flush (ret_flush);
79                                 return (CMD_ERROR);
80                         }
81
82                         ret_flush->identifiers = id;
83                         id = ret_flush->identifiers + ret_flush->identifiers_num;
84                         ret_flush->identifiers_num++;
85                         if (parse_identifier (opt_value,
86                                                 &id->host, &id->plugin, &id->plugin_instance,
87                                                 &id->type, &id->type_instance,
88                                                 opts->identifier_default_host) != 0)
89                         {
90                                 cmd_error (CMD_PARSE_ERROR, err,
91                                                 "Invalid identifier `%s'.", opt_value);
92                                 cmd_destroy_flush (ret_flush);
93                                 return (CMD_PARSE_ERROR);
94                         }
95                 }
96                 else if (strcasecmp ("timeout", opt_key) == 0)
97                 {
98                         char *endptr;
99
100                         errno = 0;
101                         endptr = NULL;
102                         ret_flush->timeout = strtod (opt_value, &endptr);
103
104                         if ((endptr == opt_value) || (errno != 0)
105                                         || (!isfinite (ret_flush->timeout)))
106                         {
107                                 cmd_error (CMD_PARSE_ERROR, err,
108                                                 "Invalid value for option `timeout': %s",
109                                                 opt_value);
110                                 cmd_destroy_flush (ret_flush);
111                                 return (CMD_PARSE_ERROR);
112                         }
113                         else if (ret_flush->timeout < 0.0)
114                         {
115                                 ret_flush->timeout = 0.0;
116                         }
117                 }
118                 else
119                 {
120                         cmd_error (CMD_PARSE_ERROR, err,
121                                         "Cannot parse option `%s'.", opt_key);
122                         cmd_destroy_flush (ret_flush);
123                         return (CMD_PARSE_ERROR);
124                 }
125         }
126
127         return (CMD_OK);
128 } /* cmd_status_t cmd_parse_flush */
129
130 cmd_status_t cmd_handle_flush (FILE *fh, char *buffer)
131 {
132         cmd_error_handler_t err = { cmd_error_fh, fh };
133         cmd_t cmd;
134
135         int success = 0;
136         int error   = 0;
137         int status;
138
139         size_t i;
140
141         if ((fh == NULL) || (buffer == NULL))
142                 return (-1);
143
144         DEBUG ("utils_cmd_flush: cmd_handle_flush (fh = %p, buffer = %s);",
145                         (void *) fh, buffer);
146
147         if ((status = cmd_parse (buffer, &cmd, NULL, &err)) != CMD_OK)
148                 return (status);
149         if (cmd.type != CMD_FLUSH)
150         {
151                 cmd_error (CMD_UNKNOWN_COMMAND, &err, "Unexpected command: `%s'.",
152                                 CMD_TO_STRING (cmd.type));
153                 cmd_destroy (&cmd);
154                 return (CMD_UNKNOWN_COMMAND);
155         }
156
157         for (i = 0; (i == 0) || (i < cmd.cmd.flush.plugins_num); i++)
158         {
159                 char *plugin = NULL;
160
161                 if (cmd.cmd.flush.plugins_num != 0)
162                         plugin = cmd.cmd.flush.plugins[i];
163
164                 for (size_t j = 0; (j == 0) || (j < cmd.cmd.flush.identifiers_num); j++)
165                 {
166                         char *identifier = NULL;
167                         char buffer[1024];
168                         int status;
169
170                         if (cmd.cmd.flush.identifiers_num != 0)
171                         {
172                                 identifier_t *id = cmd.cmd.flush.identifiers + j;
173                                 if (format_name (buffer, sizeof (buffer),
174                                                         id->host, id->plugin, id->plugin_instance,
175                                                         id->type, id->type_instance) != 0)
176                                 {
177                                         error++;
178                                         continue;
179                                 }
180                                 identifier = buffer;
181                         }
182
183                         status = plugin_flush (plugin,
184                                         DOUBLE_TO_CDTIME_T (cmd.cmd.flush.timeout),
185                                         identifier);
186                         if (status == 0)
187                                 success++;
188                         else
189                                 error++;
190                 }
191         }
192
193         cmd_error (CMD_OK, &err, "Done: %i successful, %i errors",
194                         success, error);
195
196         cmd_destroy (&cmd);
197         return (0);
198 #undef PRINT_TO_SOCK
199 } /* cmd_status_t cmd_handle_flush */
200
201 void cmd_destroy_flush (cmd_flush_t *flush)
202 {
203         if (flush == NULL)
204                 return;
205
206         strarray_free (flush->plugins, flush->plugins_num);
207         flush->plugins = NULL;
208         flush->plugins_num = 0;
209
210         sfree (flush->identifiers);
211         flush->identifiers_num = 0;
212 } /* void cmd_destroy_flush */
213
214 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
215