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 **/
22 #if HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 #include "libcollectdclient/client.h"
28 #include <assert.h>
30 #include <errno.h>
32 #include <getopt.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include <unistd.h>
40 #define DEFAULT_SOCK LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
42 extern char *optarg;
43 extern int optind;
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"
49 "Available options:\n"
50 " -s Path to collectd's UNIX socket.\n"
51 " Default: "DEFAULT_SOCK"\n"
53 "\n -h Display this help and exit.\n"
55 "\nAvailable commands:\n\n"
57 " * flush [timeout=<seconds>] [plugin=<name>] [identifier=<id>]\n"
59 "\nIdentifiers:\n\n"
61 "An identifier has the following format:\n\n"
63 " [<hostname>/]<plugin>[-<plugin_instance>]/<type>[-<type_instance>]\n\n"
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"
68 "\nExample:\n\n"
70 " collectd-flush flush plugin=rrdtool identifie=somehost/cpu-0/cpu-wait\n\n"
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"
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 }
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;
87 while (*str != '\0') {
88 if (*str == chr) {
89 count++;
90 }
91 str++;
92 }
94 return count;
95 } /* count_chars */
97 static int parse_identifier (lcc_connection_t *c,
98 const char *value, lcc_identifier_t *ident)
99 {
100 char hostname[1024];
101 char ident_str[1024] = "";
102 int n_slashes;
104 int status;
106 n_slashes = count_chars (value, '/');
107 if (n_slashes == 1) {
108 /* The user has omitted the hostname part of the identifier
109 * (there is only one '/' in the identifier)
110 * Let's add the local hostname */
111 if (gethostname (hostname, sizeof (hostname)) != 0) {
112 fprintf (stderr, "ERROR: Failed to get local hostname: %s",
113 strerror (errno));
114 return (-1);
115 }
116 hostname[sizeof (hostname) - 1] = '\0';
118 snprintf (ident_str, sizeof (ident_str), "%s/%s", hostname, value);
119 ident_str[sizeof(ident_str) - 1] = '\0';
120 }
121 else {
122 strncpy (ident_str, value, sizeof (ident_str));
123 ident_str[sizeof (ident_str) - 1] = '\0';
124 }
126 status = lcc_string_to_identifier (c, ident, ident_str);
127 if (status != 0) {
128 fprintf (stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
129 ident_str, lcc_strerror(c));
130 return (-1);
131 }
132 return (0);
133 } /* parse_identifier */
135 static int getval (lcc_connection_t *c, int argc, char **argv)
136 {
137 lcc_identifier_t ident;
139 size_t ret_values_num = 0;
140 gauge_t *ret_values = NULL;
141 char **ret_values_names = NULL;
143 int status;
144 size_t i;
146 assert (strcasecmp (argv[0], "getval") == 0);
148 if (argc != 2) {
149 fprintf (stderr, "ERROR: getval: Missing identifier.\n");
150 return (-1);
151 }
153 memset (&ident, 0, sizeof (ident));
154 status = parse_identifier (c, argv[1], &ident);
155 if (status != 0)
156 return (status);
158 #define BAIL_OUT(s) \
159 do { \
160 if (ret_values != NULL) \
161 free (ret_values); \
162 if (ret_values_names != NULL) { \
163 for (i = 0; i < ret_values_num; ++i) \
164 free (ret_values_names[i]); \
165 free (ret_values_names); \
166 } \
167 ret_values_num = 0; \
168 return (s); \
169 } while (0)
171 status = lcc_getval (c, &ident,
172 &ret_values_num, &ret_values, &ret_values_names);
173 if (status != 0)
174 BAIL_OUT (-1);
176 for (i = 0; i < ret_values_num; ++i)
177 printf ("%s=%e\n", ret_values_names[i], ret_values[i]);
178 BAIL_OUT (0);
179 } /* getval */
181 static int flush (lcc_connection_t *c, int argc, char **argv)
182 {
183 lcc_identifier_t ident;
184 lcc_identifier_t *identp = NULL;
186 char *plugin = NULL;
187 int timeout = -1;
189 int status;
190 int i;
192 assert (strcasecmp (argv[0], "flush") == 0);
194 for (i = 1; i < argc; ++i) {
195 char *key, *value;
197 key = argv[i];
198 value = strchr (argv[i], (int)'=');
200 if (! value) {
201 fprintf (stderr, "ERROR: flush: Invalid option ``%s''.\n", argv[i]);
202 return (-1);
203 }
205 *value = '\0';
206 ++value;
208 if (strcasecmp (key, "timeout") == 0) {
209 char *endptr = NULL;
211 timeout = strtol (value, &endptr, 0);
213 if (endptr == value) {
214 fprintf (stderr, "ERROR: Failed to parse timeout as number: %s.\n",
215 value);
216 return (-1);
217 }
218 else if ((endptr != NULL) && (*endptr != '\0')) {
219 fprintf (stderr, "WARNING: Ignoring trailing garbage after timeout: "
220 "%s.\n", endptr);
221 }
222 }
223 else if (strcasecmp (key, "plugin") == 0) {
224 plugin = value;
225 }
226 else if (strcasecmp (key, "identifier") == 0) {
227 int status;
229 memset (&ident, 0, sizeof (ident));
230 status = parse_identifier (c, value, &ident);
231 if (status != 0)
232 return (status);
233 identp = &ident;
234 }
235 }
237 status = lcc_flush (c, plugin, identp, timeout);
238 if (status != 0) {
239 fprintf (stderr, "ERROR: Flushing failed: %s.\n",
240 lcc_strerror (c));
241 return (-1);
242 }
244 return 0;
245 } /* flush */
247 int main (int argc, char **argv) {
248 char address[1024] = "unix:"DEFAULT_SOCK;
250 lcc_connection_t *c;
252 int status;
254 while (42) {
255 int c;
257 c = getopt (argc, argv, "s:h");
259 if (c == -1)
260 break;
262 switch (c) {
263 case 's':
264 snprintf (address, sizeof (address), "unix:%s", optarg);
265 address[sizeof (address) - 1] = '\0';
266 break;
267 case 'h':
268 exit_usage (argv[0], 0);
269 break;
270 default:
271 exit_usage (argv[0], 1);
272 }
273 }
275 if (optind >= argc) {
276 fprintf (stderr, "%s: missing command\n", argv[0]);
277 exit_usage (argv[0], 1);
278 }
280 c = NULL;
281 status = lcc_connect (address, &c);
282 if (status != 0) {
283 fprintf (stderr, "ERROR: Failed to connect to daemon at %s: %s.\n",
284 address, strerror (errno));
285 return (1);
286 }
288 if (strcasecmp (argv[optind], "getval") == 0)
289 status = getval (c, argc - optind, argv + optind);
290 else if (strcasecmp (argv[optind], "flush") == 0)
291 status = flush (c, argc - optind, argv + optind);
292 else {
293 fprintf (stderr, "%s: invalid command: %s\n", argv[0], argv[optind]);
294 return (1);
295 }
297 LCC_DESTROY (c);
299 if (status != 0)
300 return (status);
301 return (0);
302 } /* main */
304 /* vim: set sw=2 ts=2 tw=78 expandtab : */