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 do { \
31 if (fprintf (fh, __VA_ARGS__) < 0) { \
32 char errbuf[1024]; \
33 WARNING ("handle_getval: failed to write to socket #%i: %s", \
34 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
35 return -1; \
36 } \
37 fflush(fh); \
38 } while (0)
40 int handle_getval (FILE *fh, char *buffer)
41 {
42 char *command;
43 char *identifier;
44 char *identifier_copy;
46 char *hostname;
47 char *plugin;
48 char *plugin_instance;
49 char *type;
50 char *type_instance;
51 gauge_t *values;
52 size_t values_num;
54 const data_set_t *ds;
56 int status;
57 size_t i;
59 if ((fh == NULL) || (buffer == NULL))
60 return (-1);
62 DEBUG ("utils_cmd_getval: handle_getval (fh = %p, buffer = %s);",
63 (void *) fh, buffer);
65 command = NULL;
66 status = parse_string (&buffer, &command);
67 if (status != 0)
68 {
69 print_to_socket (fh, "-1 Cannot parse command.\n");
70 return (-1);
71 }
72 assert (command != NULL);
74 if (strcasecmp ("GETVAL", command) != 0)
75 {
76 print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
77 return (-1);
78 }
80 identifier = NULL;
81 status = parse_string (&buffer, &identifier);
82 if (status != 0)
83 {
84 print_to_socket (fh, "-1 Cannot parse identifier.\n");
85 return (-1);
86 }
87 assert (identifier != NULL);
89 if (*buffer != 0)
90 {
91 print_to_socket (fh, "-1 Garbage after end of command: %s\n", buffer);
92 return (-1);
93 }
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);
102 if (status != 0)
103 {
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);
107 return (-1);
108 }
110 ds = plugin_get_ds (type);
111 if (ds == NULL)
112 {
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);
116 return (-1);
117 }
119 values = NULL;
120 values_num = 0;
121 status = uc_get_rate_by_name (identifier, &values, &values_num);
122 if (status != 0)
123 {
124 print_to_socket (fh, "-1 No such value\n");
125 sfree (identifier_copy);
126 return (-1);
127 }
129 if ((size_t) ds->ds_num != values_num)
130 {
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");
135 sfree (values);
136 sfree (identifier_copy);
137 return (-1);
138 }
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++)
143 {
144 print_to_socket (fh, "%s=", ds->ds[i].name);
145 if (isnan (values[i]))
146 {
147 print_to_socket (fh, "NaN\n");
148 }
149 else
150 {
151 print_to_socket (fh, "%12e\n", values[i]);
152 }
153 }
155 sfree (values);
156 sfree (identifier_copy);
158 return (0);
159 } /* int handle_getval */
161 /* vim: set sw=2 sts=2 ts=8 : */