b82ec727ab3d49761a85384bee33380ef413c48e
[collectd.git] / src / collectd-flush.c
1 /**
2  * collectd-flush - src/collectd-flush.c
3  * Copyright (C) 2010 Håkon J Dugstad Johnsen
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Håkon J Dugstad Johnsen <hakon-dugstad.johnsen at telenor.com>
20  **/
21
22 #if HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include "libcollectdclient/client.h"
27
28 #include <assert.h>
29
30 #include <errno.h>
31
32 #include <getopt.h>
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include <unistd.h>
39
40 #define DEFAULT_SOCK LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
41
42 extern char *optarg;
43 extern int   optind;
44
45 static void exit_usage (const char *name, int status) {
46   fprintf ((status == 0) ? stdout : stderr,
47       "Usage: %s [options] <command> [cmd options]\n\n"
48
49       "Available options:\n"
50       "  -s       Path to collectd's UNIX socket.\n"
51       "           Default: "DEFAULT_SOCK"\n"
52
53       "\n  -h       Display this help and exit.\n"
54
55       "\nAvailable commands:\n\n"
56
57       " * flush [timeout=<seconds>] [plugin=<name>] [identifier=<id>]\n"
58
59       "\nIdentifiers:\n\n"
60
61       "An identifier has the following format:\n\n"
62
63       "  [<hostname>/]<plugin>[-<plugin_instance>]/<type>[-<type_instance>]\n\n"
64
65       "Hostname defaults to the local hostname if omitted (e.g., uptime/uptime).\n"
66       "No error is returned if the specified identifier does not exist.\n"
67
68       "\nExample:\n\n"
69
70       "  collectd-flush flush plugin=rrdtool identifie=somehost/cpu-0/cpu-wait\n\n"
71
72       "Flushes all CPU wait RRD values of the first CPU of the local host.\n"
73       "I.e., writes all pending RRD updates of that data-source to disk.\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 flush (lcc_connection_t *c, int argc, char **argv)
98 {
99   lcc_identifier_t  ident;
100   lcc_identifier_t *identp = NULL;
101
102   char *plugin  = NULL;
103   int   timeout = -1;
104
105   int status;
106   int i;
107
108   assert (strcasecmp (argv[0], "flush") == 0);
109
110   for (i = 1; i < argc; ++i) {
111     char *key, *value;
112
113     key   = argv[i];
114     value = strchr (argv[i], (int)'=');
115
116     if (! value) {
117       fprintf (stderr, "ERROR: Invalid option ``%s''.\n", argv[i]);
118       return (-1);
119     }
120
121     *value = '\0';
122     ++value;
123
124     if (strcasecmp (key, "timeout") == 0) {
125       char *endptr = NULL;
126
127       timeout = strtol (value, &endptr, 0);
128
129       if (endptr == value) {
130         fprintf (stderr, "ERROR: Failed to parse timeout as number: %s.\n",
131             value);
132         return (-1);
133       }
134       else if ((endptr != NULL) && (*endptr != '\0')) {
135         fprintf (stderr, "WARNING: Ignoring trailing garbage after timeout: "
136             "%s.\n", endptr);
137       }
138     }
139     else if (strcasecmp (key, "plugin") == 0) {
140       plugin = value;
141     }
142     else if (strcasecmp (key, "identifier") == 0) {
143       char hostname[1024];
144       char ident_str[1024] = "";
145       int  n_slashes;
146
147       n_slashes = count_chars (value, '/');
148       if (n_slashes == 1) {
149         /* The user has omitted the hostname part of the identifier
150          * (there is only one '/' in the identifier)
151          * Let's add the local hostname */
152         if (gethostname (hostname, sizeof (hostname)) != 0) {
153           fprintf (stderr, "ERROR: Failed to get local hostname: %s",
154               strerror (errno));
155           return (-1);
156         }
157         hostname[sizeof (hostname) - 1] = '\0';
158
159         snprintf (ident_str, sizeof (ident_str), "%s/%s", hostname, value);
160         ident_str[sizeof(ident_str) - 1] = '\0';
161       }
162       else {
163         strncpy (ident_str, value, sizeof (ident_str));
164         ident_str[sizeof (ident_str) - 1] = '\0';
165       }
166
167       status = lcc_string_to_identifier (c, &ident, ident_str);
168       if (status != 0) {
169         fprintf (stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
170             ident_str, lcc_strerror(c));
171         return (-1);
172       }
173       identp = &ident;
174     }
175   }
176
177   status = lcc_flush (c, plugin, identp, timeout);
178   if (status != 0) {
179     fprintf (stderr, "ERROR: Flushing failed: %s.\n",
180         lcc_strerror (c));
181     return (-1);
182   }
183
184   return 0;
185 } /* flush */
186
187 int main (int argc, char **argv) {
188   char address[1024] = "unix:"DEFAULT_SOCK;
189
190   lcc_connection_t *c;
191
192   int status;
193
194   while (42) {
195     int c;
196
197     c = getopt (argc, argv, "s:h");
198
199     if (c == -1)
200       break;
201
202     switch (c) {
203       case 's':
204         snprintf (address, sizeof (address), "unix:%s", optarg);
205         address[sizeof (address) - 1] = '\0';
206         break;
207       case 'h':
208         exit_usage (argv[0], 0);
209         break;
210       default:
211         exit_usage (argv[0], 1);
212     }
213   }
214
215   if (optind >= argc) {
216     fprintf (stderr, "%s: missing command\n", argv[0]);
217     exit_usage (argv[0], 1);
218   }
219
220   c = NULL;
221   status = lcc_connect (address, &c);
222   if (status != 0) {
223     fprintf (stderr, "ERROR: Failed to connect to daemon at %s: %s.\n",
224         address, strerror (errno));
225     return (1);
226   }
227
228   if (strcasecmp (argv[optind], "flush") == 0)
229     status = flush (c, argc - optind, argv + optind);
230   else {
231     fprintf (stderr, "%s: invalid command: %s\n", argv[0], argv[optind]);
232     return (1);
233   }
234
235   LCC_DESTROY (c);
236
237   if (status != 0)
238     return (status);
239   return (0);
240 } /* main */
241
242 /* vim: set sw=2 ts=2 tw=78 expandtab : */
243