1 /*
2 * collectd - src/utils_tail_match.c
3 * Copyright (C) 2007-2008 C-Ware, Inc.
4 * Copyright (C) 2008 Florian Forster
5 *
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.
9 *
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.
14 *
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
18 *
19 * Author:
20 * Luke Heberling <lukeh at c-ware.com>
21 * Florian Forster <octo at verplant.org>
22 *
23 * Description:
24 * Encapsulates useful code to plugins which must parse a log file.
25 */
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "utils_match.h"
31 #include "utils_tail.h"
32 #include "utils_tail_match.h"
34 struct cu_tail_match_simple_s
35 {
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];
40 cdtime_t interval;
41 };
42 typedef struct cu_tail_match_simple_s cu_tail_match_simple_t;
44 struct cu_tail_match_match_s
45 {
46 cu_match_t *match;
47 void *user_data;
48 int (*submit) (cu_match_t *match, void *user_data);
49 void (*free) (void *user_data);
50 };
51 typedef struct cu_tail_match_match_s cu_tail_match_match_t;
53 struct cu_tail_match_s
54 {
55 int flags;
56 cu_tail_t *tail;
58 cu_tail_match_match_t *matches;
59 size_t matches_num;
60 };
62 /*
63 * Private functions
64 */
65 static int simple_submit_match (cu_match_t *match, void *user_data)
66 {
67 cu_tail_match_simple_t *data = (cu_tail_match_simple_t *) user_data;
68 cu_match_value_t *match_value;
69 value_list_t vl = VALUE_LIST_INIT (/* default_interval = */ 0);
70 value_t values[1];
72 match_value = (cu_match_value_t *) match_get_user_data (match);
73 if (match_value == NULL)
74 return (-1);
76 if ((match_value->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
77 && (match_value->values_num == 0))
78 values[0].gauge = NAN;
79 else
80 values[0] = match_value->value;
82 vl.values = values;
83 vl.values_len = 1;
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, data->type, sizeof (vl.type));
89 sstrncpy (vl.type_instance, data->type_instance,
90 sizeof (vl.type_instance));
91 vl.interval = data->interval;
93 plugin_dispatch_values (&vl);
95 if (match_value->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
96 {
97 match_value->value.gauge = NAN;
98 match_value->values_num = 0;
99 }
101 return (0);
102 } /* int simple_submit_match */
104 static int tail_callback (void *data, char *buf,
105 int __attribute__((unused)) buflen)
106 {
107 cu_tail_match_t *obj = (cu_tail_match_t *) data;
108 size_t i;
110 for (i = 0; i < obj->matches_num; i++)
111 match_apply (obj->matches[i].match, buf);
113 return (0);
114 } /* int tail_callback */
116 /*
117 * Public functions
118 */
119 cu_tail_match_t *tail_match_create (const char *filename)
120 {
121 cu_tail_match_t *obj;
123 obj = (cu_tail_match_t *) malloc (sizeof (cu_tail_match_t));
124 if (obj == NULL)
125 return (NULL);
126 memset (obj, '\0', sizeof (cu_tail_match_t));
128 obj->tail = cu_tail_create (filename);
129 if (obj->tail == NULL)
130 {
131 sfree (obj);
132 return (NULL);
133 }
135 return (obj);
136 } /* cu_tail_match_t *tail_match_create */
138 void tail_match_destroy (cu_tail_match_t *obj)
139 {
140 size_t i;
142 if (obj == NULL)
143 return;
145 if (obj->tail != NULL)
146 {
147 cu_tail_destroy (obj->tail);
148 obj->tail = NULL;
149 }
151 for (i = 0; i < obj->matches_num; i++)
152 {
153 cu_tail_match_match_t *match = obj->matches + i;
154 if (match->match != NULL)
155 {
156 match_destroy (match->match);
157 match->match = NULL;
158 }
160 if ((match->user_data != NULL)
161 && (match->free != NULL))
162 (*match->free) (match->user_data);
163 match->user_data = NULL;
164 }
166 sfree (obj->matches);
167 sfree (obj);
168 } /* void tail_match_destroy */
170 int tail_match_add_match (cu_tail_match_t *obj, cu_match_t *match,
171 int (*submit_match) (cu_match_t *match, void *user_data),
172 void *user_data,
173 void (*free_user_data) (void *user_data))
174 {
175 cu_tail_match_match_t *temp;
177 temp = (cu_tail_match_match_t *) realloc (obj->matches,
178 sizeof (cu_tail_match_match_t) * (obj->matches_num + 1));
179 if (temp == NULL)
180 return (-1);
182 obj->matches = temp;
183 obj->matches_num++;
185 temp = obj->matches + (obj->matches_num - 1);
187 temp->match = match;
188 temp->user_data = user_data;
189 temp->submit = submit_match;
190 temp->free = free_user_data;
192 return (0);
193 } /* int tail_match_add_match */
195 int tail_match_add_match_simple (cu_tail_match_t *obj,
196 const char *regex, const char *excluderegex, int ds_type,
197 const char *plugin, const char *plugin_instance,
198 const char *type, const char *type_instance,
199 cdtime_t interval)
200 {
201 cu_match_t *match;
202 cu_tail_match_simple_t *user_data;
203 int status;
205 match = match_create_simple (regex, excluderegex, ds_type);
206 if (match == NULL)
207 return (-1);
209 user_data = (cu_tail_match_simple_t *) malloc (sizeof (cu_tail_match_simple_t));
210 if (user_data == NULL)
211 {
212 match_destroy (match);
213 return (-1);
214 }
215 memset (user_data, '\0', sizeof (cu_tail_match_simple_t));
217 sstrncpy (user_data->plugin, plugin, sizeof (user_data->plugin));
218 if (plugin_instance != NULL)
219 sstrncpy (user_data->plugin_instance, plugin_instance,
220 sizeof (user_data->plugin_instance));
222 sstrncpy (user_data->type, type, sizeof (user_data->type));
223 if (type_instance != NULL)
224 sstrncpy (user_data->type_instance, type_instance,
225 sizeof (user_data->type_instance));
227 user_data->interval = interval;
229 status = tail_match_add_match (obj, match, simple_submit_match,
230 user_data, free);
232 if (status != 0)
233 {
234 match_destroy (match);
235 sfree (user_data);
236 }
238 return (status);
239 } /* int tail_match_add_match_simple */
241 int tail_match_read (cu_tail_match_t *obj)
242 {
243 char buffer[4096];
244 int status;
245 size_t i;
247 status = cu_tail_read (obj->tail, buffer, sizeof (buffer), tail_callback,
248 (void *) obj);
249 if (status != 0)
250 {
251 ERROR ("tail_match: cu_tail_read failed.");
252 return (status);
253 }
255 for (i = 0; i < obj->matches_num; i++)
256 {
257 cu_tail_match_match_t *lt_match = obj->matches + i;
259 if (lt_match->submit == NULL)
260 continue;
262 (*lt_match->submit) (lt_match->match, lt_match->user_data);
263 }
265 return (0);
266 } /* int tail_match_read */
268 /* vim: set sw=2 sts=2 ts=8 : */