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 **/
27 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
32 #include "utils_cache.h"
33 #include "utils_cmd_getval.h"
34 #include "utils_parse_option.h"
36 #define print_to_socket(fh, ...) \
37 do { \
38 if (fprintf(fh, __VA_ARGS__) < 0) { \
39 char errbuf[1024]; \
40 WARNING("handle_getval: failed to write to socket #%i: %s", fileno(fh), \
41 sstrerror(errno, errbuf, sizeof(errbuf))); \
42 return -1; \
43 } \
44 fflush(fh); \
45 } while (0)
47 int handle_getval(FILE *fh, char *buffer) {
48 char *command;
49 char *identifier;
50 char *identifier_copy;
52 char *hostname;
53 char *plugin;
54 char *plugin_instance;
55 char *type;
56 char *type_instance;
57 gauge_t *values;
58 size_t values_num;
60 const data_set_t *ds;
62 int status;
64 if ((fh == NULL) || (buffer == NULL))
65 return (-1);
67 DEBUG("utils_cmd_getval: handle_getval (fh = %p, buffer = %s);", (void *)fh,
68 buffer);
70 command = NULL;
71 status = parse_string(&buffer, &command);
72 if (status != 0) {
73 print_to_socket(fh, "-1 Cannot parse command.\n");
74 return (-1);
75 }
76 assert(command != NULL);
78 if (strcasecmp("GETVAL", command) != 0) {
79 print_to_socket(fh, "-1 Unexpected command: `%s'.\n", command);
80 return (-1);
81 }
83 identifier = NULL;
84 status = parse_string(&buffer, &identifier);
85 if (status != 0) {
86 print_to_socket(fh, "-1 Cannot parse identifier.\n");
87 return (-1);
88 }
89 assert(identifier != NULL);
91 if (*buffer != 0) {
92 print_to_socket(fh, "-1 Garbage after end of command: %s\n", buffer);
93 return (-1);
94 }
96 /* parse_identifier() modifies its first argument,
97 * returning pointers into it */
98 identifier_copy = sstrdup(identifier);
100 status = parse_identifier(identifier_copy, &hostname, &plugin,
101 &plugin_instance, &type, &type_instance);
102 if (status != 0) {
103 DEBUG("handle_getval: Cannot parse identifier `%s'.", identifier);
104 print_to_socket(fh, "-1 Cannot parse identifier `%s'.\n", identifier);
105 sfree(identifier_copy);
106 return (-1);
107 }
109 ds = plugin_get_ds(type);
110 if (ds == NULL) {
111 DEBUG("handle_getval: plugin_get_ds (%s) == NULL;", type);
112 print_to_socket(fh, "-1 Type `%s' is unknown.\n", type);
113 sfree(identifier_copy);
114 return (-1);
115 }
117 values = NULL;
118 values_num = 0;
119 status = uc_get_rate_by_name(identifier, &values, &values_num);
120 if (status != 0) {
121 print_to_socket(fh, "-1 No such value\n");
122 sfree(identifier_copy);
123 return (-1);
124 }
126 if (ds->ds_num != values_num) {
127 ERROR("ds[%s]->ds_num = %zu, "
128 "but uc_get_rate_by_name returned %zu values.",
129 ds->type, ds->ds_num, values_num);
130 print_to_socket(fh, "-1 Error reading value from cache.\n");
131 sfree(values);
132 sfree(identifier_copy);
133 return (-1);
134 }
136 print_to_socket(fh, "%zu Value%s found\n", values_num,
137 (values_num == 1) ? "" : "s");
138 for (size_t i = 0; i < values_num; i++) {
139 print_to_socket(fh, "%s=", ds->ds[i].name);
140 if (isnan(values[i])) {
141 print_to_socket(fh, "NaN\n");
142 } else {
143 print_to_socket(fh, "%12e\n", values[i]);
144 }
145 }
147 sfree(values);
148 sfree(identifier_copy);
150 return (0);
151 } /* int handle_getval */
153 /* vim: set sw=2 sts=2 ts=8 : */