summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: c9a388f)
raw | patch | inline | side by side (parent: c9a388f)
author | Florian Forster <octo@noris.net> | |
Wed, 27 Aug 2008 13:49:09 +0000 (15:49 +0200) | ||
committer | Florian Forster <octo@noris.net> | |
Wed, 27 Aug 2008 13:49:09 +0000 (15:49 +0200) |
index d4499246cdf2497a5c6b01f39387a38c9e95adcd..c13622127cc431a64209df253bdd147769934b04 100644 (file)
return ($ident);
} # _parse_identifier
+sub _escape_argument
+{
+ my $string = shift;
+
+ if ($string =~ m/^\w+$/)
+ {
+ return ("$string");
+ }
+
+ $string =~ s#\\#\\\\#g;
+ $string =~ s#"#\\"#g;
+ $string = "\"$string\"";
+
+ return ($string);
+}
+
=head1 PUBLIC METHODS
=over 4
my $ret = {};
$identifier = _create_identifier (\%args) or return;
- if ($identifier =~ m/[\s"]/)
- {
- $identifier =~ s#\\#\\\\#g;
- $identifier =~ s#"#\\"#g;
- $identifier = "\"$identifier\"";
- }
- $msg = "GETVAL $identifier\n";
+ $msg = 'GETVAL ' . _escape_argument ($identifier) . "\n";
#print "-> $msg";
send ($fh, $msg, 0) or confess ("send: $!");
if (defined $args{'interval'})
{
- $interval = ' interval=' . $args{'interval'};
+ $interval = ' interval='
+ . _escape_argument ($args{'interval'});
}
$identifier = _create_identifier (\%args) or return;
$values = join (':', $time, map { defined ($_) ? $_ : 'U' } (@{$args{'values'}}));
}
- $msg = "PUTVAL $identifier$interval $values\n";
+ $msg = 'PUTVAL '
+ . _escape_argument ($identifier)
+ . $interval
+ . ' ' . _escape_argument ($values) . "\n";
#print "-> $msg";
send ($fh, $msg, 0) or confess ("send: $!");
$msg = undef;
{
return;
}
- if ($ident_str =~ m/[\s"]/)
- {
- $ident_str =~ s#\\#\\\\#g;
- $ident_str =~ s#"#\\"#g;
- $ident_str = "\"$ident_str\"";
- }
- $msg .= " identifier=$ident_str";
+ $msg .= ' identifier=' . _escape_argument ($ident_str);
}
}
diff --git a/src/exec.c b/src/exec.c
index a80de1e7a321b452b0971e2263ab0c1be9c2abf2..88026b662a0be208daa58b24c418357a1c63399a 100644 (file)
--- a/src/exec.c
+++ b/src/exec.c
@@ -490,19 +490,22 @@ static int fork_child (program_list_t *pl, int *fd_in, int *fd_out, int *fd_err)
static int parse_line (char *buffer) /* {{{ */
{
- char *fields[256];
- int fields_num;
-
- fields[0] = "PUTVAL";
- fields_num = strsplit (buffer, fields + 1, STATIC_ARRAY_SIZE(fields) - 1);
-
- if (strcasecmp (fields[1], "putval") == 0)
- return (handle_putval (stdout, fields + 1, fields_num));
+ if (strncasecmp ("PUTVAL", buffer, strlen ("PUTVAL")) == 0)
+ return (handle_putval (stdout, buffer));
+#if !COLLECT_DEBUG
+#error "TODO: PUTNOTIF"
else if (strcasecmp (fields[1], "putnotif") == 0)
return (handle_putnotif (stdout, fields + 1, fields_num));
-
- /* compatibility code */
- return (handle_putval (stdout, fields, fields_num + 1));
+#endif
+ else
+ {
+ /* For backwards compatibility */
+ char tmp[1220];
+ /* Let's annoy the user a bit.. */
+ INFO ("exec plugin: Prepending `PUTVAL' to this line: %s", buffer);
+ ssnprintf (tmp, sizeof (tmp), "PUTVAL %s", buffer);
+ return (handle_putval (stdout, tmp));
+ }
} /* int parse_line }}} */
static void *exec_read_one (void *arg) /* {{{ */
diff --git a/src/unixsock.c b/src/unixsock.c
index db75809432dc3542161a00d392a2af398d183ca2..6a8628a55eaf8fa3dbae2c152c1e1ac461053668 100644 (file)
--- a/src/unixsock.c
+++ b/src/unixsock.c
}
else if (strcasecmp (fields[0], "putval") == 0)
{
- handle_putval (fhout, fields, fields_num);
+ handle_putval (fhout, buffer);
}
else if (strcasecmp (fields[0], "listval") == 0)
{
diff --git a/src/utils_cmd_getval.c b/src/utils_cmd_getval.c
index 33b40e09af7532a951d2611f83fe9fe2493360ee..186ef9b56d60b6385fde3336316bc0462008c474 100644 (file)
--- a/src/utils_cmd_getval.c
+++ b/src/utils_cmd_getval.c
}
assert (identifier != NULL);
+ if (*buffer != 0)
+ {
+ print_to_socket (fh, "-1 Garbage after end of command: %s\n", buffer);
+ return (-1);
+ }
+
/* parse_identifier() modifies its first argument,
* returning pointers into it */
identifier_copy = sstrdup (identifier);
&type, &type_instance);
if (status != 0)
{
- DEBUG ("unixsock plugin: Cannot parse identifier `%s'.", identifier);
+ DEBUG ("handle_getval: Cannot parse identifier `%s'.", identifier);
print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n", identifier);
sfree (identifier_copy);
return (-1);
ds = plugin_get_ds (type);
if (ds == NULL)
{
- DEBUG ("unixsock plugin: plugin_get_ds (%s) == NULL;", type);
+ DEBUG ("handle_getval: plugin_get_ds (%s) == NULL;", type);
print_to_socket (fh, "-1 Type `%s' is unknown.\n", type);
sfree (identifier_copy);
return (-1);
diff --git a/src/utils_cmd_putval.c b/src/utils_cmd_putval.c
index 9cbba126f48496c5750d623dc6cd5fe8fd0cdccf..91b70160f063590e477cd415253e38d1099abc1b 100644 (file)
--- a/src/utils_cmd_putval.c
+++ b/src/utils_cmd_putval.c
/**
* collectd - src/utils_cms_putval.c
- * Copyright (C) 2007 Florian octo Forster
+ * Copyright (C) 2007,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
#include "common.h"
#include "plugin.h"
+#include "utils_parse_option.h"
+
#define print_to_socket(fh, ...) \
if (fprintf (fh, __VA_ARGS__) < 0) { \
char errbuf[1024]; \
return (0);
} /* int parse_value */
-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))
- return (-1);
-
- value = strchr (option, '=');
- if (value == NULL)
+ if ((vl == NULL) || (key == NULL) || (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;
+ int tmp;
+ char *endptr;
+
+ endptr = NULL;
+ errno = 0;
+ tmp = strtol (value, &endptr, 0);
+
+ if ((errno == 0) && (endptr != NULL)
+ && (endptr != value) && (tmp > 0))
+ vl->interval = tmp;
}
else
return (1);
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);
- print_to_socket (fh, "-1 Wrong number of fields: Got %i, "
- "expected at least 3.\n",
- fields_num);
+ 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]);
- print_to_socket (fh, "-1 Cannot parse identifier.\n");
+ DEBUG ("handle_putval: Cannot parse identifier `%s'.",
+ identifier);
+ print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n",
+ identifier);
sfree (identifier_copy);
return (-1);
}
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)
{
print_to_socket (fh, "-1 malloc failed.\n");
- sfree (identifier_copy);
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, 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)
- {
- print_to_socket (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 = parse_value (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)
- print_to_socket (fh, "0 Success\n");
+ 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 */
diff --git a/src/utils_cmd_putval.h b/src/utils_cmd_putval.h
index 2ae45323eed7f2581b6a4d5cec489247ba39c4af..9ba52cc8d80f8a98b54dfaf25755179e9594849a 100644 (file)
--- a/src/utils_cmd_putval.h
+++ b/src/utils_cmd_putval.h
#ifndef UTILS_CMD_PUTVAL_H
#define UTILS_CMD_PUTVAL_H 1
-int handle_putval (FILE *fh, char **fields, int fields_num);
+int handle_putval (FILE *fh, char *buffer);
#endif /* UTILS_CMD_PUTVAL_H */