f697e5fd27817b2b74d600e33765b2dd38f587c6
[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 parse_identifier (lcc_connection_t *c,
102     const char *value, lcc_identifier_t *ident)
103 {
104   char hostname[1024];
105   char ident_str[1024] = "";
106   int  n_slashes;
107
108   int status;
109
110   n_slashes = count_chars (value, '/');
111   if (n_slashes == 1) {
112     /* The user has omitted the hostname part of the identifier
113      * (there is only one '/' in the identifier)
114      * Let's add the local hostname */
115     if (gethostname (hostname, sizeof (hostname)) != 0) {
116       fprintf (stderr, "ERROR: Failed to get local hostname: %s",
117           strerror (errno));
118       return (-1);
119     }
120     hostname[sizeof (hostname) - 1] = '\0';
121
122     snprintf (ident_str, sizeof (ident_str), "%s/%s", hostname, value);
123     ident_str[sizeof(ident_str) - 1] = '\0';
124   }
125   else {
126     strncpy (ident_str, value, sizeof (ident_str));
127     ident_str[sizeof (ident_str) - 1] = '\0';
128   }
129
130   status = lcc_string_to_identifier (c, ident, ident_str);
131   if (status != 0) {
132     fprintf (stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
133         ident_str, lcc_strerror(c));
134     return (-1);
135   }
136   return (0);
137 } /* parse_identifier */
138
139 static int getval (lcc_connection_t *c, int argc, char **argv)
140 {
141   lcc_identifier_t ident;
142
143   size_t   ret_values_num   = 0;
144   gauge_t *ret_values       = NULL;
145   char   **ret_values_names = NULL;
146
147   int status;
148   size_t i;
149
150   assert (strcasecmp (argv[0], "getval") == 0);
151
152   if (argc != 2) {
153     fprintf (stderr, "ERROR: getval: Missing identifier.\n");
154     return (-1);
155   }
156
157   memset (&ident, 0, sizeof (ident));
158   status = parse_identifier (c, argv[1], &ident);
159   if (status != 0)
160     return (status);
161
162 #define BAIL_OUT(s) \
163   do { \
164     if (ret_values != NULL) \
165       free (ret_values); \
166     if (ret_values_names != NULL) { \
167       for (i = 0; i < ret_values_num; ++i) \
168         free (ret_values_names[i]); \
169       free (ret_values_names); \
170     } \
171     ret_values_num = 0; \
172     return (s); \
173   } while (0)
174
175   status = lcc_getval (c, &ident,
176       &ret_values_num, &ret_values, &ret_values_names);
177   if (status != 0) {
178     fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
179     BAIL_OUT (-1);
180   }
181
182   for (i = 0; i < ret_values_num; ++i)
183     printf ("%s=%e\n", ret_values_names[i], ret_values[i]);
184   BAIL_OUT (0);
185 } /* getval */
186
187 static int flush (lcc_connection_t *c, int argc, char **argv)
188 {
189   lcc_identifier_t  ident;
190   lcc_identifier_t *identp = NULL;
191
192   char *plugin  = NULL;
193   int   timeout = -1;
194
195   int status;
196   int i;
197
198   assert (strcasecmp (argv[0], "flush") == 0);
199
200   for (i = 1; i < argc; ++i) {
201     char *key, *value;
202
203     key   = argv[i];
204     value = strchr (argv[i], (int)'=');
205
206     if (! value) {
207       fprintf (stderr, "ERROR: flush: Invalid option ``%s''.\n", argv[i]);
208       return (-1);
209     }
210
211     *value = '\0';
212     ++value;
213
214     if (strcasecmp (key, "timeout") == 0) {
215       char *endptr = NULL;
216
217       timeout = strtol (value, &endptr, 0);
218
219       if (endptr == value) {
220         fprintf (stderr, "ERROR: Failed to parse timeout as number: %s.\n",
221             value);
222         return (-1);
223       }
224       else if ((endptr != NULL) && (*endptr != '\0')) {
225         fprintf (stderr, "WARNING: Ignoring trailing garbage after timeout: "
226             "%s.\n", endptr);
227       }
228     }
229     else if (strcasecmp (key, "plugin") == 0) {
230       plugin = value;
231     }
232     else if (strcasecmp (key, "identifier") == 0) {
233       int status;
234
235       memset (&ident, 0, sizeof (ident));
236       status = parse_identifier (c, value, &ident);
237       if (status != 0)
238         return (status);
239       identp = &ident;
240     }
241   }
242
243   status = lcc_flush (c, plugin, identp, timeout);
244   if (status != 0) {
245     fprintf (stderr, "ERROR: Flushing failed: %s.\n",
246         lcc_strerror (c));
247     return (-1);
248   }
249
250   return 0;
251 #undef BAIL_OUT
252 } /* flush */
253
254 static int listval (lcc_connection_t *c, int argc, char **argv)
255 {
256   lcc_identifier_t *ret_ident     = NULL;
257   size_t            ret_ident_num = 0;
258
259   int status;
260   size_t i;
261
262   assert (strcasecmp (argv[0], "listval") == 0);
263
264   if (argc != 1) {
265     fprintf (stderr, "ERROR: listval: Does not accept any arguments.\n");
266     return (-1);
267   }
268
269 #define BAIL_OUT(s) \
270   do { \
271     if (ret_ident != NULL) \
272       free (ret_ident); \
273     ret_ident_num = 0; \
274     return (s); \
275   } while (0)
276
277   status = lcc_listval (c, &ret_ident, &ret_ident_num);
278   if (status != 0) {
279     fprintf (stderr, "ERROR: %s\n", lcc_strerror (c));
280     BAIL_OUT (status);
281   }
282
283   for (i = 0; i < ret_ident_num; ++i) {
284     char id[1024];
285
286     status = lcc_identifier_to_string (c, id, sizeof (id), ret_ident + i);
287     if (status != 0) {
288       fprintf (stderr, "ERROR: listval: Failed to convert returned "
289           "identifier to a string: %s\n", lcc_strerror (c));
290       continue;
291     }
292
293     printf ("%s\n", id);
294   }
295   BAIL_OUT (0);
296 #undef BAIL_OUT
297 } /* listval */
298
299 int main (int argc, char **argv) {
300   char address[1024] = "unix:"DEFAULT_SOCK;
301
302   lcc_connection_t *c;
303
304   int status;
305
306   while (42) {
307     int c;
308
309     c = getopt (argc, argv, "s:h");
310
311     if (c == -1)
312       break;
313
314     switch (c) {
315       case 's':
316         snprintf (address, sizeof (address), "unix:%s", optarg);
317         address[sizeof (address) - 1] = '\0';
318         break;
319       case 'h':
320         exit_usage (argv[0], 0);
321         break;
322       default:
323         exit_usage (argv[0], 1);
324     }
325   }
326
327   if (optind >= argc) {
328     fprintf (stderr, "%s: missing command\n", argv[0]);
329     exit_usage (argv[0], 1);
330   }
331
332   c = NULL;
333   status = lcc_connect (address, &c);
334   if (status != 0) {
335     fprintf (stderr, "ERROR: Failed to connect to daemon at %s: %s.\n",
336         address, strerror (errno));
337     return (1);
338   }
339
340   if (strcasecmp (argv[optind], "getval") == 0)
341     status = getval (c, argc - optind, argv + optind);
342   else if (strcasecmp (argv[optind], "flush") == 0)
343     status = flush (c, argc - optind, argv + optind);
344   else if (strcasecmp (argv[optind], "listval") == 0)
345     status = listval (c, argc - optind, argv + optind);
346   else {
347     fprintf (stderr, "%s: invalid command: %s\n", argv[0], argv[optind]);
348     return (1);
349   }
350
351   LCC_DESTROY (c);
352
353   if (status != 0)
354     return (status);
355   return (0);
356 } /* main */
357
358 /* vim: set sw=2 ts=2 tw=78 expandtab : */
359