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_parse_option.h"
34 #include "utils_cmd_getval.h"
36 #define print_to_socket(fh, ...) \
38 if (fprintf (fh, __VA_ARGS__) < 0) { \
40 WARNING ("handle_getval: failed to write to socket #%i: %s", \
41 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
47 int handle_getval (FILE *fh, char *buffer)
51 char *identifier_copy;
55 char *plugin_instance;
66 if ((fh == NULL) || (buffer == NULL))
69 DEBUG ("utils_cmd_getval: handle_getval (fh = %p, buffer = %s);",
73 status = parse_string (&buffer, &command);
76 print_to_socket (fh, "-1 Cannot parse command.\n");
79 assert (command != NULL);
81 if (strcasecmp ("GETVAL", command) != 0)
83 print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
88 status = parse_string (&buffer, &identifier);
91 print_to_socket (fh, "-1 Cannot parse identifier.\n");
94 assert (identifier != NULL);
98 print_to_socket (fh, "-1 Garbage after end of command: %s\n", buffer);
102 /* parse_identifier() modifies its first argument,
103 * returning pointers into it */
104 identifier_copy = sstrdup (identifier);
106 status = parse_identifier (identifier_copy, &hostname,
107 &plugin, &plugin_instance,
108 &type, &type_instance);
111 DEBUG ("handle_getval: Cannot parse identifier `%s'.", identifier);
112 print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n", identifier);
113 sfree (identifier_copy);
117 ds = plugin_get_ds (type);
120 DEBUG ("handle_getval: plugin_get_ds (%s) == NULL;", type);
121 print_to_socket (fh, "-1 Type `%s' is unknown.\n", type);
122 sfree (identifier_copy);
128 status = uc_get_rate_by_name (identifier, &values, &values_num);
131 print_to_socket (fh, "-1 No such value\n");
132 sfree (identifier_copy);
136 if (ds->ds_num != values_num)
138 ERROR ("ds[%s]->ds_num = %zu, "
139 "but uc_get_rate_by_name returned %zu values.",
140 ds->type, ds->ds_num, values_num);
141 print_to_socket (fh, "-1 Error reading value from cache.\n");
143 sfree (identifier_copy);
147 print_to_socket (fh, "%zu Value%s found\n", values_num,
148 (values_num == 1) ? "" : "s");
149 for (i = 0; i < values_num; i++)
151 print_to_socket (fh, "%s=", ds->ds[i].name);
152 if (isnan (values[i]))
154 print_to_socket (fh, "NaN\n");
158 print_to_socket (fh, "%12e\n", values[i]);
163 sfree (identifier_copy);
166 } /* int handle_getval */
168 /* vim: set sw=2 sts=2 ts=8 : */