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"
65 "An identifier has the following format:\n\n"
67 " [<hostname>/]<plugin>[-<plugin_instance>]/<type>[-<type_instance>]\n\n"
69 "Hostname defaults to the local hostname if omitted (e.g., uptime/uptime).\n"
70 "No error is returned if the specified identifier does not exist.\n"
74 " collectdctl flush plugin=rrdtool identifie=somehost/cpu-0/cpu-wait\n\n"
76 "Flushes all CPU wait RRD values of the first CPU of the local host.\n"
77 "I.e., writes all pending RRD updates of that data-source to disk.\n"
79 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
80 "by Florian octo Forster <octo@verplant.org>\n"
81 "for contributions see `AUTHORS'\n"
86 /* Count the number of occurrences of the character 'chr'
87 * in the specified string. */
88 static int count_chars (const char *str, char chr) {
91 while (*str != '\0') {
101 static int array_grow (void **array, int *array_len, size_t elem_size)
105 assert ((array != NULL) && (array_len != NULL));
107 tmp = realloc (*array, (*array_len + 1) * elem_size);
109 fprintf (stderr, "ERROR: Failed to allocate memory.\n");
118 static int parse_identifier (lcc_connection_t *c,
119 const char *value, lcc_identifier_t *ident)
122 char ident_str[1024] = "";
127 n_slashes = count_chars (value, '/');
128 if (n_slashes == 1) {
129 /* The user has omitted the hostname part of the identifier
130 * (there is only one '/' in the identifier)
131 * Let's add the local hostname */
132 if (gethostname (hostname, sizeof (hostname)) != 0) {
133 fprintf (stderr, "ERROR: Failed to get local hostname: %s",
137 hostname[sizeof (hostname) - 1] = '\0';
139 snprintf (ident_str, sizeof (ident_str), "%s/%s", hostname, value);
140 ident_str[sizeof(ident_str) - 1] = '\0';
143 strncpy (ident_str, value, sizeof (ident_str));
144 ident_str[sizeof (ident_str) - 1] = '\0';
147 status = lcc_string_to_identifier (c, ident, ident_str);
149 fprintf (stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
150 ident_str, lcc_strerror(c));
154 } /* parse_identifier */
156 static int getval (lcc_connection_t *c, int argc, char **argv)
158 lcc_identifier_t ident;
160 size_t ret_values_num = 0;
161 gauge_t *ret_values = NULL;
162 char **ret_values_names = NULL;
167 assert (strcasecmp (argv[0], "getval") == 0);
170 fprintf (stderr, "ERROR: getval: Missing identifier.\n");
174 memset (&ident, 0, sizeof (ident));
175 status = parse_identifier (c, argv[1], &ident);
179 #define BAIL_OUT(s) \
181 if (ret_values != NULL) \
183 if (ret_values_names != NULL) { \
184 for (i = 0; i < ret_values_num; ++i) \
185 free (ret_values_names[i]); \
186 free (ret_values_names); \
188 ret_values_num = 0; \
192 status = lcc_getval (c, &ident,
193 &ret_values_num, &ret_values, &ret_values_names);
195 fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
199 for (i = 0; i < ret_values_num; ++i)
200 printf ("%s=%e\n", ret_values_names[i], ret_values[i]);
205 static int flush (lcc_connection_t *c, int argc, char **argv)
209 lcc_identifier_t *identifiers = NULL;
210 int identifiers_num = 0;
212 char **plugins = NULL;
218 assert (strcasecmp (argv[0], "flush") == 0);
220 #define BAIL_OUT(s) \
222 if (identifiers != NULL) \
223 free (identifiers); \
224 identifiers_num = 0; \
225 if (plugins != NULL) \
231 for (i = 1; i < argc; ++i) {
235 value = strchr (argv[i], (int)'=');
238 fprintf (stderr, "ERROR: flush: Invalid option ``%s''.\n", argv[i]);
245 if (strcasecmp (key, "timeout") == 0) {
248 timeout = strtol (value, &endptr, 0);
250 if (endptr == value) {
251 fprintf (stderr, "ERROR: Failed to parse timeout as number: %s.\n",
255 else if ((endptr != NULL) && (*endptr != '\0')) {
256 fprintf (stderr, "WARNING: Ignoring trailing garbage after timeout: "
260 else if (strcasecmp (key, "plugin") == 0) {
261 status = array_grow ((void **)&plugins, &plugins_num,
266 plugins[plugins_num - 1] = value;
268 else if (strcasecmp (key, "identifier") == 0) {
269 status = array_grow ((void **)&identifiers, &identifiers_num,
270 sizeof (*identifiers));
274 memset (identifiers + (identifiers_num - 1), 0, sizeof (*identifiers));
275 status = parse_identifier (c, value,
276 identifiers + (identifiers_num - 1));
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 int main (int argc, char **argv) {
366 char address[1024] = "unix:"DEFAULT_SOCK;
375 c = getopt (argc, argv, "s:h");
382 snprintf (address, sizeof (address), "unix:%s", optarg);
383 address[sizeof (address) - 1] = '\0';
386 exit_usage (argv[0], 0);
389 exit_usage (argv[0], 1);
393 if (optind >= argc) {
394 fprintf (stderr, "%s: missing command\n", argv[0]);
395 exit_usage (argv[0], 1);
399 status = lcc_connect (address, &c);
401 fprintf (stderr, "ERROR: Failed to connect to daemon at %s: %s.\n",
402 address, strerror (errno));
406 if (strcasecmp (argv[optind], "getval") == 0)
407 status = getval (c, argc - optind, argv + optind);
408 else if (strcasecmp (argv[optind], "flush") == 0)
409 status = flush (c, argc - optind, argv + optind);
410 else if (strcasecmp (argv[optind], "listval") == 0)
411 status = listval (c, argc - optind, argv + optind);
413 fprintf (stderr, "%s: invalid command: %s\n", argv[0], argv[optind]);
424 /* vim: set sw=2 ts=2 tw=78 expandtab : */