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"
28 #define print_to_socket(fh, ...) \
29 if (fprintf (fh, __VA_ARGS__) < 0) { \
31 WARNING ("handle_getval: failed to write to socket #%i: %s", \
32 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
36 int handle_getval (FILE *fh, char **fields, int fields_num)
40 char *plugin_instance;
46 char *identifier_copy;
55 DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
56 print_to_socket (fh, "-1 Wrong number of fields: Got %i, expected 2.\n",
60 DEBUG ("unixsock plugin: Got query for `%s'", fields[1]);
62 if (strlen (fields[1]) < strlen ("h/p/t"))
64 print_to_socket (fh, "-1 Invalied identifier, %s\n", fields[1]);
68 /* parse_identifier() modifies its first argument,
69 * returning pointers into it */
70 identifier_copy = sstrdup (fields[1]);
72 status = parse_identifier (identifier_copy, &hostname,
73 &plugin, &plugin_instance,
74 &type, &type_instance);
77 DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
78 print_to_socket (fh, "-1 Cannot parse identifier.\n");
79 sfree (identifier_copy);
83 ds = plugin_get_ds (type);
86 DEBUG ("unixsock plugin: plugin_get_ds (%s) == NULL;", type);
87 print_to_socket (fh, "-1 Type `%s' is unknown.\n", type);
88 sfree (identifier_copy);
94 status = uc_get_rate_by_name (fields[1], &values, &values_num);
97 print_to_socket (fh, "-1 No such value\n");
98 sfree (identifier_copy);
102 if (ds->ds_num != values_num)
104 ERROR ("ds[%s]->ds_num = %i, "
105 "but uc_get_rate_by_name returned %i values.",
106 ds->type, ds->ds_num, values_num);
107 print_to_socket (fh, "-1 Error reading value from cache.\n");
109 sfree (identifier_copy);
113 print_to_socket (fh, "%u Value%s found\n", (unsigned int) values_num,
114 (values_num == 1) ? "" : "s");
115 for (i = 0; i < values_num; i++)
117 print_to_socket (fh, "%s=", ds->ds[i].name);
118 if (isnan (values[i]))
120 print_to_socket (fh, "NaN\n");
124 print_to_socket (fh, "%12e\n", values[i]);
129 sfree (identifier_copy);
132 } /* int handle_getval */
134 /* vim: set sw=2 sts=2 ts=8 : */