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