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>
28 #include "libcollectdclient/client.h"
35 const char *ident_str,
38 lcc_connection_t *connection;
39 lcc_identifier_t ident;
41 // Pointer which is passed to lcc_flush.
42 //Either a null pointer or it points to ident
43 lcc_identifier_t *identp;
47 status = lcc_connect(address, &connection);
50 fprintf (stderr, "ERROR: Connecting to daemon at %s failed: %s.\n",
51 address, strerror (errno));
56 if (ident_str != NULL && *ident_str != '\0') {
58 status = lcc_string_to_identifier (connection, &ident, ident_str);
60 fprintf (stderr, "ERROR: Creating and identifier failed: %s.\n",
61 lcc_strerror(connection));
62 LCC_DESTROY (connection);
68 status = lcc_flush (connection, plugin, identp, timeout);
70 fprintf (stderr, "ERROR: Flushing failed: %s.\n",
71 lcc_strerror (connection));
72 LCC_DESTROY (connection);
77 LCC_DESTROY (connection);
82 void usage (const char *name) {
83 fprintf (stderr, "Usage: %s [options]\n"
85 "Valid options are:\n"
86 " -h, --help Display this help message.\n"
87 " -s, --socket=<socket> Path to collectd's UNIX socket. Default: /var/run/collectd-unixsock\n"
88 " -p, --plugin=<plugin> Plugin to flush _to_ (not from). Example: rrdtool\n"
89 " -i, --identifier=<identifier>\n"
90 " Only flush data specified by <identifier>, which has the format: \n"
92 " [<hostname>/]<plugin>[-<plugin_instance>]/<type>[-<type_instance>]\n"
94 " Hostname defaults to the local hostname if omitted.\n"
95 " No error is returned if the specified identifier does not exist.\n"
96 " Examples: uptime/uptime\n"
97 " somehost/cpu-0/cpu-wait\n"
98 " -t, --timeout=<timeout> Only flush values older than this timeout.\n", name);
102 * Count how many occurences there are of a char in a string.
104 int charoccurences (const char *str, char chr) {
106 while (*str != '\0') {
116 int main (int argc, char **argv) {
117 char address[1024] = "unix:/var/run/collectd-unixsock";
119 char ident_str[1024] = "";
124 static struct option long_options[] =
126 {"help", no_argument, 0, 'h'},
127 {"socket", required_argument, 0, 's'},
128 {"plugin", required_argument, 0, 'p'},
129 {"identifier", required_argument, 0, 'i'},
130 {"timeout", required_argument, 0, 't'}
132 int option_index = 0;
135 while ((c = getopt_long (argc, argv, "s:p:i:ht:", long_options, &option_index)) != -1) {
138 snprintf (address, sizeof (address), "unix:%s", optarg);
144 if(charoccurences(optarg, '/') == 1) {
145 // The user has omitted the hostname part of the identifier
146 // (there is only one '/' in the identifier)
147 // Let's add the local hostname
148 if(gethostname(hostname, sizeof(hostname)) != 0) {
149 fprintf (stderr, "Could not get local hostname: %s", strerror(errno));
152 // Make sure hostname is zero-terminated
153 hostname[sizeof(hostname)-1] = '\0';
154 snprintf (ident_str, sizeof (ident_str), "%s/%s", hostname, optarg);
155 // Make sure ident_str is zero terminated
156 ident_str[sizeof(ident_str)-1] = '\0';
158 strncpy(ident_str, optarg, sizeof (ident_str));
159 // Make sure identifier is zero terminated
160 ident_str[sizeof(ident_str)-1] = '\0';
164 timeout = atoi (optarg);
175 return flush(address, plugin, ident_str, timeout);