X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Futils_cmd_getval.c;h=ce3e28e003eafc8f671dde5acab5462a5c57847f;hb=31ee0e5282b59f89ac5c9bcaebb993450e45dba1;hp=470d3022e4881dfa8b8f32b282ff260ce511aac2;hpb=903ebad4e6fe24e6b04097f18048b63ebb3bcb29;p=collectd.git diff --git a/src/utils_cmd_getval.c b/src/utils_cmd_getval.c index 470d3022..ce3e28e0 100644 --- a/src/utils_cmd_getval.c +++ b/src/utils_cmd_getval.c @@ -24,6 +24,7 @@ #include "plugin.h" #include "utils_cache.h" +#include "utils_parse_option.h" #define print_to_socket(fh, ...) \ if (fprintf (fh, __VA_ARGS__) < 0) { \ @@ -33,8 +34,12 @@ return -1; \ } -int handle_getval (FILE *fh, char **fields, int fields_num) +int handle_getval (FILE *fh, char *buffer) { + char *command; + char *identifier; + char *identifier_copy; + char *hostname; char *plugin; char *plugin_instance; @@ -43,39 +48,58 @@ int handle_getval (FILE *fh, char **fields, int fields_num) gauge_t *values; size_t values_num; - char *identifier_copy; - const data_set_t *ds; int status; - int i; + size_t i; + + if ((fh == NULL) || (buffer == NULL)) + return (-1); + + DEBUG ("utils_cmd_getval: handle_getval (fh = %p, buffer = %s);", + (void *) fh, buffer); - if (fields_num != 2) + command = NULL; + status = parse_string (&buffer, &command); + if (status != 0) { - DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num); - print_to_socket (fh, "-1 Wrong number of fields: Got %i, expected 2.\n", - fields_num); + print_to_socket (fh, "-1 Cannot parse command.\n"); return (-1); } - DEBUG ("unixsock plugin: Got query for `%s'", fields[1]); + assert (command != NULL); - if (strlen (fields[1]) < strlen ("h/p/t")) + if (strcasecmp ("GETVAL", command) != 0) { - print_to_socket (fh, "-1 Invalied identifier, %s\n", fields[1]); + print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command); + return (-1); + } + + identifier = NULL; + status = parse_string (&buffer, &identifier); + if (status != 0) + { + print_to_socket (fh, "-1 Cannot parse identifier.\n"); + return (-1); + } + assert (identifier != NULL); + + if (*buffer != 0) + { + print_to_socket (fh, "-1 Garbage after end of command: %s\n", buffer); return (-1); } /* parse_identifier() modifies its first argument, * returning pointers into it */ - identifier_copy = sstrdup (fields[1]); + identifier_copy = sstrdup (identifier); status = parse_identifier (identifier_copy, &hostname, &plugin, &plugin_instance, &type, &type_instance); if (status != 0) { - DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]); - print_to_socket (fh, "-1 Cannot parse identifier.\n"); + DEBUG ("handle_getval: Cannot parse identifier `%s'.", identifier); + print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n", identifier); sfree (identifier_copy); return (-1); } @@ -83,7 +107,7 @@ int handle_getval (FILE *fh, char **fields, int fields_num) ds = plugin_get_ds (type); if (ds == NULL) { - DEBUG ("unixsock plugin: plugin_get_ds (%s) == NULL;", type); + DEBUG ("handle_getval: plugin_get_ds (%s) == NULL;", type); print_to_socket (fh, "-1 Type `%s' is unknown.\n", type); sfree (identifier_copy); return (-1); @@ -91,7 +115,7 @@ int handle_getval (FILE *fh, char **fields, int fields_num) values = NULL; values_num = 0; - status = uc_get_rate_by_name (fields[1], &values, &values_num); + status = uc_get_rate_by_name (identifier, &values, &values_num); if (status != 0) { print_to_socket (fh, "-1 No such value\n"); @@ -99,7 +123,7 @@ int handle_getval (FILE *fh, char **fields, int fields_num) return (-1); } - if (ds->ds_num != values_num) + if ((size_t) ds->ds_num != values_num) { ERROR ("ds[%s]->ds_num = %i, " "but uc_get_rate_by_name returned %u values.",