fa5cdf2c92616afdf95301245d744342cba7e3b2
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 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
26 #include "utils_cache.h"
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;
38 const data_set_t *ds;
40 int status;
41 int i;
43 if (fields_num != 2)
44 {
45 DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
46 fprintf (fh, "-1 Wrong number of fields: Got %i, expected 2.\n",
47 fields_num);
48 fflush (fh);
49 return (-1);
50 }
51 DEBUG ("unixsock plugin: Got query for `%s'", fields[1]);
53 if (strlen (fields[1]) < strlen ("h/p/t"))
54 {
55 fprintf (fh, "-1 Invalied identifier, %s", fields[1]);
56 fflush (fh);
57 return (-1);
58 }
60 status = parse_identifier (fields[1], &hostname,
61 &plugin, &plugin_instance,
62 &type, &type_instance);
63 if (status != 0)
64 {
65 DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
66 fprintf (fh, "-1 Cannot parse identifier.\n");
67 fflush (fh);
68 return (-1);
69 }
71 ds = plugin_get_ds (type);
72 if (ds == NULL)
73 {
74 DEBUG ("unixsock plugin: plugin_get_ds (%s) == NULL;", type);
75 fprintf (fh, "-1 Type `%s' is unknown.\n", type);
76 fflush (fh);
77 return (-1);
78 }
80 values = NULL;
81 values_num = 0;
82 status = uc_get_rate_by_name (fields[1], &values, &values_num);
83 if (status != 0)
84 {
85 fprintf (fh, "-1 No such value");
86 fflush (fh);
87 return (-1);
88 }
90 if (ds->ds_num != values_num)
91 {
92 ERROR ("ds[%s]->ds_num = %i, "
93 "but uc_get_rate_by_name returned %i values.",
94 ds->type, ds->ds_num, values_num);
95 fprintf (fh, "-1 Error reading value from cache.\n");
96 fflush (fh);
97 sfree (values);
98 return (-1);
99 }
101 fprintf (fh, "%u Value%s found\n", (unsigned int) values_num,
102 (values_num == 1) ? "" : "s");
103 for (i = 0; i < values_num; i++)
104 {
105 fprintf (fh, "%s=", ds->ds[i].name);
106 if (isnan (values[i]))
107 fprintf (fh, "NaN\n");
108 else
109 fprintf (fh, "%12e\n", values[i]);
110 }
111 fflush (fh);
113 sfree (values);
115 return (0);
116 } /* int handle_getval */
118 /* vim: set sw=2 sts=2 ts=8 : */