From: Florian Forster Date: Sun, 24 Feb 2008 08:48:35 +0000 (+0100) Subject: Renamed: src/utils_logtail.[ch] -> src/utils_tail_match.[ch] X-Git-Tag: collectd-4.4.0~89^2~4 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=41c75b9df38ae4d91438693564580eaf796578b0;p=collectd.git Renamed: src/utils_logtail.[ch] -> src/utils_tail_match.[ch] --- diff --git a/src/Makefile.am b/src/Makefile.am index 30518b9f..a51c161b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 \ diff --git a/src/utils_logtail.c b/src/utils_logtail.c deleted file mode 100644 index c40c129a..00000000 --- a/src/utils_logtail.c +++ /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 - * Florian Forster - * - * 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 index c8f945e9..00000000 --- a/src/utils_logtail.h +++ /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 - * Florian Forster - * - * 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 : */ diff --git a/src/utils_tail_match.c b/src/utils_tail_match.c new file mode 100644 index 00000000..b71e2059 --- /dev/null +++ b/src/utils_tail_match.c @@ -0,0 +1,252 @@ +/* + * 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 + * Florian Forster + * + * 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); + + 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_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 index 00000000..d08c728e --- /dev/null +++ b/src/utils_tail_match.h @@ -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 + * Florian Forster + * + * 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 : */