On Mon, 2012-01-02 at 16:55 +0100, Gergely Nagy wrote:
This patch adds a new template function: $(UUID) (also available as $(uuid)), which can be used to generate Universally Unique IDentifiers.
The use case for this is to add an UUID to each message as soon as they're received, so that they can be identified later, accross destinations. This allows me to pre-generate an ID for messages going into mongodb, and send those messages to a different destination too, with the same ID included.
Use it like this:
rewrite r_add_uuid { set("$(UUID)" value("UUID")); }; log default { source(s_network); rewrite(r_add_uuid); destination(d_mongo); destination(d_program); };
That's a great idea, see my comments on the implementation inline.
The module uses the widely available libuuid library to generate the UUIDs.
Signed-off-by: Gergely Nagy <algernon@balabit.hu> --- configure.in | 16 ++++++++++- modules/Makefile.am | 2 +- modules/tfuuid/Makefile.am | 11 ++++++++ modules/tfuuid/tfuuid.c | 62 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 modules/tfuuid/Makefile.am create mode 100644 modules/tfuuid/tfuuid.c
diff --git a/configure.in b/configure.in index 734d703..83b81ac 100644 --- a/configure.in +++ b/configure.in +if test "x$enable_uuid" = "xyes" || test "x$enable_uuid" = "xauto"; then + PKG_CHECK_MODULES(UUID, uuid, with_uuid="yes", with_uuid="no") + if test "x$with_uuid" = "xno" && test "x$enable_uuid" = "xyes"; then + AC_MSG_ERROR([Could not find libuuid, and uuid support was explicitly enabled.]) + fi + enable_uuid="$with_uuid" +fi +
If possible I'd avoid using another library, there'd a random based UUID generator function in modules/dbparser/patternize.c Can you check if libuuid does anything more than that?
diff --git a/modules/tfuuid/tfuuid.c b/modules/tfuuid/tfuuid.c new file mode 100644 index 0000000..c426cb8 --- /dev/null +++ b/modules/tfuuid/tfuuid.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2012 BalaBit IT Ltd, Budapest, Hungary + * Copyright (c) 2012 Gergely Nagy <algernon@balabit.hu> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "plugin.h" +#include "templates.h" +#include "cfg.h" + +#include "config.h" + +#include <uuid/uuid.h> + +static void +tf_uuid(LogMessage *msg, gint argc, GString *argv[], GString *result) +{ + uuid_t uuid; + gint i; + + uuid_generate (uuid); + + for (i = 0; i < sizeof (uuid); i++) + g_string_append_printf (result, "%02x", uuid[i]); +}
hmm... UUID's have a well defined format (with some dashes) and I'd prefer to use that, if the function is called $(uuid). Here's the uuid generator function in patternize.c, could probably be moved to the lib directory (perhaps lib/uuid.c). I wrote that, so relicencing it to LGPL wouldn't be a problem. static void uuid_gen_random(gchar *buf, gsize buflen) { union { struct { guint32 time_low; guint16 time_mid; guint16 time_hi_and_version; guint8 clk_seq_hi_res; guint8 clk_seq_low; guint8 node[6]; guint16 node_low; guint32 node_hi; }; guchar __rnd[16]; } uuid; RAND_bytes(uuid.__rnd, sizeof(uuid)); uuid.clk_seq_hi_res = (uuid.clk_seq_hi_res & ~0xC0) | 0x80; uuid.time_hi_and_version = htons((uuid.time_hi_and_version & ~0xF000) | 0x4000); g_snprintf(buf, buflen, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", uuid.time_low, uuid.time_mid, uuid.time_hi_and_version, uuid.clk_seq_hi_res, uuid.clk_seq_low, uuid.node[0], uuid.node[1], uuid.node[2], uuid.node[3], uuid.node[4], uuid.node[5]); }
+ +TEMPLATE_FUNCTION_SIMPLE(tf_uuid); + +static Plugin tfuuid_plugins[] = + { + TEMPLATE_FUNCTION_PLUGIN(tf_uuid, "UUID"), + TEMPLATE_FUNCTION_PLUGIN(tf_uuid, "uuid"), + };
why use two names? I'd prefer the lower case one. -- Bazsi