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 int handle_getval (FILE *fh, char **fields, int fields_num)
32 char *plugin_instance;
38 char *identifier_copy;
47 DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
48 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 2.\n",
53 DEBUG ("unixsock plugin: Got query for `%s'", fields[1]);
55 if (strlen (fields[1]) < strlen ("h/p/t"))
57 fprintf (fh, "-1 Invalied identifier, %s\n", fields[1]);
62 /* parse_identifier() modifies its first argument,
63 * returning pointers into it */
64 identifier_copy = sstrdup (fields[1]);
66 status = parse_identifier (identifier_copy, &hostname,
67 &plugin, &plugin_instance,
68 &type, &type_instance);
71 DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
72 fprintf (fh, "-1 Cannot parse identifier.\n");
74 sfree (identifier_copy);
78 ds = plugin_get_ds (type);
81 DEBUG ("unixsock plugin: plugin_get_ds (%s) == NULL;", type);
82 fprintf (fh, "-1 Type `%s' is unknown.\n", type);
84 sfree (identifier_copy);
90 status = uc_get_rate_by_name (fields[1], &values, &values_num);
93 fprintf (fh, "-1 No such value\n");
95 sfree (identifier_copy);
99 if (ds->ds_num != values_num)
101 ERROR ("ds[%s]->ds_num = %i, "
102 "but uc_get_rate_by_name returned %i values.",
103 ds->type, ds->ds_num, values_num);
104 fprintf (fh, "-1 Error reading value from cache.\n");
107 sfree (identifier_copy);
111 fprintf (fh, "%u Value%s found\n", (unsigned int) values_num,
112 (values_num == 1) ? "" : "s");
113 for (i = 0; i < values_num; i++)
115 fprintf (fh, "%s=", ds->ds[i].name);
116 if (isnan (values[i]))
117 fprintf (fh, "NaN\n");
119 fprintf (fh, "%12e\n", values[i]);
124 sfree (identifier_copy);
127 } /* int handle_getval */
129 /* vim: set sw=2 sts=2 ts=8 : */