Merge branch 'ad/redis'
[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 <math.h>
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include <unistd.h>
43
44 #define DEFAULT_SOCK LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
45
46 extern char *optarg;
47 extern int   optind;
48
49 static void exit_usage (const char *name, int status) {
50   fprintf ((status == 0) ? stdout : stderr,
51       "Usage: %s [options] <command> [cmd options]\n\n"
52
53       "Available options:\n"
54       "  -s       Path to collectd's UNIX socket.\n"
55       "           Default: "DEFAULT_SOCK"\n"
56
57       "\n  -h       Display this help and exit.\n"
58
59       "\nAvailable commands:\n\n"
60
61       " * getval <identifier>\n"
62       " * flush [timeout=<seconds>] [plugin=<name>] [identifier=<id>]\n"
63       " * listval\n"
64       " * putval <identifier> [interval=<seconds>] <value-list(s)>\n"
65
66       "\nIdentifiers:\n\n"
67
68       "An identifier has the following format:\n\n"
69
70       "  [<hostname>/]<plugin>[-<plugin_instance>]/<type>[-<type_instance>]\n\n"
71
72       "Hostname defaults to the local hostname if omitted (e.g., uptime/uptime).\n"
73       "No error is returned if the specified identifier does not exist.\n"
74
75       "\n"PACKAGE" "VERSION", http://collectd.org/\n"
76       "by Florian octo Forster <octo@verplant.org>\n"
77       "for contributions see `AUTHORS'\n"
78       , name);
79   exit (status);
80 }
81
82 /* Count the number of occurrences of the character 'chr'
83  * in the specified string. */
84 static int count_chars (const char *str, char chr) {
85   int count = 0;
86
87   while (*str != '\0') {
88     if (*str == chr) {
89       count++;
90     }
91     str++;
92   }
93
94   return count;
95 } /* count_chars */
96
97 static int array_grow (void **array, int *array_len, size_t elem_size)
98 {
99   void *tmp;
100
101   assert ((array != NULL) && (array_len != NULL));
102
103   tmp = realloc (*array, (*array_len + 1) * elem_size);
104   if (tmp == NULL) {
105     fprintf (stderr, "ERROR: Failed to allocate memory.\n");
106     return (-1);
107   }
108
109   *array = tmp;
110   ++(*array_len);
111   return (0);
112 } /* array_grow */
113
114 static int parse_identifier (lcc_connection_t *c,
115     const char *value, lcc_identifier_t *ident)
116 {
117   char hostname[1024];
118   char ident_str[1024] = "";
119   int  n_slashes;
120
121   int status;
122
123   n_slashes = count_chars (value, '/');
124   if (n_slashes == 1) {
125     /* The user has omitted the hostname part of the identifier
126      * (there is only one '/' in the identifier)
127      * Let's add the local hostname */
128     if (gethostname (hostname, sizeof (hostname)) != 0) {
129       fprintf (stderr, "ERROR: Failed to get local hostname: %s",
130           strerror (errno));
131       return (-1);
132     }
133     hostname[sizeof (hostname) - 1] = '\0';
134
135     snprintf (ident_str, sizeof (ident_str), "%s/%s", hostname, value);
136     ident_str[sizeof(ident_str) - 1] = '\0';
137   }
138   else {
139     strncpy (ident_str, value, sizeof (ident_str));
140     ident_str[sizeof (ident_str) - 1] = '\0';
141   }
142
143   status = lcc_string_to_identifier (c, ident, ident_str);
144   if (status != 0) {
145     fprintf (stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
146         ident_str, lcc_strerror(c));
147     return (-1);
148   }
149   return (0);
150 } /* parse_identifier */
151
152 static int getval (lcc_connection_t *c, int argc, char **argv)
153 {
154   lcc_identifier_t ident;
155
156   size_t   ret_values_num   = 0;
157   gauge_t *ret_values       = NULL;
158   char   **ret_values_names = NULL;
159
160   int status;
161   size_t i;
162
163   assert (strcasecmp (argv[0], "getval") == 0);
164
165   if (argc != 2) {
166     fprintf (stderr, "ERROR: getval: Missing identifier.\n");
167     return (-1);
168   }
169
170   memset (&ident, 0, sizeof (ident));
171   status = parse_identifier (c, argv[1], &ident);
172   if (status != 0)
173     return (status);
174
175 #define BAIL_OUT(s) \
176   do { \
177     if (ret_values != NULL) \
178       free (ret_values); \
179     if (ret_values_names != NULL) { \
180       for (i = 0; i < ret_values_num; ++i) \
181         free (ret_values_names[i]); \
182       free (ret_values_names); \
183     } \
184     ret_values_num = 0; \
185     return (s); \
186   } while (0)
187
188   status = lcc_getval (c, &ident,
189       &ret_values_num, &ret_values, &ret_values_names);
190   if (status != 0) {
191     fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
192     BAIL_OUT (-1);
193   }
194
195   for (i = 0; i < ret_values_num; ++i)
196     printf ("%s=%e\n", ret_values_names[i], ret_values[i]);
197   BAIL_OUT (0);
198 #undef BAIL_OUT
199 } /* getval */
200
201 static int flush (lcc_connection_t *c, int argc, char **argv)
202 {
203   int timeout = -1;
204
205   lcc_identifier_t *identifiers = NULL;
206   int identifiers_num = 0;
207
208   char **plugins = NULL;
209   int plugins_num = 0;
210
211   int status;
212   int i;
213
214   assert (strcasecmp (argv[0], "flush") == 0);
215
216 #define BAIL_OUT(s) \
217   do { \
218     if (identifiers != NULL) \
219       free (identifiers); \
220     identifiers_num = 0; \
221     if (plugins != NULL) \
222       free (plugins); \
223     plugins_num = 0; \
224     return (s); \
225   } while (0)
226
227   for (i = 1; i < argc; ++i) {
228     char *key, *value;
229
230     key   = argv[i];
231     value = strchr (argv[i], (int)'=');
232
233     if (! value) {
234       fprintf (stderr, "ERROR: flush: Invalid option ``%s''.\n", argv[i]);
235       BAIL_OUT (-1);
236     }
237
238     *value = '\0';
239     ++value;
240
241     if (strcasecmp (key, "timeout") == 0) {
242       char *endptr = NULL;
243
244       timeout = (int) strtol (value, &endptr, 0);
245
246       if (endptr == value) {
247         fprintf (stderr, "ERROR: Failed to parse timeout as number: %s.\n",
248             value);
249         BAIL_OUT (-1);
250       }
251       else if ((endptr != NULL) && (*endptr != '\0')) {
252         fprintf (stderr, "WARNING: Ignoring trailing garbage after timeout: "
253             "%s.\n", endptr);
254       }
255     }
256     else if (strcasecmp (key, "plugin") == 0) {
257       status = array_grow ((void **)&plugins, &plugins_num,
258           sizeof (*plugins));
259       if (status != 0)
260         BAIL_OUT (status);
261
262       plugins[plugins_num - 1] = value;
263     }
264     else if (strcasecmp (key, "identifier") == 0) {
265       status = array_grow ((void **)&identifiers, &identifiers_num,
266           sizeof (*identifiers));
267       if (status != 0)
268         BAIL_OUT (status);
269
270       memset (identifiers + (identifiers_num - 1), 0, sizeof (*identifiers));
271       status = parse_identifier (c, value,
272           identifiers + (identifiers_num - 1));
273       if (status != 0)
274         BAIL_OUT (status);
275     }
276     else {
277       fprintf (stderr, "ERROR: flush: Unknown option `%s'.\n", key);
278       BAIL_OUT (-1);
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 static int putval (lcc_connection_t *c, int argc, char **argv)
366 {
367   lcc_value_list_t vl = LCC_VALUE_LIST_INIT;
368
369   /* 64 ought to be enough for anybody ;-) */
370   value_t values[64];
371   int     values_types[64];
372   size_t  values_len = 0;
373
374   int status;
375   int i;
376
377   assert (strcasecmp (argv[0], "putval") == 0);
378
379   if (argc < 3) {
380     fprintf (stderr, "ERROR: putval: Missing identifier "
381         "and/or value list.\n");
382     return (-1);
383   }
384
385   vl.values       = values;
386   vl.values_types = values_types;
387
388   status = parse_identifier (c, argv[1], &vl.identifier);
389   if (status != 0)
390     return (status);
391
392   for (i = 2; i < argc; ++i) {
393     char *tmp;
394
395     tmp = strchr (argv[i], (int)'=');
396
397     if (tmp != NULL) { /* option */
398       char *key   = argv[i];
399       char *value = tmp;
400
401       *value = '\0';
402       ++value;
403
404       if (strcasecmp (key, "interval") == 0) {
405         char *endptr;
406
407         vl.interval = strtol (value, &endptr, 0);
408
409         if (endptr == value) {
410           fprintf (stderr, "ERROR: Failed to parse interval as number: %s.\n",
411               value);
412           return (-1);
413         }
414         else if ((endptr != NULL) && (*endptr != '\0')) {
415           fprintf (stderr, "WARNING: Ignoring trailing garbage after "
416               "interval: %s.\n", endptr);
417         }
418       }
419       else {
420         fprintf (stderr, "ERROR: putval: Unknown option `%s'.\n", key);
421         return (-1);
422       }
423     }
424     else { /* value list */
425       char *value;
426
427       tmp = strchr (argv[i], (int)':');
428
429       if (tmp == NULL) {
430         fprintf (stderr, "ERROR: putval: Invalid value list: %s.\n",
431             argv[i]);
432         return (-1);
433       }
434
435       *tmp = '\0';
436       ++tmp;
437
438       if (strcasecmp (argv[i], "N") == 0) {
439         vl.time = 0;
440       }
441       else {
442         char *endptr;
443
444         vl.time = strtol (argv[i], &endptr, 0);
445
446         if (endptr == value) {
447           fprintf (stderr, "ERROR: Failed to parse time as number: %s.\n",
448               argv[i]);
449           return (-1);
450         }
451         else if ((endptr != NULL) && (*endptr != '\0')) {
452           fprintf (stderr, "ERROR: Garbage after time: %s.\n", endptr);
453           return (-1);
454         }
455       }
456
457       values_len = 0;
458       value = tmp;
459       while (value != 0) {
460         char *dot, *endptr;
461
462         tmp = strchr (argv[i], (int)':');
463
464         if (tmp != NULL) {
465           *tmp = '\0';
466           ++tmp;
467         }
468
469         /* This is a bit of a hack, but parsing types.db just does not make
470          * much sense imho -- the server might have different types defined
471          * anyway. Also, lcc uses the type information for formatting the
472          * number only, so the real meaning does not matter. -tokkee */
473         dot = strchr (value, (int)'.');
474         endptr = NULL;
475         if (strcasecmp (value, "U") == 0) {
476           values[values_len].gauge = NAN;
477           values_types[values_len] = LCC_TYPE_GAUGE;
478         }
479         else if (dot) { /* floating point value */
480           values[values_len].gauge = strtod (value, &endptr);
481           values_types[values_len] = LCC_TYPE_GAUGE;
482         }
483         else { /* integer */
484           values[values_len].counter = strtol (value, &endptr, 0);
485           values_types[values_len] = LCC_TYPE_COUNTER;
486         }
487         ++values_len;
488
489         if (endptr == value) {
490           fprintf (stderr, "ERROR: Failed to parse value as number: %s.\n",
491               argv[i]);
492           return (-1);
493         }
494         else if ((endptr != NULL) && (*endptr != '\0')) {
495           fprintf (stderr, "ERROR: Garbage after value: %s.\n", endptr);
496           return (-1);
497         }
498
499         value = tmp;
500       }
501
502       assert (values_len >= 1);
503       vl.values_len = values_len;
504
505       status = lcc_putval (c, &vl);
506       if (status != 0) {
507         fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
508         return (-1);
509       }
510     }
511   }
512
513   if (values_len == 0) {
514     fprintf (stderr, "ERROR: putval: Missing value list(s).\n");
515     return (-1);
516   }
517   return (0);
518 } /* putval */
519
520 int main (int argc, char **argv) {
521   char address[1024] = "unix:"DEFAULT_SOCK;
522
523   lcc_connection_t *c;
524
525   int status;
526
527   while (42) {
528     int c;
529
530     c = getopt (argc, argv, "s:h");
531
532     if (c == -1)
533       break;
534
535     switch (c) {
536       case 's':
537         snprintf (address, sizeof (address), "unix:%s", optarg);
538         address[sizeof (address) - 1] = '\0';
539         break;
540       case 'h':
541         exit_usage (argv[0], 0);
542         break;
543       default:
544         exit_usage (argv[0], 1);
545     }
546   }
547
548   if (optind >= argc) {
549     fprintf (stderr, "%s: missing command\n", argv[0]);
550     exit_usage (argv[0], 1);
551   }
552
553   c = NULL;
554   status = lcc_connect (address, &c);
555   if (status != 0) {
556     fprintf (stderr, "ERROR: Failed to connect to daemon at %s: %s.\n",
557         address, strerror (errno));
558     return (1);
559   }
560
561   if (strcasecmp (argv[optind], "getval") == 0)
562     status = getval (c, argc - optind, argv + optind);
563   else if (strcasecmp (argv[optind], "flush") == 0)
564     status = flush (c, argc - optind, argv + optind);
565   else if (strcasecmp (argv[optind], "listval") == 0)
566     status = listval (c, argc - optind, argv + optind);
567   else if (strcasecmp (argv[optind], "putval") == 0)
568     status = putval (c, argc - optind, argv + optind);
569   else {
570     fprintf (stderr, "%s: invalid command: %s\n", argv[0], argv[optind]);
571     return (1);
572   }
573
574   LCC_DESTROY (c);
575
576   if (status != 0)
577     return (status);
578   return (0);
579 } /* main */
580
581 /* vim: set sw=2 ts=2 tw=78 expandtab : */
582