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));
275 fprintf (stderr, "ERROR: flush: Unknown option `%s'.\n", key);
280 if (plugins_num == 0) {
281 status = array_grow ((void **)&plugins, &plugins_num, sizeof (*plugins));
285 assert (plugins_num == 1);
289 for (i = 0; i < plugins_num; ++i) {
290 if (identifiers_num == 0) {
291 status = lcc_flush (c, plugins[i], NULL, timeout);
293 fprintf (stderr, "ERROR: Failed to flush plugin `%s': %s.\n",
294 (plugins[i] == NULL) ? "(all)" : plugins[i], lcc_strerror (c));
299 for (j = 0; j < identifiers_num; ++j) {
300 status = lcc_flush (c, plugins[i], identifiers + j, timeout);
304 lcc_identifier_to_string (c, id, sizeof (id), identifiers + j);
305 fprintf (stderr, "ERROR: Failed to flush plugin `%s', "
306 "identifier `%s': %s.\n",
307 (plugins[i] == NULL) ? "(all)" : plugins[i],
308 id, lcc_strerror (c));
318 static int listval (lcc_connection_t *c, int argc, char **argv)
320 lcc_identifier_t *ret_ident = NULL;
321 size_t ret_ident_num = 0;
326 assert (strcasecmp (argv[0], "listval") == 0);
329 fprintf (stderr, "ERROR: listval: Does not accept any arguments.\n");
333 #define BAIL_OUT(s) \
335 if (ret_ident != NULL) \
341 status = lcc_listval (c, &ret_ident, &ret_ident_num);
343 fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
347 for (i = 0; i < ret_ident_num; ++i) {
350 status = lcc_identifier_to_string (c, id, sizeof (id), ret_ident + i);
352 fprintf (stderr, "ERROR: listval: Failed to convert returned "
353 "identifier to a string: %s\n", lcc_strerror (c));
363 static int putval (lcc_connection_t *c, int argc, char **argv)
365 lcc_value_list_t vl = LCC_VALUE_LIST_INIT;
367 /* 64 ought to be enough for anybody ;-) */
369 int values_types[64];
370 size_t values_len = 0;
375 assert (strcasecmp (argv[0], "putval") == 0);
378 fprintf (stderr, "ERROR: putval: Missing identifier "
379 "and/or value list.\n");
384 vl.values_types = values_types;
386 status = parse_identifier (c, argv[1], &vl.identifier);
390 for (i = 2; i < argc; ++i) {
393 tmp = strchr (argv[i], (int)'=');
395 if (tmp != NULL) { /* option */
402 if (strcasecmp (key, "interval") == 0) {
405 vl.interval = strtol (value, &endptr, 0);
407 if (endptr == value) {
408 fprintf (stderr, "ERROR: Failed to parse interval as number: %s.\n",
412 else if ((endptr != NULL) && (*endptr != '\0')) {
413 fprintf (stderr, "WARNING: Ignoring trailing garbage after "
414 "interval: %s.\n", endptr);
418 fprintf (stderr, "ERROR: putval: Unknown option `%s'.\n", key);
422 else { /* value list */
425 tmp = strchr (argv[i], (int)':');
428 fprintf (stderr, "ERROR: putval: Invalid value list: %s.\n",
436 if (strcasecmp (argv[i], "N") == 0) {
442 vl.time = strtol (argv[i], &endptr, 0);
444 if (endptr == value) {
445 fprintf (stderr, "ERROR: Failed to parse time as number: %s.\n",
449 else if ((endptr != NULL) && (*endptr != '\0')) {
450 fprintf (stderr, "ERROR: Garbage after time: %s.\n", endptr);
460 tmp = strchr (argv[i], (int)':');
467 /* This is a bit of a hack, but parsing types.db just does not make
468 * much sense imho -- the server might have different types defined
469 * anyway. Also, lcc uses the type information for formatting the
470 * number only, so the real meaning does not matter. -tokkee */
471 dot = strchr (value, (int)'.');
472 if (dot) { /* floating point value */
473 values[values_len].gauge = strtod (value, &endptr);
474 values_types[values_len] = LCC_TYPE_GAUGE;
477 values[values_len].counter = strtol (value, &endptr, 0);
478 values_types[values_len] = LCC_TYPE_COUNTER;
482 if (endptr == value) {
483 fprintf (stderr, "ERROR: Failed to parse value as number: %s.\n",
487 else if ((endptr != NULL) && (*endptr != '\0')) {
488 fprintf (stderr, "ERROR: Garbage after value: %s.\n", endptr);
495 assert (values_len >= 1);
496 vl.values_len = values_len;
498 status = lcc_putval (c, &vl);
500 fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
506 if (values_len == 0) {
507 fprintf (stderr, "ERROR: putval: Missing value list(s).\n");
513 int main (int argc, char **argv) {
514 char address[1024] = "unix:"DEFAULT_SOCK;
523 c = getopt (argc, argv, "s:h");
530 snprintf (address, sizeof (address), "unix:%s", optarg);
531 address[sizeof (address) - 1] = '\0';
534 exit_usage (argv[0], 0);
537 exit_usage (argv[0], 1);
541 if (optind >= argc) {
542 fprintf (stderr, "%s: missing command\n", argv[0]);
543 exit_usage (argv[0], 1);
547 status = lcc_connect (address, &c);
549 fprintf (stderr, "ERROR: Failed to connect to daemon at %s: %s.\n",
550 address, strerror (errno));
554 if (strcasecmp (argv[optind], "getval") == 0)
555 status = getval (c, argc - optind, argv + optind);
556 else if (strcasecmp (argv[optind], "flush") == 0)
557 status = flush (c, argc - optind, argv + optind);
558 else if (strcasecmp (argv[optind], "listval") == 0)
559 status = listval (c, argc - optind, argv + optind);
560 else if (strcasecmp (argv[optind], "putval") == 0)
561 status = putval (c, argc - optind, argv + optind);
563 fprintf (stderr, "%s: invalid command: %s\n", argv[0], argv[optind]);
574 /* vim: set sw=2 ts=2 tw=78 expandtab : */