collectd-flush: Switched to a command based syntax.
[collectd.git] / src / collectd-flush.c
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  **/
21
22 #if HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include "libcollectdclient/client.h"
27
28 #include <assert.h>
29
30 #include <errno.h>
31
32 #include <getopt.h>
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include <unistd.h>
39
40 #define DEFAULT_SOCK LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
41
42 extern char *optarg;
43 extern int   optind;
44
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"
48
49       "Available options:\n"
50       "  -s       Path to collectd's UNIX socket.\n"
51       "           Default: "DEFAULT_SOCK"\n"
52
53       "\n  -h       Display this help and exit.\n"
54
55       "\nAvailable commands:\n\n"
56
57       " * flush [timeout=<seconds>] [plugin=<name>] [identifier=<id>]\n"
58
59       "\nIdentifiers:\n\n"
60
61       "An identifier has the following format:\n\n"
62
63       "  [<hostname>/]<plugin>[-<plugin_instance>]/<type>[-<type_instance>]\n\n"
64
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"
67
68       "\nExample:\n\n"
69
70       "  collectd-flush flush plugin=rrdtool identifie=somehost/cpu-0/cpu-wait\n\n"
71
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"
74
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 }
81
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;
86
87   while (*str != '\0') {
88     if (*str == chr) {
89       count++;
90     }
91     str++;
92   }
93
94   return count;
95 } /* count_chars */
96
97 static int flush (const char *address, int argc, char **argv)
98 {
99   lcc_connection_t *connection;
100
101   lcc_identifier_t  ident;
102   lcc_identifier_t *identp = NULL;
103
104   char *plugin  = NULL;
105   int   timeout = -1;
106
107   int status;
108   int i;
109
110   assert (strcasecmp (argv[0], "flush") == 0);
111
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   }
119
120   for (i = 1; i < argc; ++i) {
121     char *key, *value;
122
123     key   = argv[i];
124     value = strchr (argv[i], (int)'=');
125
126     if (! value) {
127       fprintf (stderr, "ERROR: Invalid option ``%s''.\n", argv[i]);
128       return (-1);
129     }
130
131     *value = '\0';
132     ++value;
133
134     if (strcasecmp (key, "timeout") == 0) {
135       char *endptr = NULL;
136
137       timeout = strtol (value, &endptr, 0);
138
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;
156
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';
168
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       }
176
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   }
187
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   }
195
196   LCC_DESTROY (connection);
197
198   return 0;
199 } /* flush */
200
201 int main (int argc, char **argv) {
202   char address[1024] = "unix:"DEFAULT_SOCK;
203
204   int status;
205
206   while (42) {
207     int c;
208
209     c = getopt (argc, argv, "s:h");
210
211     if (c == -1)
212       break;
213
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   }
226
227   if (optind >= argc) {
228     fprintf (stderr, "%s: missing command\n", argv[0]);
229     exit_usage (argv[0], 1);
230   }
231
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   }
238
239   if (status != 0)
240     return (status);
241   return (0);
242 } /* main */
243
244 /* vim: set sw=2 ts=2 tw=78 expandtab : */
245