parse_identifier: Make hostname optional, if a default has been specified.
[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 cmd_status_t cmd_parse_getval (size_t argc, char **argv,
37     cmd_getval_t *ret_getval, cmd_error_handler_t *err)
38 {
39   char *identifier_copy;
40   int status;
41
42   if (argc != 1)
43   {
44     if (argc == 0)
45       cmd_error (CMD_PARSE_ERROR, err, "Missing identifier.");
46     else
47       cmd_error (CMD_PARSE_ERROR, err,
48           "Garbage after identifier: `%s'.", argv[1]);
49     return (CMD_PARSE_ERROR);
50   }
51
52   /* parse_identifier() modifies its first argument,
53    * returning pointers into it */
54   identifier_copy = sstrdup (argv[0]);
55
56   status = parse_identifier (argv[0], &ret_getval->identifier.host,
57       &ret_getval->identifier.plugin, &ret_getval->identifier.plugin_instance,
58       &ret_getval->identifier.type, &ret_getval->identifier.type_instance,
59       NULL);
60   if (status != 0)
61   {
62     DEBUG ("cmd_parse_getval: Cannot parse identifier `%s'.", identifier_copy);
63     cmd_error (CMD_PARSE_ERROR, err,
64         "Cannot parse identifier `%s'.", identifier_copy);
65     sfree (identifier_copy);
66     return (CMD_PARSE_ERROR);
67   }
68
69   ret_getval->raw_identifier = identifier_copy;
70   return (CMD_OK);
71 } /* cmd_status_t cmd_parse_getval */
72
73 #define print_to_socket(fh, ...) \
74   do { \
75     if (fprintf (fh, __VA_ARGS__) < 0) { \
76       char errbuf[1024]; \
77       WARNING ("cmd_handle_getval: failed to write to socket #%i: %s", \
78           fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
79       return -1; \
80     } \
81     fflush(fh); \
82   } while (0)
83
84 cmd_status_t cmd_handle_getval (FILE *fh, char *buffer)
85 {
86   cmd_error_handler_t err = { cmd_error_fh, fh };
87   cmd_status_t status;
88   cmd_t cmd;
89
90   gauge_t *values;
91   size_t values_num;
92
93   const data_set_t *ds;
94
95
96   if ((fh == NULL) || (buffer == NULL))
97     return (-1);
98
99   DEBUG ("utils_cmd_getval: cmd_handle_getval (fh = %p, buffer = %s);",
100       (void *) fh, buffer);
101
102   if ((status = cmd_parse (buffer, &cmd, &err)) != CMD_OK)
103     return (status);
104   if (cmd.type != CMD_GETVAL)
105   {
106     cmd_error (CMD_UNKNOWN_COMMAND, &err,
107         "Unexpected command: `%s'.", CMD_TO_STRING (cmd.type));
108     cmd_destroy (&cmd);
109     return (CMD_UNKNOWN_COMMAND);
110   }
111
112   ds = plugin_get_ds (cmd.cmd.getval.identifier.type);
113   if (ds == NULL)
114   {
115     DEBUG ("cmd_handle_getval: plugin_get_ds (%s) == NULL;",
116         cmd.cmd.getval.identifier.type);
117     cmd_error (CMD_ERROR, &err, "Type `%s' is unknown.\n",
118         cmd.cmd.getval.identifier.type);
119     cmd_destroy (&cmd);
120     return (-1);
121   }
122
123   values = NULL;
124   values_num = 0;
125   status = uc_get_rate_by_name (cmd.cmd.getval.raw_identifier, &values, &values_num);
126   if (status != 0)
127   {
128     cmd_error (CMD_ERROR, &err, "No such value.");
129     cmd_destroy (&cmd);
130     return (CMD_ERROR);
131   }
132
133   if (ds->ds_num != values_num)
134   {
135     ERROR ("ds[%s]->ds_num = %zu, "
136         "but uc_get_rate_by_name returned %zu values.",
137         ds->type, ds->ds_num, values_num);
138     cmd_error (CMD_ERROR, &err, "Error reading value from cache.");
139     sfree (values);
140     cmd_destroy (&cmd);
141     return (CMD_ERROR);
142   }
143
144   print_to_socket (fh, "%zu Value%s found\n", values_num,
145       (values_num == 1) ? "" : "s");
146   for (size_t i = 0; i < values_num; i++)
147   {
148     print_to_socket (fh, "%s=", ds->ds[i].name);
149     if (isnan (values[i]))
150     {
151       print_to_socket (fh, "NaN\n");
152     }
153     else
154     {
155       print_to_socket (fh, "%12e\n", values[i]);
156     }
157   }
158
159   sfree (values);
160   cmd_destroy (&cmd);
161
162   return (CMD_OK);
163 } /* cmd_status_t cmd_handle_getval */
164
165 void cmd_destroy_getval (cmd_getval_t *getval)
166 {
167   if (getval == NULL)
168     return;
169
170   sfree (getval->raw_identifier);
171 } /* void cmd_destroy_getval */
172
173 /* vim: set sw=2 sts=2 ts=8 : */