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>
37 #if NAN_STATIC_DEFAULT
39 /* #endif NAN_STATIC_DEFAULT*/
42 # define DISABLE_ISOC99 1
43 # define __USE_ISOC99 1
44 # endif /* !defined(__USE_ISOC99) */
47 # undef DISABLE_ISOC99
49 # endif /* DISABLE_ISOC99 */
50 /* #endif NAN_STATIC_ISOC */
56 # define NAN (0.0 / 0.0)
58 # define isnan(f) ((f) != (f))
59 # endif /* !defined(isnan) */
61 # define isfinite(f) (((f) - (f)) == 0.0)
64 # define isinf(f) (!isfinite(f) && !isnan(f))
66 #endif /* NAN_ZERO_ZERO */
68 #include "libcollectdclient/collectd/client.h"
70 #define DEFAULT_SOCK LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
75 static void exit_usage (const char *name, int status) {
76 fprintf ((status == 0) ? stdout : stderr,
77 "Usage: %s [options] <command> [cmd options]\n\n"
79 "Available options:\n"
80 " -s Path to collectd's UNIX socket.\n"
81 " Default: "DEFAULT_SOCK"\n"
83 "\n -h Display this help and exit.\n"
85 "\nAvailable commands:\n\n"
87 " * getval <identifier>\n"
88 " * flush [timeout=<seconds>] [plugin=<name>] [identifier=<id>]\n"
90 " * putval <identifier> [interval=<seconds>] <value-list(s)>\n"
94 "An identifier has the following format:\n\n"
96 " [<hostname>/]<plugin>[-<plugin_instance>]/<type>[-<type_instance>]\n\n"
98 "Hostname defaults to the local hostname if omitted (e.g., uptime/uptime).\n"
99 "No error is returned if the specified identifier does not exist.\n"
101 "\n"PACKAGE_NAME" "PACKAGE_VERSION", http://collectd.org/\n"
102 "by Florian octo Forster <octo@collectd.org>\n"
103 "for contributions see `AUTHORS'\n"
108 /* Count the number of occurrences of the character 'chr'
109 * in the specified string. */
110 static int count_chars (const char *str, char chr) {
113 while (*str != '\0') {
123 static int array_grow (void **array, size_t *array_len, size_t elem_size)
127 assert ((array != NULL) && (array_len != NULL));
129 tmp = realloc (*array, (*array_len + 1) * elem_size);
131 fprintf (stderr, "ERROR: Failed to allocate memory.\n");
140 static int parse_identifier (lcc_connection_t *c,
141 const char *value, lcc_identifier_t *ident)
144 char ident_str[1024] = "";
149 n_slashes = count_chars (value, '/');
150 if (n_slashes == 1) {
151 /* The user has omitted the hostname part of the identifier
152 * (there is only one '/' in the identifier)
153 * Let's add the local hostname */
154 if (gethostname (hostname, sizeof (hostname)) != 0) {
155 fprintf (stderr, "ERROR: Failed to get local hostname: %s",
159 hostname[sizeof (hostname) - 1] = '\0';
161 snprintf (ident_str, sizeof (ident_str), "%s/%s", hostname, value);
162 ident_str[sizeof(ident_str) - 1] = '\0';
165 strncpy (ident_str, value, sizeof (ident_str));
166 ident_str[sizeof (ident_str) - 1] = '\0';
169 status = lcc_string_to_identifier (c, ident, ident_str);
171 fprintf (stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
172 ident_str, lcc_strerror(c));
176 } /* parse_identifier */
178 static int getval (lcc_connection_t *c, int argc, char **argv)
180 lcc_identifier_t ident;
182 size_t ret_values_num = 0;
183 gauge_t *ret_values = NULL;
184 char **ret_values_names = NULL;
189 assert (strcasecmp (argv[0], "getval") == 0);
192 fprintf (stderr, "ERROR: getval: Missing identifier.\n");
196 memset (&ident, 0, sizeof (ident));
197 status = parse_identifier (c, argv[1], &ident);
201 #define BAIL_OUT(s) \
203 if (ret_values != NULL) \
205 if (ret_values_names != NULL) { \
206 for (i = 0; i < ret_values_num; ++i) \
207 free (ret_values_names[i]); \
208 free (ret_values_names); \
210 ret_values_num = 0; \
214 status = lcc_getval (c, &ident,
215 &ret_values_num, &ret_values, &ret_values_names);
217 fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
221 for (i = 0; i < ret_values_num; ++i)
222 printf ("%s=%e\n", ret_values_names[i], ret_values[i]);
227 static int flush (lcc_connection_t *c, int argc, char **argv)
231 lcc_identifier_t *identifiers = NULL;
232 size_t identifiers_num = 0;
234 char **plugins = NULL;
235 size_t plugins_num = 0;
240 assert (strcasecmp (argv[0], "flush") == 0);
242 #define BAIL_OUT(s) \
244 if (identifiers != NULL) \
245 free (identifiers); \
246 identifiers_num = 0; \
247 if (plugins != NULL) \
253 for (i = 1; i < argc; ++i) {
257 value = strchr (argv[i], (int)'=');
260 fprintf (stderr, "ERROR: flush: Invalid option ``%s''.\n", argv[i]);
267 if (strcasecmp (key, "timeout") == 0) {
270 timeout = (int) strtol (value, &endptr, 0);
272 if (endptr == value) {
273 fprintf (stderr, "ERROR: Failed to parse timeout as number: %s.\n",
277 else if ((endptr != NULL) && (*endptr != '\0')) {
278 fprintf (stderr, "WARNING: Ignoring trailing garbage after timeout: "
282 else if (strcasecmp (key, "plugin") == 0) {
283 status = array_grow ((void *)&plugins, &plugins_num,
288 plugins[plugins_num - 1] = value;
290 else if (strcasecmp (key, "identifier") == 0) {
291 status = array_grow ((void *)&identifiers, &identifiers_num,
292 sizeof (*identifiers));
296 memset (identifiers + (identifiers_num - 1), 0, sizeof (*identifiers));
297 status = parse_identifier (c, value,
298 identifiers + (identifiers_num - 1));
303 fprintf (stderr, "ERROR: flush: Unknown option `%s'.\n", key);
308 if (plugins_num == 0) {
309 status = array_grow ((void *)&plugins, &plugins_num, sizeof (*plugins));
313 assert (plugins_num == 1);
317 for (i = 0; i < plugins_num; ++i) {
318 if (identifiers_num == 0) {
319 status = lcc_flush (c, plugins[i], NULL, timeout);
321 fprintf (stderr, "ERROR: Failed to flush plugin `%s': %s.\n",
322 (plugins[i] == NULL) ? "(all)" : plugins[i], lcc_strerror (c));
327 for (j = 0; j < identifiers_num; ++j) {
328 status = lcc_flush (c, plugins[i], identifiers + j, timeout);
332 lcc_identifier_to_string (c, id, sizeof (id), identifiers + j);
333 fprintf (stderr, "ERROR: Failed to flush plugin `%s', "
334 "identifier `%s': %s.\n",
335 (plugins[i] == NULL) ? "(all)" : plugins[i],
336 id, lcc_strerror (c));
346 static int listval (lcc_connection_t *c, int argc, char **argv)
348 lcc_identifier_t *ret_ident = NULL;
349 size_t ret_ident_num = 0;
354 assert (strcasecmp (argv[0], "listval") == 0);
357 fprintf (stderr, "ERROR: listval: Does not accept any arguments.\n");
361 #define BAIL_OUT(s) \
363 if (ret_ident != NULL) \
369 status = lcc_listval (c, &ret_ident, &ret_ident_num);
371 fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
375 for (i = 0; i < ret_ident_num; ++i) {
378 status = lcc_identifier_to_string (c, id, sizeof (id), ret_ident + i);
380 fprintf (stderr, "ERROR: listval: Failed to convert returned "
381 "identifier to a string: %s\n", lcc_strerror (c));
391 static int putval (lcc_connection_t *c, int argc, char **argv)
393 lcc_value_list_t vl = LCC_VALUE_LIST_INIT;
395 /* 64 ought to be enough for anybody ;-) */
397 int values_types[64];
398 size_t values_len = 0;
403 assert (strcasecmp (argv[0], "putval") == 0);
406 fprintf (stderr, "ERROR: putval: Missing identifier "
407 "and/or value list.\n");
412 vl.values_types = values_types;
414 status = parse_identifier (c, argv[1], &vl.identifier);
418 for (i = 2; i < argc; ++i) {
421 tmp = strchr (argv[i], (int)'=');
423 if (tmp != NULL) { /* option */
430 if (strcasecmp (key, "interval") == 0) {
433 vl.interval = strtol (value, &endptr, 0);
435 if (endptr == value) {
436 fprintf (stderr, "ERROR: Failed to parse interval as number: %s.\n",
440 else if ((endptr != NULL) && (*endptr != '\0')) {
441 fprintf (stderr, "WARNING: Ignoring trailing garbage after "
442 "interval: %s.\n", endptr);
446 fprintf (stderr, "ERROR: putval: Unknown option `%s'.\n", key);
450 else { /* value list */
453 tmp = strchr (argv[i], (int)':');
456 fprintf (stderr, "ERROR: putval: Invalid value list: %s.\n",
464 if (strcasecmp (argv[i], "N") == 0) {
470 vl.time = strtol (argv[i], &endptr, 0);
472 if (endptr == argv[i]) {
473 fprintf (stderr, "ERROR: Failed to parse time as number: %s.\n",
477 else if ((endptr != NULL) && (*endptr != '\0')) {
478 fprintf (stderr, "ERROR: Garbage after time: %s.\n", endptr);
488 tmp = strchr (value, (int)':');
495 /* This is a bit of a hack, but parsing types.db just does not make
496 * much sense imho -- the server might have different types defined
497 * anyway. Also, lcc uses the type information for formatting the
498 * number only, so the real meaning does not matter. -tokkee */
499 dot = strchr (value, (int)'.');
501 if (strcasecmp (value, "U") == 0) {
502 values[values_len].gauge = NAN;
503 values_types[values_len] = LCC_TYPE_GAUGE;
505 else if (dot) { /* floating point value */
506 values[values_len].gauge = strtod (value, &endptr);
507 values_types[values_len] = LCC_TYPE_GAUGE;
510 values[values_len].counter = (counter_t) strtoull (value, &endptr, 0);
511 values_types[values_len] = LCC_TYPE_COUNTER;
515 if (endptr == value) {
516 fprintf (stderr, "ERROR: Failed to parse value as number: %s.\n",
520 else if ((endptr != NULL) && (*endptr != '\0')) {
521 fprintf (stderr, "ERROR: Garbage after value: %s.\n", endptr);
528 assert (values_len >= 1);
529 vl.values_len = values_len;
531 status = lcc_putval (c, &vl);
533 fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
539 if (values_len == 0) {
540 fprintf (stderr, "ERROR: putval: Missing value list(s).\n");
546 int main (int argc, char **argv) {
547 char address[1024] = "unix:"DEFAULT_SOCK;
556 opt = getopt (argc, argv, "s:h");
563 snprintf (address, sizeof (address), "unix:%s", optarg);
564 address[sizeof (address) - 1] = '\0';
567 exit_usage (argv[0], 0);
570 exit_usage (argv[0], 1);
574 if (optind >= argc) {
575 fprintf (stderr, "%s: missing command\n", argv[0]);
576 exit_usage (argv[0], 1);
580 status = lcc_connect (address, &c);
582 fprintf (stderr, "ERROR: Failed to connect to daemon at %s: %s.\n",
583 address, strerror (errno));
587 if (strcasecmp (argv[optind], "getval") == 0)
588 status = getval (c, argc - optind, argv + optind);
589 else if (strcasecmp (argv[optind], "flush") == 0)
590 status = flush (c, argc - optind, argv + optind);
591 else if (strcasecmp (argv[optind], "listval") == 0)
592 status = listval (c, argc - optind, argv + optind);
593 else if (strcasecmp (argv[optind], "putval") == 0)
594 status = putval (c, argc - optind, argv + optind);
596 fprintf (stderr, "%s: invalid command: %s\n", argv[0], argv[optind]);
607 /* vim: set sw=2 ts=2 tw=78 expandtab : */