2 * collectd - src/utils_cms_listval.c
3 * Copyright (C) 2008-2011 Florian Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian "octo" Forster <octo at collectd.org>
28 #include "utils_cmd_listval.h"
29 #include "utils_cache.h"
30 #include "utils_parse_option.h"
32 /* Not very nice, but oh so handy ... */
33 #define FREE_EVERYTHING_AND_RETURN(status) do { \
35 for (j = 0; j < number; j++) { \
41 if (have_re_host) { regfree (&re_host); } \
42 if (have_re_plugin) { regfree (&re_plugin); } \
43 if (have_re_plugin_instance) { regfree (&re_plugin_instance); } \
44 if (have_re_type) { regfree (&re_type); } \
45 if (have_re_type_instance) { regfree (&re_type_instance); } \
49 #define print_to_socket(fh, ...) \
50 if (fprintf (fh, __VA_ARGS__) < 0) { \
52 WARNING ("handle_listval: failed to write to socket #%i: %s", \
53 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
54 FREE_EVERYTHING_AND_RETURN (-1); \
57 int handle_listval (FILE *fh, char *buffer)
61 cdtime_t *times = NULL;
68 regex_t re_plugin_instance;
70 regex_t re_type_instance;
71 _Bool have_re_host = 0;
72 _Bool have_re_plugin = 0;
73 _Bool have_re_plugin_instance = 0;
74 _Bool have_re_type = 0;
75 _Bool have_re_type_instance = 0;
77 DEBUG ("utils_cmd_listval: handle_listval (fh = %p, buffer = %s);",
81 status = parse_string (&buffer, &command);
84 print_to_socket (fh, "-1 Cannot parse command.\n");
85 FREE_EVERYTHING_AND_RETURN (-1);
87 assert (command != NULL);
89 if (strcasecmp ("LISTVAL", command) != 0)
91 print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
92 FREE_EVERYTHING_AND_RETURN (-1);
95 /* Parse the options which may still be contained in the buffer. Valid
96 * options are "host", "plugin", "plugin_instance", "type" and
97 * "type_instance"; each option takes a regular expression as argument which
98 * is used to filter the returned identifiers. */
108 status = parse_option (&buffer, &opt_key, &opt_value);
111 print_to_socket (fh, "-1 Parsing options failed.\n");
112 FREE_EVERYTHING_AND_RETURN (-1);
115 if (strcasecmp ("host", opt_key) == 0)
118 have_re = &have_re_host;
120 else if (strcasecmp ("plugin", opt_key) == 0)
123 have_re = &have_re_plugin;
125 else if (strcasecmp ("plugin_instance", opt_key) == 0)
127 re = &re_plugin_instance;
128 have_re = &have_re_plugin_instance;
130 else if (strcasecmp ("type", opt_key) == 0)
133 have_re = &have_re_type;
135 else if (strcasecmp ("type_instance", opt_key) == 0)
137 re = &re_type_instance;
138 have_re = &have_re_type_instance;
142 print_to_socket (fh, "-1 Unknown option: %s\n", opt_key);
143 FREE_EVERYTHING_AND_RETURN (-1);
146 /* Free a previous regular expression */
149 NOTICE ("listval command: More than one match for part \"%s\". "
150 "Only the last regular expression will be used to search "
151 "for matching value lists!",
157 /* Compile the regular expression. */
158 status = regcomp (re, opt_value, REG_EXTENDED | REG_NOSUB);
162 regerror (status, re, errbuf, sizeof (errbuf));
163 errbuf[sizeof (errbuf) - 1] = 0;
164 print_to_socket (fh, "-1 Compiling %s regex failed: %s\n",
166 FREE_EVERYTHING_AND_RETURN (-1);
169 } /* while (*buffer != 0) */
171 /* Get a list of values from the cache. */
172 status = uc_get_names (&names, ×, &number);
175 DEBUG ("command listval: uc_get_names failed with status %i", status);
176 print_to_socket (fh, "-1 uc_get_names failed.\n");
177 FREE_EVERYTHING_AND_RETURN (-1);
180 /* If no regex has been specified, take the easy way out. This will avoid a
181 * lot of pointless if-blocks. */
184 && !have_re_plugin_instance
186 && !have_re_type_instance)
188 print_to_socket (fh, "%i Value%s found\n",
189 (int) number, (number == 1) ? "" : "s");
190 for (i = 0; i < number; i++)
191 print_to_socket (fh, "%.3f %s\n", CDTIME_T_TO_DOUBLE (times[i]),
194 else /* At least one regular expression is present. */
196 char *matching_names[number];
197 cdtime_t matching_times[number];
198 size_t matching_number = 0;
200 /* We need to figure out how many values we're going to return for the
201 * status line first. We save all matched values in the above arrays to
202 * avoid doing the matching twice. */
203 for (i = 0; i < number; i++)
205 value_list_t vl = VALUE_LIST_INIT;
207 status = parse_identifier_vl (names[i], &vl);
211 /* If a regex exists and doesn't match, ignore this value and continue
212 * with the next one. */
213 if (have_re_host && (regexec (&re_host,
214 /* string = */ vl.host,
217 /* flags = */ 0) == REG_NOMATCH))
219 if (have_re_plugin && (regexec (&re_plugin,
220 /* string = */ vl.plugin,
223 /* flags = */ 0) == REG_NOMATCH))
225 if (have_re_plugin_instance && (regexec (&re_plugin_instance,
226 /* string = */ vl.plugin_instance,
229 /* flags = */ 0) == REG_NOMATCH))
231 if (have_re_type && (regexec (&re_type,
232 /* string = */ vl.type,
235 /* flags = */ 0) == REG_NOMATCH))
237 if (have_re_type_instance && (regexec (&re_type_instance,
238 /* string = */ vl.type_instance,
241 /* flags = */ 0) == REG_NOMATCH))
244 matching_names[matching_number] = names[i];
245 matching_times[matching_number] = times[i];
249 print_to_socket (fh, "%zu Matching value%s\n",
250 matching_number, (matching_number == 1) ? "" : "s");
251 for (i = 0; i < matching_number; i++)
252 print_to_socket (fh, "%.3f %s\n",
253 CDTIME_T_TO_DOUBLE (matching_times[i]),
257 FREE_EVERYTHING_AND_RETURN (0);
258 } /* int handle_listval */
260 /* vim: set sw=2 sts=2 ts=8 et : */