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