{GPL, other}: Relicense to MIT license.
[collectd.git] / src / utils_cmd_putval.c
index 98b5043..366b413 100644 (file)
 /**
- * collectd - src/utils_cms_putval.c
- * Copyright (C) 2007  Florian octo Forster
+ * collectd - src/utils_cmd_putval.c
+ * Copyright (C) 2007-2009  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.
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
  *
- * 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.
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
  *
- * 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
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
  *
- * Author:
- *   Florian octo Forster <octo at verplant.org>
+ * Authors:
+ *   Florian octo Forster <octo at collectd.org>
  **/
 
 #include "collectd.h"
 #include "common.h"
 #include "plugin.h"
 
-static int parse_value (const data_set_t *ds, value_list_t *vl,
-               const char *type,
-               FILE *fh, char *buffer)
-{
-       char *dummy;
-       char *ptr;
-       char *saveptr;
-       int i;
-
-       char *time_str = buffer;
-       char *value_str = strchr (time_str, ':');
-       if (value_str == NULL)
-       {
-               fprintf (fh, "-1 No time found.\n");
-               return (-1);
-       }
-       *value_str = '\0'; value_str++;
-
-       vl->time = (time_t) atoi (time_str);
-       if (vl->time == 0)
-               vl->time = time (NULL);
+#include "utils_parse_option.h"
 
-       i = 0;
-       dummy = value_str;
-       saveptr = NULL;
-       while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
-       {
-               dummy = NULL;
-
-               if (i >= vl->values_len)
-               {
-                       i = vl->values_len + 1;
-                       break;
-               }
-
-               if (strcmp (ptr, "U") == 0)
-                       vl->values[i].gauge = NAN;
-               else if (ds->ds[i].type == DS_TYPE_COUNTER)
-                       vl->values[i].counter = atoll (ptr);
-               else if (ds->ds[i].type == DS_TYPE_GAUGE)
-                       vl->values[i].gauge = atof (ptr);
+#define print_to_socket(fh, ...) \
+       if (fprintf (fh, __VA_ARGS__) < 0) { \
+               char errbuf[1024]; \
+               WARNING ("handle_putval: failed to write to socket #%i: %s", \
+                               fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
+               return -1; \
+       }
 
-               i++;
-       } /* while (strtok_r) */
+static int dispatch_values (const data_set_t *ds, value_list_t *vl,
+               FILE *fh, char *buffer)
+{
+       int status;
 
-       if (i != vl->values_len)
+       status = parse_values (buffer, vl, ds);
+       if (status != 0)
        {
-               char identifier[128];
-               FORMAT_VL (identifier, sizeof (identifier), vl, ds);
-               ERROR ("cmd putval: parse_value: "
-                               "Number of values incorrect: "
-                               "Got %i, expected %i. Identifier is `%s'.",
-                               i, vl->values_len, identifier);
-               fprintf (fh, "-1 Number of values incorrect: "
-                               "Got %i, expected %i.\n",
-                               i, vl->values_len);
+               print_to_socket (fh, "-1 Parsing the values string failed.\n");
                return (-1);
        }
 
-       plugin_dispatch_values (type, vl);
+       plugin_dispatch_values (vl);
        return (0);
-} /* int parse_value */
+} /* int dispatch_values */
 
-static int parse_option (value_list_t *vl, char *buffer)
+static int set_option (value_list_t *vl, const char *key, const char *value)
 {
-       char *option = buffer;
-       char *value;
-
-       if ((vl == NULL) || (option == NULL))
+       if ((vl == NULL) || (key == NULL) || (value == NULL))
                return (-1);
 
-       value = strchr (option, '=');
-       if (value == NULL)
-               return (-1);
-       *value = '\0'; value++;
-
-       if (strcasecmp ("interval", option) == 0)
+       if (strcasecmp ("interval", key) == 0)
        {
-               vl->interval = atoi (value);
-               if (vl->interval <= 0)
-                       vl->interval = interval_g;
+               double tmp;
+               char *endptr;
+
+               endptr = NULL;
+               errno = 0;
+               tmp = strtod (value, &endptr);
+
+               if ((errno == 0) && (endptr != NULL)
+                               && (endptr != value) && (tmp > 0.0))
+                       vl->interval = DOUBLE_TO_CDTIME_T (tmp);
        }
        else
                return (1);
@@ -111,44 +78,63 @@ static int parse_option (value_list_t *vl, char *buffer)
        return (0);
 } /* int parse_option */
 
-int handle_putval (FILE *fh, char **fields, int fields_num)
+int handle_putval (FILE *fh, char *buffer)
 {
+       char *command;
+       char *identifier;
        char *hostname;
        char *plugin;
        char *plugin_instance;
        char *type;
        char *type_instance;
        int   status;
-       int   i;
+       int   values_submitted;
 
        char *identifier_copy;
 
        const data_set_t *ds;
        value_list_t vl = VALUE_LIST_INIT;
 
-       if (fields_num < 3)
+       DEBUG ("utils_cmd_putval: handle_putval (fh = %p, buffer = %s);",
+                       (void *) fh, buffer);
+
+       command = NULL;
+       status = parse_string (&buffer, &command);
+       if (status != 0)
        {
-               DEBUG ("cmd putval: Wrong number of fields: %i",
-                               fields_num);
-               fprintf (fh, "-1 Wrong number of fields: Got %i, "
-                               "expected at least 3.\n",
-                               fields_num);
-               fflush (fh);
+               print_to_socket (fh, "-1 Cannot parse command.\n");
                return (-1);
        }
+       assert (command != NULL);
+
+       if (strcasecmp ("PUTVAL", command) != 0)
+       {
+               print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
+               return (-1);
+       }
+
+       identifier = NULL;
+       status = parse_string (&buffer, &identifier);
+       if (status != 0)
+       {
+               print_to_socket (fh, "-1 Cannot parse identifier.\n");
+               return (-1);
+       }
+       assert (identifier != NULL);
 
        /* parse_identifier() modifies its first argument,
         * returning pointers into it */
-       identifier_copy = sstrdup (fields[1]);
+       identifier_copy = sstrdup (identifier);
 
        status = parse_identifier (identifier_copy, &hostname,
                        &plugin, &plugin_instance,
                        &type, &type_instance);
        if (status != 0)
        {
-               DEBUG ("cmd putval: Cannot parse `%s'", fields[1]);
-               fprintf (fh, "-1 Cannot parse identifier.\n");
-               fflush (fh);
+               DEBUG ("handle_putval: Cannot parse identifier `%s'.",
+                               identifier);
+               print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n",
+                               identifier);
                sfree (identifier_copy);
                return (-1);
        }
@@ -160,72 +146,117 @@ int handle_putval (FILE *fh, char **fields, int fields_num)
                        || ((type_instance != NULL)
                                && (strlen (type_instance) >= sizeof (vl.type_instance))))
        {
-               fprintf (fh, "-1 Identifier too long.\n");
-               fflush (fh);
+               print_to_socket (fh, "-1 Identifier too long.\n");
                sfree (identifier_copy);
                return (-1);
        }
 
