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 <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <strings.h>
32 #include <unistd.h>
34 #include <assert.h>
35 #include <errno.h>
37 #if NAN_STATIC_DEFAULT
38 #include <math.h>
39 /* #endif NAN_STATIC_DEFAULT*/
40 #elif NAN_STATIC_ISOC
41 #ifndef __USE_ISOC99
42 #define DISABLE_ISOC99 1
43 #define __USE_ISOC99 1
44 #endif /* !defined(__USE_ISOC99) */
45 #include <math.h>
46 #if DISABLE_ISOC99
47 #undef DISABLE_ISOC99
48 #undef __USE_ISOC99
49 #endif /* DISABLE_ISOC99 */
50 /* #endif NAN_STATIC_ISOC */
51 #elif NAN_ZERO_ZERO
52 #include <math.h>
53 #ifdef NAN
54 #undef NAN
55 #endif
56 #define NAN (0.0 / 0.0)
57 #ifndef isnan
58 #define isnan(f) ((f) != (f))
59 #endif /* !defined(isnan) */
60 #ifndef isfinite
61 #define isfinite(f) (((f) - (f)) == 0.0)
62 #endif
63 #ifndef isinf
64 #define isinf(f) (!isfinite(f) && !isnan(f))
65 #endif
66 #endif /* NAN_ZERO_ZERO */
68 #include "libcollectdclient/collectd/client.h"
70 #ifndef PREFIX
71 #define PREFIX "/opt/" PACKAGE_NAME
72 #endif
74 #ifndef LOCALSTATEDIR
75 #define LOCALSTATEDIR PREFIX "/var"
76 #endif
78 #define DEFAULT_SOCK LOCALSTATEDIR "/run/" PACKAGE_NAME "-unixsock"
80 extern char *optarg;
81 extern int optind;
83 __attribute__((noreturn)) static void exit_usage(const char *name, int status) {
84 fprintf(
85 (status == 0) ? stdout : stderr,
86 "Usage: %s [options] <command> [cmd options]\n\n"
88 "Available options:\n"
89 " -s Path to collectd's UNIX socket.\n"
90 " Default: " DEFAULT_SOCK "\n"
92 "\n -h Display this help and exit.\n"
94 "\nAvailable commands:\n\n"
96 " * getval <identifier>\n"
97 " * flush [timeout=<seconds>] [plugin=<name>] [identifier=<id>]\n"
98 " * listval\n"
99 " * putval <identifier> [interval=<seconds>] <value-list(s)>\n"
101 "\nIdentifiers:\n\n"
103 "An identifier has the following format:\n\n"
105 " [<hostname>/]<plugin>[-<plugin_instance>]/<type>[-<type_instance>]\n\n"
107 "Hostname defaults to the local hostname if omitted (e.g., "
108 "uptime/uptime).\n"
109 "No error is returned if the specified identifier does not exist.\n"
111 "\n" PACKAGE_NAME " " PACKAGE_VERSION ", http://collectd.org/\n"
112 "by Florian octo Forster <octo@collectd.org>\n"
113 "for contributions see `AUTHORS'\n",
114 name);
115 exit(status);
116 }
118 /* Count the number of occurrences of the character 'chr'
119 * in the specified string. */
120 static int count_chars(const char *str, char chr) {
121 int count = 0;
123 while (*str != '\0') {
124 if (*str == chr) {
125 count++;
126 }
127 str++;
128 }
130 return count;
131 } /* count_chars */
133 static int array_grow(void **array, size_t *array_len, size_t elem_size) {
134 void *tmp;
136 assert((array != NULL) && (array_len != NULL));
138 tmp = realloc(*array, (*array_len + 1) * elem_size);
139 if (tmp == NULL) {
140 fprintf(stderr, "ERROR: Failed to allocate memory.\n");
141 return (-1);
142 }
144 *array = tmp;
145 ++(*array_len);
146 return (0);
147 } /* array_grow */
149 static int parse_identifier(lcc_connection_t *c, const char *value,
150 lcc_identifier_t *ident) {
151 char hostname[1024];
152 char ident_str[1024] = "";
153 int n_slashes;
155 int status;
157 n_slashes = count_chars(value, '/');
158 if (n_slashes == 1) {
159 /* The user has omitted the hostname part of the identifier
160 * (there is only one '/' in the identifier)
161 * Let's add the local hostname */
162 if (gethostname(hostname, sizeof(hostname)) != 0) {
163 fprintf(stderr, "ERROR: Failed to get local hostname: %s",
164 strerror(errno));
165 return (-1);
166 }
167 hostname[sizeof(hostname) - 1] = '\0';
169 snprintf(ident_str, sizeof(ident_str), "%s/%s", hostname, value);
170 ident_str[sizeof(ident_str) - 1] = '\0';
171 } else {
172 strncpy(ident_str, value, sizeof(ident_str));
173 ident_str[sizeof(ident_str) - 1] = '\0';
174 }
176 status = lcc_string_to_identifier(c, ident, ident_str);
177 if (status != 0) {
178 fprintf(stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
179 ident_str, lcc_strerror(c));
180 return (-1);
181 }
182 return (0);
183 } /* parse_identifier */
185 static int getval(lcc_connection_t *c, int argc, char **argv) {
186 lcc_identifier_t ident;
188 size_t ret_values_num = 0;
189 gauge_t *ret_values = NULL;
190 char **ret_values_names = NULL;
192 int status;
194 assert(strcasecmp(argv[0], "getval") == 0);
196 if (argc != 2) {
197 fprintf(stderr, "ERROR: getval: Missing identifier.\n");
198 return (-1);
199 }
201 status = parse_identifier(c, argv[1], &ident);
202 if (status != 0)
203 return (status);
205 #define BAIL_OUT(s) \
206 do { \
207 if (ret_values != NULL) \
208 free(ret_values); \
209 if (ret_values_names != NULL) { \
210 for (size_t i = 0; i < ret_values_num; ++i) \
211 free(ret_values_names[i]); \
212 free(ret_values_names); \
213 } \
214 ret_values_num = 0; \
215 return (s); \
216 } while (0)
218 status =
219 lcc_getval(c, &ident, &ret_values_num, &ret_values, &ret_values_names);
220 if (status != 0) {
221 fprintf(stderr, "ERROR: %s\n", lcc_strerror(c));
222 BAIL_OUT(-1);
223 }
225 for (size_t i = 0; i < ret_values_num; ++i)
226 printf("%s=%e\n", ret_values_names[i], ret_values[i]);
227 BAIL_OUT(0);
228 #undef BAIL_OUT
229 } /* getval */
231 static int flush(lcc_connection_t *c, int argc, char **argv) {
232 int timeout = -1;
234 lcc_identifier_t *identifiers = NULL;
235 size_t identifiers_num = 0;
237 char **plugins = NULL;
238 size_t plugins_num = 0;
240 int status;
242 assert(strcasecmp(argv[0], "flush") == 0);
244 #define BAIL_OUT(s) \
245 do { \
246 if (identifiers != NULL) \
247 free(identifiers); \
248 identifiers_num = 0; \
249 if (plugins != NULL) \
250 free(plugins); \
251 plugins_num = 0; \
252 return (s); \
253 } while (0)
255 for (int i = 1; i < argc; ++i) {
256 char *key, *value;
258 key = argv[i];
259 value = strchr(argv[i], (int)'=');
261 if (!value) {
262 fprintf(stderr, "ERROR: flush: Invalid option ``%s''.\n", argv[i]);
263 BAIL_OUT(-1);
264 }
266 *value = '\0';
267 ++value;
269 if (strcasecmp(key, "timeout") == 0) {
270 char *endptr = NULL;
272 timeout = (int)strtol(value, &endptr, 0);
274 if (endptr == value) {
275 fprintf(stderr, "ERROR: Failed to parse timeout as number: %s.\n",
276 value);
277 BAIL_OUT(-1);
278 } else if ((endptr != NULL) && (*endptr != '\0')) {
279 fprintf(stderr, "WARNING: Ignoring trailing garbage after timeout: "
280 "%s.\n",
281 endptr);
282 }
283 } else if (strcasecmp(key, "plugin") == 0) {
284 status = array_grow((void *)&plugins, &plugins_num, sizeof(*plugins));
285 if (status != 0)
286 BAIL_OUT(status);
288 plugins[plugins_num - 1] = value;
289 } else if (strcasecmp(key, "identifier") == 0) {
290 status = array_grow((void *)&identifiers, &identifiers_num,
291 sizeof(*identifiers));
292 if (status != 0)
293 BAIL_OUT(status);
295 memset(identifiers + (identifiers_num - 1), 0, sizeof(*identifiers));
296 status = parse_identifier(c, value, identifiers + (identifiers_num - 1));
297 if (status != 0)
298 BAIL_OUT(status);
299 } else {
300 fprintf(stderr, "ERROR: flush: Unknown option `%s'.\n", key);
301 BAIL_OUT(-1);
302 }
303 }
305 if (plugins_num == 0) {
306 status = array_grow((void *)&plugins, &plugins_num, sizeof(*plugins));
307 if (status != 0)
308 BAIL_OUT(status);
310 assert(plugins_num == 1);
311 plugins[0] = NULL;
312 }
314 for (size_t i = 0; i < plugins_num; ++i) {
315 if (identifiers_num == 0) {
316 status = lcc_flush(c, plugins[i], NULL, timeout);
317 if (status != 0)
318 fprintf(stderr, "ERROR: Failed to flush plugin `%s': %s.\n",
319 (plugins[i] == NULL) ? "(all)" : plugins[i], lcc_strerror(c));
320 } else {
321 for (size_t j = 0; j < identifiers_num; ++j) {
322 status = lcc_flush(c, plugins[i], identifiers + j, timeout);
323 if (status != 0) {
324 char id[1024];
326 lcc_identifier_to_string(c, id, sizeof(id), identifiers + j);
327 fprintf(stderr, "ERROR: Failed to flush plugin `%s', "
328 "identifier `%s': %s.\n",
329 (plugins[i] == NULL) ? "(all)" : plugins[i], id,
330 lcc_strerror(c));
331 }
332 }
333 }
334 }
336 BAIL_OUT(0);
337 #undef BAIL_OUT
338 } /* flush */
340 static int listval(lcc_connection_t *c, int argc, char **argv) {
341 lcc_identifier_t *ret_ident = NULL;
342 size_t ret_ident_num = 0;
344 int status;
346 assert(strcasecmp(argv[0], "listval") == 0);
348 if (argc != 1) {
349 fprintf(stderr, "ERROR: listval: Does not accept any arguments.\n");
350 return (-1);
351 }
353 #define BAIL_OUT(s) \
354 do { \
355 if (ret_ident != NULL) \
356 free(ret_ident); \
357 ret_ident_num = 0; \
358 return (s); \
359 } while (0)
361 status = lcc_listval(c, &ret_ident, &ret_ident_num);
362 if (status != 0) {
363 fprintf(stderr, "ERROR: %s\n", lcc_strerror(c));
364 BAIL_OUT(status);
365 }
367 for (size_t i = 0; i < ret_ident_num; ++i) {
368 char id[1024];
370 status = lcc_identifier_to_string(c, id, sizeof(id), ret_ident + i);
371 if (status != 0) {
372 fprintf(stderr, "ERROR: listval: Failed to convert returned "
373 "identifier to a string: %s\n",
374 lcc_strerror(c));
375 continue;
376 }
378 printf("%s\n", id);
379 }
380 BAIL_OUT(0);
381 #undef BAIL_OUT
382 } /* listval */
384 static int putval(lcc_connection_t *c, int argc, char **argv) {
385 lcc_value_list_t vl = LCC_VALUE_LIST_INIT;
387 /* 64 ought to be enough for anybody ;-) */
388 value_t values[64];
389 int values_types[64];
390 size_t values_len = 0;
392 int status;
394 assert(strcasecmp(argv[0], "putval") == 0);
396 if (argc < 3) {
397 fprintf(stderr, "ERROR: putval: Missing identifier "
398 "and/or value list.\n");
399 return (-1);
400 }
402 vl.values = values;
403 vl.values_types = values_types;
405 status = parse_identifier(c, argv[1], &vl.identifier);
406 if (status != 0)
407 return (status);
409 for (int i = 2; i < argc; ++i) {
410 char *tmp;
412 tmp = strchr(argv[i], (int)'=');
414 if (tmp != NULL) { /* option */
415 char *key = argv[i];
416 char *value = tmp;
418 *value = '\0';
419 ++value;
421 if (strcasecmp(key, "interval") == 0) {
422 char *endptr;
424 vl.interval = strtol(value, &endptr, 0);
426 if (endptr == value) {
427 fprintf(stderr, "ERROR: Failed to parse interval as number: %s.\n",
428 value);
429 return (-1);
430 } else if ((endptr != NULL) && (*endptr != '\0')) {
431 fprintf(stderr, "WARNING: Ignoring trailing garbage after "
432 "interval: %s.\n",
433 endptr);
434 }
435 } else {
436 fprintf(stderr, "ERROR: putval: Unknown option `%s'.\n", key);
437 return (-1);
438 }
439 } else { /* value list */
440 char *value;
442 tmp = strchr(argv[i], (int)':');
444 if (tmp == NULL) {
445 fprintf(stderr, "ERROR: putval: Invalid value list: %s.\n", argv[i]);
446 return (-1);
447 }
449 *tmp = '\0';
450 ++tmp;
452 if (strcasecmp(argv[i], "N") == 0) {
453 vl.time = 0;
454 } else {
455 char *endptr;
457 vl.time = strtol(argv[i], &endptr, 0);
459 if (endptr == argv[i]) {
460 fprintf(stderr, "ERROR: Failed to parse time as number: %s.\n",
461 argv[i]);
462 return (-1);
463 } else if ((endptr != NULL) && (*endptr != '\0')) {
464 fprintf(stderr, "ERROR: Garbage after time: %s.\n", endptr);
465 return (-1);
466 }
467 }
469 values_len = 0;
470 value = tmp;
471 while (value != NULL) {
472 char *dot, *endptr;
474 tmp = strchr(value, (int)':');
476 if (tmp != NULL) {
477 *tmp = '\0';
478 ++tmp;
479 }
481 /* This is a bit of a hack, but parsing types.db just does not make
482 * much sense imho -- the server might have different types defined
483 * anyway. Also, lcc uses the type information for formatting the
484 * number only, so the real meaning does not matter. -tokkee */
485 dot = strchr(value, (int)'.');
486 endptr = NULL;
487 if (strcasecmp(value, "U") == 0) {
488 values[values_len].gauge = NAN;
489 values_types[values_len] = LCC_TYPE_GAUGE;
490 } else if (dot) { /* floating point value */
491 values[values_len].gauge = strtod(value, &endptr);
492 values_types[values_len] = LCC_TYPE_GAUGE;
493 } else { /* integer */
494 values[values_len].counter = (counter_t)strtoull(value, &endptr, 0);
495 values_types[values_len] = LCC_TYPE_COUNTER;
496 }
497 ++values_len;
499 if (endptr == value) {
500 fprintf(stderr, "ERROR: Failed to parse value as number: %s.\n",
501 argv[i]);
502 return (-1);
503 } else if ((endptr != NULL) && (*endptr != '\0')) {
504 fprintf(stderr, "ERROR: Garbage after value: %s.\n", endptr);
505 return (-1);
506 }
508 value = tmp;
509 }
511 assert(values_len >= 1);
512 vl.values_len = values_len;
514 status = lcc_putval(c, &vl);
515 if (status != 0) {
516 fprintf(stderr, "ERROR: %s\n", lcc_strerror(c));
517 return (-1);
518 }
519 }
520 }
522 if (values_len == 0) {
523 fprintf(stderr, "ERROR: putval: Missing value list(s).\n");
524 return (-1);
525 }
526 return (0);
527 } /* putval */
529 int main(int argc, char **argv) {
530 char address[1024] = "unix:" DEFAULT_SOCK;
532 lcc_connection_t *c;
534 int status;
536 while (42) {
537 int opt;
539 opt = getopt(argc, argv, "s:h");
541 if (opt == -1)
542 break;
544 switch (opt) {
545 case 's':
546 snprintf(address, sizeof(address), "unix:%s", optarg);
547 address[sizeof(address) - 1] = '\0';
548 break;
549 case 'h':
550 exit_usage(argv[0], 0);
551 default:
552 exit_usage(argv[0], 1);
553 }
554 }
556 if (optind >= argc) {
557 fprintf(stderr, "%s: missing command\n", argv[0]);
558 exit_usage(argv[0], 1);
559 }
561 c = NULL;
562 status = lcc_connect(address, &c);
563 if (status != 0) {
564 fprintf(stderr, "ERROR: Failed to connect to daemon at %s: %s.\n", address,
565 strerror(errno));
566 return (1);
567 }
569 if (strcasecmp(argv[optind], "getval") == 0)
570 status = getval(c, argc - optind, argv + optind);
571 else if (strcasecmp(argv[optind], "flush") == 0)
572 status = flush(c, argc - optind, argv + optind);
573 else if (strcasecmp(argv[optind], "listval") == 0)
574 status = listval(c, argc - optind, argv + optind);
575 else if (strcasecmp(argv[optind], "putval") == 0)
576 status = putval(c, argc - optind, argv + optind);
577 else {
578 fprintf(stderr, "%s: invalid command: %s\n", argv[0], argv[optind]);
579 return (1);
580 }
582 LCC_DESTROY(c);
584 if (status != 0)
585 return (status);
586 return (0);
587 } /* main */