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 };
41 typedef struct cu_tail_match_simple_s cu_tail_match_simple_t;
43 struct cu_tail_match_match_s
44 {
45 cu_match_t *match;
46 void *user_data;
47 int (*submit) (cu_match_t *match, void *user_data);
48 void (*free) (void *user_data);
49 };
50 typedef struct cu_tail_match_match_s cu_tail_match_match_t;
52 struct cu_tail_match_s
53 {
54 int flags;
55 cu_tail_t *tail;
57 cu_tail_match_match_t *matches;
58 size_t matches_num;
59 };
61 /*
62 * Private functions
63 */
64 static int simple_submit_match (cu_match_t *match, void *user_data)
65 {
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;
69 value_t values[1];
71 match_value = (cu_match_value_t *) match_get_user_data (match);
72 if (match_value == NULL)
73 return (-1);
75 if ((match_value->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
76 && (match_value->values_num == 0))
77 values[0].gauge = NAN;
78 else
79 values[0] = match_value->value;
81 vl.values = values;
82 vl.values_len = 1;
83 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
84 sstrncpy (vl.plugin, data->plugin, sizeof (vl.plugin));
85 sstrncpy (vl.plugin_instance, data->plugin_instance,
86 sizeof (vl.plugin_instance));
87 sstrncpy (vl.type, data->type, sizeof (vl.type));
88 sstrncpy (vl.type_instance, data->type_instance,
89 sizeof (vl.type_instance));
91 plugin_dispatch_values (&vl);
93 if (match_value->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
94 {
95 match_value->value.gauge = NAN;
96 match_value->values_num = 0;
97 }
99 return (0);
100 } /* int simple_submit_match */
102 static int tail_callback (void *data, char *buf,
103 int __attribute__((unused)) buflen)
104 {
105 cu_tail_match_t *obj = (cu_tail_match_t *) data;
106 size_t i;
108 for (i = 0; i < obj->matches_num; i++)
109 match_apply (obj->matches[i].match, buf);
111 return (0);
112 } /* int tail_callback */
114 /*
115 * Public functions
116 */
117 cu_tail_match_t *tail_match_create (const char *filename)
118 {
119 cu_tail_match_t *obj;
121 obj = (cu_tail_match_t *) malloc (sizeof (cu_tail_match_t));
122 if (obj == NULL)
123 return (NULL);
124 memset (obj, '\0', sizeof (cu_tail_match_t));
126 obj->tail = cu_tail_create (filename);
127 if (obj->tail == NULL)
128 {
129 sfree (obj);
130 return (NULL);
131 }
133 return (obj);
134 } /* cu_tail_match_t *tail_match_create */
136 void tail_match_destroy (cu_tail_match_t *obj)
137 {
138 size_t i;
140 if (obj == NULL)
141 return;
143 if (obj->tail != NULL)
144 {
145 cu_tail_destroy (obj->tail);
146 obj->tail = NULL;
147 }
149 for (i = 0; i < obj->matches_num; i++)
150 {
151 cu_tail_match_match_t *match = obj->matches + i;
152 if (match->match != NULL)
153 {
154 match_destroy (match->match);
155 match->match = NULL;
156 }
158 if ((match->user_data != NULL)
159 && (match->free != NULL))
160 (*match->free) (match->user_data);
161 match->user_data = NULL;
162 }
164 sfree (obj->matches);
165 sfree (obj);
166 } /* void tail_match_destroy */
168 int tail_match_add_match (cu_tail_match_t *obj, cu_match_t *match,
169 int (*submit_match) (cu_match_t *match, void *user_data),
170 void *user_data,
171 void (*free_user_data) (void *user_data))
172 {
173 cu_tail_match_match_t *temp;
175 temp = (cu_tail_match_match_t *) realloc (obj->matches,
176 sizeof (cu_tail_match_match_t) * (obj->matches_num + 1));
177 if (temp == NULL)
178 return (-1);
180 obj->matches = temp;
181 obj->matches_num++;
183 temp = obj->matches + (obj->matches_num - 1);
185 temp->match = match;
186 temp->user_data = user_data;
187 temp->submit = submit_match;
188 temp->free = free_user_data;
190 return (0);
191 } /* int tail_match_add_match */
193 int tail_match_add_match_simple (cu_tail_match_t *obj,
194 const char *regex, const char *excluderegex, int ds_type,
195 const char *plugin, const char *plugin_instance,
196 const char *type, const char *type_instance)
197 {
198 cu_match_t *match;
199 cu_tail_match_simple_t *user_data;
200 int status;
202 match = match_create_simple (regex, excluderegex, ds_type);
203 if (match == NULL)
204 return (-1);
206 user_data = (cu_tail_match_simple_t *) malloc (sizeof (cu_tail_match_simple_t));
207 if (user_data == NULL)
208 {
209 match_destroy (match);
210 return (-1);
211 }
212 memset (user_data, '\0', sizeof (cu_tail_match_simple_t));
214 sstrncpy (user_data->plugin, plugin, sizeof (user_data->plugin));
215 if (plugin_instance != NULL)
216 sstrncpy (user_data->plugin_instance, plugin_instance,
217 sizeof (user_data->plugin_instance));
219 sstrncpy (user_data->type, type, sizeof (user_data->type));
220 if (type_instance != NULL)
221 sstrncpy (user_data->type_instance, type_instance,
222 sizeof (user_data->type_instance));
224 status = tail_match_add_match (obj, match, simple_submit_match,
225 user_data, free);
227 if (status != 0)
228 {
229 match_destroy (match);
230 sfree (user_data);
231 }
233 return (status);
234 } /* int tail_match_add_match_simple */
236 int tail_match_read (cu_tail_match_t *obj)
237 {
238 char buffer[4096];
239 int status;
240 size_t i;
242 status = cu_tail_read (obj->tail, buffer, sizeof (buffer), tail_callback,
243 (void *) obj);
244 if (status != 0)
245 {
246 ERROR ("tail_match: cu_tail_read failed.");
247 return (status);
248 }
250 for (i = 0; i < obj->matches_num; i++)
251 {
252 cu_tail_match_match_t *lt_match = obj->matches + i;
254 if (lt_match->submit == NULL)
255 continue;
257 (*lt_match->submit) (lt_match->match, lt_match->user_data);
258 }
260 return (0);
261 } /* int tail_match_read */
263 /* vim: set sw=2 sts=2 ts=8 : */