-       strcpy (vl.host, hostname);
-       strcpy (vl.plugin, plugin);
+       sstrncpy (vl.host, hostname, sizeof (vl.host));
+       sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
+       sstrncpy (vl.type, type, sizeof (vl.type));
        if (plugin_instance != NULL)
-               strcpy (vl.plugin_instance, plugin_instance);
+               sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
        if (type_instance != NULL)
-               strcpy (vl.type_instance, type_instance);
+               sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
        ds = plugin_get_ds (type);
        if (ds == NULL) {
+               print_to_socket (fh, "-1 Type `%s' isn't defined.\n", type);
                sfree (identifier_copy);
                return (-1);
        }
 
+       /* Free identifier_copy */
+       hostname = NULL;
+       plugin = NULL; plugin_instance = NULL;
+       type = NULL;   type_instance = NULL;
+       sfree (identifier_copy);
+
        vl.values_len = ds->ds_num;
        vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
        if (vl.values == NULL)
        {
-               fprintf (fh, "-1 malloc failed.\n");
-               fflush (fh);
-               sfree (identifier_copy);
+               print_to_socket (fh, "-1 malloc failed.\n");
                return (-1);
        }
 
        /* All the remaining fields are part of the optionlist. */
-       for (i = 2; i < fields_num; i++)
+       values_submitted = 0;
+       while (*buffer != 0)
        {
-               if (strchr (fields[i], ':') != NULL)
+               char *string = NULL;
+               char *value  = NULL;
+
+               status = parse_option (&buffer, &string, &value);
+               if (status < 0)
                {
-                       /* It's parse_value's job to write an error to `fh'.
-                        * This is not the case with `parse_option below.
-                        * Neither will write an success message. */
-                       if (parse_value (ds, &vl, type, fh, fields[i]) != 0)
-                               break;
+                       /* parse_option failed, buffer has been modified.
+                        * => we need to abort */
+                       print_to_socket (fh, "-1 Misformatted option.\n");
+                       return (-1);
                }
-               else if (strchr (fields[i], '=') != NULL)
+               else if (status == 0)
                {
-                       if (parse_option (&vl, fields[i]) != 0)
-                       {
-                               fprintf (fh, "-1 Error parsing option `%s'\n",
-                                               fields[i]);
-                               break;
-                       }
+                       assert (string != NULL);
+                       assert (value != NULL);
+                       set_option (&vl, string, value);
+                       continue;
                }
-               else
+               /* else: parse_option but buffer has not been modified. This is
+                * the default if no `=' is found.. */
+
+               status = parse_string (&buffer, &string);
+               if (status != 0)
                {
-                       WARNING ("cmd putval: handle_putval: "
-                                       "Cannot parse field #%i `%s'; "
-                                       "Ignoring it.\n",
-                                       i, fields[i]);
+                       print_to_socket (fh, "-1 Misformatted value.\n");
+                       return (-1);
                }
-       }
+               assert (string != NULL);
+
+               status = dispatch_values (ds, &vl, fh, string);
+               if (status != 0)
+               {
+                       /* An error has already been printed. */
+                       return (-1);
+               }
+               values_submitted++;
+       } /* while (*buffer != 0) */
        /* Done parsing the options. */
 
