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/collectd/client.h"
44 #define DEFAULT_SOCK LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
49 static void exit_usage (const char *name, int status) {
50 fprintf ((status == 0) ? stdout : stderr,
51 "Usage: %s [options] <command> [cmd options]\n\n"
53 "Available options:\n"
54 " -s Path to collectd's UNIX socket.\n"
55 " Default: "DEFAULT_SOCK"\n"
57 "\n -h Display this help and exit.\n"
59 "\nAvailable commands:\n\n"
61 " * getval <identifier>\n"
62 " * flush [timeout=<seconds>] [plugin=<name>] [identifier=<id>]\n"
64 " * putval <identifier> [interval=<seconds>] <value-list(s)>\n"
68 "An identifier has the following format:\n\n"
70 " [<hostname>/]<plugin>[-<plugin_instance>]/<type>[-<type_instance>]\n\n"
72 "Hostname defaults to the local hostname if omitted (e.g., uptime/uptime).\n"
73 "No error is returned if the specified identifier does not exist.\n"
75 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
76 "by Florian octo Forster <octo@verplant.org>\n"
77 "for contributions see `AUTHORS'\n"
82 /* Count the number of occurrences of the character 'chr'
83 * in the specified string. */
84 static int count_chars (const char *str, char chr) {
87 while (*str != '\0') {
97 static int array_grow (void **array, int *array_len, size_t elem_size)
101 assert ((array != NULL) && (array_len != NULL));
103 tmp = realloc (*array, (*array_len + 1) * elem_size);
105 fprintf (stderr, "ERROR: Failed to allocate memory.\n");
114 static int parse_identifier (lcc_connection_t *c,
115 const char *value, lcc_identifier_t *ident)
118 char ident_str[1024] = "";
123 n_slashes = count_chars (value, '/');
124 if (n_slashes == 1) {
125 /* The user has omitted the hostname part of the identifier
126 * (there is only one '/' in the identifier)
127 * Let's add the local hostname */
128 if (gethostname (hostname, sizeof (hostname)) != 0) {
129 fprintf (stderr, "ERROR: Failed to get local hostname: %s",
133 hostname[sizeof (hostname) - 1] = '\0';
135 snprintf (ident_str, sizeof (ident_str), "%s/%s", hostname, value);
136 ident_str[sizeof(ident_str) - 1] = '\0';
139 strncpy (ident_str, value, sizeof (ident_str));
140 ident_str[sizeof (ident_str) - 1] = '\0';
143 status = lcc_string_to_identifier (c, ident, ident_str);
145 fprintf (stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
146 ident_str, lcc_strerror(c));
150 } /* parse_identifier */
152 static int getval (lcc_connection_t *c, int argc, char **argv)
154 lcc_identifier_t ident;
156 size_t ret_values_num = 0;
157 gauge_t *ret_values = NULL;
158 char **ret_values_names = NULL;
163 assert (strcasecmp (argv[0], "getval") == 0);
166 fprintf (stderr, "ERROR: getval: Missing identifier.\n");
170 memset (&ident, 0, sizeof (ident));
171 status = parse_identifier (c, argv[1], &ident);
175 #define BAIL_OUT(s) \
177 if (ret_values != NULL) \
179 if (ret_values_names != NULL) { \
180 for (i = 0; i < ret_values_num; ++i) \
181 free (ret_values_names[i]); \
182 free (ret_values_names); \
184 ret_values_num = 0; \
188 status = lcc_getval (c, &ident,
189 &ret_values_num, &ret_values, &ret_values_names);
191 fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
195 for (i = 0; i < ret_values_num; ++i)
196 printf ("%s=%e\n", ret_values_names[i], ret_values[i]);
201 static int flush (lcc_connection_t *c, int argc, char **argv)
205 lcc_identifier_t *identifiers = NULL;
206 int identifiers_num = 0;
208 char **plugins = NULL;
214 assert (strcasecmp (argv[0], "flush") == 0);
216 #define BAIL_OUT(s) \
218 if (identifiers != NULL) \
219 free (identifiers); \
220 identifiers_num = 0; \
221 if (plugins != NULL) \
227 for (i = 1; i < argc; ++i) {
231 value = strchr (argv[i], (int)'=');
234 fprintf (stderr, "ERROR: flush: Invalid option ``%s''.\n", argv[i]);
241 if (strcasecmp (key, "timeout") == 0) {
244 timeout = (int) strtol (value, &endptr, 0);
246 if (endptr == value) {
247 fprintf (stderr, "ERROR: Failed to parse timeout as number: %s.\n",
251 else if ((endptr != NULL) && (*endptr != '\0')) {
252 fprintf (stderr, "WARNING: Ignoring trailing garbage after timeout: "
256 else if (strcasecmp (key, "plugin") == 0) {
257 status = array_grow ((void **)&plugins, &plugins_num,
262 plugins[plugins_num - 1] = value;
264 else if (strcasecmp (key, "identifier") == 0) {
265 status = array_grow ((void **)&identifiers, &identifiers_num,
266 sizeof (*identifiers));
270 memset (identifiers + (identifiers_num - 1), 0, sizeof (*identifiers));
271 status = parse_identifier (c, value,
272 identifiers + (identifiers_num - 1));
277 fprintf (stderr, "ERROR: flush: Unknown option `%s'.\n", key);
282 if (plugins_num == 0) {
283 status = array_grow ((void **)&plugins, &plugins_num, sizeof (*plugins));
287 assert (plugins_num == 1);
291 for (i = 0; i < plugins_num; ++i) {
292 if (identifiers_num == 0) {
293 status = lcc_flush (c, plugins[i], NULL, timeout);
295 fprintf (stderr, "ERROR: Failed to flush plugin `%s': %s.\n",
296 (plugins[i] == NULL) ? "(all)" : plugins[i], lcc_strerror (c));
301 for (j = 0; j < identifiers_num; ++j) {
302 status = lcc_flush (c, plugins[i], identifiers + j, timeout);
306 lcc_identifier_to_string (c, id, sizeof (id), identifiers + j);
307 fprintf (stderr, "ERROR: Failed to flush plugin `%s', "
308 "identifier `%s': %s.\n",
309 (plugins[i] == NULL) ? "(all)" : plugins[i],
310 id, lcc_strerror (c));
320 static int listval (lcc_connection_t *c, int argc, char **argv)
322 lcc_identifier_t *ret_ident = NULL;
323 size_t ret_ident_num = 0;
328 assert (strcasecmp (argv[0], "listval") == 0);
331 fprintf (stderr, "ERROR: listval: Does not accept any arguments.\n");
335 #define BAIL_OUT(s) \
337 if (ret_ident != NULL) \
343 status = lcc_listval (c, &ret_ident, &ret_ident_num);
345 fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
349 for (i = 0; i < ret_ident_num; ++i) {
352 status = lcc_identifier_to_string (c, id, sizeof (id), ret_ident + i);
354 fprintf (stderr, "ERROR: listval: Failed to convert returned "
355 "identifier to a string: %s\n", lcc_strerror (c));
365 static int putval (lcc_connection_t *c, int argc, char **argv)
367 lcc_value_list_t vl = LCC_VALUE_LIST_INIT;
369 /* 64 ought to be enough for anybody ;-) */
371 int values_types[64];
372 size_t values_len = 0;
377 assert (strcasecmp (argv[0], "putval") == 0);
380 fprintf (stderr, "ERROR: putval: Missing identifier "
381 "and/or value list.\n");
386 vl.values_types = values_types;
388 status = parse_identifier (c, argv[1], &vl.identifier);
392 for (i = 2; i < argc; ++i) {
395 tmp = strchr (argv[i], (int)'=');
397 if (tmp != NULL) { /* option */
404 if (strcasecmp (key, "interval") == 0) {
407 vl.interval = strtol (value, &endptr, 0);
409 if (endptr == value) {
410 fprintf (stderr, "ERROR: Failed to parse interval as number: %s.\n",
414 else if ((endptr != NULL) && (*endptr != '\0')) {
415 fprintf (stderr, "WARNING: Ignoring trailing garbage after "
416 "interval: %s.\n", endptr);
420 fprintf (stderr, "ERROR: putval: Unknown option `%s'.\n", key);
424 else { /* value list */
427 tmp = strchr (argv[i], (int)':');
430 fprintf (stderr, "ERROR: putval: Invalid value list: %s.\n",
438 if (strcasecmp (argv[i], "N") == 0) {
444 vl.time = strtol (argv[i], &endptr, 0);
446 if (endptr == value) {
447 fprintf (stderr, "ERROR: Failed to parse time as number: %s.\n",
451 else if ((endptr != NULL) && (*endptr != '\0')) {
452 fprintf (stderr, "ERROR: Garbage after time: %s.\n", endptr);
462 tmp = strchr (argv[i], (int)':');
469 /* This is a bit of a hack, but parsing types.db just does not make
470 * much sense imho -- the server might have different types defined
471 * anyway. Also, lcc uses the type information for formatting the
472 * number only, so the real meaning does not matter. -tokkee */
473 dot = strchr (value, (int)'.');
475 if (strcasecmp (value, "U") == 0) {
476 values[values_len].gauge = NAN;
477 values_types[values_len] = LCC_TYPE_GAUGE;
479 else if (dot) { /* floating point value */
480 values[values_len].gauge = strtod (value, &endptr);
481 values_types[values_len] = LCC_TYPE_GAUGE;
484 values[values_len].counter = strtol (value, &endptr, 0);
485 values_types[values_len] = LCC_TYPE_COUNTER;
489 if (endptr == value) {
490 fprintf (stderr, "ERROR: Failed to parse value as number: %s.\n",
494 else if ((endptr != NULL) && (*endptr != '\0')) {
495 fprintf (stderr, "ERROR: Garbage after value: %s.\n", endptr);
502 assert (values_len >= 1);
503 vl.values_len = values_len;
505 status = lcc_putval (c, &vl);
507 fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
513 if (values_len == 0) {
514 fprintf (stderr, "ERROR: putval: Missing value list(s).\n");
520 int main (int argc, char **argv) {
521 char address[1024] = "unix:"DEFAULT_SOCK;
530 c = getopt (argc, argv, "s:h");
537 snprintf (address, sizeof (address), "unix:%s", optarg);
538 address[sizeof (address) - 1] = '\0';
541 exit_usage (argv[0], 0);
544 exit_usage (argv[0], 1);
548 if (optind >= argc) {
549 fprintf (stderr, "%s: missing command\n", argv[0]);
550 exit_usage (argv[0], 1);
554 status = lcc_connect (address, &c);
556 fprintf (stderr, "ERROR: Failed to connect to daemon at %s: %s.\n",
557 address, strerror (errno));
561 if (strcasecmp (argv[optind], "getval") == 0)
562 status = getval (c, argc - optind, argv + optind);
563 else if (strcasecmp (argv[optind], "flush") == 0)
564 status = flush (c, argc - optind, argv + optind);
565 else if (strcasecmp (argv[optind], "listval") == 0)
566 status = listval (c, argc - optind, argv + optind);
567 else if (strcasecmp (argv[optind], "putval") == 0)
568 status = putval (c, argc - optind, argv + optind);
570 fprintf (stderr, "%s: invalid command: %s\n", argv[0], argv[optind]);
581 /* vim: set sw=2 ts=2 tw=78 expandtab : */