From: Florian Forster Date: Wed, 27 Feb 2008 09:16:31 +0000 (+0100) Subject: src/utils_cmd_getval.[ch]: Moved the `getval' command out of the unixsock plugin. X-Git-Tag: collectd-4.4.0~87^2~4^2~1 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=02821d073fcc467e75f445d0b899dc17ddd15fd9;p=collectd.git src/utils_cmd_getval.[ch]: Moved the `getval' command out of the unixsock plugin. --- diff --git a/src/Makefile.am b/src/Makefile.am index c3d0b1d3..da9ca9b4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -592,7 +592,10 @@ endif if BUILD_PLUGIN_UNIXSOCK pkglib_LTLIBRARIES += unixsock.la -unixsock_la_SOURCES = unixsock.c utils_cmd_putval.h utils_cmd_putval.c utils_cmd_putnotif.h utils_cmd_putnotif.c +unixsock_la_SOURCES = unixsock.c \ + utils_cmd_getval.h utils_cmd_getval.c \ + utils_cmd_putval.h utils_cmd_putval.c \ + utils_cmd_putnotif.h utils_cmd_putnotif.c unixsock_la_LDFLAGS = -module -avoid-version -lpthread collectd_LDADD += "-dlopen" unixsock.la collectd_DEPENDENCIES += unixsock.la diff --git a/src/unixsock.c b/src/unixsock.c index ca7aa2e6..5f859b3f 100644 --- a/src/unixsock.c +++ b/src/unixsock.c @@ -24,9 +24,7 @@ #include "plugin.h" #include "configfile.h" -/* FIXME: Replace this with "utils_cmd_getval.h" asap */ -#include "utils_cache.h" - +#include "utils_cmd_getval.h" #include "utils_cmd_putval.h" #include "utils_cmd_putnotif.h" @@ -423,97 +421,6 @@ static int us_open_socket (void) return (0); } /* int us_open_socket */ -static int us_handle_getval (FILE *fh, char **fields, int fields_num) -{ - char *hostname; - char *plugin; - char *plugin_instance; - char *type; - char *type_instance; - gauge_t *values; - size_t values_num; - - const data_set_t *ds; - - int status; - int i; - - if (fields_num != 2) - { - DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num); - fprintf (fh, "-1 Wrong number of fields: Got %i, expected 2.\n", - fields_num); - fflush (fh); - return (-1); - } - DEBUG ("unixsock plugin: Got query for `%s'", fields[1]); - - if (strlen (fields[1]) < strlen ("h/p/t")) - { - fprintf (fh, "-1 Invalied identifier, %s", fields[1]); - fflush (fh); - return (-1); - } - - status = parse_identifier (fields[1], &hostname, - &plugin, &plugin_instance, - &type, &type_instance); - if (status != 0) - { - DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]); - fprintf (fh, "-1 Cannot parse identifier.\n"); - fflush (fh); - return (-1); - } - - ds = plugin_get_ds (type); - if (ds == NULL) - { - DEBUG ("unixsock plugin: plugin_get_ds (%s) == NULL;", type); - fprintf (fh, "-1 Type `%s' is unknown.\n", type); - fflush (fh); - return (-1); - } - - values = NULL; - values_num = 0; - status = uc_get_rate_by_name (fields[1], &values, &values_num); - if (status != 0) - { - fprintf (fh, "-1 No such value"); - fflush (fh); - return (-1); - } - - if (ds->ds_num != values_num) - { - ERROR ("ds[%s]->ds_num = %i, " - "but uc_get_rate_by_name returned %i values.", - ds->type, ds->ds_num, values_num); - fprintf (fh, "-1 Error reading value from cache.\n"); - fflush (fh); - sfree (values); - return (-1); - } - - fprintf (fh, "%u", (unsigned int) values_num); - for (i = 0; i < values_num; i++) - { - fprintf (fh, " %s=", ds->ds[i].name); - if (isnan (values[i])) - fprintf (fh, "NaN"); - else - fprintf (fh, "%12e", values[i]); - } - - fprintf (fh, "\n"); - fflush (fh); - - sfree (values); - - return (0); -} /* int us_handle_getval */ - static int us_handle_listval (FILE *fh, char **fields, int fields_num) { char buffer[1024]; @@ -613,7 +520,7 @@ static void *us_handle_client (void *arg) if (strcasecmp (fields[0], "getval") == 0) { - us_handle_getval (fh, fields, fields_num); + handle_getval (fh, fields, fields_num); } else if (strcasecmp (fields[0], "putval") == 0) { diff --git a/src/utils_cmd_getval.c b/src/utils_cmd_getval.c new file mode 100644 index 00000000..a4edf4f9 --- /dev/null +++ b/src/utils_cmd_getval.c @@ -0,0 +1,119 @@ +/** + * collectd - src/utils_cms_getval.c + * Copyright (C) 2008 Florian octo Forster + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; only version 2 of the License is applicable. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: + * Florian octo Forster + **/ + +#include "collectd.h" +#include "common.h" +#include "plugin.h" + +#include "utils_cache.h" + +int handle_getval (FILE *fh, char **fields, int fields_num) +{ + char *hostname; + char *plugin; + char *plugin_instance; + char *type; + char *type_instance; + gauge_t *values; + size_t values_num; + + const data_set_t *ds; + + int status; + int i; + + if (fields_num != 2) + { + DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num); + fprintf (fh, "-1 Wrong number of fields: Got %i, expected 2.\n", + fields_num); + fflush (fh); + return (-1); + } + DEBUG ("unixsock plugin: Got query for `%s'", fields[1]); + + if (strlen (fields[1]) < strlen ("h/p/t")) + { + fprintf (fh, "-1 Invalied identifier, %s", fields[1]); + fflush (fh); + return (-1); + } + + status = parse_identifier (fields[1], &hostname, + &plugin, &plugin_instance, + &type, &type_instance); + if (status != 0) + { + DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]); + fprintf (fh, "-1 Cannot parse identifier.\n"); + fflush (fh); + return (-1); + } + + ds = plugin_get_ds (type); + if (ds == NULL) + { + DEBUG ("unixsock plugin: plugin_get_ds (%s) == NULL;", type); + fprintf (fh, "-1 Type `%s' is unknown.\n", type); + fflush (fh); + return (-1); + } + + values = NULL; + values_num = 0; + status = uc_get_rate_by_name (fields[1], &values, &values_num); + if (status != 0) + { + fprintf (fh, "-1 No such value"); + fflush (fh); + return (-1); + } + + if (ds->ds_num != values_num) + { + ERROR ("ds[%s]->ds_num = %i, " + "but uc_get_rate_by_name returned %i values.", + ds->type, ds->ds_num, values_num); + fprintf (fh, "-1 Error reading value from cache.\n"); + fflush (fh); + sfree (values); + return (-1); + } + + fprintf (fh, "%u", (unsigned int) values_num); + for (i = 0; i < values_num; i++) + { + fprintf (fh, " %s=", ds->ds[i].name); + if (isnan (values[i])) + fprintf (fh, "NaN"); + else + fprintf (fh, "%12e", values[i]); + } + + fprintf (fh, "\n"); + fflush (fh); + + sfree (values); + + return (0); +} /* int handle_getval */ + +/* vim: set sw=2 sts=2 ts=8 : */ diff --git a/src/utils_cmd_getval.h b/src/utils_cmd_getval.h new file mode 100644 index 00000000..d7bd1151 --- /dev/null +++ b/src/utils_cmd_getval.h @@ -0,0 +1,29 @@ +/** + * collectd - src/utils_cms_getval.h + * Copyright (C) 2008 Florian octo Forster + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; only version 2 of the License is applicable. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: + * Florian octo Forster + **/ + +#ifndef UTILS_CMD_GETVAL_H +#define UTILS_CMD_GETVAL_H 1 + +int handle_getval (FILE *fh, char **fields, int fields_num); + +#endif /* UTILS_CMD_GETVAL_H */ + +/* vim: set sw=2 sts=2 ts=8 : */