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 flush (const char *address, int argc, char **argv)
98 {
99 lcc_connection_t *connection;
101 lcc_identifier_t ident;
102 lcc_identifier_t *identp = NULL;
104 char *plugin = NULL;
105 int timeout = -1;
107 int status;
108 int i;
110 assert (strcasecmp (argv[0], "flush") == 0);
112 connection = NULL;
113 status = lcc_connect (address, &connection);
114 if (status != 0) {
115 fprintf (stderr, "ERROR: Failed to connect to daemon at %s: %s.\n",
116 address, strerror (errno));
117 return (1);
118 }
120 for (i = 1; i < argc; ++i) {
121 char *key, *value;
123 key = argv[i];
124 value = strchr (argv[i], (int)'=');
126 if (! value) {
127 fprintf (stderr, "ERROR: Invalid option ``%s''.\n", argv[i]);
128 return (-1);
129 }
131 *value = '\0';
132 ++value;
134 if (strcasecmp (key, "timeout") == 0) {
135 char *endptr = NULL;
137 timeout = strtol (value, &endptr, 0);
139 if (endptr == value) {
140 fprintf (stderr, "ERROR: Failed to parse timeout as number: %s.\n",
141 value);
142 return (-1);
143 }
144 else if ((endptr != NULL) && (*endptr != '\0')) {
145 fprintf (stderr, "WARNING: Ignoring trailing garbage after timeout: "
146 "%s.\n", endptr);
147 }
148 }
149 else if (strcasecmp (key, "plugin") == 0) {
150 plugin = value;
151 }
152 else if (strcasecmp (key, "identifier") == 0) {
153 char hostname[1024];
154 char ident_str[1024] = "";
155 int n_slashes;
157 n_slashes = count_chars (value, '/');
158 if (n_slashes == 1) {
159 /* The user has omitted the hostname part of the identifier
160 * (there is only one '/' in the identifier)
161 * Let's add the local hostname */
162 if (gethostname (hostname, sizeof (hostname)) != 0) {
163 fprintf (stderr, "ERROR: Failed to get local hostname: %s",
164 strerror (errno));
165 return (-1);
166 }
167 hostname[sizeof (hostname) - 1] = '\0';
169 snprintf (ident_str, sizeof (ident_str), "%s/%s", hostname, value);
170 ident_str[sizeof(ident_str) - 1] = '\0';
171 }
172 else {
173 strncpy (ident_str, value, sizeof (ident_str));
174 ident_str[sizeof (ident_str) - 1] = '\0';
175 }
177 status = lcc_string_to_identifier (connection, &ident, ident_str);
178 if (status != 0) {
179 fprintf (stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
180 ident_str, lcc_strerror(connection));
181 LCC_DESTROY (connection);
182 return (-1);
183 }
184 identp = &ident;
185 }
186 }
188 status = lcc_flush (connection, plugin, identp, timeout);
189 if (status != 0) {
190 fprintf (stderr, "ERROR: Flushing failed: %s.\n",
191 lcc_strerror (connection));
192 LCC_DESTROY (connection);
193 return (-1);
194 }
196 LCC_DESTROY (connection);
198 return 0;
199 } /* flush */
201 int main (int argc, char **argv) {
202 char address[1024] = "unix:"DEFAULT_SOCK;
204 int status;
206 while (42) {
207 int c;
209 c = getopt (argc, argv, "s:h");
211 if (c == -1)
212 break;
214 switch (c) {
215 case 's':
216 snprintf (address, sizeof (address), "unix:%s", optarg);
217 address[sizeof (address) - 1] = '\0';
218 break;
219 case 'h':
220 exit_usage (argv[0], 0);
221 break;
222 default:
223 exit_usage (argv[0], 1);
224 }
225 }
227 if (optind >= argc) {
228 fprintf (stderr, "%s: missing command\n", argv[0]);
229 exit_usage (argv[0], 1);
230 }
232 if (strcasecmp (argv[optind], "flush") == 0)
233 status = flush (address, argc - optind, argv + optind);
234 else {
235 fprintf (stderr, "%s: invalid command: %s\n", argv[0], argv[optind]);
236 return (1);
237 }
239 if (status != 0)
240 return (status);
241 return (0);
242 } /* main */
244 /* vim: set sw=2 ts=2 tw=78 expandtab : */