Merge branch 'ff/tail'
authorFlorian Forster <octo@huhu.verplant.org>
Sun, 24 Feb 2008 14:43:14 +0000 (15:43 +0100)
committerFlorian Forster <octo@huhu.verplant.org>
Sun, 24 Feb 2008 14:43:14 +0000 (15:43 +0100)
README
configure.in
src/Makefile.am
src/collectd.conf.pod
src/tail.c [new file with mode: 0644]
src/utils_logtail.c [deleted file]
src/utils_logtail.h [deleted file]
src/utils_match.c
src/utils_match.h
src/utils_tail_match.c [new file with mode: 0644]
src/utils_tail_match.h [new file with mode: 0644]

diff --git a/README b/README
index 8c0be1d..9dba7cd 100644 (file)
--- a/README
+++ b/README
@@ -156,6 +156,10 @@ Features
     - swap
       Pages swapped out onto harddisk or whatever is called `swap' by the OS..
 
+    - tail
+      Follows (tails) logfiles, parses them by lines and submits matched
+      values.
+
     - tape
       Bytes and operations read and written on tape devices. Solaris only.
 
index 69ee322..8f129cb 100644 (file)
@@ -2072,6 +2072,7 @@ AC_PLUGIN([serial],      [$plugin_serial],     [serial port traffic])
 AC_PLUGIN([snmp],        [$with_libnetsnmp],   [SNMP querying plugin])
 AC_PLUGIN([swap],        [$plugin_swap],       [Swap usage statistics])
 AC_PLUGIN([syslog],      [$have_syslog],       [Syslog logging plugin])
+AC_PLUGIN([tail],        [yes],                [Parsing of logfiles])
 AC_PLUGIN([tape],        [$plugin_tape],       [Tape drive statistics])
 AC_PLUGIN([tcpconns],    [$plugin_tcpconns],   [TCP connection statistics])
 AC_PLUGIN([unixsock],    [yes],                [Unixsock communication plugin])
index 30518b9..c3d0b1d 100644 (file)
@@ -30,7 +30,7 @@ collectd_SOURCES = collectd.c collectd.h \
                   utils_cache.c utils_cache.h \
                   utils_ignorelist.c utils_ignorelist.h \
                   utils_llist.c utils_llist.h \
-                  utils_logtail.c utils_logtail.h \
+                  utils_tail_match.c utils_tail_match.h \
                   utils_match.c utils_match.h \
                   utils_mount.c utils_mount.h \
                   utils_tail.c utils_tail.h \
@@ -566,6 +566,14 @@ collectd_LDADD += "-dlopen" syslog.la
 collectd_DEPENDENCIES += syslog.la
 endif
 
+if BUILD_PLUGIN_TAIL
+pkglib_LTLIBRARIES += tail.la
+tail_la_SOURCES = tail.c
+tail_la_LDFLAGS = -module -avoid-version
+collectd_LDADD += "-dlopen" tail.la
+collectd_DEPENDENCIES += tail.la
+endif
+
 if BUILD_PLUGIN_TAPE
 pkglib_LTLIBRARIES += tape.la
 tape_la_SOURCES = tape.c
index 383dda6..7afe3f1 100644 (file)
@@ -958,6 +958,112 @@ debugging support.
 
 =back
 
+=head2 Plugin C<tail>
+
+The C<tail plugin> plugins follows logfiles, just like L<tail(1)> does, parses
+each line and dispatches found values. What is matched can be configured by the
+user using (extended) regular expressions, as described in L<regex(7)>.
+
+  <Plugin "tail">
+    <File "/var/log/exim4/mainlog">
+      Instance "exim"
+      <Match>
+       Regex "S=([1-9][0-9]*)"
+       DSType "CounterAdd"
+       Type "ipt_bytes"
+       Instance "total"
+      </Match>
+      <Match>
+       Regex "\\<R=local_user\\>"
+       DSType "CounterInc"
+       Type "email_count"
+       Instance "local_user"
+      </Match>
+    </File>
+  </Plugin>
+
+The config consists of one or more B<File> blocks, each of which configures one
+logfile to parse. Within each B<File> block, there are one or more B<Match>
+blocks, which configure a regular expression to search for.
+
+The B<Instance> option in the B<File> block may be used to set the plugin
+instance. So in the above example the plugin name C<tail-foo> would be used.
+This plugin instance is for all B<Match> blocks that B<follow> it, until the
+next B<Instance> option. This way you can extract several plugin instances from
+one logfile, handy when parsing syslog and the like.
+
+Each B<Match> block has the following options to describe how the match should
+be performed:
+
+=over 4
+
+=item B<Regex> I<regex>
+
+Sets the regular expression to use for matching against a line. The first
+subexpression has to match something that can be turned into a number by
+L<strtoll(3)> or L<strtod(3)>, depending on the value of C<CounterAdd>, see
+below. Because B<extended> regular expressions are used, you do not need to use
+backslashes for subexpressions! If in doubt, please consult L<regex(7)>. Due to
+collectd's config parsing you need to escape backslashes, though. So if you
+want to match literal parentheses you need to do the following:
+
+  Regex "SPAM \\(Score: (-?[0-9]+\\.[0-9]+)\\)"
+
+=item B<DSType> I<Type>
+
+Sets how the values are cumulated. I<Type> is one of:
+
+=over 4
+
+=item B<GaugeAverage>
+
+Calculate the average.
+
+=item B<GaugeMin>
+
+Use the smallest number only.
+
+=item B<GaugeMax>
+
+Use the greatest number only.
+
+=item B<GaugeLast>
+
+Use the last number found.
+
+=item B<CounterSet>
+
+The matched number is a counter. Simply sets the internal counter to this
+value.
+
+=item B<CounterAdd>
+
+Add the matched value to the internal counter.
+
+=item B<CounterInc>
+
+Increase the internal counter by one. This B<DSType> is the only one that does
+not use the matched subexpression, but simply counts the number of matched
+lines. Thus, you may use a regular expression without submatch in this case.
+
+=back
+
+As you'd expect the B<Gauge*> types interpret the submatch as a floating point
+number, using L<strtod(3)>. The B<CounterSet> and B<CounterAdd> interpret the
+submatch as an integer using L<strtoll(3)>. B<CounterInc> does not use the
+submatch at all and it may be omitted in this case.
+
+=item B<Type> I<Type>
+
+Sets the type used to dispatch this value. Detailed information about types and
+their configuration can be found in L<types.db(5)>.
+
+=item B<Instance> I<TypeInstance>
+
+This optional setting sets the type instance to use.
+
+=back
+
 =head2 Plugin C<tcpconns>
 
 The C<tcpconns plugin> counts the number of currently established TCP
diff --git a/src/tail.c b/src/tail.c
new file mode 100644 (file)
index 0000000..01bf629
--- /dev/null
@@ -0,0 +1,353 @@
+/**
+ * collectd - src/tail.c
+ * Copyright (C) 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
+ * Free Software Foundation; only version 2 of the License is applicable.
+ *
+ * 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.
+ *
+ * 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
+ *
+ * Authors:
+ *   Florian octo Forster <octo at verplant.org>
+ **/
+
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+#include "utils_tail_match.h"
+
+/*
+ *  <Plugin tail>
+ *    <File "/var/log/exim4/mainlog">
+ *     Instance "exim"
+ *     <Match>
+ *       Regex "S=([1-9][0-9]*)"
+ *       DSType "CounterAdd"
+ *       Type "ipt_bytes"
+ *       Instance "total"
+ *     </Match>
+ *    </File>
+ *  </Plugin>
+ */
+
+struct ctail_config_match_s
+{
+  char *regex;
+  int flags;
+  char *type;
+  char *type_instance;
+};
+typedef struct ctail_config_match_s ctail_config_match_t;
+
+cu_tail_match_t **tail_match_list = NULL;
+size_t tail_match_list_num = 0;
+
+static int ctail_config_add_string (const char *name, char **dest, oconfig_item_t *ci)
+{
+  if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
+  {
+    WARNING ("tail plugin: `%s' needs exactly one string argument.", name);
+    return (-1);
+  }
+
+  sfree (*dest);
+  *dest = strdup (ci->values[0].value.string);
+  if (*dest == NULL)
+    return (-1);
+
+  return (0);
+} /* int ctail_config_add_string */
+
+static int ctail_config_add_match_dstype (ctail_config_match_t *cm,
+    oconfig_item_t *ci)
+{
+  if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
+  {
+    WARNING ("tail plugin: `DSType' needs exactly one string argument.");
+    return (-1);
+  }
+
+  if (strncasecmp ("Gauge", ci->values[0].value.string, strlen ("Gauge")) == 0)
+  {
+    cm->flags = UTILS_MATCH_DS_TYPE_GAUGE;
+    if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
+      cm->flags |= UTILS_MATCH_CF_GAUGE_AVERAGE;
+    else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
+      cm->flags |= UTILS_MATCH_CF_GAUGE_MIN;
+    else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
+      cm->flags |= UTILS_MATCH_CF_GAUGE_MAX;
+    else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
+      cm->flags |= UTILS_MATCH_CF_GAUGE_LAST;
+    else
+      cm->flags = 0;
+  }
+  else if (strncasecmp ("Counter", ci->values[0].value.string, strlen ("Counter")) == 0)
+  {
+    cm->flags = UTILS_MATCH_DS_TYPE_COUNTER;
+    if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
+      cm->flags |= UTILS_MATCH_CF_COUNTER_SET;
+    else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
+      cm->flags |= UTILS_MATCH_CF_COUNTER_ADD;
+    else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
+      cm->flags |= UTILS_MATCH_CF_COUNTER_INC;
+    else
+      cm->flags = 0;
+  }
+  else
+  {
+    cm->flags = 0;
+  }
+
+  if (cm->flags == 0)
+  {
+    WARNING ("tail plugin: `%s' is not a valid argument to `DSType'.",
+       ci->values[0].value.string);
+    return (-1);
+  }
+
+  return (0);
+} /* int ctail_config_add_match_dstype */
+
+static int ctail_config_add_match (cu_tail_match_t *tm,
+    const char *plugin_instance, oconfig_item_t *ci)
+{
+  ctail_config_match_t cm;
+  int status;
+  int i;
+
+  memset (&cm, '\0', sizeof (cm));
+
+  if (ci->values_num != 0)
+  {
+    WARNING ("tail plugin: Ignoring arguments for the `Match' block.");
+  }
+
+  status = 0;
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *option = ci->children + i;
+
+    if (strcasecmp ("Regex", option->key) == 0)
+      status = ctail_config_add_string ("Regex", &cm.regex, option);
+    else if (strcasecmp ("DSType", option->key) == 0)
+      status = ctail_config_add_match_dstype (&cm, option);
+    else if (strcasecmp ("Type", option->key) == 0)
+      status = ctail_config_add_string ("Type", &cm.type, option);
+    else if (strcasecmp ("Instance", option->key) == 0)
+      status = ctail_config_add_string ("Instance", &cm.type_instance, option);
+    else
+    {
+      WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
+      status = -1;
+    }
+
+    if (status != 0)
+      break;
+  } /* for (i = 0; i < ci->children_num; i++) */
+
+  while (status == 0)
+  {
+    if (cm.regex == NULL)
+    {
+      WARNING ("tail plugin: `Regex' missing in `Match' block.");
+      status = -1;
+      break;
+    }
+
+    if (cm.type == NULL)
+    {
+      WARNING ("tail plugin: `Type' missing in `Match' block.");
+      status = -1;
+      break;
+    }
+
+    if (cm.flags == 0)
+    {
+      WARNING ("tail plugin: `DSType' missing in `Match' block.");
+      status = -1;
+      break;
+    }
+
+    break;
+  } /* while (status == 0) */
+
+  if (status == 0)
+  {
+    status = tail_match_add_match_simple (tm, cm.regex, cm.flags,
+       "tail", plugin_instance, cm.type, cm.type_instance);
+
+    if (status != 0)
+    {
+      ERROR ("tail plugin: tail_match_add_match_simple failed.");
+    }
+  }
+
+  sfree (cm.regex);
+  sfree (cm.type);
+  sfree (cm.type_instance);
+
+  return (status);
+} /* int ctail_config_add_match */
+
+static int ctail_config_add_file (oconfig_item_t *ci)
+{
+  cu_tail_match_t *tm;
+  char *plugin_instance = NULL;
+  int num_matches = 0;
+  int status;
+  int i;
+
+  if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
+  {
+    WARNING ("tail plugin: `File' needs exactly one string argument.");
+    return (-1);
+  }
+
+  tm = tail_match_create (ci->values[0].value.string);
+  if (tm == NULL)
+  {
+    ERROR ("tail plugin: tail_match_create (%s) failed.",
+       ci->values[0].value.string);
+    return (-1);
+  }
+
+  status = 0;
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *option = ci->children + i;
+
+    if (strcasecmp ("Match", option->key) == 0)
+    {
+      status = ctail_config_add_match (tm, plugin_instance, option);
+      if (status == 0)
+       num_matches++;
+      /* Be mild with failed matches.. */
+      status = 0;
+    }
+    else if (strcasecmp ("Instance", option->key) == 0)
+      status = ctail_config_add_string ("Instance", &plugin_instance, option);
+    else
+    {
+      WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
+      status = -1;
+    }
+
+    if (status != 0)
+      break;
+  } /* for (i = 0; i < ci->children_num; i++) */
+
+  if (num_matches == 0)
+  {
+    ERROR ("tail plugin: No (valid) matches found for file `%s'.",
+       ci->values[0].value.string);
+    tail_match_destroy (tm);
+    return (-1);
+  }
+  else
+  {
+    cu_tail_match_t **temp;
+
+    temp = (cu_tail_match_t **) realloc (tail_match_list,
+       sizeof (cu_tail_match_t *) * (tail_match_list_num + 1));
+    if (temp == NULL)
+    {
+      ERROR ("tail plugin: realloc failed.");
+      tail_match_destroy (tm);
+      return (-1);
+    }
+
+    tail_match_list = temp;
+    tail_match_list[tail_match_list_num] = tm;
+    tail_match_list_num++;
+  }
+
+  return (0);
+} /* int ctail_config_add_file */
+
+static int ctail_config (oconfig_item_t *ci)
+{
+  int i;
+
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *option = ci->children + i;
+
+    if (strcasecmp ("File", option->key) == 0)
+      ctail_config_add_file (option);
+    else
+    {
+      WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
+    }
+  } /* for (i = 0; i < ci->children_num; i++) */
+
+  return (0);
+} /* int ctail_config */
+
+static int ctail_init (void)
+{
+  if (tail_match_list_num == 0)
+  {
+    WARNING ("tail plugin: File list is empty. Returning an error.");
+    return (-1);
+  }
+
+  return (0);
+} /* int ctail_init */
+
+static int ctail_read (void)
+{
+  int success = 0;
+  int i;
+
+  for (i = 0; i < tail_match_list_num; i++)
+  {
+    int status;
+
+    status = tail_match_read (tail_match_list[i]);
+    if (status != 0)
+    {
+      ERROR ("tail plugin: tail_match_read[%i] failed.", i);
+    }
+    else
+    {
+      success++;
+    }
+  }
+
+  if (success == 0)
+    return (-1);
+  return (0);
+} /* int ctail_read */
+
+static int ctail_shutdown (void)
+{
+  int i;
+
+  for (i = 0; i < tail_match_list_num; i++)
+  {
+    tail_match_destroy (tail_match_list[i]);
+    tail_match_list[i] = NULL;
+  }
+  sfree (tail_match_list);
+  tail_match_list_num = 0;
+
+  return (0);
+} /* int ctail_shutdown */
+
+void module_register (void)
+{
+  plugin_register_complex_config ("tail", ctail_config);
+  plugin_register_init ("tail", ctail_init);
+  plugin_register_read ("tail", ctail_read);
+  plugin_register_shutdown ("tail", ctail_shutdown);
+} /* void module_register */
+
+/* vim: set sw=2 sts=2 ts=8 : */
diff --git a/src/utils_logtail.c b/src/utils_logtail.c
deleted file mode 100644 (file)
index c40c129..0000000
+++ /dev/null
@@ -1,252 +0,0 @@
-/*
- * collectd - src/utils_logtail.c
- * Copyright (C) 2007-2008  C-Ware, Inc.
- * Copyright (C) 2008       Florian 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.
- *
- * 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.
- *
- * 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
- *
- * Author:
- *   Luke Heberling <lukeh at c-ware.com>
- *   Florian Forster <octo at verplant.org>
- *
- * Description:
- *   Encapsulates useful code to plugins which must parse a log file.
- */
-
-#include "collectd.h"
-#include "common.h"
-#include "plugin.h"
-#include "utils_match.h"
-#include "utils_tail.h"
-#include "utils_logtail.h"
-
-struct cu_logtail_simple_s
-{
-  char plugin[DATA_MAX_NAME_LEN];
-  char plugin_instance[DATA_MAX_NAME_LEN];
-  char type[DATA_MAX_NAME_LEN];
-  char type_instance[DATA_MAX_NAME_LEN];
-};
-typedef struct cu_logtail_simple_s cu_logtail_simple_t;
-
-struct cu_logtail_match_s
-{
-  cu_match_t *match;
-  void *user_data;
-  int (*submit) (cu_match_t *match, void *user_data);
-  void (*free) (void *user_data);
-};
-typedef struct cu_logtail_match_s cu_logtail_match_t;
-
-struct cu_logtail_s
-{
-  int flags;
-  cu_tail_t *tail;
-
-  cu_logtail_match_t *matches;
-  size_t matches_num;
-};
-
-/*
- * Private functions
- */
-static int simple_submit_match (cu_match_t *match, void *user_data)
-{
-  cu_logtail_simple_t *data = (cu_logtail_simple_t *) user_data;
-  cu_match_value_t *match_value;
-  value_list_t vl = VALUE_LIST_INIT;
-  value_t values[1];
-
-  match_value = (cu_match_value_t *) match_get_user_data (match);
-  if (match_value == NULL)
-    return (-1);
-
-  values[0] = match_value->value;
-
-  vl.values = values;
-  vl.values_len = 1;
-  vl.time = time (NULL);
-  sstrncpy (vl.host, hostname_g, sizeof (vl.host));
-  sstrncpy (vl.plugin, data->plugin, sizeof (vl.plugin));
-  sstrncpy (vl.plugin_instance, data->plugin_instance,
-      sizeof (vl.plugin_instance));
-  sstrncpy (vl.type_instance, data->type_instance,
-      sizeof (vl.type_instance));
-
-  plugin_dispatch_values (data->type, &vl);
-
-  return (0);
-} /* int simple_submit_match */
-
-static int tail_callback (void *data, char *buf, int buflen)
-{
-  cu_logtail_t *obj = (cu_logtail_t *) data;
-  int i;
-
-  for (i = 0; i < obj->matches_num; i++)
-    match_apply (obj->matches[i].match, buf);
-
-  return (0);
-} /* int tail_callback */
-
-/*
- * Public functions
- */
-cu_logtail_t *logtail_create (const char *filename)
-{
-  cu_logtail_t *obj;
-
-  obj = (cu_logtail_t *) malloc (sizeof (cu_logtail_t));
-  if (obj == NULL)
-    return (NULL);
-  memset (obj, '\0', sizeof (cu_logtail_t));
-
-  obj->tail = cu_tail_create (filename);
-  if (obj->tail == NULL)
-  {
-    sfree (obj);
-    return (NULL);
-  }
-
-  return (obj);
-} /* cu_logtail_t *logtail_create */
-
-void logtail_destroy (cu_logtail_t *obj)
-{
-  int i;
-
-  if (obj == NULL)
-    return;
-
-  if (obj->tail != NULL)
-  {
-    cu_tail_destroy (obj->tail);
-    obj->tail = NULL;
-  }
-
-  for (i = 0; i < obj->matches_num; i++)
-  {
-    cu_logtail_match_t *match = obj->matches + i;
-    if (match->match != NULL)
-    {
-      match_destroy (match->match);
-      match->match = NULL;
-    }
-
-    if ((match->user_data != NULL)
-       && (match->free != NULL))
-      (*match->free) (match->user_data);
-    match->user_data = NULL;
-  }
-
-  sfree (obj->matches);
-  sfree (obj);
-} /* void logtail_destroy */
-
-int logtail_add_match (cu_logtail_t *obj, cu_match_t *match,
-    int (*submit_match) (cu_match_t *match, void *user_data),
-    void *user_data,
-    void (*free_user_data) (void *user_data))
-{
-  cu_logtail_match_t *temp;
-
-  temp = (cu_logtail_match_t *) realloc (obj->matches,
-      sizeof (cu_logtail_match_t) * (obj->matches_num + 1));
-  if (temp == NULL)
-    return (-1);
-
-  obj->matches = temp;
-  obj->matches_num++;
-
-  temp = obj->matches + (obj->matches_num - 1);
-
-  temp->match = match;
-  temp->user_data = user_data;
-  temp->submit = submit_match;
-  temp->free = free_user_data;
-
-  return (0);
-} /* int logtail_add_match */
-
-int logtail_add_match_simple (cu_logtail_t *obj,
-    const char *regex, int ds_type,
-    const char *plugin, const char *plugin_instance,
-    const char *type, const char *type_instance)
-{
-  cu_match_t *match;
-  cu_logtail_simple_t *user_data;
-  int status;
-
-  match = match_create_simple (regex, ds_type);
-  if (match == NULL)
-    return (-1);
-
-  user_data = (cu_logtail_simple_t *) malloc (sizeof (cu_logtail_simple_t));
-  if (user_data == NULL)
-  {
-    match_destroy (match);
-    return (-1);
-  }
-  memset (user_data, '\0', sizeof (cu_logtail_simple_t));
-
-  sstrncpy (user_data->plugin, plugin, sizeof (user_data->plugin));
-  if (plugin_instance != NULL)
-    sstrncpy (user_data->plugin_instance, plugin_instance,
-       sizeof (user_data->plugin_instance));
-
-  sstrncpy (user_data->type, type, sizeof (user_data->type));
-  if (type_instance != NULL)
-    sstrncpy (user_data->type_instance, type_instance,
-       sizeof (user_data->type_instance));
-
-  status = logtail_add_match (obj, match, simple_submit_match,
-      user_data, free);
-
-  if (status != 0)
-  {
-    match_destroy (match);
-    sfree (user_data);
-  }
-
-  return (status);
-} /* int logtail_add_match_simple */
-
-int logtail_read (cu_logtail_t *obj)
-{
-  char buffer[4096];
-  int status;
-  int i;
-
-  status = cu_tail_read (obj->tail, buffer, sizeof (buffer), tail_callback,
-      (void *) obj);
-  if (status != 0)
-  {
-    ERROR ("logtail: cu_tail_read failed.");
-    return (status);
-  }
-
-  for (i = 0; i < obj->matches_num; i++)
-  {
-    cu_logtail_match_t *lt_match = obj->matches + i;
-
-    if (lt_match->submit == NULL)
-      continue;
-
-    (*lt_match->submit) (lt_match->match, lt_match->user_data);
-  }
-
-  return (0);
-} /* int logtail_read */
-
-/* vim: set sw=2 sts=2 ts=8 : */
diff --git a/src/utils_logtail.h b/src/utils_logtail.h
deleted file mode 100644 (file)
index c8f945e..0000000
+++ /dev/null
@@ -1,126 +0,0 @@
-/*
- * collectd - src/utils_logtail.h
- * Copyright (C) 2007-2008  C-Ware, Inc.
- * Copyright (C) 2008       Florian 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.
- *
- * 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.
- *
- * 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
- *
- * Authors:
- *   Luke Heberling <lukeh at c-ware.com>
- *   Florian Forster <octo at verplant.org>
- *
- * Description:
- *   `logtail' uses `utils_tail' and `utils_match' to tail a file and try to
- *   match it using several regular expressions. Matches are then passed to
- *   user-provided callback functions or default handlers. This should keep all
- *   of the parsing logic out of the actual plugin, which only operate with
- *   regular expressions.
- */
-
-#include "utils_match.h"
-
-struct cu_logtail_s;
-typedef struct cu_logtail_s cu_logtail_t;
-
-/*
- * NAME
- *   logtail_create
- *
- * DESCRIPTION
- *   Allocates, initializes and returns a new `cu_logtail_t' object.
- *
- * PARAMETERS
- *   `filename'  The name to read data from.
- *
- * RETURN VALUE
- *   Returns NULL upon failure, non-NULL otherwise.
- */
-cu_logtail_t *logtail_create (const char *filename);
-
-/*
- * NAME
- *   logtail_destroy
- *
- * DESCRIPTION
- *   Releases resources used by the `cu_logtail_t' object.
- *
- * PARAMETERS
- *   The object to destroy.
- */
-void logtail_destroy (cu_logtail_t *obj);
-
-/*
- * NAME
- *   logtail_add_match
- *
- * DESCRIPTION
- *   Adds a match, in form of a `cu_match_t' object, to the object.
- *   After data has been read from the logfile (using utils_tail) the callback
- *   function `submit_match' is called with the match object and the user
- *   supplied data.
- *   Please note that his function is called regardless whether this match
- *   matched any lines recently or not.
- *   When `logtail_destroy' is called the `user_data' pointer is freed using
- *   the `free_user_data' callback - if it is not NULL.
- *   When using this interface the `logtail' module doesn't dispatch any values
- *   itself - all that has to happen in either the match-callbacks or the
- *   submit_match callback.
- *
- * RETURN VALUE
- *   Zero upon success, non-zero otherwise.
- */
-int logtail_add_match (cu_logtail_t *obj, cu_match_t *match,
-    int (*submit_match) (cu_match_t *match, void *user_data),
-    void *user_data,
-    void (*free_user_data) (void *user_data));
-
-/*
- * NAME
- *  logtail_add_match_simple
- *
- * DESCRIPTION
- *  A simplified version of `logtail_add_match'. The regular expressen `regex'
- *  must match a number, which is then dispatched according to `ds_type'. See
- *  the `match_create_simple' function in utils_match.h for a description how
- *  this flag effects calculation of a new value.
- *  The values gathered are dispatched by the logtail module in this case. The
- *  passed `plugin', `plugin_instance', `type', and `type_instance' are
- *  directly used when submitting these values.
- *
- * RETURN VALUE
- *   Zero upon success, non-zero otherwise.
- */
-int logtail_add_match_simple (cu_logtail_t *obj,
-    const char *regex, int ds_type,
-    const char *plugin, const char *plugin_instance,
-    const char *type, const char *type_instance);
-
-/*
- * NAME
- *   logtail_read
- *
- * DESCRIPTION
- *   This function should be called periodically by plugins. It reads new lines
- *   from the logfile using `utils_tail' and tries to match them using all
- *   added `utils_match' objects.
- *   After all lines have been read and processed, the submit_match callback is
- *   called or, in case of logtail_add_match_simple, the data is dispatched to
- *   the daemon directly.
- *
- * RETURN VALUE
- *   Zero on success, nonzero on failure.
-*/
-int logtail_read (cu_logtail_t *obj);
-
-/* vim: set sw=2 sts=2 ts=8 : */
index d4ac05a..dccb40a 100644 (file)
@@ -46,7 +46,7 @@ static int default_callback (const char *str, void *user_data)
 {
   cu_match_value_t *data = (cu_match_value_t *) user_data;
 
-  if (data->ds_type == UTILS_MATCH_DS_TYPE_GAUGE)
+  if (data->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
   {
     gauge_t value;
     char *endptr = NULL;
@@ -55,29 +55,66 @@ static int default_callback (const char *str, void *user_data)
     if (str == endptr)
       return (-1);
 
-    data->value.gauge = value;
+    if ((data->values_num == 0)
+       || (data->ds_type & UTILS_MATCH_CF_GAUGE_LAST))
+    {
+      data->value.gauge = value;
+    }
+    else if (data->ds_type & UTILS_MATCH_CF_GAUGE_AVERAGE)
+    {
+      double f = ((double) data->values_num)
+       / ((double) (data->values_num + 1));
+      data->value.gauge = (data->value.gauge * f) + (value * (1.0 - f));
+    }
+    else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MIN)
+    {
+      if (data->value.gauge > value)
+       data->value.gauge = value;
+    }
+    else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MAX)
+    {
+      if (data->value.gauge < value)
+       data->value.gauge = value;
+    }
+    else
+    {
+      ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
+      return (-1);
+    }
+
+    data->values_num++;
   }
