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