Code

Renamed ‘collectd-flush’ to ‘collectdctl’ (collectd control interface).
[collectd.git] / src / collectdctl.c
1 /**
2  * collectd - src/collectdctl.c
3  * Copyright (C) 2010 Håkon J Dugstad Johnsen
4  * Copyright (C) 2010 Sebastian Harl
5  *
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.
9  *
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.
14  *
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
18  *
19  * Authors:
20  *   Håkon J Dugstad Johnsen <hakon-dugstad.johnsen at telenor.com>
21  *   Sebastian "tokkee" Harl <sh@tokkee.org>
22  **/
24 #if HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include "libcollectdclient/client.h"
30 #include <assert.h>
32 #include <errno.h>
34 #include <getopt.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
40 #include <unistd.h>
42 #define DEFAULT_SOCK LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
44 extern char *optarg;
45 extern int   optind;
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       "\nIdentifiers:\n\n"
64       "An identifier has the following format:\n\n"
66       "  [<hostname>/]<plugin>[-<plugin_instance>]/<type>[-<type_instance>]\n\n"
68       "Hostname defaults to the local hostname if omitted (e.g., uptime/uptime).\n"
69       "No error is returned if the specified identifier does not exist.\n"
71       "\nExample:\n\n"
73       "  collectdctl flush plugin=rrdtool identifie=somehost/cpu-0/cpu-wait\n\n"
75       "Flushes all CPU wait RRD values of the first CPU of the local host.\n"
76       "I.e., writes all pending RRD updates of that data-source to disk.\n"
78       "\n"PACKAGE" "VERSION", http://collectd.org/\n"
79       "by Florian octo Forster <octo@verplant.org>\n"
80       "for contributions see `AUTHORS'\n"
81       , name);
82   exit (status);
83 }
85 /* Count the number of occurrences of the character 'chr'
86  * in the specified string. */
87 static int count_chars (const char *str, char chr) {
88   int count = 0;
90   while (*str != '\0') {
91     if (*str == chr) {
92       count++;
93     }
94     str++;
95   }
97   return count;
98 } /* count_chars */
100 static int parse_identifier (lcc_connection_t *c,
101     const char *value, lcc_identifier_t *ident)
103   char hostname[1024];
104   char ident_str[1024] = "";
105   int  n_slashes;
107   int status;
109   n_slashes = count_chars (value, '/');
110   if (n_slashes == 1) {
111     /* The user has omitted the hostname part of the identifier
112      * (there is only one '/' in the identifier)
113      * Let's add the local hostname */
114     if (gethostname (hostname, sizeof (hostname)) != 0) {
115       fprintf (stderr, "ERROR: Failed to get local hostname: %s",
116           strerror (errno));
117       return (-1);
118     }
119     hostname[sizeof (hostname) - 1] = '\0';
121     snprintf (ident_str, sizeof (ident_str), "%s/%s", hostname, value);
122     ident_str[sizeof(ident_str) - 1] = '\0';
123   }
124   else {
125     strncpy (ident_str, value, sizeof (ident_str));
126     ident_str[sizeof (ident_str) - 1] = '\0';
127   }
129   status = lcc_string_to_identifier (c, ident, ident_str);
130   if (status != 0) {
131     fprintf (stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
132         ident_str, lcc_strerror(c));
133     return (-1);
134   }
135   return (0);
136 } /* parse_identifier */
138 static int getval (lcc_connection_t *c, int argc, char **argv)
140   lcc_identifier_t ident;
142   size_t   ret_values_num   = 0;
143   gauge_t *ret_values       = NULL;
144   char   **ret_values_names = NULL;
146   int status;
147   size_t i;
149   assert (strcasecmp (argv[0], "getval") == 0);
151   if (argc != 2) {
152     fprintf (stderr, "ERROR: getval: Missing identifier.\n");
153     return (-1);
154   }
156   memset (&ident, 0, sizeof (ident));
157   status = parse_identifier (c, argv[1], &ident);
158   if (status != 0)
159     return (status);
161 #define BAIL_OUT(s) \
162   do { \
163     if (ret_values != NULL) \
164       free (ret_values); \
165     if (ret_values_names != NULL) { \
166       for (i = 0; i < ret_values_num; ++i) \
167         free (ret_values_names[i]); \
168       free (ret_values_names); \
169     } \
170     ret_values_num = 0; \
171     return (s); \
172   } while (0)
174   status = lcc_getval (c, &ident,
175       &ret_values_num, &ret_values, &ret_values_names);
176   if (status != 0)
177     BAIL_OUT (-1);
179   for (i = 0; i < ret_values_num; ++i)
180     printf ("%s=%e\n", ret_values_names[i], ret_values[i]);
181   BAIL_OUT (0);
182 } /* getval */
184 static int flush (lcc_connection_t *c, int argc, char **argv)
186   lcc_identifier_t  ident;
187   lcc_identifier_t *identp = NULL;
189   char *plugin  = NULL;
190   int   timeout = -1;
192   int status;
193   int i;
195   assert (strcasecmp (argv[0], "flush") == 0);
197   for (i = 1; i < argc; ++i) {
198     char *key, *value;
200     key   = argv[i];
201     value = strchr (argv[i], (int)'=');
203     if (! value) {
204       fprintf (stderr, "ERROR: flush: Invalid option ``%s''.\n", argv[i]);
205       return (-1);
206     }
208     *value = '\0';
209     ++value;
211     if (strcasecmp (key, "timeout") == 0) {
212       char *endptr = NULL;
214       timeout = strtol (value, &endptr, 0);
216       if (endptr == value) {
217         fprintf (stderr, "ERROR: Failed to parse timeout as number: %s.\n",
218             value);
219         return (-1);
220       }
221       else if ((endptr != NULL) && (*endptr != '\0')) {
222         fprintf (stderr, "WARNING: Ignoring trailing garbage after timeout: "
223             "%s.\n", endptr);
224       }
225     }
226     else if (strcasecmp (key, "plugin") == 0) {
227       plugin = value;
228     }
229     else if (strcasecmp (key, "identifier") == 0) {
230       int status;
232       memset (&ident, 0, sizeof (ident));
233       status = parse_identifier (c, value, &ident);
234       if (status != 0)
235         return (status);
236       identp = &ident;
237     }
238   }
240   status = lcc_flush (c, plugin, identp, timeout);
241   if (status != 0) {
242     fprintf (stderr, "ERROR: Flushing failed: %s.\n",
243         lcc_strerror (c));
244     return (-1);
245   }
247   return 0;
248 } /* flush */
250 int main (int argc, char **argv) {
251   char address[1024] = "unix:"DEFAULT_SOCK;
253   lcc_connection_t *c;
255   int status;
257   while (42) {
258     int c;
260     c = getopt (argc, argv, "s:h");
262     if (c == -1)
263       break;
265     switch (c) {
266       case 's':
267         snprintf (address, sizeof (address), "unix:%s", optarg);
268         address[sizeof (address) - 1] = '\0';
269         break;
270       case 'h':
271         exit_usage (argv[0], 0);
272         break;
273       default:
274         exit_usage (argv[0], 1);
275     }
276   }
278   if (optind >= argc) {
279     fprintf (stderr, "%s: missing command\n", argv[0]);
280     exit_usage (argv[0], 1);
281   }
283   c = NULL;
284   status = lcc_connect (address, &c);
285   if (status != 0) {
286     fprintf (stderr, "ERROR: Failed to connect to daemon at %s: %s.\n",
287         address, strerror (errno));
288     return (1);
289   }
291   if (strcasecmp (argv[optind], "getval") == 0)
292     status = getval (c, argc - optind, argv + optind);
293   else if (strcasecmp (argv[optind], "flush") == 0)
294     status = flush (c, argc - optind, argv + optind);
295   else {
296     fprintf (stderr, "%s: invalid command: %s\n", argv[0], argv[optind]);
297     return (1);
298   }
300   LCC_DESTROY (c);
302   if (status != 0)
303     return (status);
304   return (0);
305 } /* main */
307 /* vim: set sw=2 ts=2 tw=78 expandtab : */