-       if (i == fields_num)
-               fprintf (fh, "0 Success\n");
-       fflush (fh);
+       print_to_socket (fh, "0 Success: %i %s been dispatched.\n",
+                       values_submitted,
+                       (values_submitted == 1) ? "value has" : "values have");
 
        sfree (vl.values); 
-       sfree (identifier_copy);
 
        return (0);
 } /* int handle_putval */
 
+int create_putval (char *ret, size_t ret_len, /* {{{ */
+       const data_set_t *ds, const value_list_t *vl)
+{
+       char buffer_ident[6 * DATA_MAX_NAME_LEN];
+       char buffer_values[1024];
+       int status;
+
+       status = FORMAT_VL (buffer_ident, sizeof (buffer_ident), vl);
+       if (status != 0)
+               return (status);
+       escape_string (buffer_ident, sizeof (buffer_ident));
+
+       status = format_values (buffer_values, sizeof (buffer_values),
+                       ds, vl, /* store rates = */ 0);
+       if (status != 0)
+               return (status);
+       escape_string (buffer_values, sizeof (buffer_values));
+
+       ssnprintf (ret, ret_len,
+                       "PUTVAL %s interval=%.3f %s",
+                       buffer_ident,
+                       (vl->interval > 0)
+                       ? CDTIME_T_TO_DOUBLE (vl->interval)
+                       : CDTIME_T_TO_DOUBLE (plugin_get_interval ()),
+                       buffer_values);
+
+       return (0);
+} /* }}} int create_putval */