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 #include "libcollectdclient/client.h"
42 #define DEFAULT_SOCK LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
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"
62 " * putval <identifier> [interval=<seconds>] <value-list(s)>\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"
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) {
85 while (*str != '\0') {
95 static int array_grow (void **array, int *array_len, size_t elem_size)
99 assert ((array != NULL) && (array_len != NULL));
101 tmp = realloc (*array, (*array_len + 1) * elem_size);
103 fprintf (stderr, "ERROR: Failed to allocate memory.\n");
112 static int parse_identifier (lcc_connection_t *c,
113 const char *value, lcc_identifier_t *ident)
116 char ident_str[1024] = "";
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",
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';
137 strncpy (ident_str, value, sizeof (ident_str));
138 ident_str[sizeof (ident_str) - 1] = '\0';
141 status = lcc_string_to_identifier (c, ident, ident_str);
143 fprintf (stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
144 ident_str, lcc_strerror(c));
148 } /* parse_identifier */
150 static int getval (lcc_connection_t *c, int argc, char **argv)
152 lcc_identifier_t ident;
154 size_t ret_values_num = 0;
155 gauge_t *ret_values = NULL;
156 char **ret_values_names = NULL;
161 assert (strcasecmp (argv[0], "getval") == 0);
164 fprintf (stderr, "ERROR: getval: Missing identifier.\n");
168 memset (&ident, 0, sizeof (ident));
169 status = parse_identifier (c, argv[1], &ident);
173 #define BAIL_OUT(s) \
175 if (ret_values != NULL) \
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); \
182 ret_values_num = 0; \
186 status = lcc_getval (c, &ident,
187 &ret_values_num, &ret_values, &ret_values_names);
189 fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
193 for (i = 0; i < ret_values_num; ++i)
194 printf ("%s=%e\n", ret_values_names[i], ret_values[i]);
199 static int flush (lcc_connection_t *c, int argc, char **argv)
203 lcc_identifier_t *identifiers = NULL;
204 int identifiers_num = 0;
206 char **plugins = NULL;
212 assert (strcasecmp (argv[0], "flush") == 0);
214 #define BAIL_OUT(s) \
216 if (identifiers != NULL) \
217 free (identifiers); \
218 identifiers_num = 0; \
219 if (plugins != NULL) \
225 for (i = 1; i < argc; ++i) {
229 value = strchr (argv[i], (int)'=');
232 fprintf (stderr, "ERROR: flush: Invalid option ``%s''.\n", argv[i]);
239 if (strcasecmp (key, "timeout") == 0) {
242 timeout = strtol (value, &endptr, 0);
244 if (endptr == value) {
245 fprintf (stderr, "ERROR: Failed to parse timeout as number: %s.\n",
249 else if ((endptr != NULL) && (*endptr != '\0')) {
250 fprintf (stderr, "WARNING: Ignoring trailing garbage after timeout: "
254 else if (strcasecmp (key, "plugin") == 0) {
255 status = array_grow ((void **)&plugins, &plugins_num,
260 plugins[plugins_num - 1] = value;
262 else if (strcasecmp (key, "identifier") == 0) {
263 status = array_grow ((void **)&identifiers, &identifiers_num,
264 sizeof (*identifiers));
268 memset (identifiers + (identifiers_num - 1), 0, sizeof (*identifiers));
269 status = parse_identifier (c, value,
270 identifiers + (identifiers_num - 1));
276 if (plugins_num == 0) {
277 status = array_grow ((void **)&plugins, &plugins_num, sizeof (*plugins));
281 assert (plugins_num == 1);
285 for (i = 0; i < plugins_num; ++i) {
286 if (identifiers_num == 0) {
287 status = lcc_flush (c, plugins[i], NULL, timeout);
289 fprintf (stderr, "ERROR: Failed to flush plugin `%s': %s.\n",
290 (plugins[i] == NULL) ? "(all)" : plugins[i], lcc_strerror (c));
295 for (j = 0; j < identifiers_num; ++j) {
296 status = lcc_flush (c, plugins[i], identifiers + j, timeout);
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));
314 static int listval (lcc_connection_t *c, int argc, char **argv)
316 lcc_identifier_t *ret_ident = NULL;
317 size_t ret_ident_num = 0;
322 assert (strcasecmp (argv[0], "listval") == 0);
325 fprintf (stderr, "ERROR: listval: Does not accept any arguments.\n");
329 #define BAIL_OUT(s) \
331 if (ret_ident != NULL) \
337 status = lcc_listval (c, &ret_ident, &ret_ident_num);
339 fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
343 for (i = 0; i < ret_ident_num; ++i) {
346 status = lcc_identifier_to_string (c, id, sizeof (id), ret_ident + i);
348 fprintf (stderr, "ERROR: listval: Failed to convert returned "
349 "identifier to a string: %s\n", lcc_strerror (c));
359 static int putval (lcc_connection_t *c, int argc, char **argv)
361 lcc_value_list_t vl = LCC_VALUE_LIST_INIT;
363 /* 64 ought to be enough for anybody ;-) */
365 int values_types[64];
366 size_t values_len = 0;
371 assert (strcasecmp (argv[0], "putval") == 0);
374 fprintf (stderr, "ERROR: putval: Missing identifier "
375 "and/or value list.\n");
380 vl.values_types = values_types;
382 status = parse_identifier (c, argv[1], &vl.identifier);
386 for (i = 2; i < argc; ++i) {
389 tmp = strchr (argv[i], (int)'=');
391 if (tmp != NULL) { /* option */
398 if (strcasecmp (key, "interval") == 0) {
401 vl.interval = strtol (value, &endptr, 0);
403 if (endptr == value) {
404 fprintf (stderr, "ERROR: Failed to parse interval as number: %s.\n",
408 else if ((endptr != NULL) && (*endptr != '\0')) {
409 fprintf (stderr, "WARNING: Ignoring trailing garbage after "
410 "interval: %s.\n", endptr);
414 fprintf (stderr, "ERROR: putval: Unknown option `%s'.\n", key);
418 else { /* value list */
421 tmp = strchr (argv[i], (int)':');
424 fprintf (stderr, "ERROR: putval: Invalid value list: %s.\n",
432 if (strcasecmp (argv[i], "N") == 0) {
438 vl.time = strtol (argv[i], &endptr, 0);
440 if (endptr == value) {
441 fprintf (stderr, "ERROR: Failed to parse time as number: %s.\n",
445 else if ((endptr != NULL) && (*endptr != '\0')) {
446 fprintf (stderr, "ERROR: Garbage after time: %s.\n", endptr);
456 tmp = strchr (argv[i], (int)':');
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;
473 values[values_len].counter = strtol (value, &endptr, 0);
474 values_types[values_len] = LCC_TYPE_COUNTER;
478 if (endptr == value) {
479 fprintf (stderr, "ERROR: Failed to parse value as number: %s.\n",
483 else if ((endptr != NULL) && (*endptr != '\0')) {
484 fprintf (stderr, "ERROR: Garbage after value: %s.\n", endptr);
491 assert (values_len >= 1);
492 vl.values_len = values_len;
494 status = lcc_putval (c, &vl);
496 fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
502 if (values_len == 0) {
503 fprintf (stderr, "ERROR: putval: Missing value list(s).\n");
509 int main (int argc, char **argv) {
510 char address[1024] = "unix:"DEFAULT_SOCK;
519 c = getopt (argc, argv, "s:h");
526 snprintf (address, sizeof (address), "unix:%s", optarg);
527 address[sizeof (address) - 1] = '\0';
530 exit_usage (argv[0], 0);
533 exit_usage (argv[0], 1);
537 if (optind >= argc) {
538 fprintf (stderr, "%s: missing command\n", argv[0]);
539 exit_usage (argv[0], 1);
543 status = lcc_connect (address, &c);
545 fprintf (stderr, "ERROR: Failed to connect to daemon at %s: %s.\n",
546 address, strerror (errno));
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);
559 fprintf (stderr, "%s: invalid command: %s\n", argv[0], argv[optind]);
570 /* vim: set sw=2 ts=2 tw=78 expandtab : */