b31cb1eb3e15edab08f1e085e9f1c46cae35af8f
[collectd.git] / src / utils_cmd_listval.c
1 /**
2  * collectd - src/utils_cmd_listval.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_cmd_listval.h"
33 #include "utils_cache.h"
34 #include "utils_parse_option.h"
35
36 cmd_status_t cmd_parse_listval (size_t argc, char **argv,
37     cmd_listval_t *ret_listval __attribute__((unused)),
38     cmd_error_handler_t *err)
39 {
40   if (argc != 0)
41   {
42     cmd_error (CMD_PARSE_ERROR, err,
43         "Garbage after end of command: `%s'.", argv[0]);
44     return (CMD_PARSE_ERROR);
45   }
46
47   return (CMD_OK);
48 } /* cmd_status_t cmd_parse_listval */
49
50 #define free_everything_and_return(status) do { \
51     for (size_t j = 0; j < number; j++) { \
52       sfree(names[j]); \
53       names[j] = NULL; \
54     } \
55     sfree(names); \
56     sfree(times); \
57     return (status); \
58   } while (0)
59
60 #define print_to_socket(fh, ...) \
61   do { \
62     if (fprintf (fh, __VA_ARGS__) < 0) { \
63       char errbuf[1024]; \
64       WARNING ("handle_listval: failed to write to socket #%i: %s", \
65           fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
66       free_everything_and_return (CMD_ERROR); \
67     } \
68     fflush(fh); \
69   } while (0)
70
71 cmd_status_t cmd_handle_listval (FILE *fh, char *buffer)
72 {
73   cmd_error_handler_t err = { cmd_error_fh, fh };
74   cmd_status_t status;
75   cmd_t cmd;
76
77   char **names = NULL;
78   cdtime_t *times = NULL;
79   size_t number = 0;
80
81   DEBUG ("utils_cmd_listval: handle_listval (fh = %p, buffer = %s);",
82       (void *) fh, buffer);
83
84   if ((status = cmd_parse (buffer, &cmd, &err)) != CMD_OK)
85     return (status);
86   if (cmd.type != CMD_LISTVAL)
87   {
88     cmd_error (CMD_UNKNOWN_COMMAND, &err,
89         "Unexpected command: `%s'.", CMD_TO_STRING (cmd.type));
90     free_everything_and_return (CMD_UNKNOWN_COMMAND);
91   }
92
93   status = uc_get_names (&names, &times, &number);
94   if (status != 0)
95   {
96     DEBUG ("command listval: uc_get_names failed with status %i", status);
97     cmd_error (CMD_ERROR, &err, "uc_get_names failed.");
98     free_everything_and_return (CMD_ERROR);
99   }
100
101   print_to_socket (fh, "%i Value%s found\n",
102       (int) number, (number == 1) ? "" : "s");
103   for (size_t i = 0; i < number; i++)
104     print_to_socket (fh, "%.3f %s\n", CDTIME_T_TO_DOUBLE (times[i]),
105         names[i]);
106
107   free_everything_and_return (CMD_OK);
108 } /* cmd_status_t cmd_handle_listval */
109
110 /* vim: set sw=2 sts=2 ts=8 : */