Code

collectdctl: Add the "show" command.
[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 <stdlib.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <strings.h>
34 #include <assert.h>
35 #include <errno.h>
37 #if NAN_STATIC_DEFAULT
38 # include <math.h>
39 /* #endif NAN_STATIC_DEFAULT*/
40 #elif NAN_STATIC_ISOC
41 # ifndef __USE_ISOC99
42 #  define DISABLE_ISOC99 1
43 #  define __USE_ISOC99 1
44 # endif /* !defined(__USE_ISOC99) */
45 # include <math.h>
46 # if DISABLE_ISOC99
47 #  undef DISABLE_ISOC99
48 #  undef __USE_ISOC99
49 # endif /* DISABLE_ISOC99 */
50 /* #endif NAN_STATIC_ISOC */
51 #elif NAN_ZERO_ZERO
52 # include <math.h>
53 # ifdef NAN
54 #  undef NAN
55 # endif
56 # define NAN (0.0 / 0.0)
57 # ifndef isnan
58 #  define isnan(f) ((f) != (f))
59 # endif /* !defined(isnan) */
60 # ifndef isfinite
61 #  define isfinite(f) (((f) - (f)) == 0.0)
62 # endif
63 # ifndef isinf
64 #  define isinf(f) (!isfinite(f) && !isnan(f))
65 # endif
66 #endif /* NAN_ZERO_ZERO */
68 #include "libcollectdclient/client.h"
70 #define DEFAULT_SOCK LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
72 extern char *optarg;
73 extern int   optind;
75 /* This function is implemented in collectdctl-show.c, because it requires
76  * some state which is kept in global variables there. */
77 int show (lcc_connection_t *c, int argc, char **argv);
79 static void exit_usage (const char *name, int status) {
80   fprintf ((status == 0) ? stdout : stderr,
81       "Usage: %s [options] <command> [cmd options]\n\n"
83       "Available options:\n"
84       "  -s       Path to collectd's UNIX socket.\n"
85       "           Default: "DEFAULT_SOCK"\n"
87       "\n  -h       Display this help and exit.\n"
89       "\nAvailable commands:\n\n"
91       " * getval <identifier>\n"
92       " * flush [timeout=<seconds>] [plugin=<name>] [identifier=<id>]\n"
93       " * listval\n"
94       " * putval <identifier> [interval=<seconds>] <value-list(s)>\n"
95       " * show <selector> <aggregation> [<aggregation> ...]\n"
97       "\nIdentifiers:\n\n"
99       "An identifier has the following format:\n\n"
101       "  [<hostname>/]<plugin>[-<plugin_instance>]/<type>[-<type_instance>]\n\n"
103       "Hostname defaults to the local hostname if omitted (e.g., uptime/uptime).\n"
104       "No error is returned if the specified identifier does not exist.\n"
106       "\n"PACKAGE" "VERSION", http://collectd.org/\n"
107       "by Florian octo Forster <octo@verplant.org>\n"
108       "for contributions see `AUTHORS'\n"
109       , name);
110   exit (status);
113 /* Count the number of occurrences of the character 'chr'
114  * in the specified string. */
115 static int count_chars (const char *str, char chr) {
116   int count = 0;
118   while (*str != '\0') {
119     if (*str == chr) {
120       count++;
121     }
122     str++;
123   }
125   return count;
126 } /* count_chars */
128 static int array_grow (void **array, int *array_len, size_t elem_size)
130   void *tmp;
132   assert ((array != NULL) && (array_len != NULL));
134   tmp = realloc (*array, (*array_len + 1) * elem_size);
135   if (tmp == NULL) {
136     fprintf (stderr, "ERROR: Failed to allocate memory.\n");
137     return (-1);
138   }
140   *array = tmp;
141   ++(*array_len);
142   return (0);
143 } /* array_grow */
145 static int parse_identifier (lcc_connection_t *c,
146     const char *value, lcc_identifier_t *ident)
148   char hostname[1024];
149   char ident_str[1024] = "";
150   int  n_slashes;
152   int status;
154   n_slashes = count_chars (value, '/');
155   if (n_slashes == 1) {
156     /* The user has omitted the hostname part of the identifier
157      * (there is only one '/' in the identifier)
158      * Let's add the local hostname */
159     if (gethostname (hostname, sizeof (hostname)) != 0) {
160       fprintf (stderr, "ERROR: Failed to get local hostname: %s",
161           strerror (errno));
162       return (-1);
163     }
164     hostname[sizeof (hostname) - 1] = '\0';
166     snprintf (ident_str, sizeof (ident_str), "%s/%s", hostname, value);
167     ident_str[sizeof(ident_str) - 1] = '\0';
168   }
169   else {
170     strncpy (ident_str, value, sizeof (ident_str));
171     ident_str[sizeof (ident_str) - 1] = '\0';
172   }
174   status = lcc_string_to_identifier (c, ident, ident_str);
175   if (status != 0) {
176     fprintf (stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
177         ident_str, lcc_strerror(c));
178     return (-1);
179   }
180   return (0);
181 } /* parse_identifier */
183 static int getval (lcc_connection_t *c, int argc, char **argv)
185   lcc_identifier_t ident;
187   size_t   ret_values_num   = 0;
188   gauge_t *ret_values       = NULL;
189   char   **ret_values_names = NULL;
191   int status;
192   size_t i;
194   assert (strcasecmp (argv[0], "getval") == 0);
196   if (argc != 2) {
197     fprintf (stderr, "ERROR: getval: Missing identifier.\n");
198     return (-1);
199   }
201   memset (&ident, 0, sizeof (ident));
202   status = parse_identifier (c, argv[1], &ident);
203   if (status != 0)
204     return (status);
206 #define BAIL_OUT(s) \
207   do { \
208     if (ret_values != NULL) \
209       free (ret_values); \
210     if (ret_values_names != NULL) { \
211       for (i = 0; i < ret_values_num; ++i) \
212         free (ret_values_names[i]); \
213       free (ret_values_names); \
214     } \
215     ret_values_num = 0; \
216     return (s); \
217   } while (0)
219   status = lcc_getval (c, &ident,
220       &ret_values_num, &ret_values, &ret_values_names);
221   if (status != 0) {
222     fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
223     BAIL_OUT (-1);
224   }
226   for (i = 0; i < ret_values_num; ++i)
227     printf ("%s=%e\n", ret_values_names[i], ret_values[i]);
228   BAIL_OUT (0);
229 #undef BAIL_OUT
230 } /* getval */
232 static int flush (lcc_connection_t *c, int argc, char **argv)
234   int timeout = -1;
236   lcc_identifier_t *identifiers = NULL;
237   int identifiers_num = 0;
239   char **plugins = NULL;
240   int plugins_num = 0;
242   int status;
243   int i;
245   assert (strcasecmp (argv[0], "flush") == 0);
247 #define BAIL_OUT(s) \
248   do { \
249     if (identifiers != NULL) \
250       free (identifiers); \
251     identifiers_num = 0; \
252     if (plugins != NULL) \
253       free (plugins); \
254     plugins_num = 0; \
255     return (s); \
256   } while (0)
258   for (i = 1; i < argc; ++i) {
259     char *key, *value;
261     key   = argv[i];
262     value = strchr (argv[i], (int)'=');
264     if (! value) {
265       fprintf (stderr, "ERROR: flush: Invalid option ``%s''.\n", argv[i]);
266       BAIL_OUT (-1);
267     }
269     *value = '\0';
270     ++value;
272     if (strcasecmp (key, "timeout") == 0) {
273       char *endptr = NULL;
275       timeout = (int) strtol (value, &endptr, 0);
277       if (endptr == value) {
278         fprintf (stderr, "ERROR: Failed to parse timeout as number: %s.\n",
279             value);
280         BAIL_OUT (-1);
281       }
282       else if ((endptr != NULL) && (*endptr != '\0')) {
283         fprintf (stderr, "WARNING: Ignoring trailing garbage after timeout: "
284             "%s.\n", endptr);
285       }
286     }
287     else if (strcasecmp (key, "plugin") == 0) {
288       status = array_grow ((void *)&plugins, &plugins_num,
289           sizeof (*plugins));
290       if (status != 0)
291         BAIL_OUT (status);
293       plugins[plugins_num - 1] = value;
294     }
295     else if (strcasecmp (key, "identifier") == 0) {
296       status = array_grow ((void *)&identifiers, &identifiers_num,
297           sizeof (*identifiers));
298       if (status != 0)
299         BAIL_OUT (status);
301       memset (identifiers + (identifiers_num - 1), 0, sizeof (*identifiers));
302       status = parse_identifier (c, value,
303           identifiers + (identifiers_num - 1));
304       if (status != 0)
305         BAIL_OUT (status);
306     }
307     else {
308       fprintf (stderr, "ERROR: flush: Unknown option `%s'.\n", key);
309       BAIL_OUT (-1);
310     }
311   }
313   if (plugins_num == 0) {
314     status = array_grow ((void *)&plugins, &plugins_num, sizeof (*plugins));
315     if (status != 0)
316       BAIL_OUT (status);
318     assert (plugins_num == 1);
319     plugins[0] = NULL;
320   }
322   for (i = 0; i < plugins_num; ++i) {
323     if (identifiers_num == 0) {
324       status = lcc_flush (c, plugins[i], NULL, timeout);
325       if (status != 0)
326         fprintf (stderr, "ERROR: Failed to flush plugin `%s': %s.\n",
327             (plugins[i] == NULL) ? "(all)" : plugins[i], lcc_strerror (c));
328     }
329     else {
330       int j;
332       for (j = 0; j < identifiers_num; ++j) {
333         status = lcc_flush (c, plugins[i], identifiers + j, timeout);
334         if (status != 0) {
335           char id[1024];
337           lcc_identifier_to_string (c, id, sizeof (id), identifiers + j);
338           fprintf (stderr, "ERROR: Failed to flush plugin `%s', "
339               "identifier `%s': %s.\n",
340               (plugins[i] == NULL) ? "(all)" : plugins[i],
341               id, lcc_strerror (c));
342         }
343       }
344     }
345   }
347   BAIL_OUT (0);
348 #undef BAIL_OUT
349 } /* flush */
351 static int listval (lcc_connection_t *c, int argc, char **argv)
353   lcc_identifier_t *ret_ident     = NULL;
354   size_t            ret_ident_num = 0;
356   int status;
357   size_t i;
359   assert (strcasecmp (argv[0], "listval") == 0);
361   if (argc != 1) {
362     fprintf (stderr, "ERROR: listval: Does not accept any arguments.\n");
363     return (-1);
364   }
366 #define BAIL_OUT(s) \
367   do { \
368     if (ret_ident != NULL) \
369       free (ret_ident); \
370     ret_ident_num = 0; \
371     return (s); \
372   } while (0)
374   status = lcc_listval (c, &ret_ident, &ret_ident_num);
375   if (status != 0) {
376     fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
377     BAIL_OUT (status);
378   }
380   for (i = 0; i < ret_ident_num; ++i) {
381     char id[1024];
383     status = lcc_identifier_to_string (c, id, sizeof (id), ret_ident + i);
384     if (status != 0) {
385       fprintf (stderr, "ERROR: listval: Failed to convert returned "
386           "identifier to a string: %s\n", lcc_strerror (c));
387       continue;
388     }
390     printf ("%s\n", id);
391   }
392   BAIL_OUT (0);
393 #undef BAIL_OUT
394 } /* listval */
396 static int putval (lcc_connection_t *c, int argc, char **argv)
398   lcc_value_list_t vl = LCC_VALUE_LIST_INIT;
400   /* 64 ought to be enough for anybody ;-) */
401   value_t values[64];
402   int     values_types[64];
403   size_t  values_len = 0;
405   int status;
406   int i;
408   assert (strcasecmp (argv[0], "putval") == 0);
410   if (argc < 3) {
411     fprintf (stderr, "ERROR: putval: Missing identifier "
412         "and/or value list.\n");
413     return (-1);
414   }
416   vl.values       = values;
417   vl.values_types = values_types;
419   status = parse_identifier (c, argv[1], &vl.identifier);
420   if (status != 0)
421     return (status);
423   for (i = 2; i < argc; ++i) {
424     char *tmp;
426     tmp = strchr (argv[i], (int)'=');
428     if (tmp != NULL) { /* option */
429       char *key   = argv[i];
430       char *value = tmp;
432       *value = '\0';
433       ++value;
435       if (strcasecmp (key, "interval") == 0) {
436         char *endptr;
438         vl.interval = strtol (value, &endptr, 0);
440         if (endptr == value) {
441           fprintf (stderr, "ERROR: Failed to parse interval as number: %s.\n",
442               value);
443           return (-1);
444         }
445         else if ((endptr != NULL) && (*endptr != '\0')) {
446           fprintf (stderr, "WARNING: Ignoring trailing garbage after "
447               "interval: %s.\n", endptr);
448         }
449       }
450       else {
451         fprintf (stderr, "ERROR: putval: Unknown option `%s'.\n", key);
452         return (-1);
453       }
454     }
455     else { /* value list */
456       char *value;
458       tmp = strchr (argv[i], (int)':');
460       if (tmp == NULL) {
461         fprintf (stderr, "ERROR: putval: Invalid value list: %s.\n",
462             argv[i]);
463         return (-1);
464       }
466       *tmp = '\0';
467       ++tmp;
469       if (strcasecmp (argv[i], "N") == 0) {
470         vl.time = 0;
471       }
472       else {
473         char *endptr;
475         vl.time = strtol (argv[i], &endptr, 0);
477         if (endptr == argv[i]) {
478           fprintf (stderr, "ERROR: Failed to parse time as number: %s.\n",
479               argv[i]);
480           return (-1);
481         }
482         else if ((endptr != NULL) && (*endptr != '\0')) {
483           fprintf (stderr, "ERROR: Garbage after time: %s.\n", endptr);
484           return (-1);
485         }
486       }
488       values_len = 0;
489       value = tmp;
490       while (value != 0) {
491         char *dot, *endptr;
493         tmp = strchr (argv[i], (int)':');
495         if (tmp != NULL) {
496           *tmp = '\0';
497           ++tmp;
498         }
500         /* This is a bit of a hack, but parsing types.db just does not make
501          * much sense imho -- the server might have different types defined
502          * anyway. Also, lcc uses the type information for formatting the
503          * number only, so the real meaning does not matter. -tokkee */
504         dot = strchr (value, (int)'.');
505         endptr = NULL;
506         if (strcasecmp (value, "U") == 0) {
507           values[values_len].gauge = NAN;
508           values_types[values_len] = LCC_TYPE_GAUGE;
509         }
510         else if (dot) { /* floating point value */
511           values[values_len].gauge = strtod (value, &endptr);
512           values_types[values_len] = LCC_TYPE_GAUGE;
513         }
514         else { /* integer */
515           values[values_len].counter = strtol (value, &endptr, 0);
516           values_types[values_len] = LCC_TYPE_COUNTER;
517         }
518         ++values_len;
520         if (endptr == value) {
521           fprintf (stderr, "ERROR: Failed to parse value as number: %s.\n",
522               argv[i]);
523           return (-1);
524         }
525         else if ((endptr != NULL) && (*endptr != '\0')) {
526           fprintf (stderr, "ERROR: Garbage after value: %s.\n", endptr);
527           return (-1);
528         }
530         value = tmp;
531       }
533       assert (values_len >= 1);
534       vl.values_len = values_len;
536       status = lcc_putval (c, &vl);
537       if (status != 0) {
538         fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
539         return (-1);
540       }
541     }
542   }
544   if (values_len == 0) {
545     fprintf (stderr, "ERROR: putval: Missing value list(s).\n");
546     return (-1);
547   }
548   return (0);
549 } /* putval */
551 int main (int argc, char **argv) {
552   char address[1024] = "unix:"DEFAULT_SOCK;
554   lcc_connection_t *c;
556   int status;
558   while (42) {
559     int c;
561     c = getopt (argc, argv, "s:h");
563     if (c == -1)
564       break;
566     switch (c) {
567       case 's':
568         snprintf (address, sizeof (address), "unix:%s", optarg);
569         address[sizeof (address) - 1] = '\0';
570         break;
571       case 'h':
572         exit_usage (argv[0], 0);
573         break;
574       default:
575         exit_usage (argv[0], 1);
576     }
577   }
579   if (optind >= argc) {
580     fprintf (stderr, "%s: missing command\n", argv[0]);
581     exit_usage (argv[0], 1);
582   }
584   c = NULL;
585   status = lcc_connect (address, &c);
586   if (status != 0) {
587     fprintf (stderr, "ERROR: Failed to connect to daemon at %s: %s.\n",
588         address, strerror (errno));
589     return (1);
590   }
592   if (strcasecmp (argv[optind], "getval") == 0)
593     status = getval (c, argc - optind, argv + optind);
594   else if (strcasecmp (argv[optind], "flush") == 0)
595     status = flush (c, argc - optind, argv + optind);
596   else if (strcasecmp (argv[optind], "listval") == 0)
597     status = listval (c, argc - optind, argv + optind);
598   else if (strcasecmp (argv[optind], "putval") == 0)
599     status = putval (c, argc - optind, argv + optind);
600   else if (strcasecmp (argv[optind], "show") == 0)
601     status = show (c, argc - optind, argv + optind);
602   else {
603     fprintf (stderr, "%s: invalid command: %s\n", argv[0], argv[optind]);
604     return (1);
605   }
607   LCC_DESTROY (c);
609   if (status != 0)
610     return (status);
611   return (0);
612 } /* main */
614 /* vim: set sw=2 ts=2 tw=78 expandtab : */