1 /**
2 * collectd - src/utils_cmd_getval.c
3 * Copyright (C) 2008 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
31 #include "utils_cache.h"
32 #include "utils_parse_option.h"
34 #define print_to_socket(fh, ...) \
35 do { \
36 if (fprintf (fh, __VA_ARGS__) < 0) { \
37 char errbuf[1024]; \
38 WARNING ("handle_getval: failed to write to socket #%i: %s", \
39 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
40 return -1; \
41 } \
42 fflush(fh); \
43 } while (0)
45 int handle_getval (FILE *fh, char *buffer)
46 {
47 char *command;
48 char *identifier;
49 char *identifier_copy;
51 char *hostname;
52 char *plugin;
53 char *plugin_instance;
54 char *type;
55 char *type_instance;
56 gauge_t *values;
57 size_t values_num;
59 const data_set_t *ds;
61 int status;
62 size_t i;
64 if ((fh == NULL) || (buffer == NULL))
65 return (-1);
67 DEBUG ("utils_cmd_getval: handle_getval (fh = %p, buffer = %s);",
68 (void *) fh, buffer);
70 command = NULL;
71 status = parse_string (&buffer, &command);
72 if (status != 0)
73 {
74 print_to_socket (fh, "-1 Cannot parse command.\n");
75 return (-1);
76 }
77 assert (command != NULL);
79 if (strcasecmp ("GETVAL", command) != 0)
80 {
81 print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
82 return (-1);
83 }
85 identifier = NULL;
86 status = parse_string (&buffer, &identifier);
87 if (status != 0)
88 {
89 print_to_socket (fh, "-1 Cannot parse identifier.\n");
90 return (-1);
91 }
92 assert (identifier != NULL);
94 if (*buffer != 0)
95 {
96 print_to_socket (fh, "-1 Garbage after end of command: %s\n", buffer);
97 return (-1);
98 }
100 /* parse_identifier() modifies its first argument,
101 * returning pointers into it */
102 identifier_copy = sstrdup (identifier);
104 status = parse_identifier (identifier_copy, &hostname,
105 &plugin, &plugin_instance,
106 &type, &type_instance);
107 if (status != 0)
108 {
109 DEBUG ("handle_getval: Cannot parse identifier `%s'.", identifier);
110 print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n", identifier);
111 sfree (identifier_copy);
112 return (-1);
113 }
115 ds = plugin_get_ds (type);
116 if (ds == NULL)
117 {
118 DEBUG ("handle_getval: plugin_get_ds (%s) == NULL;", type);
119 print_to_socket (fh, "-1 Type `%s' is unknown.\n", type);
120 sfree (identifier_copy);
121 return (-1);
122 }
124 values = NULL;
125 values_num = 0;
126 status = uc_get_rate_by_name (identifier, &values, &values_num);
127 if (status != 0)
128 {
129 print_to_socket (fh, "-1 No such value\n");
130 sfree (identifier_copy);
131 return (-1);
132 }
134 if ((size_t) ds->ds_num != values_num)
135 {
136 ERROR ("ds[%s]->ds_num = %i, "
137 "but uc_get_rate_by_name returned %u values.",
138 ds->type, ds->ds_num, (unsigned int) values_num);
139 print_to_socket (fh, "-1 Error reading value from cache.\n");
140 sfree (values);
141 sfree (identifier_copy);
142 return (-1);
143 }
145 print_to_socket (fh, "%u Value%s found\n", (unsigned int) values_num,
146 (values_num == 1) ? "" : "s");
147 for (i = 0; i < values_num; i++)
148 {
149 print_to_socket (fh, "%s=", ds->ds[i].name);
150 if (isnan (values[i]))
151 {
152 print_to_socket (fh, "NaN\n");
153 }
154 else
155 {
156 print_to_socket (fh, "%12e\n", values[i]);
157 }
158 }
160 sfree (values);
161 sfree (identifier_copy);
163 return (0);
164 } /* int handle_getval */
166 /* vim: set sw=2 sts=2 ts=8 : */