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