collectdctl: ‘flush’ now supports multiple plugins/identifiers.
[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  **/
23
24 #if HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "libcollectdclient/client.h"
29
30 #include <assert.h>
31
32 #include <errno.h>
33
34 #include <getopt.h>
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include <unistd.h>
41
42 #define DEFAULT_SOCK LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
43
44 extern char *optarg;
45 extern int   optind;
46
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"
50
51       "Available options:\n"
52       "  -s       Path to collectd's UNIX socket.\n"
53       "           Default: "DEFAULT_SOCK"\n"
54
55       "\n  -h       Display this help and exit.\n"
56
57       "\nAvailable commands:\n\n"
58
59       " * getval <identifier>\n"
60       " * flush [timeout=<seconds>] [plugin=<name>] [identifier=<id>]\n"
61       " * listval\n"
62
63       "\nIdentifiers:\n\n"
64
65       "An identifier has the following format:\n\n"
66
67       "  [<hostname>/]<plugin>[-<plugin_instance>]/<type>[-<type_instance>]\n\n"
68
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"
71
72       "\nExample:\n\n"
73
74       "  collectdctl flush plugin=rrdtool identifie=somehost/cpu-0/cpu-wait\n\n"
75
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"
78
79       "\n"PACKAGE" "VERSION", http://collectd.org/\n"
80       "by Florian octo Forster <octo@verplant.org>\n"
81       "for contributions see `AUTHORS'\n"
82       , name);
83   exit (status);
84 }
85
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) {
89   int count = 0;
90
91   while (*str != '\0') {
92     if (*str == chr) {
93       count++;
94     }
95     str++;
96   }
97
98   return count;
99 } /* count_chars */
100
101 static int array_grow (void **array, int *array_len, size_t elem_size)
102 {
103   void *tmp;
104
105   assert ((array != NULL) && (array_len != NULL));
106
107   tmp = realloc (*array, (*array_len + 1) * elem_size);
108   if (tmp == NULL) {
109     fprintf (stderr, "ERROR: Failed to allocate memory.\n");
110     return (-1);
111   }
112
113   *array = tmp;
114   ++(*array_len);
115   return (0);
116 } /* array_grow */
117
118 static int parse_identifier (lcc_connection_t *c,
119     const char *value, lcc_identifier_t *ident)
120 {
121   char hostname[1024];
122   char ident_str[1024] = "";
123   int  n_slashes;
124
125   int status;
126
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",
134           strerror (errno));
135       return (-1);
136     }
137     hostname[sizeof (hostname) - 1] = '\0';
138
139     snprintf (ident_str, sizeof (ident_str), "%s/%s", hostname, value);
140     ident_str[sizeof(ident_str) - 1] = '\0';
141   }
142   else {
143     strncpy (ident_str, value, sizeof (ident_str));
144     ident_str[sizeof (ident_str) - 1] = '\0';
145   }
146
147   status = lcc_string_to_identifier (c, ident, ident_str);
148   if (status != 0) {
149     fprintf (stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
150         ident_str, lcc_strerror(c));
151     return (-1);
152   }
153   return (0);
154 } /* parse_identifier */
155
156 static int getval (lcc_connection_t *c, int argc, char **argv)
157 {
158   lcc_identifier_t ident;
159
160   size_t   ret_values_num   = 0;
161   gauge_t *ret_values       = NULL;
162   char   **ret_values_names = NULL;
163
164   int status;
165   size_t i;
166
167   assert (strcasecmp (argv[0], "getval") == 0);
168
169   if (argc != 2) {
170     fprintf (stderr, "ERROR: getval: Missing identifier.\n");
171     return (-1);
172   }
173
174   memset (&ident, 0, sizeof (ident));
175   status = parse_identifier (c, argv[1], &ident);
176   if (status != 0)
177     return (status);
178
179 #define BAIL_OUT(s) \
180   do { \
181     if (ret_values != NULL) \
182       free (ret_values); \
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); \
187     } \
188     ret_values_num = 0; \
189     return (s); \
190   } while (0)
191
192   status = lcc_getval (c, &ident,
193       &ret_values_num, &ret_values, &ret_values_names);
194   if (status != 0) {
195     fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
196     BAIL_OUT (-1);
197   }
198
199   for (i = 0; i < ret_values_num; ++i)
200     printf ("%s=%e\n", ret_values_names[i], ret_values[i]);
201   BAIL_OUT (0);
202 #undef BAIL_OUT
203 } /* getval */
204
205 static int flush (lcc_connection_t *c, int argc, char **argv)
206 {
207   int timeout = -1;
208
209   lcc_identifier_t *identifiers = NULL;
210   int identifiers_num = 0;
211
212   char **plugins = NULL;
213   int plugins_num = 0;
214
215   int status;
216   int i;
217
218   assert (strcasecmp (argv[0], "flush") == 0);
219
220 #define BAIL_OUT(s) \
221   do { \
222     if (identifiers != NULL) \
223       free (identifiers); \
224     identifiers_num = 0; \
225     if (plugins != NULL) \
226       free (plugins); \
227     plugins_num = 0; \
228     return (s); \
229   } while (0)
230
231   for (i = 1; i < argc; ++i) {
232     char *key, *value;
233
234     key   = argv[i];
235     value = strchr (argv[i], (int)'=');
236
237     if (! value) {
238       fprintf (stderr, "ERROR: flush: Invalid option ``%s''.\n", argv[i]);
239       BAIL_OUT (-1);
240     }
241
242     *value = '\0';
243     ++value;
244
245     if (strcasecmp (key, "timeout") == 0) {
246       char *endptr = NULL;
247
248       timeout = strtol (value, &endptr, 0);
249
250       if (endptr == value) {
251         fprintf (stderr, "ERROR: Failed to parse timeout as number: %s.\n",
252             value);
253         BAIL_OUT (-1);
254       }
255       else if ((endptr != NULL) && (*endptr != '\0')) {
256         fprintf (stderr, "WARNING: Ignoring trailing garbage after timeout: "
257             "%s.\n", endptr);
258       }
259     }
260     else if (strcasecmp (key, "plugin") == 0) {
261       status = array_grow ((void **)&plugins, &plugins_num,
262           sizeof (*plugins));
263       if (status != 0)
264         BAIL_OUT (status);
265
266       plugins[plugins_num - 1] = value;
267     }
268     else if (strcasecmp (key, "identifier") == 0) {
269       status = array_grow ((void **)&identifiers, &identifiers_num,
270           sizeof (*identifiers));
271       if (status != 0)
272         BAIL_OUT (status);
273
274       memset (identifiers + (identifiers_num - 1), 0, sizeof (*identifiers));
275       status = parse_identifier (c, value,
276           identifiers + (identifiers_num - 1));
277       if (status != 0)
278         BAIL_OUT (status);
279     }
280   }
281
282   if (plugins_num == 0) {
283     status = array_grow ((void **)&plugins, &plugins_num, sizeof (*plugins));
284     if (status != 0)
285       BAIL_OUT (status);
286
287     assert (plugins_num == 1);
288     plugins[0] = NULL;
289   }
290
291   for (i = 0; i < plugins_num; ++i) {
292     if (identifiers_num == 0) {
293       status = lcc_flush (c, plugins[i], NULL, timeout);
294       if (status != 0)
295         fprintf (stderr, "ERROR: Failed to flush plugin `%s': %s.\n",
296             (plugins[i] == NULL) ? "(all)" : plugins[i], lcc_strerror (c));
297     }
298     else {
299       int j;
300
301       for (j = 0; j < identifiers_num; ++j) {
302         status = lcc_flush (c, plugins[i], identifiers + j, timeout);
303         if (status != 0) {
304           char id[1024];
305
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));
311         }
312       }
313     }
314   }
315
316   BAIL_OUT (0);
317 #undef BAIL_OUT
318 } /* flush */
319
320 static int listval (lcc_connection_t *c, int argc, char **argv)
321 {
322   lcc_identifier_t *ret_ident     = NULL;
323   size_t            ret_ident_num = 0;
324
325   int status;
326   size_t i;
327
328   assert (strcasecmp (argv[0], "listval") == 0);
329
330   if (argc != 1) {
331     fprintf (stderr, "ERROR: listval: Does not accept any arguments.\n");
332     return (-1);
333   }
334
335 #define BAIL_OUT(s) \
336   do { \
337     if (ret_ident != NULL) \
338       free (ret_ident); \
339     ret_ident_num = 0; \
340     return (s); \
341   } while (0)
342
343   status = lcc_listval (c, &ret_ident, &ret_ident_num);
344   if (status != 0) {
345     fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
346     BAIL_OUT (status);
347   }
348
349   for (i = 0; i < ret_ident_num; ++i) {
350     char id[1024];
351
352     status = lcc_identifier_to_string (c, id, sizeof (id), ret_ident + i);
353     if (status != 0) {
354       fprintf (stderr, "ERROR: listval: Failed to convert returned "
355           "identifier to a string: %s\n", lcc_strerror (c));
356       continue;
357     }
358
359     printf ("%s\n", id);
360   }
361   BAIL_OUT (0);
362 #undef BAIL_OUT
363 } /* listval */
364
365 int main (int argc, char **argv) {
366   char address[1024] = "unix:"DEFAULT_SOCK;
367
368   lcc_connection_t *c;
369
370   int status;
371
372   while (42) {
373     int c;
374
375     c = getopt (argc, argv, "s:h");
376
377     if (c == -1)
378       break;
379
380     switch (c) {
381       case 's':
382         snprintf (address, sizeof (address), "unix:%s", optarg);
383         address[sizeof (address) - 1] = '\0';
384         break;
385       case 'h':
386         exit_usage (argv[0], 0);
387         break;
388       default:
389         exit_usage (argv[0], 1);
390     }
391   }
392
393   if (optind >= argc) {
394     fprintf (stderr, "%s: missing command\n", argv[0]);
395     exit_usage (argv[0], 1);
396   }
397
398   c = NULL;
399   status = lcc_connect (address, &c);
400   if (status != 0) {
401     fprintf (stderr, "ERROR: Failed to connect to daemon at %s: %s.\n",
402         address, strerror (errno));
403     return (1);
404   }
405
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);
412   else {
413     fprintf (stderr, "%s: invalid command: %s\n", argv[0], argv[optind]);
414     return (1);
415   }
416
417   LCC_DESTROY (c);
418
419   if (status != 0)
420     return (status);
421   return (0);
422 } /* main */
423
424 /* vim: set sw=2 ts=2 tw=78 expandtab : */
425