Merge branch 'collectd-4.3' into collectd-4.4
[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
28 #define print_to_socket(fh, ...) \
29   if (fprintf (fh, __VA_ARGS__) < 0) { \
30     char errbuf[1024]; \
31     WARNING ("handle_getval: failed to write to socket #%i: %s", \
32         fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
33     return -1; \
34   }
35
36 int handle_getval (FILE *fh, char **fields, int fields_num)
37 {
38   char *hostname;
39   char *plugin;
40   char *plugin_instance;
41   char *type;
42   char *type_instance;
43   gauge_t *values;
44   size_t values_num;
45
46   char *identifier_copy;
47
48   const data_set_t *ds;
49
50   int   status;
51   int   i;
52
53   if (fields_num != 2)
54   {
55     DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
56     print_to_socket (fh, "-1 Wrong number of fields: Got %i, expected 2.\n",
57         fields_num);
58     return (-1);
59   }
60   DEBUG ("unixsock plugin: Got query for `%s'", fields[1]);
61
62   if (strlen (fields[1]) < strlen ("h/p/t"))
63   {
64     print_to_socket (fh, "-1 Invalied identifier, %s\n", fields[1]);
65     return (-1);
66   }
67
68   /* parse_identifier() modifies its first argument,
69    * returning pointers into it */
70   identifier_copy = sstrdup (fields[1]);
71
72   status = parse_identifier (identifier_copy, &hostname,
73       &plugin, &plugin_instance,
74       &type, &type_instance);
75   if (status != 0)
76   {
77     DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
78     print_to_socket (fh, "-1 Cannot parse identifier.\n");
79     sfree (identifier_copy);
80     return (-1);
81   }
82
83   ds = plugin_get_ds (type);
84   if (ds == NULL)
85   {
86     DEBUG ("unixsock plugin: plugin_get_ds (%s) == NULL;", type);
87     print_to_socket (fh, "-1 Type `%s' is unknown.\n", type);
88     sfree (identifier_copy);
89     return (-1);
90   }
91
92   values = NULL;
93   values_num = 0;
94   status = uc_get_rate_by_name (fields[1], &values, &values_num);
95   if (status != 0)
96   {
97     print_to_socket (fh, "-1 No such value\n");
98     sfree (identifier_copy);
99     return (-1);
100   }
101
102   if (ds->ds_num != values_num)
103   {
104     ERROR ("ds[%s]->ds_num = %i, "
105         "but uc_get_rate_by_name returned %u values.",
106         ds->type, ds->ds_num, (unsigned int) values_num);
107     print_to_socket (fh, "-1 Error reading value from cache.\n");
108     sfree (values);
109     sfree (identifier_copy);
110     return (-1);
111   }
112
113   print_to_socket (fh, "%u Value%s found\n", (unsigned int) values_num,
114       (values_num == 1) ? "" : "s");
115   for (i = 0; i < values_num; i++)
116   {
117     print_to_socket (fh, "%s=", ds->ds[i].name);
118     if (isnan (values[i]))
119     {
120       print_to_socket (fh, "NaN\n");
121     }
122     else
123     {
124       print_to_socket (fh, "%12e\n", values[i]);
125     }
126   }
127
128   sfree (values);
129   sfree (identifier_copy);
130
131   return (0);
132 } /* int handle_getval */
133
134 /* vim: set sw=2 sts=2 ts=8 : */