2 * collectd - src/collectdctl.c
3 * Copyright (C) 2010 Håkon J Dugstad Johnsen
4 * Copyright (C) 2010 Sebastian Harl
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.
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.
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
20 * Håkon J Dugstad Johnsen <hakon-dugstad.johnsen at telenor.com>
21 * Sebastian "tokkee" Harl <sh@tokkee.org>
28 #ifndef _ISOC99_SOURCE
29 # define _ISOC99_SOURCE
32 #ifndef _POSIX_C_SOURCE
33 # define _POSIX_C_SOURCE 200112L
37 # define _XOPEN_SOURCE 600
50 #include "libcollectdclient/client.h"
53 #define DEFAULT_SOCK LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
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"
73 " * putval <identifier> [interval=<seconds>] <value-list(s)>\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"
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) {
96 while (*str != '\0') {
106 static int array_grow (void **array, int *array_len, size_t elem_size)
110 assert ((array != NULL) && (array_len != NULL));
112 tmp = realloc (*array, (*array_len + 1) * elem_size);
114 fprintf (stderr, "ERROR: Failed to allocate memory.\n");
123 static int parse_identifier (lcc_connection_t *c,
124 const char *value, lcc_identifier_t *ident)
127 char ident_str[1024] = "";
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",
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';
148 strncpy (ident_str, value, sizeof (ident_str));
149 ident_str[sizeof (ident_str) - 1] = '\0';
152 status = lcc_string_to_identifier (c, ident, ident_str);
154 fprintf (stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
155 ident_str, lcc_strerror(c));
159 } /* parse_identifier */
161 static int getval (lcc_connection_t *c, int argc, char **argv)
163 lcc_identifier_t ident;
165 size_t ret_values_num = 0;
166 gauge_t *ret_values = NULL;
167 char **ret_values_names = NULL;
172 assert (strcasecmp (argv[0], "getval") == 0);
175 fprintf (stderr, "ERROR: getval: Missing identifier.\n");
179 memset (&ident, 0, sizeof (ident));
180 status = parse_identifier (c, argv[1], &ident);
184 #define BAIL_OUT(s) \
186 if (ret_values != NULL) \
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); \
193 ret_values_num = 0; \
197 status = lcc_getval (c, &ident,
198 &ret_values_num, &ret_values, &ret_values_names);
200 fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
204 for (i = 0; i < ret_values_num; ++i)
205 printf ("%s=%e\n", ret_values_names[i], ret_values[i]);
210 static int flush (lcc_connection_t *c, int argc, char **argv)
214 lcc_identifier_t *identifiers = NULL;
215 int identifiers_num = 0;
217 char **plugins = NULL;
223 assert (strcasecmp (argv[0], "flush") == 0);
225 #define BAIL_OUT(s) \
227 if (identifiers != NULL) \
228 free (identifiers); \
229 identifiers_num = 0; \
230 if (plugins != NULL) \
236 for (i = 1; i < argc; ++i) {
240 value = strchr (argv[i], (int)'=');
243 fprintf (stderr, "ERROR: flush: Invalid option ``%s''.\n", argv[i]);
250 if (strcasecmp (key, "timeout") == 0) {
253 timeout = (int) strtol (value, &endptr, 0);
255 if (endptr == value) {
256 fprintf (stderr, "ERROR: Failed to parse timeout as number: %s.\n",
260 else if ((endptr != NULL) && (*endptr != '\0')) {
261 fprintf (stderr, "WARNING: Ignoring trailing garbage after timeout: "
265 else if (strcasecmp (key, "plugin") == 0) {
266 status = array_grow ((void *)&plugins, &plugins_num,
271 plugins[plugins_num - 1] = value;
273 else if (strcasecmp (key, "identifier") == 0) {
274 status = array_grow ((void *)&identifiers, &identifiers_num,
275 sizeof (*identifiers));
279 memset (identifiers + (identifiers_num - 1), 0, sizeof (*identifiers));
280 status = parse_identifier (c, value,
281 identifiers + (identifiers_num - 1));
286 fprintf (stderr, "ERROR: flush: Unknown option `%s'.\n", key);
291 if (plugins_num == 0) {
292 status = array_grow ((void *)&plugins, &plugins_num, sizeof (*plugins));
296 assert (plugins_num == 1);
300 for (i = 0; i < plugins_num; ++i) {
301 if (identifiers_num == 0) {
302 status = lcc_flush (c, plugins[i], NULL, timeout);
304 fprintf (stderr, "ERROR: Failed to flush plugin `%s': %s.\n",
305 (plugins[i] == NULL) ? "(all)" : plugins[i], lcc_strerror (c));
310 for (j = 0; j < identifiers_num; ++j) {
311 status = lcc_flush (c, plugins[i], identifiers + j, timeout);
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));
329 static int listval (lcc_connection_t *c, int argc, char **argv)
331 lcc_identifier_t *ret_ident = NULL;
332 size_t ret_ident_num = 0;
337 assert (strcasecmp (argv[0], "listval") == 0);
340 fprintf (stderr, "ERROR: listval: Does not accept any arguments.\n");
344 #define BAIL_OUT(s) \
346 if (ret_ident != NULL) \
352 status = lcc_listval (c, &ret_ident, &ret_ident_num);
354 fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
358 for (i = 0; i < ret_ident_num; ++i) {
361 status = lcc_identifier_to_string (c, id, sizeof (id), ret_ident + i);
363 fprintf (stderr, "ERROR: listval: Failed to convert returned "
364 "identifier to a string: %s\n", lcc_strerror (c));
374 static int putval (lcc_connection_t *c, int argc, char **argv)
376 lcc_value_list_t vl = LCC_VALUE_LIST_INIT;
378 /* 64 ought to be enough for anybody ;-) */
380 int values_types[64];
381 size_t values_len = 0;
386 assert (strcasecmp (argv[0], "putval") == 0);
389 fprintf (stderr, "ERROR: putval: Missing identifier "
390 "and/or value list.\n");
395 vl.values_types = values_types;
397 status = parse_identifier (c, argv[1], &vl.identifier);
401 for (i = 2; i < argc; ++i) {
404 tmp = strchr (argv[i], (int)'=');
406 if (tmp != NULL) { /* option */
413 if (strcasecmp (key, "interval") == 0) {
416 vl.interval = strtol (value, &endptr, 0);
418 if (endptr == value) {
419 fprintf (stderr, "ERROR: Failed to parse interval as number: %s.\n",
423 else if ((endptr != NULL) && (*endptr != '\0')) {
424 fprintf (stderr, "WARNING: Ignoring trailing garbage after "
425 "interval: %s.\n", endptr);
429 fprintf (stderr, "ERROR: putval: Unknown option `%s'.\n", key);
433 else { /* value list */
436 tmp = strchr (argv[i], (int)':');
439 fprintf (stderr, "ERROR: putval: Invalid value list: %s.\n",
447 if (strcasecmp (argv[i], "N") == 0) {
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",
460 else if ((endptr != NULL) && (*endptr != '\0')) {
461 fprintf (stderr, "ERROR: Garbage after time: %s.\n", endptr);
471 tmp = strchr (argv[i], (int)':');
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)'.');
484 if (strcasecmp (value, "U") == 0) {
485 values[values_len].gauge = NAN;
486 values_types[values_len] = LCC_TYPE_GAUGE;
488 else if (dot) { /* floating point value */
489 values[values_len].gauge = strtod (value, &endptr);
490 values_types[values_len] = LCC_TYPE_GAUGE;
493 values[values_len].counter = strtol (value, &endptr, 0);
494 values_types[values_len] = LCC_TYPE_COUNTER;
498 if (endptr == value) {
499 fprintf (stderr, "ERROR: Failed to parse value as number: %s.\n",
503 else if ((endptr != NULL) && (*endptr != '\0')) {
504 fprintf (stderr, "ERROR: Garbage after value: %s.\n", endptr);
511 assert (values_len >= 1);
512 vl.values_len = values_len;
514 status = lcc_putval (c, &vl);
516 fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
522 if (values_len == 0) {
523 fprintf (stderr, "ERROR: putval: Missing value list(s).\n");
529 int main (int argc, char **argv) {
530 char address[1024] = "unix:"DEFAULT_SOCK;
539 c = getopt (argc, argv, "s:h");
546 snprintf (address, sizeof (address), "unix:%s", optarg);
547 address[sizeof (address) - 1] = '\0';
550 exit_usage (argv[0], 0);
553 exit_usage (argv[0], 1);
557 if (optind >= argc) {
558 fprintf (stderr, "%s: missing command\n", argv[0]);
559 exit_usage (argv[0], 1);
563 status = lcc_connect (address, &c);
565 fprintf (stderr, "ERROR: Failed to connect to daemon at %s: %s.\n",
566 address, strerror (errno));
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);
579 fprintf (stderr, "%s: invalid command: %s\n", argv[0], argv[optind]);
590 /* vim: set sw=2 ts=2 tw=78 expandtab : */