From dfdde4f67574cc6c02ca4bb26a1b54b3b7250fcd Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Sun, 8 Aug 2010 22:15:15 +0200 Subject: [PATCH] =?utf8?q?collectd-flush:=20Added=20support=20for=20the=20?= =?utf8?q?=E2=80=98getval=E2=80=99=20command.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/collectd-flush.c | 124 ++++++++++++++++++++++++++++++++----------- 1 file changed, 93 insertions(+), 31 deletions(-) diff --git a/src/collectd-flush.c b/src/collectd-flush.c index b82ec727..53e4872d 100644 --- a/src/collectd-flush.c +++ b/src/collectd-flush.c @@ -94,6 +94,90 @@ static int count_chars (const char *str, char chr) { 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; @@ -114,7 +198,7 @@ static int flush (lcc_connection_t *c, int argc, char **argv) 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); } @@ -140,36 +224,12 @@ static int flush (lcc_connection_t *c, int argc, char **argv) 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; } } @@ -225,7 +285,9 @@ int main (int argc, char **argv) { 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]); -- 2.30.2