2 * collectd - src/utils_cmd_getval.c
3 * Copyright (C) 2008 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
32 #include "utils_cache.h"
33 #include "utils_cmd_getval.h"
34 #include "utils_parse_option.h"
36 cmd_status_t cmd_parse_getval(size_t argc, char **argv,
37 cmd_getval_t *ret_getval,
38 const cmd_options_t *opts,
39 cmd_error_handler_t *err) {
40 char *identifier_copy;
43 if ((ret_getval == NULL) || (opts == NULL)) {
45 cmd_error(CMD_ERROR, err, "Invalid arguments to cmd_parse_getval.");
51 cmd_error(CMD_PARSE_ERROR, err, "Missing identifier.");
53 cmd_error(CMD_PARSE_ERROR, err, "Garbage after identifier: `%s'.",
55 return CMD_PARSE_ERROR;
58 /* parse_identifier() modifies its first argument,
59 * returning pointers into it */
60 identifier_copy = sstrdup(argv[0]);
62 status = parse_identifier(
63 argv[0], &ret_getval->identifier.host, &ret_getval->identifier.plugin,
64 &ret_getval->identifier.plugin_instance, &ret_getval->identifier.type,
65 &ret_getval->identifier.type_instance, opts->identifier_default_host);
67 DEBUG("cmd_parse_getval: Cannot parse identifier `%s'.", identifier_copy);
68 cmd_error(CMD_PARSE_ERROR, err, "Cannot parse identifier `%s'.",
70 sfree(identifier_copy);
71 return CMD_PARSE_ERROR;
74 ret_getval->raw_identifier = identifier_copy;
76 } /* cmd_status_t cmd_parse_getval */
78 #define print_to_socket(fh, ...) \
80 if (fprintf(fh, __VA_ARGS__) < 0) { \
81 WARNING("cmd_handle_getval: failed to write to socket #%i: %s", \
82 fileno(fh), STRERRNO); \
88 cmd_status_t cmd_handle_getval(FILE *fh, char *buffer) {
89 cmd_error_handler_t err = {cmd_error_fh, fh};
98 if ((fh == NULL) || (buffer == NULL))
101 DEBUG("utils_cmd_getval: cmd_handle_getval (fh = %p, buffer = %s);",
104 if ((status = cmd_parse(buffer, &cmd, NULL, &err)) != CMD_OK)
106 if (cmd.type != CMD_GETVAL) {
107 cmd_error(CMD_UNKNOWN_COMMAND, &err, "Unexpected command: `%s'.",
108 CMD_TO_STRING(cmd.type));
110 return CMD_UNKNOWN_COMMAND;
113 ds = plugin_get_ds(cmd.cmd.getval.identifier.type);
115 DEBUG("cmd_handle_getval: plugin_get_ds (%s) == NULL;",
116 cmd.cmd.getval.identifier.type);
117 cmd_error(CMD_ERROR, &err, "Type `%s' is unknown.\n",
118 cmd.cmd.getval.identifier.type);
126 uc_get_rate_by_name(cmd.cmd.getval.raw_identifier, &values, &values_num);
128 cmd_error(CMD_ERROR, &err, "No such value.");
133 if (ds->ds_num != values_num) {
134 ERROR("ds[%s]->ds_num = %" PRIsz ", "
135 "but uc_get_rate_by_name returned %" PRIsz " values.",
136 ds->type, ds->ds_num, values_num);
137 cmd_error(CMD_ERROR, &err, "Error reading value from cache.");
143 print_to_socket(fh, "%" PRIsz " Value%s found\n", values_num,
144 (values_num == 1) ? "" : "s");
145 for (size_t i = 0; i < values_num; i++) {
146 print_to_socket(fh, "%s=", ds->ds[i].name);
147 if (isnan(values[i])) {
148 print_to_socket(fh, "NaN\n");
150 print_to_socket(fh, "%12e\n", values[i]);
158 } /* cmd_status_t cmd_handle_getval */
160 void cmd_destroy_getval(cmd_getval_t *getval) {
164 sfree(getval->raw_identifier);
165 } /* void cmd_destroy_getval */