f16a70dd94285861bc5d8bac19debfc66a4f74f0
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 #ifndef _ISOC99_SOURCE
29 # define _ISOC99_SOURCE
30 #endif
32 #ifndef _POSIX_C_SOURCE
33 # define _POSIX_C_SOURCE 200112L
34 #endif
36 #ifndef _XOPEN_SOURCE
37 # define _XOPEN_SOURCE 600
38 #endif
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <strings.h>
46 #include <assert.h>
47 #include <errno.h>
48 #include <math.h>
50 #include "libcollectdclient/collectd/client.h"
53 #define DEFAULT_SOCK LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
55 extern char *optarg;
56 extern int optind;
58 static void exit_usage (const char *name, int status) {
59 fprintf ((status == 0) ? stdout : stderr,
60 "Usage: %s [options] <command> [cmd options]\n\n"
62 "Available options:\n"
63 " -s Path to collectd's UNIX socket.\n"
64 " Default: "DEFAULT_SOCK"\n"
66 "\n -h Display this help and exit.\n"
68 "\nAvailable commands:\n\n"
70 " * getval <identifier>\n"
71 " * flush [timeout=<seconds>] [plugin=<name>] [identifier=<id>]\n"
72 " * listval\n"
73 " * putval <identifier> [interval=<seconds>] <value-list(s)>\n"
75 "\nIdentifiers:\n\n"
77 "An identifier has the following format:\n\n"
79 " [<hostname>/]<plugin>[-<plugin_instance>]/<type>[-<type_instance>]\n\n"
81 "Hostname defaults to the local hostname if omitted (e.g., uptime/uptime).\n"
82 "No error is returned if the specified identifier does not exist.\n"
84 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
85 "by Florian octo Forster <octo@verplant.org>\n"
86 "for contributions see `AUTHORS'\n"
87 , name);
88 exit (status);
89 }
91 /* Count the number of occurrences of the character 'chr'
92 * in the specified string. */
93 static int count_chars (const char *str, char chr) {
94 int count = 0;
96 while (*str != '\0') {
97 if (*str == chr) {
98 count++;
99 }
100 str++;
101 }
103 return count;
104 } /* count_chars */
106 static int array_grow (void **array, int *array_len, size_t elem_size)
107 {
108 void *tmp;
110 assert ((array != NULL) && (array_len != NULL));
112 tmp = realloc (*array, (*array_len + 1) * elem_size);
113 if (tmp == NULL) {
114 fprintf (stderr, "ERROR: Failed to allocate memory.\n");
115 return (-1);
116 }
118 *array = tmp;
119 ++(*array_len);
120 return (0);
121 } /* array_grow */
123 static int parse_identifier (lcc_connection_t *c,
124 const char *value, lcc_identifier_t *ident)
125 {
126 char hostname[1024];
127 char ident_str[1024] = "";
128 int n_slashes;
130 int status;
132 n_slashes = count_chars (value, '/');
133 if (n_slashes == 1) {
134 /* The user has omitted the hostname part of the identifier
135 * (there is only one '/' in the identifier)
136 * Let's add the local hostname */
137 if (gethostname (hostname, sizeof (hostname)) != 0) {
138 fprintf (stderr, "ERROR: Failed to get local hostname: %s",
139 strerror (errno));
140 return (-1);
141 }
142 hostname[sizeof (hostname) - 1] = '\0';
144 snprintf (ident_str, sizeof (ident_str), "%s/%s", hostname, value);
145 ident_str[sizeof(ident_str) - 1] = '\0';
146 }
147 else {
148 strncpy (ident_str, value, sizeof (ident_str));
149 ident_str[sizeof (ident_str) - 1] = '\0';
150 }
152 status = lcc_string_to_identifier (c, ident, ident_str);
153 if (status != 0) {
154 fprintf (stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
155 ident_str, lcc_strerror(c));
156 return (-1);
157 }
158 return (0);
159 } /* parse_identifier */
161 static int getval (lcc_connection_t *c, int argc, char **argv)
162 {
163 lcc_identifier_t ident;
165 size_t ret_values_num = 0;
166 gauge_t *ret_values = NULL;
167 char **ret_values_names = NULL;
169 int status;
170 size_t i;
172 assert (strcasecmp (argv[0], "getval") == 0);
174 if (argc != 2) {
175 fprintf (stderr, "ERROR: getval: Missing identifier.\n");
176 return (-1);
177 }
179 memset (&ident, 0, sizeof (ident));
180 status = parse_identifier (c, argv[1], &ident);
181 if (status != 0)
182 return (status);
184 #define BAIL_OUT(s) \
185 do { \
186 if (ret_values != NULL) \
187 free (ret_values); \
188 if (ret_values_names != NULL) { \
189 for (i = 0; i < ret_values_num; ++i) \
190 free (ret_values_names[i]); \
191 free (ret_values_names); \
192 } \
193 ret_values_num = 0; \
194 return (s); \
195 } while (0)
197 status = lcc_getval (c, &ident,
198 &ret_values_num, &ret_values, &ret_values_names);
199 if (status != 0) {
200 fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
201 BAIL_OUT (-1);
202 }
204 for (i = 0; i < ret_values_num; ++i)
205 printf ("%s=%e\n", ret_values_names[i], ret_values[i]);
206 BAIL_OUT (0);
207 #undef BAIL_OUT
208 } /* getval */
210 static int flush (lcc_connection_t *c, int argc, char **argv)
211 {
212 int timeout = -1;
214 lcc_identifier_t *identifiers = NULL;
215 int identifiers_num = 0;
217 char **plugins = NULL;
218 int plugins_num = 0;
220 int status;
221 int i;
223 assert (strcasecmp (argv[0], "flush") == 0);
225 #define BAIL_OUT(s) \
226 do { \
227 if (identifiers != NULL) \
228 free (identifiers); \
229 identifiers_num = 0; \
230 if (plugins != NULL) \
231 free (plugins); \
232 plugins_num = 0; \
233 return (s); \
234 } while (0)
236 for (i = 1; i < argc; ++i) {
237 char *key, *value;
239 key = argv[i];
240 value = strchr (argv[i], (int)'=');
242 if (! value) {
243 fprintf (stderr, "ERROR: flush: Invalid option ``%s''.\n", argv[i]);
244 BAIL_OUT (-1);
245 }
247 *value = '\0';
248 ++value;
250 if (strcasecmp (key, "timeout") == 0) {
251 char *endptr = NULL;
253 timeout = (int) strtol (value, &endptr, 0);
255 if (endptr == value) {
256 fprintf (stderr, "ERROR: Failed to parse timeout as number: %s.\n",
257 value);
258 BAIL_OUT (-1);
259 }
260 else if ((endptr != NULL) && (*endptr != '\0')) {
261 fprintf (stderr, "WARNING: Ignoring trailing garbage after timeout: "
262 "%s.\n", endptr);
263 }
264 }
265 else if (strcasecmp (key, "plugin") == 0) {
266 status = array_grow ((void *)&plugins, &plugins_num,
267 sizeof (*plugins));
268 if (status != 0)
269 BAIL_OUT (status);
271 plugins[plugins_num - 1] = value;
272 }
273 else if (strcasecmp (key, "identifier") == 0) {
274 status = array_grow ((void *)&identifiers, &identifiers_num,
275 sizeof (*identifiers));
276 if (status != 0)
277 BAIL_OUT (status);
279 memset (identifiers + (identifiers_num - 1), 0, sizeof (*identifiers));
280 status = parse_identifier (c, value,
281 identifiers + (identifiers_num - 1));
282 if (status != 0)
283 BAIL_OUT (status);
284 }
285 else {
286 fprintf (stderr, "ERROR: flush: Unknown option `%s'.\n", key);
287 BAIL_OUT (-1);
288 }
289 }
291 if (plugins_num == 0) {
292 status = array_grow ((void *)&plugins, &plugins_num, sizeof (*plugins));
293 if (status != 0)
294 BAIL_OUT (status);
296 assert (plugins_num == 1);
297 plugins[0] = NULL;
298 }
300 for (i = 0; i < plugins_num; ++i) {
301 if (identifiers_num == 0) {
302 status = lcc_flush (c, plugins[i], NULL, timeout);
303 if (status != 0)
304 fprintf (stderr, "ERROR: Failed to flush plugin `%s': %s.\n",
305 (plugins[i] == NULL) ? "(all)" : plugins[i], lcc_strerror (c));
306 }
307 else {
308 int j;
310 for (j = 0; j < identifiers_num; ++j) {
311 status = lcc_flush (c, plugins[i], identifiers + j, timeout);
312 if (status != 0) {
313 char id[1024];
315 lcc_identifier_to_string (c, id, sizeof (id), identifiers + j);
316 fprintf (stderr, "ERROR: Failed to flush plugin `%s', "
317 "identifier `%s': %s.\n",
318 (plugins[i] == NULL) ? "(all)" : plugins[i],
319 id, lcc_strerror (c));
320 }
321 }
322 }
323 }
325 BAIL_OUT (0);
326 #undef BAIL_OUT
327 } /* flush */
329 static int listval (lcc_connection_t *c, int argc, char **argv)
330 {
331 lcc_identifier_t *ret_ident = NULL;
332 size_t ret_ident_num = 0;
334 int status;
335 size_t i;
337 assert (strcasecmp (argv[0], "listval") == 0);
339 if (argc != 1) {
340 fprintf (stderr, "ERROR: listval: Does not accept any arguments.\n");
341 return (-1);
342 }
344 #define BAIL_OUT(s) \
345 do { \
346 if (ret_ident != NULL) \
347 free (ret_ident); \
348 ret_ident_num = 0; \
349 return (s); \
350 } while (0)
352 status = lcc_listval (c, &ret_ident, &ret_ident_num);
353 if (status != 0) {
354 fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
355 BAIL_OUT (status);
356 }
358 for (i = 0; i < ret_ident_num; ++i) {
359 char id[1024];
361 status = lcc_identifier_to_string (c, id, sizeof (id), ret_ident + i);
362 if (status != 0) {
363 fprintf (stderr, "ERROR: listval: Failed to convert returned "
364 "identifier to a string: %s\n", lcc_strerror (c));
365 continue;
366 }
368 printf ("%s\n", id);
369 }
370 BAIL_OUT (0);
371 #undef BAIL_OUT
372 } /* listval */
374 static int putval (lcc_connection_t *c, int argc, char **argv)
375 {
376 lcc_value_list_t vl = LCC_VALUE_LIST_INIT;
378 /* 64 ought to be enough for anybody ;-) */
379 value_t values[64];
380 int values_types[64];
381 size_t values_len = 0;
383 int status;
384 int i;
386 assert (strcasecmp (argv[0], "putval") == 0);
388 if (argc < 3) {
389 fprintf (stderr, "ERROR: putval: Missing identifier "
390 "and/or value list.\n");
391 return (-1);
392 }
394 vl.values = values;
395 vl.values_types = values_types;
397 status = parse_identifier (c, argv[1], &vl.identifier);
398 if (status != 0)
399 return (status);
401 for (i = 2; i < argc; ++i) {
402 char *tmp;
404 tmp = strchr (argv[i], (int)'=');
406 if (tmp != NULL) { /* option */
407 char *key = argv[i];
408 char *value = tmp;
410 *value = '\0';
411 ++value;
413 if (strcasecmp (key, "interval") == 0) {
414 char *endptr;
416 vl.interval = strtol (value, &endptr, 0);
418 if (endptr == value) {
419 fprintf (stderr, "ERROR: Failed to parse interval as number: %s.\n",
420 value);
421 return (-1);
422 }
423 else if ((endptr != NULL) && (*endptr != '\0')) {
424 fprintf (stderr, "WARNING: Ignoring trailing garbage after "
425 "interval: %s.\n", endptr);
426 }
427 }
428 else {
429 fprintf (stderr, "ERROR: putval: Unknown option `%s'.\n", key);
430 return (-1);
431 }
432 }
433 else { /* value list */
434 char *value;
436 tmp = strchr (argv[i], (int)':');
438 if (tmp == NULL) {
439 fprintf (stderr, "ERROR: putval: Invalid value list: %s.\n",
440 argv[i]);
441 return (-1);
442 }
444 *tmp = '\0';
445 ++tmp;
447 if (strcasecmp (argv[i], "N") == 0) {
448 vl.time = 0;
449 }
450 else {
451 char *endptr;
453 vl.time = strtol (argv[i], &endptr, 0);
455 if (endptr == argv[i]) {
456 fprintf (stderr, "ERROR: Failed to parse time as number: %s.\n",
457 argv[i]);
458 return (-1);
459 }
460 else if ((endptr != NULL) && (*endptr != '\0')) {
461 fprintf (stderr, "ERROR: Garbage after time: %s.\n", endptr);
462 return (-1);
463 }
464 }
466 values_len = 0;
467 value = tmp;
468 while (value != 0) {
469 char *dot, *endptr;
471 tmp = strchr (argv[i], (int)':');
473 if (tmp != NULL) {
474 *tmp = '\0';
475 ++tmp;
476 }
478 /* This is a bit of a hack, but parsing types.db just does not make
479 * much sense imho -- the server might have different types defined
480 * anyway. Also, lcc uses the type information for formatting the
481 * number only, so the real meaning does not matter. -tokkee */
482 dot = strchr (value, (int)'.');
483 endptr = NULL;
484 if (strcasecmp (value, "U") == 0) {
485 values[values_len].gauge = NAN;
486 values_types[values_len] = LCC_TYPE_GAUGE;
487 }
488 else if (dot) { /* floating point value */
489 values[values_len].gauge = strtod (value, &endptr);
490 values_types[values_len] = LCC_TYPE_GAUGE;
491 }
492 else { /* integer */
493 values[values_len].counter = strtol (value, &endptr, 0);
494 values_types[values_len] = LCC_TYPE_COUNTER;
495 }
496 ++values_len;
498 if (endptr == value) {
499 fprintf (stderr, "ERROR: Failed to parse value as number: %s.\n",
500 argv[i]);
501 return (-1);
502 }
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 c;
539 c = getopt (argc, argv, "s:h");
541 if (c == -1)
542 break;
544 switch (c) {
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 break;
552 default:
553 exit_usage (argv[0], 1);
554 }
555 }
557 if (optind >= argc) {
558 fprintf (stderr, "%s: missing command\n", argv[0]);
559 exit_usage (argv[0], 1);
560 }
562 c = NULL;
563 status = lcc_connect (address, &c);
564 if (status != 0) {
565 fprintf (stderr, "ERROR: Failed to connect to daemon at %s: %s.\n",
566 address, strerror (errno));
567 return (1);
568 }
570 if (strcasecmp (argv[optind], "getval") == 0)
571 status = getval (c, argc - optind, argv + optind);
572 else if (strcasecmp (argv[optind], "flush") == 0)
573 status = flush (c, argc - optind, argv + optind);
574 else if (strcasecmp (argv[optind], "listval") == 0)
575 status = listval (c, argc - optind, argv + optind);
576 else if (strcasecmp (argv[optind], "putval") == 0)
577 status = putval (c, argc - optind, argv + optind);
578 else {
579 fprintf (stderr, "%s: invalid command: %s\n", argv[0], argv[optind]);
580 return (1);
581 }
583 LCC_DESTROY (c);
585 if (status != 0)
586 return (status);
587 return (0);
588 } /* main */
590 /* vim: set sw=2 ts=2 tw=78 expandtab : */