078a5275a88fa8d39af3095ebd6c312f539d2b1a
1 /**
2 * collectd - src/utils_cms_listval.c
3 * Copyright (C) 2008-2011 Florian Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Author:
19 * Florian "octo" Forster <octo at collectd.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
26 #include <regex.h>
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 { \
34 size_t j; \
35 for (j = 0; j < number; j++) { \
36 sfree(names[j]); \
37 names[j] = NULL; \
38 } \
39 sfree(names); \
40 sfree(times); \
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); } \
46 return (status); \
47 } while (0)
49 #define print_to_socket(fh, ...) \
50 if (fprintf (fh, __VA_ARGS__) < 0) { \
51 char errbuf[1024]; \
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); \
55 }
57 int handle_listval (FILE *fh, char *buffer)
58 {
59 char *command;
60 char **names = NULL;
61 cdtime_t *times = NULL;
62 size_t number = 0;
63 size_t i;
64 int status;
66 regex_t re_host;
67 regex_t re_plugin;
68 regex_t re_plugin_instance;
69 regex_t re_type;
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);",
78 (void *) fh, buffer);
80 command = NULL;
81 status = parse_string (&buffer, &command);
82 if (status != 0)
83 {
84 print_to_socket (fh, "-1 Cannot parse command.\n");
85 FREE_EVERYTHING_AND_RETURN (-1);
86 }
87 assert (command != NULL);
89 if (strcasecmp ("LISTVAL", command) != 0)
90 {
91 print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
92 FREE_EVERYTHING_AND_RETURN (-1);
93 }
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. */
99 while (*buffer != 0)
100 {
101 char *opt_key;
102 char *opt_value;
103 regex_t *re;
104 _Bool *have_re;
106 opt_key = NULL;
107 opt_value = NULL;
108 status = parse_option (&buffer, &opt_key, &opt_value);
109 if (status != 0)
110 {
111 print_to_socket (fh, "-1 Parsing options failed.\n");
112 FREE_EVERYTHING_AND_RETURN (-1);
113 }
115 if (strcasecmp ("host", opt_key) == 0)
116 {
117 re = &re_host;
118 have_re = &have_re_host;
119 }
120 else if (strcasecmp ("plugin", opt_key) == 0)
121 {
122 re = &re_plugin;
123 have_re = &have_re_plugin;
124 }
125 else if (strcasecmp ("plugin_instance", opt_key) == 0)
126 {
127 re = &re_plugin_instance;
128 have_re = &have_re_plugin_instance;
129 }
130 else if (strcasecmp ("type", opt_key) == 0)
131 {
132 re = &re_type;
133 have_re = &have_re_type;
134 }
135 else if (strcasecmp ("type_instance", opt_key) == 0)
136 {
137 re = &re_type_instance;
138 have_re = &have_re_type_instance;
139 }
140 else
141 {
142 print_to_socket (fh, "-1 Unknown option: %s\n", opt_key);
143 FREE_EVERYTHING_AND_RETURN (-1);
144 }
146 /* Free a previous regular expression */
147 if (*have_re)
148 {
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!",
152 opt_key);
153 regfree (re);
154 *have_re = 0;
155 }
157 /* Compile the regular expression. */
158 status = regcomp (re, opt_value, REG_EXTENDED | REG_NOSUB);
159 if (status != 0)
160 {
161 char errbuf[1024];
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",
165 opt_key, errbuf);
166 FREE_EVERYTHING_AND_RETURN (-1);
167 }
168 *have_re = 1;
169 } /* while (*buffer != 0) */
171 /* Get a list of values from the cache. */
172 status = uc_get_names (&names, ×, &number);
173 if (status != 0)
174 {
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);
178 }
180 /* If no regex has been specified, take the easy way out. This will avoid a
181 * lot of pointless if-blocks. */
182 if (!have_re_host
183 && !have_re_plugin
184 && !have_re_plugin_instance
185 && !have_re_type
186 && !have_re_type_instance)
187 {
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]),
192 names[i]);
193 }
194 else /* At least one regular expression is present. */
195 {
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++)
204 {
205 value_list_t vl = VALUE_LIST_INIT;
207 status = parse_identifier_vl (names[i], &vl);
208 if (status != 0)
209 continue;
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,
215 /* nmatch = */ 0,
216 /* pmatch = */ NULL,
217 /* flags = */ 0) == REG_NOMATCH))
218 continue;
219 if (have_re_plugin && (regexec (&re_plugin,
220 /* string = */ vl.plugin,
221 /* nmatch = */ 0,
222 /* pmatch = */ NULL,
223 /* flags = */ 0) == REG_NOMATCH))
224 continue;
225 if (have_re_plugin_instance && (regexec (&re_plugin_instance,
226 /* string = */ vl.plugin_instance,
227 /* nmatch = */ 0,
228 /* pmatch = */ NULL,
229 /* flags = */ 0) == REG_NOMATCH))
230 continue;
231 if (have_re_type && (regexec (&re_type,
232 /* string = */ vl.type,
233 /* nmatch = */ 0,
234 /* pmatch = */ NULL,
235 /* flags = */ 0) == REG_NOMATCH))
236 continue;
237 if (have_re_type_instance && (regexec (&re_type_instance,
238 /* string = */ vl.type_instance,
239 /* nmatch = */ 0,
240 /* pmatch = */ NULL,
241 /* flags = */ 0) == REG_NOMATCH))
242 continue;
244 matching_names[matching_number] = names[i];
245 matching_times[matching_number] = times[i];
246 matching_number++;
247 }
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]),
254 matching_names[i]);
255 }
257 FREE_EVERYTHING_AND_RETURN (0);
258 } /* int handle_listval */
260 /* vim: set sw=2 sts=2 ts=8 et : */