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