1 /*
2 * collectd - src/utils_tail_match.c
3 * Copyright (C) 2007-2008 C-Ware, Inc.
4 * Copyright (C) 2008 Florian Forster
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Author:
25 * Luke Heberling <lukeh at c-ware.com>
26 * Florian Forster <octo at collectd.org>
27 *
28 * Description:
29 * Encapsulates useful code to plugins which must parse a log file.
30 */
32 #include "collectd.h"
34 #include "common.h"
35 #include "plugin.h"
36 #include "utils_match.h"
37 #include "utils_tail.h"
38 #include "utils_tail_match.h"
40 struct cu_tail_match_simple_s
41 {
42 char plugin[DATA_MAX_NAME_LEN];
43 char plugin_instance[DATA_MAX_NAME_LEN];
44 char type[DATA_MAX_NAME_LEN];
45 char type_instance[DATA_MAX_NAME_LEN];
46 cdtime_t interval;
47 };
48 typedef struct cu_tail_match_simple_s cu_tail_match_simple_t;
50 struct cu_tail_match_match_s
51 {
52 cu_match_t *match;
53 void *user_data;
54 int (*submit) (cu_match_t *match, void *user_data);
55 void (*free) (void *user_data);
56 };
57 typedef struct cu_tail_match_match_s cu_tail_match_match_t;
59 struct cu_tail_match_s
60 {
61 int flags;
62 cu_tail_t *tail;
64 cdtime_t interval;
65 cu_tail_match_match_t *matches;
66 size_t matches_num;
67 };
69 /*
70 * Private functions
71 */
72 static int simple_submit_match (cu_match_t *match, void *user_data)
73 {
74 cu_tail_match_simple_t *data = (cu_tail_match_simple_t *) user_data;
75 cu_match_value_t *match_value;
76 value_list_t vl = VALUE_LIST_INIT;
77 value_t values[1];
79 match_value = (cu_match_value_t *) match_get_user_data (match);
80 if (match_value == NULL)
81 return (-1);
83 if ((match_value->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
84 && (match_value->values_num == 0))
85 values[0].gauge = NAN;
86 else
87 values[0] = match_value->value;
89 vl.values = values;
90 vl.values_len = 1;
91 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
92 sstrncpy (vl.plugin, data->plugin, sizeof (vl.plugin));
93 sstrncpy (vl.plugin_instance, data->plugin_instance,
94 sizeof (vl.plugin_instance));
95 sstrncpy (vl.type, data->type, sizeof (vl.type));
96 sstrncpy (vl.type_instance, data->type_instance,
97 sizeof (vl.type_instance));
99 vl.interval = data->interval;
100 plugin_dispatch_values (&vl);
102 if (match_value->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
103 {
104 match_value->value.gauge = NAN;
105 match_value->values_num = 0;
106 }
108 return (0);
109 } /* int simple_submit_match */
111 static int tail_callback (void *data, char *buf,
112 int __attribute__((unused)) buflen)
113 {
114 cu_tail_match_t *obj = (cu_tail_match_t *) data;
116 for (size_t i = 0; i < obj->matches_num; i++)
117 match_apply (obj->matches[i].match, buf);
119 return (0);
120 } /* int tail_callback */
122 /*
123 * Public functions
124 */
125 cu_tail_match_t *tail_match_create (const char *filename)
126 {
127 cu_tail_match_t *obj;
129 obj = calloc (1, sizeof (*obj));
130 if (obj == NULL)
131 return (NULL);
133 obj->tail = cu_tail_create (filename);
134 if (obj->tail == NULL)
135 {
136 sfree (obj);
137 return (NULL);
138 }
140 return (obj);
141 } /* cu_tail_match_t *tail_match_create */
143 void tail_match_destroy (cu_tail_match_t *obj)
144 {
145 if (obj == NULL)
146 return;
148 if (obj->tail != NULL)
149 {
150 cu_tail_destroy (obj->tail);
151 obj->tail = NULL;
152 }
154 for (size_t i = 0; i < obj->matches_num; i++)
155 {
156 cu_tail_match_match_t *match = obj->matches + i;
157 if (match->match != NULL)
158 {
159 match_destroy (match->match);
160 match->match = NULL;
161 }
163 if ((match->user_data != NULL)
164 && (match->free != NULL))
165 (*match->free) (match->user_data);
166 match->user_data = NULL;
167 }
169 sfree (obj->matches);
170 sfree (obj);
171 } /* void tail_match_destroy */
173 int tail_match_add_match (cu_tail_match_t *obj, cu_match_t *match,
174 int (*submit_match) (cu_match_t *match, void *user_data),
175 void *user_data,
176 void (*free_user_data) (void *user_data))
177 {
178 cu_tail_match_match_t *temp;
180 temp = realloc (obj->matches,
181 sizeof (cu_tail_match_match_t) * (obj->matches_num + 1));
182 if (temp == NULL)
183 return (-1);
185 obj->matches = temp;
186 obj->matches_num++;
188 DEBUG ("tail_match_add_match interval %lf", CDTIME_T_TO_DOUBLE(((cu_tail_match_simple_t *)user_data)->interval));
189 temp = obj->matches + (obj->matches_num - 1);
191 temp->match = match;
192 temp->user_data = user_data;
193 temp->submit = submit_match;
194 temp->free = free_user_data;
196 return (0);
197 } /* int tail_match_add_match */
199 int tail_match_add_match_simple (cu_tail_match_t *obj,
200 const char *regex, const char *excluderegex, int ds_type,
201 const char *plugin, const char *plugin_instance,
202 const char *type, const char *type_instance, const cdtime_t interval)
203 {
204 cu_match_t *match;
205 cu_tail_match_simple_t *user_data;
206 int status;
208 match = match_create_simple (regex, excluderegex, ds_type);
209 if (match == NULL)
210 return (-1);
212 user_data = calloc (1, sizeof (*user_data));
213 if (user_data == NULL)
214 {
215 match_destroy (match);
216 return (-1);
217 }
219 sstrncpy (user_data->plugin, plugin, sizeof (user_data->plugin));
220 if (plugin_instance != NULL)
221 sstrncpy (user_data->plugin_instance, plugin_instance,
222 sizeof (user_data->plugin_instance));
224 sstrncpy (user_data->type, type, sizeof (user_data->type));
225 if (type_instance != NULL)
226 sstrncpy (user_data->type_instance, type_instance,
227 sizeof (user_data->type_instance));
229 user_data->interval = interval;
231 status = tail_match_add_match (obj, match, simple_submit_match,
232 user_data, free);
234 if (status != 0)
235 {
236 match_destroy (match);
237 sfree (user_data);
238 }
240 return (status);
241 } /* int tail_match_add_match_simple */
243 int tail_match_read (cu_tail_match_t *obj)
244 {
245 char buffer[4096];
246 int status;
248 status = cu_tail_read (obj->tail, buffer, sizeof (buffer), tail_callback,
249 (void *) obj);
250 if (status != 0)
251 {
252 ERROR ("tail_match: cu_tail_read failed.");
253 return (status);
254 }
256 for (size_t i = 0; i < obj->matches_num; i++)
257 {
258 cu_tail_match_match_t *lt_match = obj->matches + i;
260 if (lt_match->submit == NULL)
261 continue;
263 (*lt_match->submit) (lt_match->match, lt_match->user_data);
264 }
266 return (0);
267 } /* int tail_match_read */
269 /* vim: set sw=2 sts=2 ts=8 : */