-  else if ((data->ds_type == UTILS_MATCH_DS_TYPE_COUNTER_SET)
-      || (data->ds_type == UTILS_MATCH_DS_TYPE_COUNTER_ADD))
+  else if (data->ds_type & UTILS_MATCH_DS_TYPE_COUNTER)
   {
     counter_t value;
     char *endptr = NULL;
 
+    if (data->ds_type & UTILS_MATCH_CF_COUNTER_INC)
+    {
+      data->value.counter++;
+      data->values_num++;
+      return (0);
+    }
+
     value = strtoll (str, &endptr, 0);
     if (str == endptr)
       return (-1);
 
-    if (data->ds_type == UTILS_MATCH_DS_TYPE_COUNTER_SET)
+    if (data->ds_type & UTILS_MATCH_CF_COUNTER_SET)
       data->value.counter = value;
-    else
+    else if (data->ds_type & UTILS_MATCH_CF_COUNTER_ADD)
       data->value.counter += value;
-  }
-  else if (data->ds_type == UTILS_MATCH_DS_TYPE_COUNTER_INC)
-  {
-    data->value.counter++;
+    else
+    {
+      ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
+      return (-1);
+    }
+
+    data->values_num++;
   }
   else
   {
+    ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
     return (-1);
   }
 
@@ -94,6 +131,8 @@ cu_match_t *match_create_callback (const char *regex,
   cu_match_t *obj;
   int status;
 
+  DEBUG ("utils_match: match_create_callback: regex = %s", regex);
+
   obj = (cu_match_t *) malloc (sizeof (cu_match_t));
   if (obj == NULL)
     return (NULL);
index 5108e96..da9f1bc 100644 (file)
 /*
  * Defines
  */
-#define UTILS_MATCH_DS_TYPE_GAUGE       0
-#define UTILS_MATCH_DS_TYPE_COUNTER_SET 1
-#define UTILS_MATCH_DS_TYPE_COUNTER_ADD 2
-#define UTILS_MATCH_DS_TYPE_COUNTER_INC 3
+#define UTILS_MATCH_DS_TYPE_GAUGE   0x10
+#define UTILS_MATCH_DS_TYPE_COUNTER 0x20
+
+#define UTILS_MATCH_CF_GAUGE_AVERAGE 0x01
+#define UTILS_MATCH_CF_GAUGE_MIN     0x02
+#define UTILS_MATCH_CF_GAUGE_MAX     0x04
+#define UTILS_MATCH_CF_GAUGE_LAST    0x08
+
+#define UTILS_MATCH_CF_COUNTER_SET   0x01
+#define UTILS_MATCH_CF_COUNTER_ADD   0x02
+#define UTILS_MATCH_CF_COUNTER_INC   0x04
 
 /*
  * Data types
@@ -43,6 +50,7 @@ struct cu_match_value_s
 {
   int ds_type;
   value_t value;
+  unsigned int values_num;
 };
 typedef struct cu_match_value_s cu_match_value_t;
 
diff --git a/src/utils_tail_match.c b/src/utils_tail_match.c
new file mode 100644 (file)
index 0000000..34fe2dc
--- /dev/null
@@ -0,0 +1,262 @@
+/*
+ * collectd - src/utils_tail_match.c
+ * Copyright (C) 2007-2008  C-Ware, Inc.
+ * Copyright (C) 2008       Florian 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.
+ *
+ * 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.
+ *
+ * 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
+ *
+ * Author:
+ *   Luke Heberling <lukeh at c-ware.com>
+ *   Florian Forster <octo at verplant.org>
+ *
+ * Description:
+ *   Encapsulates useful code to plugins which must parse a log file.
+ */
+
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+#include "utils_match.h"
+#include "utils_tail.h"
+#include "utils_tail_match.h"
+
+struct cu_tail_match_simple_s
+{
+  char plugin[DATA_MAX_NAME_LEN];
+  char plugin_instance[DATA_MAX_NAME_LEN];
+  char type[DATA_MAX_NAME_LEN];
+  char type_instance[DATA_MAX_NAME_LEN];
+};
+typedef struct cu_tail_match_simple_s cu_tail_match_simple_t;
+
+struct cu_tail_match_match_s
+{
+  cu_match_t *match;
+  void *user_data;
+  int (*submit) (cu_match_t *match, void *user_data);
+  void (*free) (void *user_data);
+};
+typedef struct cu_tail_match_match_s cu_tail_match_match_t;
+
+struct cu_tail_match_s
+{
+  int flags;
+  cu_tail_t *tail;
+
+  cu_tail_match_match_t *matches;
+  size_t matches_num;
+};
+
+/*
+ * Private functions
+ */
+static int simple_submit_match (cu_match_t *match, void *user_data)
+{
+  cu_tail_match_simple_t *data = (cu_tail_match_simple_t *) user_data;
+  cu_match_value_t *match_value;
+  value_list_t vl = VALUE_LIST_INIT;
+  value_t values[1];
+
+  match_value = (cu_match_value_t *) match_get_user_data (match);
+  if (match_value == NULL)
+    return (-1);
+
+  if ((match_value->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
+      && (match_value->values_num == 0))
+    values[0].gauge = NAN;
+  else
+    values[0] = match_value->value;
+
+  vl.values = values;
+  vl.values_len = 1;
+  vl.time = time (NULL);
+  sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+  sstrncpy (vl.plugin, data->plugin, sizeof (vl.plugin));
+  sstrncpy (vl.plugin_instance, data->plugin_instance,
+      sizeof (vl.plugin_instance));
+  sstrncpy (vl.type_instance, data->type_instance,
+      sizeof (vl.type_instance));
+
+  plugin_dispatch_values (data->type, &vl);
+
+  if (match_value->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
+  {
+    match_value->value.gauge = NAN;
+    match_value->values_num = 0;
+  }
+
+  return (0);
+} /* int simple_submit_match */
+
+static int tail_callback (void *data, char *buf, int buflen)
+{
+  cu_tail_match_t *obj = (cu_tail_match_t *) data;
+  int i;
+
+  for (i = 0; i < obj->matches_num; i++)
+    match_apply (obj->matches[i].match, buf);
+
+  return (0);
+} /* int tail_callback */
+
+/*
+ * Public functions
+ */
+cu_tail_match_t *tail_match_create (const char *filename)
+{
+  cu_tail_match_t *obj;
+
+  obj = (cu_tail_match_t *) malloc (sizeof (cu_tail_match_t));
+  if (obj == NULL)
+    return (NULL);
+  memset (obj, '\0', sizeof (cu_tail_match_t));
+
+  obj->tail = cu_tail_create (filename);
+  if (obj->tail == NULL)
+  {
+    sfree (obj);
+    return (NULL);
+  }
+
+  return (obj);
+} /* cu_tail_match_t *tail_match_create */
+
+void tail_match_destroy (cu_tail_match_t *obj)
+{
+  int i;
+
+  if (obj == NULL)
+    return;
+
+  if (obj->tail != NULL)
+  {
+    cu_tail_destroy (obj->tail);
+    obj->tail = NULL;
+  }
+
+  for (i = 0; i < obj->matches_num; i++)
+  {
+    cu_tail_match_match_t *match = obj->matches + i;
+    if (match->match != NULL)
+    {
+      match_destroy (match->match);
+      match->match = NULL;
+    }
+
+    if ((match->user_data != NULL)
+       && (match->free != NULL))
+      (*match->free) (match->user_data);
+    match->user_data = NULL;
+  }
+
+  sfree (obj->matches);
+  sfree (obj);
+} /* void tail_match_destroy */
+
+int tail_match_add_match (cu_tail_match_t *obj, cu_match_t *match,
+    int (*submit_match) (cu_match_t *match, void *user_data),
+    void *user_data,
+    void (*free_user_data) (void *user_data))
+{
+  cu_tail_match_match_t *temp;
+
+  temp = (cu_tail_match_match_t *) realloc (obj->matches,
+      sizeof (cu_tail_match_match_t) * (obj->matches_num + 1));
+  if (temp == NULL)
+    return (-1);
+
+  obj->matches = temp;
+  obj->matches_num++;
+
+  temp = obj->matches + (obj->matches_num - 1);
+
+  temp->match = match;
+  temp->user_data = user_data;
+  temp->submit = submit_match;
+  temp->free = free_user_data;
+
+  return (0);
+} /* int tail_match_add_match */
+
+int tail_match_add_match_simple (cu_tail_match_t *obj,
+    const char *regex, int ds_type,
+    const char *plugin, const char *plugin_instance,
+    const char *type, const char *type_instance)
+{
+  cu_match_t *match;
+  cu_tail_match_simple_t *user_data;
+  int status;
+
+  match = match_create_simple (regex, ds_type);
+  if (match == NULL)
+    return (-1);
+
+  user_data = (cu_tail_match_simple_t *) malloc (sizeof (cu_tail_match_simple_t));
+  if (user_data == NULL)
+  {
+    match_destroy (match);
+    return (-1);
+  }
+  memset (user_data, '\0', sizeof (cu_tail_match_simple_t));
+
+  sstrncpy (user_data->plugin, plugin, sizeof (user_data->plugin));
+  if (plugin_instance != NULL)
+    sstrncpy (user_data->plugin_instance, plugin_instance,
+       sizeof (user_data->plugin_instance));
+
+  sstrncpy (user_data->type, type, sizeof (user_data->type));
+  if (type_instance != NULL)
+    sstrncpy (user_data->type_instance, type_instance,
+       sizeof (user_data->type_instance));
+
+  status = tail_match_add_match (obj, match, simple_submit_match,
+      user_data, free);
+
+  if (status != 0)
+  {
+    match_destroy (match);
+    sfree (user_data);
+  }
+
+  return (status);
+} /* int tail_match_add_match_simple */
+
+int tail_match_read (cu_tail_match_t *obj)
+{
+  char buffer[4096];
+  int status;
+  int i;
+
+  status = cu_tail_read (obj->tail, buffer, sizeof (buffer), tail_callback,
+      (void *) obj);
+  if (status != 0)
+  {
+    ERROR ("tail_match: cu_tail_read failed.");
+    return (status);
+  }
+
+  for (i = 0; i < obj->matches_num; i++)
+  {
+    cu_tail_match_match_t *lt_match = obj->matches + i;
+
+    if (lt_match->submit == NULL)
+      continue;
+
+    (*lt_match->submit) (lt_match->match, lt_match->user_data);
+  }
+
+  return (0);
+} /* int tail_match_read */
+
+/* vim: set sw=2 sts=2 ts=8 : */
diff --git a/src/utils_tail_match.h b/src/utils_tail_match.h
new file mode 100644 (file)
index 0000000..d08c728
--- /dev/null
@@ -0,0 +1,126 @@
+/*
+ * collectd - src/utils_tail_match.h
+ * Copyright (C) 2007-2008  C-Ware, Inc.
+ * Copyright (C) 2008       Florian 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.
+ *
+ * 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.
+ *
+ * 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
+ *
+ * Authors:
+ *   Luke Heberling <lukeh at c-ware.com>
+ *   Florian Forster <octo at verplant.org>
+ *
+ * Description:
+ *   `tail_match' uses `utils_tail' and `utils_match' to tail a file and try to
+ *   match it using several regular expressions. Matches are then passed to
+ *   user-provided callback functions or default handlers. This should keep all
+ *   of the parsing logic out of the actual plugin, which only operate with
+ *   regular expressions.
+ */
+
+#include "utils_match.h"
+
+struct cu_tail_match_s;
+typedef struct cu_tail_match_s cu_tail_match_t;
+
+/*
+ * NAME
+ *   tail_match_create
+ *
+ * DESCRIPTION
+ *   Allocates, initializes and returns a new `cu_tail_match_t' object.
+ *
+ * PARAMETERS
+ *   `filename'  The name to read data from.
+ *
+ * RETURN VALUE
+ *   Returns NULL upon failure, non-NULL otherwise.
+ */
+cu_tail_match_t *tail_match_create (const char *filename);
+
+/*
+ * NAME
+ *   tail_match_destroy
+ *
+ * DESCRIPTION
+ *   Releases resources used by the `cu_tail_match_t' object.
+ *
+ * PARAMETERS
+ *   The object to destroy.
+ */
+void tail_match_destroy (cu_tail_match_t *obj);
+
+/*
+ * NAME
+ *   tail_match_add_match
+ *
+ * DESCRIPTION
+ *   Adds a match, in form of a `cu_match_t' object, to the object.
+ *   After data has been read from the logfile (using utils_tail) the callback
+ *   function `submit_match' is called with the match object and the user
+ *   supplied data.
+ *   Please note that his function is called regardless whether this match
+ *   matched any lines recently or not.
+ *   When `tail_match_destroy' is called the `user_data' pointer is freed using
+ *   the `free_user_data' callback - if it is not NULL.
+ *   When using this interface the `tail_match' module doesn't dispatch any values
+ *   itself - all that has to happen in either the match-callbacks or the
+ *   submit_match callback.
+ *
+ * RETURN VALUE
+ *   Zero upon success, non-zero otherwise.
+ */
+int tail_match_add_match (cu_tail_match_t *obj, cu_match_t *match,
+    int (*submit_match) (cu_match_t *match, void *user_data),
+    void *user_data,
+    void (*free_user_data) (void *user_data));
+
+/*
+ * NAME
+ *  tail_match_add_match_simple
+ *
+ * DESCRIPTION
+ *  A simplified version of `tail_match_add_match'. The regular expressen `regex'
+ *  must match a number, which is then dispatched according to `ds_type'. See
+ *  the `match_create_simple' function in utils_match.h for a description how
+ *  this flag effects calculation of a new value.
+ *  The values gathered are dispatched by the tail_match module in this case. The
+ *  passed `plugin', `plugin_instance', `type', and `type_instance' are
+ *  directly used when submitting these values.
+ *
+ * RETURN VALUE
+ *   Zero upon success, non-zero otherwise.
+ */
+int tail_match_add_match_simple (cu_tail_match_t *obj,
+    const char *regex, int ds_type,
+    const char *plugin, const char *plugin_instance,
+    const char *type, const char *type_instance);
+
+/*
+ * NAME
+ *   tail_match_read
+ *
+ * DESCRIPTION
+ *   This function should be called periodically by plugins. It reads new lines
+ *   from the logfile using `utils_tail' and tries to match them using all
+ *   added `utils_match' objects.
+ *   After all lines have been read and processed, the submit_match callback is
+ *   called or, in case of tail_match_add_match_simple, the data is dispatched to
+ *   the daemon directly.
+ *
+ * RETURN VALUE
+ *   Zero on success, nonzero on failure.
+*/
+int tail_match_read (cu_tail_match_t *obj);
+
+/* vim: set sw=2 sts=2 ts=8 : */