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>
38 #if NAN_STATIC_DEFAULT
40 /* #endif NAN_STATIC_DEFAULT*/
43 #define DISABLE_ISOC99 1
44 #define __USE_ISOC99 1
45 #endif /* !defined(__USE_ISOC99) */
50 #endif /* DISABLE_ISOC99 */
51 /* #endif NAN_STATIC_ISOC */
57 #define NAN (0.0 / 0.0)
59 #define isnan(f) ((f) != (f))
60 #endif /* !defined(isnan) */
62 #define isfinite(f) (((f) - (f)) == 0.0)
65 #define isinf(f) (!isfinite(f) && !isnan(f))
67 #endif /* NAN_ZERO_ZERO */
69 #include "collectd/client.h"
72 #define PREFIX "/opt/" PACKAGE_NAME
76 #define LOCALSTATEDIR PREFIX "/var"
79 #define DEFAULT_SOCK LOCALSTATEDIR "/run/" PACKAGE_NAME "-unixsock"
84 /* ssnprintf returns zero on success, one if truncation occurred
85 and a negative integer onerror. */
86 static int _ssnprintf(char *str, size_t sz, const char *format, ...) {
90 int ret = vsnprintf(str, sz, format, ap);
97 return (size_t)ret >= sz;
98 } /* int _ssnprintf */
100 __attribute__((noreturn)) static void exit_usage(const char *name, int status) {
102 (status == 0) ? stdout : stderr,
103 "Usage: %s [options] <command> [cmd options]\n\n"
105 "Available options:\n"
106 " -s Path to collectd's UNIX socket.\n"
107 " Default: " DEFAULT_SOCK "\n"
109 "\n -h Display this help and exit.\n"
111 "\nAvailable commands:\n\n"
113 " * getval <identifier>\n"
114 " * flush [timeout=<seconds>] [plugin=<name>] [identifier=<id>]\n"
116 " * putval <identifier> [interval=<seconds>] <value-list(s)>\n"
120 "An identifier has the following format:\n\n"
122 " [<hostname>/]<plugin>[-<plugin_instance>]/<type>[-<type_instance>]\n\n"
124 "Hostname defaults to the local hostname if omitted (e.g., "
126 "No error is returned if the specified identifier does not exist.\n"
128 "\n" PACKAGE_NAME " " PACKAGE_VERSION ", http://collectd.org/\n"
129 "by Florian octo Forster <octo@collectd.org>\n"
130 "for contributions see `AUTHORS'\n",
135 /* Count the number of occurrences of the character 'chr'
136 * in the specified string. */
137 static int count_chars(const char *str, char chr) {
140 while (*str != '\0') {
150 static int array_grow(void **array, size_t *array_len, size_t elem_size) {
153 assert((array != NULL) && (array_len != NULL));
155 tmp = realloc(*array, (*array_len + 1) * elem_size);
157 fprintf(stderr, "ERROR: Failed to allocate memory.\n");
166 static int parse_identifier(lcc_connection_t *c, const char *value,
167 lcc_identifier_t *ident) {
169 char ident_str[1024] = "";
174 n_slashes = count_chars(value, '/');
175 if (n_slashes == 1) {
176 /* The user has omitted the hostname part of the identifier
177 * (there is only one '/' in the identifier)
178 * Let's add the local hostname */
179 if (gethostname(hostname, sizeof(hostname)) != 0) {
180 fprintf(stderr, "ERROR: Failed to get local hostname: %s",
184 hostname[sizeof(hostname) - 1] = '\0';
186 _ssnprintf(ident_str, sizeof(ident_str), "%s/%s", hostname, value);
187 ident_str[sizeof(ident_str) - 1] = '\0';
189 strncpy(ident_str, value, sizeof(ident_str));
190 ident_str[sizeof(ident_str) - 1] = '\0';
193 status = lcc_string_to_identifier(c, ident, ident_str);
195 fprintf(stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
196 ident_str, lcc_strerror(c));
200 } /* parse_identifier */
202 static int getval(lcc_connection_t *c, int argc, char **argv) {
203 lcc_identifier_t ident;
205 size_t ret_values_num = 0;
206 gauge_t *ret_values = NULL;
207 char **ret_values_names = NULL;
211 assert(strcasecmp(argv[0], "getval") == 0);
214 fprintf(stderr, "ERROR: getval: Missing identifier.\n");
218 status = parse_identifier(c, argv[1], &ident);
222 #define BAIL_OUT(s) \
224 if (ret_values != NULL) \
226 if (ret_values_names != NULL) { \
227 for (size_t i = 0; i < ret_values_num; ++i) \
228 free(ret_values_names[i]); \
229 free(ret_values_names); \
231 ret_values_num = 0; \
236 lcc_getval(c, &ident, &ret_values_num, &ret_values, &ret_values_names);
238 fprintf(stderr, "ERROR: %s\n", lcc_strerror(c));
242 for (size_t i = 0; i < ret_values_num; ++i)
243 printf("%s=%e\n", ret_values_names[i], ret_values[i]);
248 static int flush(lcc_connection_t *c, int argc, char **argv) {
251 lcc_identifier_t *identifiers = NULL;
252 size_t identifiers_num = 0;
254 char **plugins = NULL;
255 size_t plugins_num = 0;
259 assert(strcasecmp(argv[0], "flush") == 0);
261 #define BAIL_OUT(s) \
263 if (identifiers != NULL) \
265 identifiers_num = 0; \
266 if (plugins != NULL) \
272 for (int i = 1; i < argc; ++i) {
276 value = strchr(argv[i], (int)'=');
279 fprintf(stderr, "ERROR: flush: Invalid option ``%s''.\n", argv[i]);
286 if (strcasecmp(key, "timeout") == 0) {
289 timeout = (int)strtol(value, &endptr, 0);
291 if (endptr == value) {
292 fprintf(stderr, "ERROR: Failed to parse timeout as number: %s.\n",
295 } else if ((endptr != NULL) && (*endptr != '\0')) {
296 fprintf(stderr, "WARNING: Ignoring trailing garbage after timeout: "
300 } else if (strcasecmp(key, "plugin") == 0) {
301 status = array_grow((void *)&plugins, &plugins_num, sizeof(*plugins));
305 plugins[plugins_num - 1] = value;
306 } else if (strcasecmp(key, "identifier") == 0) {
307 status = array_grow((void *)&identifiers, &identifiers_num,
308 sizeof(*identifiers));
312 memset(identifiers + (identifiers_num - 1), 0, sizeof(*identifiers));
313 status = parse_identifier(c, value, identifiers + (identifiers_num - 1));
317 fprintf(stderr, "ERROR: flush: Unknown option `%s'.\n", key);
322 if (plugins_num == 0) {
323 status = array_grow((void *)&plugins, &plugins_num, sizeof(*plugins));
327 assert(plugins_num == 1);
331 for (size_t i = 0; i < plugins_num; ++i) {
332 if (identifiers_num == 0) {
333 status = lcc_flush(c, plugins[i], NULL, timeout);
335 fprintf(stderr, "ERROR: Failed to flush plugin `%s': %s.\n",
336 (plugins[i] == NULL) ? "(all)" : plugins[i], lcc_strerror(c));
338 for (size_t j = 0; j < identifiers_num; ++j) {
339 status = lcc_flush(c, plugins[i], identifiers + j, timeout);
343 lcc_identifier_to_string(c, id, sizeof(id), identifiers + j);
344 fprintf(stderr, "ERROR: Failed to flush plugin `%s', "
345 "identifier `%s': %s.\n",
346 (plugins[i] == NULL) ? "(all)" : plugins[i], id,
357 static int listval(lcc_connection_t *c, int argc, char **argv) {
358 lcc_identifier_t *ret_ident = NULL;
359 size_t ret_ident_num = 0;
363 assert(strcasecmp(argv[0], "listval") == 0);
366 fprintf(stderr, "ERROR: listval: Does not accept any arguments.\n");
370 #define BAIL_OUT(s) \
372 if (ret_ident != NULL) \
378 status = lcc_listval(c, &ret_ident, &ret_ident_num);
380 fprintf(stderr, "ERROR: %s\n", lcc_strerror(c));
384 for (size_t i = 0; i < ret_ident_num; ++i) {
387 status = lcc_identifier_to_string(c, id, sizeof(id), ret_ident + i);
389 fprintf(stderr, "ERROR: listval: Failed to convert returned "
390 "identifier to a string: %s\n",
401 static int putval(lcc_connection_t *c, int argc, char **argv) {
402 lcc_value_list_t vl = LCC_VALUE_LIST_INIT;
404 /* 64 ought to be enough for anybody ;-) */
406 int values_types[64];
407 size_t values_len = 0;
411 assert(strcasecmp(argv[0], "putval") == 0);
414 fprintf(stderr, "ERROR: putval: Missing identifier "
415 "and/or value list.\n");
420 vl.values_types = values_types;
422 status = parse_identifier(c, argv[1], &vl.identifier);
426 for (int i = 2; i < argc; ++i) {
429 tmp = strchr(argv[i], (int)'=');
431 if (tmp != NULL) { /* option */
438 if (strcasecmp(key, "interval") == 0) {
441 vl.interval = strtol(value, &endptr, 0);
443 if (endptr == value) {
444 fprintf(stderr, "ERROR: Failed to parse interval as number: %s.\n",
447 } else if ((endptr != NULL) && (*endptr != '\0')) {
448 fprintf(stderr, "WARNING: Ignoring trailing garbage after "
453 fprintf(stderr, "ERROR: putval: Unknown option `%s'.\n", key);
456 } else { /* value list */
459 tmp = strchr(argv[i], (int)':');
462 fprintf(stderr, "ERROR: putval: Invalid value list: %s.\n", argv[i]);
469 if (strcasecmp(argv[i], "N") == 0) {
474 vl.time = strtol(argv[i], &endptr, 0);
476 if (endptr == argv[i]) {
477 fprintf(stderr, "ERROR: Failed to parse time as number: %s.\n",
480 } else if ((endptr != NULL) && (*endptr != '\0')) {
481 fprintf(stderr, "ERROR: Garbage after time: %s.\n", endptr);
488 while (value != NULL) {
491 tmp = strchr(value, (int)':');
498 /* This is a bit of a hack, but parsing types.db just does not make
499 * much sense imho -- the server might have different types defined
500 * anyway. Also, lcc uses the type information for formatting the
501 * number only, so the real meaning does not matter. -tokkee */
502 dot = strchr(value, (int)'.');
504 if (strcasecmp(value, "U") == 0) {
505 values[values_len].gauge = NAN;
506 values_types[values_len] = LCC_TYPE_GAUGE;
507 } else if (dot) { /* floating point value */
508 values[values_len].gauge = strtod(value, &endptr);
509 values_types[values_len] = LCC_TYPE_GAUGE;
510 } else { /* integer */
511 values[values_len].counter = (counter_t)strtoull(value, &endptr, 0);
512 values_types[values_len] = LCC_TYPE_COUNTER;
516 if (endptr == value) {
517 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 _ssnprintf(address, sizeof(address), "unix:%s", optarg);
564 address[sizeof(address) - 1] = '\0';
567 exit_usage(argv[0], 0);
569 exit_usage(argv[0], 1);
573 if (optind >= argc) {
574 fprintf(stderr, "%s: missing command\n", argv[0]);
575 exit_usage(argv[0], 1);
579 status = lcc_connect(address, &c);
581 fprintf(stderr, "ERROR: Failed to connect to daemon at %s: %s.\n", address,
586 if (strcasecmp(argv[optind], "getval") == 0)
587 status = getval(c, argc - optind, argv + optind);
588 else if (strcasecmp(argv[optind], "flush") == 0)
589 status = flush(c, argc - optind, argv + optind);
590 else if (strcasecmp(argv[optind], "listval") == 0)
591 status = listval(c, argc - optind, argv + optind);
592 else if (strcasecmp(argv[optind], "putval") == 0)
593 status = putval(c, argc - optind, argv + optind);
595 fprintf(stderr, "%s: invalid command: %s\n", argv[0], argv[optind]);