2 * collectd - src/utils_logtail.c
3 * Copyright (C) 2007-2008 C-Ware, Inc.
4 * Copyright (C) 2008 Florian Forster
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Luke Heberling <lukeh at c-ware.com>
21 * Florian Forster <octo at verplant.org>
24 * Encapsulates useful code to plugins which must parse a log file.
30 #include "utils_match.h"
31 #include "utils_tail.h"
32 #include "utils_logtail.h"
34 struct cu_logtail_match_s
38 int (*submit) (cu_match_t *match, void *user_data);
39 void (*free) (void *user_data);
41 typedef struct cu_logtail_match_s cu_logtail_match_t;
48 cu_logtail_match_t *matches;
55 static int simple_submit_match (cu_match_t *match, void *user_data)
57 cu_logtail_simple_t *data = (cu_logtail_simple_t *) user_data;
58 cu_match_value_t *match_value;
59 value_list_t vl = VALUE_LIST_INIT;
62 match_value = (cu_match_value_t *) match_get_user_data (match);
63 if (match_value == NULL)
66 values[0] = match_value->value;
70 vl.time = time (NULL);
71 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
72 sstrncpy (vl.plugin, data->plugin, sizeof (vl.plugin));
73 sstrncpy (vl.plugin_instance, data->plugin_instance,
74 sizeof (vl.plugin_instance));
75 sstrncpy (vl.type_instance, data->type_instance,
76 sizeof (vl.type_instance));
78 plugin_dispatch_values (data->type, &vl);
81 } /* int simple_submit_match */
83 static int tail_callback (void *data, char *buf, int buflen)
85 cu_logtail_t *obj = (cu_logtail_t *) data;
88 for (i = 0; i < obj->matches_num; i++)
89 match_apply (obj->matches[i].match, buf);
92 } /* int tail_callback */
97 cu_logtail_t *logtail_create (const char *filename)
101 obj = (cu_logtail_t *) malloc (sizeof (cu_logtail_t));
104 memset (obj, '\0', sizeof (cu_logtail_t));
106 obj->tail = cu_tail_create (filename);
107 if (obj->tail == NULL)
114 } /* cu_logtail_t *logtail_create */
116 void logtail_destroy (cu_logtail_t *obj)
123 if (obj->tail != NULL)
125 cu_tail_destroy (obj->tail);
129 for (i = 0; i < obj->matches_num; i++)
131 cu_logtail_match_t *match = obj->matches + i;
132 if (match->match != NULL)
134 match_destroy (match->match);
138 if ((match->user_data != NULL)
139 && (match->free != NULL))
140 (*match->free) (match->user_data);
141 match->user_data = NULL;
144 sfree (obj->matches);
146 } /* void logtail_destroy */
148 int logtail_add_match (cu_logtail_t *obj, cu_match_t *match,
149 int (*submit_match) (cu_match_t *match, void *user_data),
151 void (*free_user_data) (void *user_data))
153 cu_logtail_match_t *temp;
155 temp = (cu_logtail_match_t *) realloc (obj->matches,
156 sizeof (cu_logtail_match_t) * (obj->matches_num + 1));
163 temp = obj->matches + (obj->matches_num - 1);
166 temp->user_data = user_data;
167 temp->submit = submit_match;
168 temp->free = free_user_data;
171 } /* int logtail_add_match */
173 int logtail_add_match_simple (cu_logtail_t *obj,
174 const char *regex, int ds_type,
175 const char *plugin, const char *plugin_instance,
176 const char *type, const char *type_instance)
179 cu_logtail_simple_t *user_data;
182 match = match_create_simple (regex, ds_type);
186 user_data = (cu_logtail_simple_t *) malloc (sizeof (cu_logtail_simple_t));
187 if (user_data == NULL)
189 match_destroy (match);
193 sstrncpy (user_data->plugin, plugin, sizeof (user_data->plugin));
194 sstrncpy (user_data->plugin_instance, plugin_instance,
195 sizeof (user_data->plugin_instance));
196 sstrncpy (user_data->type, type, sizeof (user_data->type));
197 sstrncpy (user_data->type_instance, type_instance,
198 sizeof (user_data->type_instance));
200 status = logtail_add_match (obj, match, simple_submit_match,
205 match_destroy (match);
210 } /* int logtail_add_match_simple */
212 int logtail_read (cu_logtail_t *obj)
218 status = cu_tail_read (obj->tail, buffer, sizeof (buffer), tail_callback,
223 for (i = 0; i < obj->matches_num; i++)
225 cu_logtail_match_t *lt_match = obj->matches + i;
227 (*lt_match->submit) (lt_match->match, lt_match->user_data);
231 } /* int logtail_read */
233 /* vim: set sw=2 sts=2 ts=8 : */