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"
28 #include "utils_cmd_getval.h"
30 #define print_to_socket(fh, ...) \
31 do { \
32 if (fprintf (fh, __VA_ARGS__) < 0) { \
33 char errbuf[1024]; \
34 WARNING ("handle_getval: failed to write to socket #%i: %s", \
35 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
36 return -1; \
37 } \
38 fflush(fh); \
39 } while (0)
41 int handle_getval (FILE *fh, char *buffer)
42 {
43 char *command;
44 char *identifier;
45 char *identifier_copy;
47 char *hostname;
48 char *plugin;
49 char *plugin_instance;
50 char *type;
51 char *type_instance;
52 gauge_t *values;
53 size_t values_num;
55 const data_set_t *ds;
57 int status;
58 size_t i;
60 if ((fh == NULL) || (buffer == NULL))
61 return (-1);
63 DEBUG ("utils_cmd_getval: handle_getval (fh = %p, buffer = %s);",
64 (void *) fh, buffer);
66 command = NULL;
67 status = parse_string (&buffer, &command);
68 if (status != 0)
69 {
70 print_to_socket (fh, "-1 Cannot parse command.\n");
71 return (-1);
72 }
73 assert (command != NULL);
75 if (strcasecmp ("GETVAL", command) != 0)
76 {
77 print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
78 return (-1);
79 }
81 identifier = NULL;
82 status = parse_string (&buffer, &identifier);
83 if (status != 0)
84 {
85 print_to_socket (fh, "-1 Cannot parse identifier.\n");
86 return (-1);
87 }
88 assert (identifier != NULL);
90 if (*buffer != 0)
91 {
92 print_to_socket (fh, "-1 Garbage after end of command: %s\n", buffer);
93 return (-1);
94 }
96 /* parse_identifier() modifies its first argument,
97 * returning pointers into it */
98 identifier_copy = sstrdup (identifier);
100 status = parse_identifier (identifier_copy, &hostname,
101 &plugin, &plugin_instance,
102 &type, &type_instance);
103 if (status != 0)
104 {
105 DEBUG ("handle_getval: Cannot parse identifier `%s'.", identifier);
106 print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n", identifier);
107 sfree (identifier_copy);
108 return (-1);
109 }
111 ds = plugin_get_ds (type);
112 if (ds == NULL)
113 {
114 DEBUG ("handle_getval: plugin_get_ds (%s) == NULL;", type);
115 print_to_socket (fh, "-1 Type `%s' is unknown.\n", type);
116 sfree (identifier_copy);
117 return (-1);
118 }
120 values = NULL;
121 values_num = 0;
122 status = uc_get_rate_by_name (identifier, &values, &values_num);
123 if (status != 0)
124 {
125 print_to_socket (fh, "-1 No such value\n");
126 sfree (identifier_copy);
127 return (-1);
128 }
130 if ((size_t) ds->ds_num != values_num)
131 {
132 ERROR ("ds[%s]->ds_num = %i, "
133 "but uc_get_rate_by_name returned %u values.",
134 ds->type, ds->ds_num, (unsigned int) values_num);
135 print_to_socket (fh, "-1 Error reading value from cache.\n");
136 sfree (values);
137 sfree (identifier_copy);
138 return (-1);
139 }
141 print_to_socket (fh, "%u Value%s found\n", (unsigned int) values_num,
142 (values_num == 1) ? "" : "s");
143 for (i = 0; i < values_num; i++)
144 {
145 print_to_socket (fh, "%s=", ds->ds[i].name);
146 if (isnan (values[i]))
147 {
148 print_to_socket (fh, "NaN\n");
149 }
150 else
151 {
152 print_to_socket (fh, "%12e\n", values[i]);
153 }
154 }
156 sfree (values);
157 sfree (identifier_copy);
159 return (0);
160 } /* int handle_getval */
162 /* vim: set sw=2 sts=2 ts=8 : */