59046f6944cbe9d5c6fbdee1774843f71eddfdbf
[collectd.git] / src / utils_cmd_getval.c
1 /**
2  * collectd - src/utils_cmd_getval.c
3  * Copyright (C) 2008       Florian octo Forster
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "plugin.h"
31
32 #include "utils_cache.h"
33 #include "utils_cmd_getval.h"
34 #include "utils_parse_option.h"
35
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;
41   int status;
42
43   if ((ret_getval == NULL) || (opts == NULL)) {
44     errno = EINVAL;
45     cmd_error(CMD_ERROR, err, "Invalid arguments to cmd_parse_getval.");
46     return CMD_ERROR;
47   }
48
49   if (argc != 1) {
50     if (argc == 0)
51       cmd_error(CMD_PARSE_ERROR, err, "Missing identifier.");
52     else
53       cmd_error(CMD_PARSE_ERROR, err, "Garbage after identifier: `%s'.",
54                 argv[1]);
55     return CMD_PARSE_ERROR;
56   }
57
58   /* parse_identifier() modifies its first argument,
59    * returning pointers into it */
60   identifier_copy = sstrdup(argv[0]);
61
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);
66   if (status != 0) {
67     DEBUG("cmd_parse_getval: Cannot parse identifier `%s'.", identifier_copy);
68     cmd_error(CMD_PARSE_ERROR, err, "Cannot parse identifier `%s'.",
69               identifier_copy);
70     sfree(identifier_copy);
71     return CMD_PARSE_ERROR;
72   }
73
74   ret_getval->raw_identifier = identifier_copy;
75   return CMD_OK;
76 } /* cmd_status_t cmd_parse_getval */
77
78 #define print_to_socket(fh, ...)                                               \
79   do {                                                                         \
80     if (fprintf(fh, __VA_ARGS__) < 0) {                                        \
81       char errbuf[1024];                                                       \
82       WARNING("cmd_handle_getval: failed to write to socket #%i: %s",          \
83               fileno(fh), sstrerror(errno, errbuf, sizeof(errbuf)));           \
84       return -1;                                                               \
85     }                                                                          \
86     fflush(fh);                                                                \
87   } while (0)
88
89 cmd_status_t cmd_handle_getval(FILE *fh, char *buffer) {
90   cmd_error_handler_t err = {cmd_error_fh, fh};
91   cmd_status_t status;
92   cmd_t cmd;
93
94   gauge_t *values;
95   size_t values_num;
96
97   const data_set_t *ds;
98
99   if ((fh == NULL) || (buffer == NULL))
100     return -1;
101
102   DEBUG("utils_cmd_getval: cmd_handle_getval (fh = %p, buffer = %s);",
103         (void *)fh, buffer);
104
105   if ((status = cmd_parse(buffer, &cmd, NULL, &err)) != CMD_OK)
106     return status;
107   if (cmd.type != CMD_GETVAL) {
108     cmd_error(CMD_UNKNOWN_COMMAND, &err, "Unexpected command: `%s'.",
109               CMD_TO_STRING(cmd.type));
110     cmd_destroy(&cmd);
111     return CMD_UNKNOWN_COMMAND;
112   }
113
114   ds = plugin_get_ds(cmd.cmd.getval.identifier.type);
115   if (ds == NULL) {
116     DEBUG("cmd_handle_getval: plugin_get_ds (%s) == NULL;",
117           cmd.cmd.getval.identifier.type);
118     cmd_error(CMD_ERROR, &err, "Type `%s' is unknown.\n",
119               cmd.cmd.getval.identifier.type);
120     cmd_destroy(&cmd);
121     return -1;
122   }
123
124   values = NULL;
125   values_num = 0;
126   status =
127       uc_get_rate_by_name(cmd.cmd.getval.raw_identifier, &values, &values_num);
128   if (status != 0) {
129     cmd_error(CMD_ERROR, &err, "No such value.");
130     cmd_destroy(&cmd);
131     return CMD_ERROR;
132   }
133
134   if (ds->ds_num != values_num) {
135     ERROR("ds[%s]->ds_num = %zu, "
136           "but uc_get_rate_by_name returned %zu values.",
137           ds->type, ds->ds_num, values_num);
138     cmd_error(CMD_ERROR, &err, "Error reading value from cache.");
139     sfree(values);
140     cmd_destroy(&cmd);
141     return CMD_ERROR;
142   }
143
144   print_to_socket(fh, "%zu Value%s found\n", values_num,
145                   (values_num == 1) ? "" : "s");
146   for (size_t i = 0; i < values_num; i++) {
147     print_to_socket(fh, "%s=", ds->ds[i].name);
148     if (isnan(values[i])) {
149       print_to_socket(fh, "NaN\n");
150     } else {
151       print_to_socket(fh, "%12e\n", values[i]);
152     }
153   }
154
155   sfree(values);
156   cmd_destroy(&cmd);
157
158   return CMD_OK;
159 } /* cmd_status_t cmd_handle_getval */
160
161 void cmd_destroy_getval(cmd_getval_t *getval) {
162   if (getval == NULL)
163     return;
164
165   sfree(getval->raw_identifier);
166 } /* void cmd_destroy_getval */