1 /**
2 * collectd - src/collectdctl.c
3 * Copyright (C) 2010 Håkon J Dugstad Johnsen
4 * Copyright (C) 2010 Sebastian Harl
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Håkon J Dugstad Johnsen <hakon-dugstad.johnsen at telenor.com>
21 * Sebastian "tokkee" Harl <sh@tokkee.org>
22 **/
24 #if HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include "libcollectdclient/client.h"
30 #include <assert.h>
32 #include <errno.h>
34 #include <getopt.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
40 #include <unistd.h>
42 #define DEFAULT_SOCK LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
44 extern char *optarg;
45 extern int optind;
47 static void exit_usage (const char *name, int status) {
48 fprintf ((status == 0) ? stdout : stderr,
49 "Usage: %s [options] <command> [cmd options]\n\n"
51 "Available options:\n"
52 " -s Path to collectd's UNIX socket.\n"
53 " Default: "DEFAULT_SOCK"\n"
55 "\n -h Display this help and exit.\n"
57 "\nAvailable commands:\n\n"
59 " * getval <identifier>\n"
60 " * flush [timeout=<seconds>] [plugin=<name>] [identifier=<id>]\n"
61 " * listval\n"
62 " * putval <identifier> [interval=<seconds>] <value-list(s)>\n"
64 "\nIdentifiers:\n\n"
66 "An identifier has the following format:\n\n"
68 " [<hostname>/]<plugin>[-<plugin_instance>]/<type>[-<type_instance>]\n\n"
70 "Hostname defaults to the local hostname if omitted (e.g., uptime/uptime).\n"
71 "No error is returned if the specified identifier does not exist.\n"
73 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
74 "by Florian octo Forster <octo@verplant.org>\n"
75 "for contributions see `AUTHORS'\n"
76 , name);
77 exit (status);
78 }
80 /* Count the number of occurrences of the character 'chr'
81 * in the specified string. */
82 static int count_chars (const char *str, char chr) {
83 int count = 0;
85 while (*str != '\0') {
86 if (*str == chr) {
87 count++;
88 }
89 str++;
90 }
92 return count;
93 } /* count_chars */
95 static int array_grow (void **array, int *array_len, size_t elem_size)
96 {
97 void *tmp;
99 assert ((array != NULL) && (array_len != NULL));
101 tmp = realloc (*array, (*array_len + 1) * elem_size);
102 if (tmp == NULL) {
103 fprintf (stderr, "ERROR: Failed to allocate memory.\n");
104 return (-1);
105 }
107 *array = tmp;
108 ++(*array_len);
109 return (0);
110 } /* array_grow */
112 static int parse_identifier (lcc_connection_t *c,
113 const char *value, lcc_identifier_t *ident)
114 {
115 char hostname[1024];
116 char ident_str[1024] = "";
117 int n_slashes;
119 int status;
121 n_slashes = count_chars (value, '/');
122 if (n_slashes == 1) {
123 /* The user has omitted the hostname part of the identifier
124 * (there is only one '/' in the identifier)
125 * Let's add the local hostname */
126 if (gethostname (hostname, sizeof (hostname)) != 0) {
127 fprintf (stderr, "ERROR: Failed to get local hostname: %s",
128 strerror (errno));
129 return (-1);
130 }
131 hostname[sizeof (hostname) - 1] = '\0';
133 snprintf (ident_str, sizeof (ident_str), "%s/%s", hostname, value);
134 ident_str[sizeof(ident_str) - 1] = '\0';
135 }
136 else {
137 strncpy (ident_str, value, sizeof (ident_str));
138 ident_str[sizeof (ident_str) - 1] = '\0';
139 }
141 status = lcc_string_to_identifier (c, ident, ident_str);
142 if (status != 0) {
143 fprintf (stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
144 ident_str, lcc_strerror(c));
145 return (-1);
146 }
147 return (0);
148 } /* parse_identifier */
150 static int getval (lcc_connection_t *c, int argc, char **argv)
151 {
152 lcc_identifier_t ident;
154 size_t ret_values_num = 0;
155 gauge_t *ret_values = NULL;
156 char **ret_values_names = NULL;
158 int status;
159 size_t i;
161 assert (strcasecmp (argv[0], "getval") == 0);
163 if (argc != 2) {
164 fprintf (stderr, "ERROR: getval: Missing identifier.\n");
165 return (-1);
166 }
168 memset (&ident, 0, sizeof (ident));
169 status = parse_identifier (c, argv[1], &ident);
170 if (status != 0)
171 return (status);
173 #define BAIL_OUT(s) \
174 do { \
175 if (ret_values != NULL) \
176 free (ret_values); \
177 if (ret_values_names != NULL) { \
178 for (i = 0; i < ret_values_num; ++i) \
179 free (ret_values_names[i]); \
180 free (ret_values_names); \
181 } \
182 ret_values_num = 0; \
183 return (s); \
184 } while (0)
186 status = lcc_getval (c, &ident,
187 &ret_values_num, &ret_values, &ret_values_names);
188 if (status != 0) {
189 fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
190 BAIL_OUT (-1);
191 }
193 for (i = 0; i < ret_values_num; ++i)
194 printf ("%s=%e\n", ret_values_names[i], ret_values[i]);
195 BAIL_OUT (0);
196 #undef BAIL_OUT
197 } /* getval */
199 static int flush (lcc_connection_t *c, int argc, char **argv)
200 {
201 int timeout = -1;
203 lcc_identifier_t *identifiers = NULL;
204 int identifiers_num = 0;
206 char **plugins = NULL;
207 int plugins_num = 0;
209 int status;
210 int i;
212 assert (strcasecmp (argv[0], "flush") == 0);
214 #define BAIL_OUT(s) \
215 do { \
216 if (identifiers != NULL) \
217 free (identifiers); \
218 identifiers_num = 0; \
219 if (plugins != NULL) \
220 free (plugins); \
221 plugins_num = 0; \
222 return (s); \
223 } while (0)
225 for (i = 1; i < argc; ++i) {
226 char *key, *value;
228 key = argv[i];
229 value = strchr (argv[i], (int)'=');
231 if (! value) {
232 fprintf (stderr, "ERROR: flush: Invalid option ``%s''.\n", argv[i]);
233 BAIL_OUT (-1);
234 }
236 *value = '\0';
237 ++value;
239 if (strcasecmp (key, "timeout") == 0) {
240 char *endptr = NULL;
242 timeout = strtol (value, &endptr, 0);
244 if (endptr == value) {
245 fprintf (stderr, "ERROR: Failed to parse timeout as number: %s.\n",
246 value);
247 BAIL_OUT (-1);
248 }
249 else if ((endptr != NULL) && (*endptr != '\0')) {
250 fprintf (stderr, "WARNING: Ignoring trailing garbage after timeout: "
251 "%s.\n", endptr);
252 }
253 }
254 else if (strcasecmp (key, "plugin") == 0) {
255 status = array_grow ((void **)&plugins, &plugins_num,
256 sizeof (*plugins));
257 if (status != 0)
258 BAIL_OUT (status);
260 plugins[plugins_num - 1] = value;
261 }
262 else if (strcasecmp (key, "identifier") == 0) {
263 status = array_grow ((void **)&identifiers, &identifiers_num,
264 sizeof (*identifiers));
265 if (status != 0)
266 BAIL_OUT (status);
268 memset (identifiers + (identifiers_num - 1), 0, sizeof (*identifiers));
269 status = parse_identifier (c, value,
270 identifiers + (identifiers_num - 1));
271 if (status != 0)
272 BAIL_OUT (status);
273 }
274 }
276 if (plugins_num == 0) {
277 status = array_grow ((void **)&plugins, &plugins_num, sizeof (*plugins));
278 if (status != 0)
279 BAIL_OUT (status);
281 assert (plugins_num == 1);
282 plugins[0] = NULL;
283 }
285 for (i = 0; i < plugins_num; ++i) {
286 if (identifiers_num == 0) {
287 status = lcc_flush (c, plugins[i], NULL, timeout);
288 if (status != 0)
289 fprintf (stderr, "ERROR: Failed to flush plugin `%s': %s.\n",
290 (plugins[i] == NULL) ? "(all)" : plugins[i], lcc_strerror (c));
291 }
292 else {
293 int j;
295 for (j = 0; j < identifiers_num; ++j) {
296 status = lcc_flush (c, plugins[i], identifiers + j, timeout);
297 if (status != 0) {
298 char id[1024];
300 lcc_identifier_to_string (c, id, sizeof (id), identifiers + j);
301 fprintf (stderr, "ERROR: Failed to flush plugin `%s', "
302 "identifier `%s': %s.\n",
303 (plugins[i] == NULL) ? "(all)" : plugins[i],
304 id, lcc_strerror (c));
305 }
306 }
307 }
308 }
310 BAIL_OUT (0);
311 #undef BAIL_OUT
312 } /* flush */
314 static int listval (lcc_connection_t *c, int argc, char **argv)
315 {
316 lcc_identifier_t *ret_ident = NULL;
317 size_t ret_ident_num = 0;
319 int status;
320 size_t i;
322 assert (strcasecmp (argv[0], "listval") == 0);
324 if (argc != 1) {
325 fprintf (stderr, "ERROR: listval: Does not accept any arguments.\n");
326 return (-1);
327 }
329 #define BAIL_OUT(s) \
330 do { \
331 if (ret_ident != NULL) \
332 free (ret_ident); \
333 ret_ident_num = 0; \
334 return (s); \
335 } while (0)
337 status = lcc_listval (c, &ret_ident, &ret_ident_num);
338 if (status != 0) {
339 fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
340 BAIL_OUT (status);
341 }
343 for (i = 0; i < ret_ident_num; ++i) {
344 char id[1024];
346 status = lcc_identifier_to_string (c, id, sizeof (id), ret_ident + i);
347 if (status != 0) {
348 fprintf (stderr, "ERROR: listval: Failed to convert returned "
349 "identifier to a string: %s\n", lcc_strerror (c));
350 continue;
351 }
353 printf ("%s\n", id);
354 }
355 BAIL_OUT (0);
356 #undef BAIL_OUT
357 } /* listval */
359 static int putval (lcc_connection_t *c, int argc, char **argv)
360 {
361 lcc_value_list_t vl = LCC_VALUE_LIST_INIT;
363 /* 64 ought to be enough for anybody ;-) */
364 value_t values[64];
365 int values_types[64];
366 size_t values_len = 0;
368 int status;
369 int i;
371 assert (strcasecmp (argv[0], "putval") == 0);
373 if (argc < 3) {
374 fprintf (stderr, "ERROR: putval: Missing identifier "
375 "and/or value list.\n");
376 return (-1);
377 }
379 vl.values = values;
380 vl.values_types = values_types;
382 status = parse_identifier (c, argv[1], &vl.identifier);
383 if (status != 0)
384 return (status);
386 for (i = 2; i < argc; ++i) {
387 char *tmp;
389 tmp = strchr (argv[i], (int)'=');
391 if (tmp != NULL) { /* option */
392 char *key = argv[i];
393 char *value = tmp;
395 *value = '\0';
396 ++value;
398 if (strcasecmp (key, "interval") == 0) {
399 char *endptr;
401 vl.interval = strtol (value, &endptr, 0);
403 if (endptr == value) {
404 fprintf (stderr, "ERROR: Failed to parse interval as number: %s.\n",
405 value);
406 return (-1);
407 }
408 else if ((endptr != NULL) && (*endptr != '\0')) {
409 fprintf (stderr, "WARNING: Ignoring trailing garbage after "
410 "interval: %s.\n", endptr);
411 }
412 }
413 else {
414 fprintf (stderr, "ERROR: putval: Unknown option `%s'.\n", key);
415 return (-1);
416 }
417 }
418 else { /* value list */
419 char *value;
421 tmp = strchr (argv[i], (int)':');
423 if (tmp == NULL) {
424 fprintf (stderr, "ERROR: putval: Invalid value list: %s.\n",
425 argv[i]);
426 return (-1);
427 }
429 *tmp = '\0';
430 ++tmp;
432 if (strcasecmp (argv[i], "N") == 0) {
433 vl.time = 0;
434 }
435 else {
436 char *endptr;
438 vl.time = strtol (argv[i], &endptr, 0);
440 if (endptr == value) {
441 fprintf (stderr, "ERROR: Failed to parse time as number: %s.\n",
442 argv[i]);
443 return (-1);
444 }
445 else if ((endptr != NULL) && (*endptr != '\0')) {
446 fprintf (stderr, "ERROR: Garbage after time: %s.\n", endptr);
447 return (-1);
448 }
449 }
451 values_len = 0;
452 value = tmp;
453 while (value != 0) {
454 char *dot, *endptr;
456 tmp = strchr (argv[i], (int)':');
458 if (tmp != NULL) {
459 *tmp = '\0';
460 ++tmp;
461 }
463 /* This is a bit of a hack, but parsing types.db just does not make
464 * much sense imho -- the server might have different types defined
465 * anyway. Also, lcc uses the type information for formatting the
466 * number only, so the real meaning does not matter. -tokkee */
467 dot = strchr (value, (int)'.');
468 if (dot) { /* floating point value */
469 values[values_len].gauge = strtod (value, &endptr);
470 values_types[values_len] = LCC_TYPE_GAUGE;
471 }
472 else { /* integer */
473 values[values_len].counter = strtol (value, &endptr, 0);
474 values_types[values_len] = LCC_TYPE_COUNTER;
475 }
476 ++values_len;
478 if (endptr == value) {
479 fprintf (stderr, "ERROR: Failed to parse value as number: %s.\n",
480 argv[i]);
481 return (-1);
482 }
483 else if ((endptr != NULL) && (*endptr != '\0')) {
484 fprintf (stderr, "ERROR: Garbage after value: %s.\n", endptr);
485 return (-1);
486 }
488 value = tmp;
489 }
491 assert (values_len >= 1);
492 vl.values_len = values_len;
494 status = lcc_putval (c, &vl);
495 if (status != 0) {
496 fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
497 return (-1);
498 }
499 }
500 }
502 if (values_len == 0) {
503 fprintf (stderr, "ERROR: putval: Missing value list(s).\n");
504 return (-1);
505 }
506 return (0);
507 } /* putval */
509 int main (int argc, char **argv) {
510 char address[1024] = "unix:"DEFAULT_SOCK;
512 lcc_connection_t *c;
514 int status;
516 while (42) {
517 int c;
519 c = getopt (argc, argv, "s:h");
521 if (c == -1)
522 break;
524 switch (c) {
525 case 's':
526 snprintf (address, sizeof (address), "unix:%s", optarg);
527 address[sizeof (address) - 1] = '\0';
528 break;
529 case 'h':
530 exit_usage (argv[0], 0);
531 break;
532 default:
533 exit_usage (argv[0], 1);
534 }
535 }
537 if (optind >= argc) {
538 fprintf (stderr, "%s: missing command\n", argv[0]);
539 exit_usage (argv[0], 1);
540 }
542 c = NULL;
543 status = lcc_connect (address, &c);
544 if (status != 0) {
545 fprintf (stderr, "ERROR: Failed to connect to daemon at %s: %s.\n",
546 address, strerror (errno));
547 return (1);
548 }
550 if (strcasecmp (argv[optind], "getval") == 0)
551 status = getval (c, argc - optind, argv + optind);
552 else if (strcasecmp (argv[optind], "flush") == 0)
553 status = flush (c, argc - optind, argv + optind);
554 else if (strcasecmp (argv[optind], "listval") == 0)
555 status = listval (c, argc - optind, argv + optind);
556 else if (strcasecmp (argv[optind], "putval") == 0)
557 status = putval (c, argc - optind, argv + optind);
558 else {
559 fprintf (stderr, "%s: invalid command: %s\n", argv[0], argv[optind]);
560 return (1);
561 }
563 LCC_DESTROY (c);
565 if (status != 0)
566 return (status);
567 return (0);
568 } /* main */
570 /* vim: set sw=2 ts=2 tw=78 expandtab : */