From: Florian Forster Date: Sun, 1 Feb 2009 14:46:27 +0000 (+0100) Subject: csv plugin: Make the output to STDOUT compatible to the exec plugin. X-Git-Tag: collectd-4.6.0~87 X-Git-Url: https://git.octo.it/?a=commitdiff_plain;h=0c75c6cd9bd52480856a9ccb8289e9b17d708c63;hp=55e53e154eeaf317b051202f3310d611949e6ff8;p=collectd.git csv plugin: Make the output to STDOUT compatible to the exec plugin. Probably not that useful, but the output written to STDOUT by the csv plugin is not in the form accepted by the exec plugin. This makes some potentially interesting hacks possible ;) --- diff --git a/src/Makefile.am b/src/Makefile.am index cd36deba..500b3895 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -37,6 +37,7 @@ collectd_SOURCES = collectd.c collectd.h \ utils_complain.c utils_complain.h \ utils_ignorelist.c utils_ignorelist.h \ utils_llist.c utils_llist.h \ + utils_parse_option.c utils_parse_option.h \ utils_tail_match.c utils_tail_match.h \ utils_match.c utils_match.h \ utils_mount.c utils_mount.h \ @@ -271,7 +272,6 @@ endif if BUILD_PLUGIN_EXEC pkglib_LTLIBRARIES += exec.la exec_la_SOURCES = exec.c \ - utils_parse_option.h utils_parse_option.c \ utils_cmd_putnotif.c utils_cmd_putnotif.h \ utils_cmd_putval.c utils_cmd_putval.h exec_la_LDFLAGS = -module -avoid-version @@ -817,7 +817,6 @@ endif if BUILD_PLUGIN_UNIXSOCK pkglib_LTLIBRARIES += unixsock.la unixsock_la_SOURCES = unixsock.c \ - utils_parse_option.h utils_parse_option.c \ utils_cmd_flush.h utils_cmd_flush.c \ utils_cmd_getval.h utils_cmd_getval.c \ utils_cmd_listval.h utils_cmd_listval.c \ diff --git a/src/csv.c b/src/csv.c index b5333b43..352557a8 100644 --- a/src/csv.c +++ b/src/csv.c @@ -1,6 +1,6 @@ /** * collectd - src/csv.c - * Copyright (C) 2007 Florian octo Forster + * 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 @@ -23,6 +23,7 @@ #include "plugin.h" #include "common.h" #include "utils_cache.h" +#include "utils_parse_option.h" /* * Private variables @@ -273,8 +274,22 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl) if (use_stdio) { + size_t i; + + escape_string (filename, sizeof (filename)); + + /* Replace commas by colons for PUTVAL compatible output. */ + for (i = 0; i < sizeof (values); i++) + { + if (values[i] == 0) + break; + else if (values[i] == ',') + values[i] = ':'; + } + fprintf (use_stdio == 1 ? stdout : stderr, - "%s=%s\n", filename, values); + "PUTVAL %s interval=%i %s\n", + filename, interval_g, values); return (0); } diff --git a/src/utils_parse_option.c b/src/utils_parse_option.c index d94c1400..2168cd1a 100644 --- a/src/utils_parse_option.c +++ b/src/utils_parse_option.c @@ -150,4 +150,55 @@ int parse_option (char **ret_buffer, char **ret_key, char **ret_value) return (0); } /* int parse_option */ +int escape_string (char *buffer, size_t buffer_size) +{ + char *temp; + size_t i; + size_t j; + + /* Check if we need to escape at all first */ + temp = strpbrk (buffer, " \t\"\\"); + if (temp == NULL) + return (0); + + temp = (char *) malloc (buffer_size); + if (temp == NULL) + return (-1); + memset (temp, 0, buffer_size); + + temp[0] = '"'; + j = 1; + + for (i = 0; i < buffer_size; i++) + { + if (buffer[i] == 0) + { + break; + } + else if ((buffer[i] == '"') || (buffer[i] == '\\')) + { + if (j > (buffer_size - 4)) + break; + temp[j] = '\\'; + temp[j + 1] = buffer[i]; + j += 2; + } + else + { + if (j > (buffer_size - 3)) + break; + temp[j] = buffer[i]; + j++; + } + } + + assert ((j + 1) < buffer_size); + temp[j] = '"'; + temp[j + 1] = 0; + + sstrncpy (buffer, temp, buffer_size); + sfree (temp); + return (0); +} /* int escape_string */ + /* vim: set sw=2 ts=8 tw=78 et : */ diff --git a/src/utils_parse_option.h b/src/utils_parse_option.h index cb7f6d32..1dfb3ae9 100644 --- a/src/utils_parse_option.h +++ b/src/utils_parse_option.h @@ -25,6 +25,8 @@ int parse_string (char **ret_buffer, char **ret_string); int parse_option (char **ret_buffer, char **ret_key, char **ret_value); +int escape_string (char *buffer, size_t buffer_size); + #endif /* UTILS_PARSE_OPTION */ /* vim: set sw=2 ts=8 tw=78 et : */