2 * collectd-flush - src/collectd-flush.c
3 * Copyright (C) 2010 Håkon J Dugstad Johnsen
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.
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.
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
19 * Håkon J Dugstad Johnsen <hakon-dugstad.johnsen at telenor.com>
26 #include "libcollectdclient/client.h"
40 #define DEFAULT_SOCK LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
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"
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"
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"
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) {
87 while (*str != '\0') {
97 static int flush (const char *address, int argc, char **argv)
99 lcc_connection_t *connection;
101 lcc_identifier_t ident;
102 lcc_identifier_t *identp = NULL;
110 assert (strcasecmp (argv[0], "flush") == 0);
113 status = lcc_connect (address, &connection);
115 fprintf (stderr, "ERROR: Failed to connect to daemon at %s: %s.\n",
116 address, strerror (errno));
120 for (i = 1; i < argc; ++i) {
124 value = strchr (argv[i], (int)'=');
127 fprintf (stderr, "ERROR: Invalid option ``%s''.\n", argv[i]);
134 if (strcasecmp (key, "timeout") == 0) {
137 timeout = strtol (value, &endptr, 0);
139 if (endptr == value) {
140 fprintf (stderr, "ERROR: Failed to parse timeout as number: %s.\n",
144 else if ((endptr != NULL) && (*endptr != '\0')) {
145 fprintf (stderr, "WARNING: Ignoring trailing garbage after timeout: "
149 else if (strcasecmp (key, "plugin") == 0) {
152 else if (strcasecmp (key, "identifier") == 0) {
154 char ident_str[1024] = "";
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",
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';
173 strncpy (ident_str, value, sizeof (ident_str));
174 ident_str[sizeof (ident_str) - 1] = '\0';
177 status = lcc_string_to_identifier (connection, &ident, ident_str);
179 fprintf (stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
180 ident_str, lcc_strerror(connection));
181 LCC_DESTROY (connection);
188 status = lcc_flush (connection, plugin, identp, timeout);
190 fprintf (stderr, "ERROR: Flushing failed: %s.\n",
191 lcc_strerror (connection));
192 LCC_DESTROY (connection);
196 LCC_DESTROY (connection);
201 int main (int argc, char **argv) {
202 char address[1024] = "unix:"DEFAULT_SOCK;
209 c = getopt (argc, argv, "s:h");
216 snprintf (address, sizeof (address), "unix:%s", optarg);
217 address[sizeof (address) - 1] = '\0';
220 exit_usage (argv[0], 0);
223 exit_usage (argv[0], 1);
227 if (optind >= argc) {
228 fprintf (stderr, "%s: missing command\n", argv[0]);
229 exit_usage (argv[0], 1);
232 if (strcasecmp (argv[optind], "flush") == 0)
233 status = flush (address, argc - optind, argv + optind);
235 fprintf (stderr, "%s: invalid command: %s\n", argv[0], argv[optind]);
244 /* vim: set sw=2 ts=2 tw=78 expandtab : */