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"
35 #define DEFAULT_SOCK LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
42 const char *ident_str,
45 lcc_connection_t *connection;
46 lcc_identifier_t ident;
48 /* Pointer which is passed to lcc_flush.
49 * Either a null pointer or it points to ident */
50 lcc_identifier_t *identp;
54 status = lcc_connect(address, &connection);
56 fprintf (stderr, "ERROR: Connecting to daemon at %s failed: %s.\n",
57 address, strerror (errno));
62 if (ident_str != NULL && *ident_str != '\0') {
63 status = lcc_string_to_identifier (connection, &ident, ident_str);
65 fprintf (stderr, "ERROR: Creating and identifier failed: %s.\n",
66 lcc_strerror(connection));
67 LCC_DESTROY (connection);
74 status = lcc_flush (connection, plugin, identp, timeout);
76 fprintf (stderr, "ERROR: Flushing failed: %s.\n",
77 lcc_strerror (connection));
78 LCC_DESTROY (connection);
83 LCC_DESTROY (connection);
88 static void exit_usage (const char *name, int status) {
89 fprintf ((status == 0) ? stdout : stderr,
90 "Usage: %s [options]\n\n"
92 "Available options:\n"
93 " -s Path to collectd's UNIX socket.\n"
94 " Default: "DEFAULT_SOCK"\n"
95 " -p <plugin> Plugin to be flushed.\n"
96 " -i <id> Flush data identified by <id> only (see below).\n"
97 " -t <seconds> Flush values older than this value only.\n"
99 "\n -h Display this help and exit.\n"
103 "An identifier (as accepted by the -i option) has the following\n"
106 " [<hostname>/]<plugin>[-<plugin_instance>]/<type>[-<type_instance>]\n\n"
108 "Hostname defaults to the local hostname if omitted (e.g., uptime/uptime).\n"
109 "No error is returned if the specified identifier does not exist.\n"
113 " collectd-flush -p rrdtool -i somehost/cpu-0/cpu-wait\n\n"
115 "Flushes all CPU wait RRD values of the first CPU of the local host.\n"
116 "I.e., writes all pending RRD updates of that data-source to disk.\n"
122 * Count how many occurences there are of a char in a string.
124 static int charoccurences (const char *str, char chr) {
126 while (*str != '\0') {
136 int main (int argc, char **argv) {
137 char address[1024] = "unix:"DEFAULT_SOCK;
139 char ident_str[1024] = "";
146 c = getopt (argc, argv, "s:p:i:ht:");
153 snprintf (address, sizeof (address), "unix:%s", optarg);
154 address[sizeof (address) - 1] = '\0';
160 if (charoccurences (optarg, '/') == 1) {
161 /* The user has omitted the hostname part of the identifier
162 * (there is only one '/' in the identifier)
163 * Let's add the local hostname */
164 if (gethostname (hostname, sizeof (hostname)) != 0) {
165 fprintf (stderr, "Could not get local hostname: %s", strerror (errno));
168 /* Make sure hostname is zero-terminated */
169 hostname[sizeof (hostname) - 1] = '\0';
170 snprintf (ident_str, sizeof (ident_str), "%s/%s", hostname, optarg);
171 /* Make sure ident_str is zero terminated */
172 ident_str[sizeof(ident_str) - 1] = '\0';
174 strncpy (ident_str, optarg, sizeof (ident_str));
175 /* Make sure identifier is zero terminated */
176 ident_str[sizeof (ident_str) - 1] = '\0';
180 timeout = atoi (optarg);
183 exit_usage (argv[0], 0);
186 exit_usage (argv[0], 1);
190 return flush(address, plugin, ident_str, timeout);
193 /* vim: set sw=2 ts=2 tw=78 expandtab : */