summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 677cdef)
raw | patch | inline | side by side (parent: 677cdef)
author | Sebastian Harl <sh@tokkee.org> | |
Sun, 8 Aug 2010 20:15:15 +0000 (22:15 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Sun, 8 Aug 2010 20:15:15 +0000 (22:15 +0200) |
src/collectd-flush.c | patch | blob | history |
diff --git a/src/collectd-flush.c b/src/collectd-flush.c
index b82ec727ab3d49761a85384bee33380ef413c48e..53e4872d6d432d03d2b9e182d54b5198b231bb14 100644 (file)
--- a/src/collectd-flush.c
+++ b/src/collectd-flush.c
return count;
} /* count_chars */
+static int parse_identifier (lcc_connection_t *c,
+ const char *value, lcc_identifier_t *ident)
+{
+ char hostname[1024];
+ char ident_str[1024] = "";
+ int n_slashes;
+
+ int status;
+
+ n_slashes = count_chars (value, '/');
+ if (n_slashes == 1) {
+ /* The user has omitted the hostname part of the identifier
+ * (there is only one '/' in the identifier)
+ * Let's add the local hostname */
+ if (gethostname (hostname, sizeof (hostname)) != 0) {
+ fprintf (stderr, "ERROR: Failed to get local hostname: %s",
+ strerror (errno));
+ return (-1);
+ }
+ hostname[sizeof (hostname) - 1] = '\0';
+
+ snprintf (ident_str, sizeof (ident_str), "%s/%s", hostname, value);
+ ident_str[sizeof(ident_str) - 1] = '\0';
+ }
+ else {
+ strncpy (ident_str, value, sizeof (ident_str));
+ ident_str[sizeof (ident_str) - 1] = '\0';
+ }
+
+ status = lcc_string_to_identifier (c, ident, ident_str);
+ if (status != 0) {
+ fprintf (stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
+ ident_str, lcc_strerror(c));
+ return (-1);
+ }
+ return (0);
+} /* parse_identifier */
+
+static int getval (lcc_connection_t *c, int argc, char **argv)
+{
+ lcc_identifier_t ident;
+
+ size_t ret_values_num = 0;
+ gauge_t *ret_values = NULL;
+ char **ret_values_names = NULL;
+
+ int status;
+ size_t i;
+
+ assert (strcasecmp (argv[0], "getval") == 0);
+
+ if (argc != 2) {
+ fprintf (stderr, "ERROR: getval: Missing identifier.\n");
+ return (-1);
+ }
+
+ memset (&ident, 0, sizeof (ident));
+ status = parse_identifier (c, argv[1], &ident);
+ if (status != 0)
+ return (status);
+
+#define BAIL_OUT(s) \
+ do { \
+ if (ret_values != NULL) \
+ free (ret_values); \
+ if (ret_values_names != NULL) { \
+ for (i = 0; i < ret_values_num; ++i) \
+ free (ret_values_names[i]); \
+ free (ret_values_names); \
+ } \
+ ret_values_num = 0; \
+ return (s); \
+ } while (0)
+
+ status = lcc_getval (c, &ident,
+ &ret_values_num, &ret_values, &ret_values_names);
+ if (status != 0)
+ BAIL_OUT (-1);
+
+ for (i = 0; i < ret_values_num; ++i)
+ printf ("%s=%e\n", ret_values_names[i], ret_values[i]);
+ BAIL_OUT (0);
+} /* getval */
+
static int flush (lcc_connection_t *c, int argc, char **argv)
{
lcc_identifier_t ident;
value = strchr (argv[i], (int)'=');
if (! value) {
- fprintf (stderr, "ERROR: Invalid option ``%s''.\n", argv[i]);
+ fprintf (stderr, "ERROR: flush: Invalid option ``%s''.\n", argv[i]);
return (-1);
}
plugin = value;
}
else if (strcasecmp (key, "identifier") == 0) {
- char hostname[1024];
- char ident_str[1024] = "";
- int n_slashes;
-
- n_slashes = count_chars (value, '/');
- if (n_slashes == 1) {
- /* The user has omitted the hostname part of the identifier
- * (there is only one '/' in the identifier)
- * Let's add the local hostname */
- if (gethostname (hostname, sizeof (hostname)) != 0) {
- fprintf (stderr, "ERROR: Failed to get local hostname: %s",
- strerror (errno));
- return (-1);
- }
- hostname[sizeof (hostname) - 1] = '\0';
-
- snprintf (ident_str, sizeof (ident_str), "%s/%s", hostname, value);
- ident_str[sizeof(ident_str) - 1] = '\0';
- }
- else {
- strncpy (ident_str, value, sizeof (ident_str));
- ident_str[sizeof (ident_str) - 1] = '\0';
- }
+ int status;
- status = lcc_string_to_identifier (c, &ident, ident_str);
- if (status != 0) {
- fprintf (stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n",
- ident_str, lcc_strerror(c));
- return (-1);
- }
+ memset (&ident, 0, sizeof (ident));
+ status = parse_identifier (c, value, &ident);
+ if (status != 0)
+ return (status);
identp = &ident;
}
}
return (1);
}
- if (strcasecmp (argv[optind], "flush") == 0)
+ if (strcasecmp (argv[optind], "getval") == 0)
+ status = getval (c, argc - optind, argv + optind);
+ else if (strcasecmp (argv[optind], "flush") == 0)
status = flush (c, argc - optind, argv + optind);
else {
fprintf (stderr, "%s: invalid command: %s\n", argv[0], argv[optind]);