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