2
* collectd - src/utils_tail_match.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_tail_match.h"
34
struct cu_tail_match_simple_s
36
char plugin[DATA_MAX_NAME_LEN];
37
char plugin_instance[DATA_MAX_NAME_LEN];
38
char type[DATA_MAX_NAME_LEN];
39
char type_instance[DATA_MAX_NAME_LEN];
41
typedef struct cu_tail_match_simple_s cu_tail_match_simple_t;
43
struct cu_tail_match_match_s
47
int (*submit) (cu_match_t *match, void *user_data);
48
void (*free) (void *user_data);
50
typedef struct cu_tail_match_match_s cu_tail_match_match_t;
52
struct cu_tail_match_s
57
cu_tail_match_match_t *matches;
64
static int simple_submit_match (cu_match_t *match, void *user_data)
66
cu_tail_match_simple_t *data = (cu_tail_match_simple_t *) user_data;
67
cu_match_value_t *match_value;
68
value_list_t vl = VALUE_LIST_INIT;
71
match_value = (cu_match_value_t *) match_get_user_data (match);
72
if (match_value == NULL)
75
if ((match_value->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
76
&& (match_value->values_num == 0))
77
values[0].gauge = NAN;
79
values[0] = match_value->value;
83
vl.time = time (NULL);
84
sstrncpy (vl.host, hostname_g, sizeof (vl.host));
85
sstrncpy (vl.plugin, data->plugin, sizeof (vl.plugin));
86
sstrncpy (vl.plugin_instance, data->plugin_instance,
87
sizeof (vl.plugin_instance));
88
sstrncpy (vl.type_instance, data->type_instance,
89
sizeof (vl.type_instance));
91
plugin_dispatch_values (data->type, &vl);
93
if (match_value->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
95
match_value->value.gauge = NAN;
96
match_value->values_num = 0;
100
} /* int simple_submit_match */
102
static int tail_callback (void *data, char *buf, int buflen)
104
cu_tail_match_t *obj = (cu_tail_match_t *) data;
107
for (i = 0; i < obj->matches_num; i++)
108
match_apply (obj->matches[i].match, buf);
111
} /* int tail_callback */
116
cu_tail_match_t *tail_match_create (const char *filename)
118
cu_tail_match_t *obj;
120
obj = (cu_tail_match_t *) malloc (sizeof (cu_tail_match_t));
123
memset (obj, '\0', sizeof (cu_tail_match_t));
125
obj->tail = cu_tail_create (filename);
126
if (obj->tail == NULL)
133
} /* cu_tail_match_t *tail_match_create */
135
void tail_match_destroy (cu_tail_match_t *obj)
142
if (obj->tail != NULL)
144
cu_tail_destroy (obj->tail);
148
for (i = 0; i < obj->matches_num; i++)
150
cu_tail_match_match_t *match = obj->matches + i;
151
if (match->match != NULL)
153
match_destroy (match->match);
157
if ((match->user_data != NULL)
158
&& (match->free != NULL))
159
(*match->free) (match->user_data);
160
match->user_data = NULL;
163
sfree (obj->matches);
165
} /* void tail_match_destroy */
167
int tail_match_add_match (cu_tail_match_t *obj, cu_match_t *match,
168
int (*submit_match) (cu_match_t *match, void *user_data),
170
void (*free_user_data) (void *user_data))
172
cu_tail_match_match_t *temp;
174
temp = (cu_tail_match_match_t *) realloc (obj->matches,
175
sizeof (cu_tail_match_match_t) * (obj->matches_num + 1));
182
temp = obj->matches + (obj->matches_num - 1);
185
temp->user_data = user_data;
186
temp->submit = submit_match;
187
temp->free = free_user_data;
190
} /* int tail_match_add_match */
192
int tail_match_add_match_simple (cu_tail_match_t *obj,
193
const char *regex, int ds_type,
194
const char *plugin, const char *plugin_instance,
195
const char *type, const char *type_instance)
198
cu_tail_match_simple_t *user_data;
201
match = match_create_simple (regex, ds_type);
205
user_data = (cu_tail_match_simple_t *) malloc (sizeof (cu_tail_match_simple_t));
206
if (user_data == NULL)
208
match_destroy (match);
211
memset (user_data, '\0', sizeof (cu_tail_match_simple_t));
213
sstrncpy (user_data->plugin, plugin, sizeof (user_data->plugin));
214
if (plugin_instance != NULL)
215
sstrncpy (user_data->plugin_instance, plugin_instance,
216
sizeof (user_data->plugin_instance));
218
sstrncpy (user_data->type, type, sizeof (user_data->type));
219
if (type_instance != NULL)
220
sstrncpy (user_data->type_instance, type_instance,
221
sizeof (user_data->type_instance));
223
status = tail_match_add_match (obj, match, simple_submit_match,
228
match_destroy (match);
233
} /* int tail_match_add_match_simple */
235
int tail_match_read (cu_tail_match_t *obj)
241
status = cu_tail_read (obj->tail, buffer, sizeof (buffer), tail_callback,
245
ERROR ("tail_match: cu_tail_read failed.");
249
for (i = 0; i < obj->matches_num; i++)
251
cu_tail_match_match_t *lt_match = obj->matches + i;
253
if (lt_match->submit == NULL)
256
(*lt_match->submit) (lt_match->match, lt_match->user_data);
260
} /* int tail_match_read */
262
/* vim: set sw=2 sts=2 ts=8 : */