1 /**
2 * collectd - src/utils_cms_getval.c
3 * Copyright (C) 2008 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Author:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
26 #include "utils_cache.h"
27 #include "utils_parse_option.h"
29 #define print_to_socket(fh, ...) \
30 if (fprintf (fh, __VA_ARGS__) < 0) { \
31 char errbuf[1024]; \
32 WARNING ("handle_getval: failed to write to socket #%i: %s", \
33 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
34 return -1; \
35 }
37 int handle_getval (FILE *fh, char *buffer)
38 {
39 char *command;
40 char *identifier;
41 char *identifier_copy;
43 char *hostname;
44 char *plugin;
45 char *plugin_instance;
46 char *type;
47 char *type_instance;
48 gauge_t *values;
49 size_t values_num;
51 const data_set_t *ds;
53 int status;
54 size_t i;
56 if ((fh == NULL) || (buffer == NULL))
57 return (-1);
59 DEBUG ("utils_cmd_getval: handle_getval (fh = %p, buffer = %s);",
60 (void *) fh, buffer);
62 command = NULL;
63 status = parse_string (&buffer, &command);
64 if (status != 0)
65 {
66 print_to_socket (fh, "-1 Cannot parse command.\n");
67 return (-1);
68 }
69 assert (command != NULL);
71 if (strcasecmp ("GETVAL", command) != 0)
72 {
73 print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
74 return (-1);
75 }
77 identifier = NULL;
78 status = parse_string (&buffer, &identifier);
79 if (status != 0)
80 {
81 print_to_socket (fh, "-1 Cannot parse identifier.\n");
82 return (-1);
83 }
84 assert (identifier != NULL);
86 if (*buffer != 0)
87 {
88 print_to_socket (fh, "-1 Garbage after end of command: %s\n", buffer);
89 return (-1);
90 }
92 /* parse_identifier() modifies its first argument,
93 * returning pointers into it */
94 identifier_copy = sstrdup (identifier);
96 status = parse_identifier (identifier_copy, &hostname,
97 &plugin, &plugin_instance,
98 &type, &type_instance);
99 if (status != 0)
100 {
101 DEBUG ("handle_getval: Cannot parse identifier `%s'.", identifier);
102 print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n", identifier);
103 sfree (identifier_copy);
104 return (-1);
105 }
107 ds = plugin_get_ds (type);
108 if (ds == NULL)
109 {
110 DEBUG ("handle_getval: plugin_get_ds (%s) == NULL;", type);
111 print_to_socket (fh, "-1 Type `%s' is unknown.\n", type);
112 sfree (identifier_copy);
113 return (-1);
114 }
116 values = NULL;
117 values_num = 0;
118 status = uc_get_rate_by_name (identifier, &values, &values_num);
119 if (status != 0)
120 {
121 print_to_socket (fh, "-1 No such value\n");
122 sfree (identifier_copy);
123 return (-1);
124 }
126 if ((size_t) ds->ds_num != values_num)
127 {
128 ERROR ("ds[%s]->ds_num = %i, "
129 "but uc_get_rate_by_name returned %u values.",
130 ds->type, ds->ds_num, (unsigned int) values_num);
131 print_to_socket (fh, "-1 Error reading value from cache.\n");
132 sfree (values);
133 sfree (identifier_copy);
134 return (-1);
135 }
137 print_to_socket (fh, "%u Value%s found\n", (unsigned int) values_num,
138 (values_num == 1) ? "" : "s");
139 for (i = 0; i < values_num; i++)
140 {
141 print_to_socket (fh, "%s=", ds->ds[i].name);
142 if (isnan (values[i]))
143 {
144 print_to_socket (fh, "NaN\n");
145 }
146 else
147 {
148 print_to_socket (fh, "%12e\n", values[i]);
149 }
150 }
152 sfree (values);
153 sfree (identifier_copy);
155 return (0);
156 } /* int handle_getval */
158 /* vim: set sw=2 sts=2 ts=8 : */