2 * collectd - src/utils_cms_getval.c
3 * Copyright (C) 2008 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
26 #include "utils_cache.h"
27 #include "utils_parse_option.h"
29 #define print_to_socket(fh, ...) \
31 if (fprintf (fh, __VA_ARGS__) < 0) { \
33 WARNING ("handle_getval: failed to write to socket #%i: %s", \
34 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
40 int handle_getval (FILE *fh, char *buffer)
44 char *identifier_copy;
48 char *plugin_instance;
59 if ((fh == NULL) || (buffer == NULL))
62 DEBUG ("utils_cmd_getval: handle_getval (fh = %p, buffer = %s);",
66 status = parse_string (&buffer, &command);
69 print_to_socket (fh, "-1 Cannot parse command.\n");
72 assert (command != NULL);
74 if (strcasecmp ("GETVAL", command) != 0)
76 print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
81 status = parse_string (&buffer, &identifier);
84 print_to_socket (fh, "-1 Cannot parse identifier.\n");
87 assert (identifier != NULL);
91 print_to_socket (fh, "-1 Garbage after end of command: %s\n", buffer);
95 /* parse_identifier() modifies its first argument,
96 * returning pointers into it */
97 identifier_copy = sstrdup (identifier);
99 status = parse_identifier (identifier_copy, &hostname,
100 &plugin, &plugin_instance,
101 &type, &type_instance);
104 DEBUG ("handle_getval: Cannot parse identifier `%s'.", identifier);
105 print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n", identifier);
106 sfree (identifier_copy);
110 ds = plugin_get_ds (type);
113 DEBUG ("handle_getval: plugin_get_ds (%s) == NULL;", type);
114 print_to_socket (fh, "-1 Type `%s' is unknown.\n", type);
115 sfree (identifier_copy);
121 status = uc_get_rate_by_name (identifier, &values, &values_num);
124 print_to_socket (fh, "-1 No such value\n");
125 sfree (identifier_copy);
129 if ((size_t) ds->ds_num != values_num)
131 ERROR ("ds[%s]->ds_num = %i, "
132 "but uc_get_rate_by_name returned %u values.",
133 ds->type, ds->ds_num, (unsigned int) values_num);
134 print_to_socket (fh, "-1 Error reading value from cache.\n");
136 sfree (identifier_copy);
140 print_to_socket (fh, "%u Value%s found\n", (unsigned int) values_num,
141 (values_num == 1) ? "" : "s");
142 for (i = 0; i < values_num; i++)
144 print_to_socket (fh, "%s=", ds->ds[i].name);
145 if (isnan (values[i]))
147 print_to_socket (fh, "NaN\n");
151 print_to_socket (fh, "%12e\n", values[i]);
156 sfree (identifier_copy);
159 } /* int handle_getval */
161 /* vim: set sw=2 sts=2 ts=8 : */