summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 7747e2a)
raw | patch | inline | side by side (parent: 7747e2a)
author | Sebastian Harl <sh@tokkee.org> | |
Sun, 5 Jun 2016 15:50:58 +0000 (17:50 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Sun, 25 Sep 2016 10:42:45 +0000 (12:42 +0200) |
That is, implement the generic interface for FLUSH and switch the FLUSH
implementation to use the generic interface.
implementation to use the generic interface.
diff --git a/src/unixsock.c b/src/unixsock.c
index c8d1950a471c3c07486692f14611968c40afc933..3098aa83e343d95634c34d8ed33e46b4459fe670 100644 (file)
--- a/src/unixsock.c
+++ b/src/unixsock.c
}
else if (strcasecmp (fields[0], "flush") == 0)
{
- handle_flush (fhout, buffer);
+ cmd_handle_flush (fhout, buffer);
}
else
{
diff --git a/src/utils_cmd_flush.c b/src/utils_cmd_flush.c
index 1876150f38a0f6155fca7de383b86a49247f3c1d..cfd6a48182677932d099abb669974df64dc0fba8 100644 (file)
--- a/src/utils_cmd_flush.c
+++ b/src/utils_cmd_flush.c
/**
* collectd - src/utils_cmd_flush.c
- * Copyright (C) 2008 Sebastian Harl
+ * Copyright (C) 2008, 2016 Sebastian Harl
* Copyright (C) 2008 Florian Forster
*
* Permission is hereby granted, free of charge, to any person obtaining a
#include "utils_parse_option.h"
#include "utils_cmd_flush.h"
-int handle_flush (FILE *fh, char *buffer)
+cmd_status_t cmd_parse_flush (size_t argc, char **argv,
+ cmd_flush_t *ret_flush, cmd_error_handler_t *err)
{
- int success = 0;
- int error = 0;
-
- double timeout = 0.0;
- char **plugins = NULL;
- size_t plugins_num = 0;
- char **identifiers = NULL;
- size_t identifiers_num = 0;
-
-#define PRINT_TO_SOCK(fh, ...) \
- do { \
- if (fprintf (fh, __VA_ARGS__) < 0) { \
- char errbuf[1024]; \
- WARNING ("handle_flush: failed to write to socket #%i: %s", \
- fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
- strarray_free (plugins, plugins_num); \
- strarray_free (identifiers, identifiers_num); \
- return -1; \
- } \
- fflush(fh); \
- } while (0)
-
- if ((fh == NULL) || (buffer == NULL))
- return (-1);
-
- DEBUG ("utils_cmd_flush: handle_flush (fh = %p, buffer = %s);",
- (void *) fh, buffer);
-
- if (strncasecmp ("FLUSH", buffer, strlen ("FLUSH")) != 0)
+ if (ret_flush == NULL)
{
- PRINT_TO_SOCK (fh, "-1 Cannot parse command.\n");
- return (-1);
+ errno = EINVAL;
+ cmd_error (CMD_ERROR, err, "Invalid arguments to cmd_parse_flush.");
+ return (CMD_ERROR);
}
- buffer += strlen ("FLUSH");
- while (*buffer != 0)
+ for (size_t i = 0; i < argc; i++)
{
char *opt_key;
char *opt_value;
opt_key = NULL;
opt_value = NULL;
- status = parse_option (&buffer, &opt_key, &opt_value);
+ status = cmd_parse_option (argv[i], &opt_key, &opt_value, err);
if (status != 0)
{
- PRINT_TO_SOCK (fh, "-1 Parsing options failed.\n");
- strarray_free (plugins, plugins_num);
- strarray_free (identifiers, identifiers_num);
- return (-1);
+ if (status == CMD_NO_OPTION)
+ cmd_error (CMD_PARSE_ERROR, err,
+ "Invalid option string `%s'.", argv[i]);
+ cmd_destroy_flush (ret_flush);
+ return (CMD_PARSE_ERROR);
}
if (strcasecmp ("plugin", opt_key) == 0)
- strarray_add (&plugins, &plugins_num, opt_value);
+ {
+ strarray_add (&ret_flush->plugins, &ret_flush->plugins_num,
+ opt_value);
+ }
else if (strcasecmp ("identifier", opt_key) == 0)
- strarray_add (&identifiers, &identifiers_num, opt_value);
+ {
+ identifier_t *id = realloc (ret_flush->identifiers,
+ (ret_flush->identifiers_num + 1) * sizeof (*id));
+ if (id == NULL)
+ {
+ cmd_error (CMD_ERROR, err, "realloc failed.");
+ cmd_destroy_flush (ret_flush);
+ return (CMD_ERROR);
+ }
+
+ ret_flush->identifiers = id;
+ id = ret_flush->identifiers + ret_flush->identifiers_num;
+ ret_flush->identifiers_num++;
+ if (parse_identifier (opt_value,
+ &id->host, &id->plugin, &id->plugin_instance,
+ &id->type, &id->type_instance) != 0)
+ {
+ cmd_error (CMD_PARSE_ERROR, err,
+ "Invalid identifier `%s'.", opt_value);
+ cmd_destroy_flush (ret_flush);
+ return (CMD_PARSE_ERROR);
+ }
+ }
else if (strcasecmp ("timeout", opt_key) == 0)
{
char *endptr;
errno = 0;
endptr = NULL;
- timeout = strtod (opt_value, &endptr);
+ ret_flush->timeout = strtod (opt_value, &endptr);
- if ((endptr == opt_value) || (errno != 0) || (!isfinite (timeout)))
+ if ((endptr == opt_value) || (errno != 0)
+ || (!isfinite (ret_flush->timeout)))
{
- PRINT_TO_SOCK (fh, "-1 Invalid value for option `timeout': "
- "%s\n", opt_value);
- strarray_free (plugins, plugins_num);
- strarray_free (identifiers, identifiers_num);
- return (-1);
+ cmd_error (CMD_PARSE_ERROR, err,
+ "Invalid value for option `timeout': %s",
+ opt_value);
+ cmd_destroy_flush (ret_flush);
+ return (CMD_PARSE_ERROR);
}
- else if (timeout < 0.0)
+ else if (ret_flush->timeout < 0.0)
{
- timeout = 0.0;
+ ret_flush->timeout = 0.0;
}
}
else
{
- PRINT_TO_SOCK (fh, "-1 Cannot parse option %s\n", opt_key);
- strarray_free (plugins, plugins_num);
- strarray_free (identifiers, identifiers_num);
- return (-1);
+ cmd_error (CMD_PARSE_ERROR, err,
+ "Cannot parse option `%s'.", opt_key);
+ cmd_destroy_flush (ret_flush);
+ return (CMD_PARSE_ERROR);
}
- } /* while (*buffer != 0) */
+ }
+
+ return (CMD_OK);
+} /* cmd_status_t cmd_parse_flush */
- for (size_t i = 0; (i == 0) || (i < plugins_num); i++)
+cmd_status_t cmd_handle_flush (FILE *fh, char *buffer)
+{
+ cmd_error_handler_t err = { cmd_error_fh, fh };
+ cmd_t cmd;
+
+ int success = 0;
+ int error = 0;
+ int status;
+
+ size_t i;
+
+ if ((fh == NULL) || (buffer == NULL))
+ return (-1);
+
+ DEBUG ("utils_cmd_flush: cmd_handle_flush (fh = %p, buffer = %s);",
+ (void *) fh, buffer);
+
+ if ((status = cmd_parse (buffer, &cmd, &err)) != CMD_OK)
+ return (status);
+ if (cmd.type != CMD_FLUSH)
+ {
+ cmd_error (CMD_UNKNOWN_COMMAND, &err, "Unexpected command: `%s'.",
+ CMD_TO_STRING (cmd.type));
+ cmd_destroy (&cmd);
+ return (CMD_UNKNOWN_COMMAND);
+ }
+
+ for (i = 0; (i == 0) || (i < cmd.cmd.flush.plugins_num); i++)
{
char *plugin = NULL;
- if (plugins_num != 0)
- plugin = plugins[i];
+ if (cmd.cmd.flush.plugins_num != 0)
+ plugin = cmd.cmd.flush.plugins[i];
- for (size_t j = 0; (j == 0) || (j < identifiers_num); j++)
+ for (size_t j = 0; (j == 0) || (j < cmd.cmd.flush.identifiers_num); j++)
{
char *identifier = NULL;
+ char buffer[1024];
int status;
- if (identifiers_num != 0)
- identifier = identifiers[j];
+ if (cmd.cmd.flush.identifiers_num != 0)
+ {
+ identifier_t *id = cmd.cmd.flush.identifiers + j;
+ if (format_name (buffer, sizeof (buffer),
+ id->host, id->plugin, id->plugin_instance,
+ id->type, id->type_instance) != 0)
+ {
+ error++;
+ continue;
+ }
+ identifier = buffer;
+ }
status = plugin_flush (plugin,
- DOUBLE_TO_CDTIME_T (timeout),
+ DOUBLE_TO_CDTIME_T (cmd.cmd.flush.timeout),
identifier);
if (status == 0)
success++;
}
}
- PRINT_TO_SOCK (fh, "0 Done: %i successful, %i errors\n",
+ cmd_error (CMD_OK, &err, "Done: %i successful, %i errors",
success, error);
- strarray_free (plugins, plugins_num);
- strarray_free (identifiers, identifiers_num);
+ cmd_destroy (&cmd);
return (0);
#undef PRINT_TO_SOCK
-} /* int handle_flush */
+} /* cmd_status_t cmd_handle_flush */
+
+void cmd_destroy_flush (cmd_flush_t *flush)
+{
+ if (flush == NULL)
+ return;
+
+ strarray_free (flush->plugins, flush->plugins_num);
+ flush->plugins = NULL;
+ flush->plugins_num = 0;
+
+ sfree (flush->identifiers);
+ flush->identifiers_num = 0;
+} /* void cmd_destroy_flush */
/* vim: set sw=4 ts=4 tw=78 noexpandtab : */
diff --git a/src/utils_cmd_flush.h b/src/utils_cmd_flush.h
index f43b25726749b20348f9c4ae641bb223dc603c5a..2afa3c711dfa18c1b60182c8201e237cdd9a9e95 100644 (file)
--- a/src/utils_cmd_flush.h
+++ b/src/utils_cmd_flush.h
/**
* collectd - src/utils_cmd_flush.h
- * Copyright (C) 2008 Sebastian Harl
+ * Copyright (C) 2008, 2016 Sebastian Harl
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
#include <stdio.h>
-int handle_flush (FILE *fh, char *buffer);
+#include "utils_cmds.h"
+
+cmd_status_t cmd_parse_flush (size_t argc, char **argv,
+ cmd_flush_t *ret_flush, cmd_error_handler_t *err);
+
+cmd_status_t cmd_handle_flush (FILE *fh, char *buffer);
+
+void cmd_destroy_flush (cmd_flush_t *flush);
#endif /* UTILS_CMD_FLUSH_H */
diff --git a/src/utils_cmd_putval.h b/src/utils_cmd_putval.h
index cd62a1b47c01173efc7a404b977dfdaed525ffdd..363194dbac1a8c203d78ac6e2fc54cca6eaf25e1 100644 (file)
--- a/src/utils_cmd_putval.h
+++ b/src/utils_cmd_putval.h
#include "utils_cmds.h"
cmd_status_t cmd_parse_putval (size_t argc, char **argv,
- cmd_putval_t *ret_cmd, cmd_error_handler_t *err);
+ cmd_putval_t *ret_putval, cmd_error_handler_t *err);
cmd_status_t cmd_handle_putval (FILE *fh, char *buffer);
diff --git a/src/utils_cmds.c b/src/utils_cmds.c
index 3bc260b4533ca181b8fd38af2f597abb879658c0..f5494a4c624d8acd3b7273f8e2558792eede4014 100644 (file)
--- a/src/utils_cmds.c
+++ b/src/utils_cmds.c
**/
#include "utils_cmds.h"
+#include "utils_cmd_flush.h"
#include "utils_cmd_putval.h"
#include "utils_parse_option.h"
#include "daemon/common.h"
memset (ret_cmd, 0, sizeof (*ret_cmd));
command = argv[0];
- if (strcasecmp ("PUTVAL", command) == 0)
+ if (strcasecmp ("FLUSH", command) == 0)
+ {
+ ret_cmd->type = CMD_FLUSH;
+ return cmd_parse_flush (argc - 1, argv + 1,
+ &ret_cmd->cmd.flush, err);
+ }
+ else if (strcasecmp ("PUTVAL", command) == 0)
{
ret_cmd->type = CMD_PUTVAL;
return cmd_parse_putval (argc - 1, argv + 1,
case CMD_UNKNOWN:
/* nothing to do */
break;
+ case CMD_FLUSH:
+ cmd_destroy_flush (&cmd->cmd.flush);
+ break;
case CMD_PUTVAL:
cmd_destroy_putval (&cmd->cmd.putval);
break;
diff --git a/src/utils_cmds.h b/src/utils_cmds.h
index 7dc80fa241183710d15cf84614a77cf30c88e3a1..21564cb855da87049640cc67dd0d1954c9f6b8fe 100644 (file)
--- a/src/utils_cmds.h
+++ b/src/utils_cmds.h
typedef enum {
CMD_UNKNOWN = 0,
- CMD_PUTVAL = 1,
+ CMD_FLUSH = 1,
+ CMD_PUTVAL = 2,
} cmd_type_t;
#define CMD_TO_STRING(type) \
- ((type) == CMD_PUTVAL) ? "PUTVAL" \
+ ((type) == CMD_FLUSH) ? "FLUSH" \
+ : ((type) == CMD_PUTVAL) ? "PUTVAL" \
: "UNKNOWN"
+typedef struct {
+ double timeout;
+
+ char **plugins;
+ size_t plugins_num;
+ identifier_t *identifiers;
+ size_t identifiers_num;
+} cmd_flush_t;
+
typedef struct {
/* The raw identifier as provided by the user. */
char *identifier;
typedef struct {
cmd_type_t type;
union {
+ cmd_flush_t flush;
cmd_putval_t putval;
} cmd;
} cmd_t;