{GPL, other}: Relicense to MIT license.
[collectd.git] / src / utils_tail_match.c
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 collectd.org>
22  *
23  * Description:
24  *   Encapsulates useful code to plugins which must parse a log file.
25  */
26
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"
33
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;
43
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;
52
53 struct cu_tail_match_s
54 {
55   int flags;
56   cu_tail_t *tail;
57
58   cdtime_t interval;
59   cu_tail_match_match_t *matches;
60   size_t matches_num;
61 };
62
63 /*
64  * Private functions
65  */
66 static int simple_submit_match (cu_match_t *match, void *user_data)
67 {
68   cu_tail_match_simple_t *data = (cu_tail_match_simple_t *) user_data;
69   cu_match_value_t *match_value;
70   value_list_t vl = VALUE_LIST_INIT;
71   value_t values[1];
72
73   match_value = (cu_match_value_t *) match_get_user_data (match);
74   if (match_value == NULL)
75     return (-1);
76
77   if ((match_value->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
78       && (match_value->values_num == 0))
79     values[0].gauge = NAN;
80   else
81     values[0] = match_value->value;
82
83   vl.values = values;
84   vl.values_len = 1;
85   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
86   sstrncpy (vl.plugin, data->plugin, sizeof (vl.plugin));
87   sstrncpy (vl.plugin_instance, data->plugin_instance,
88       sizeof (vl.plugin_instance));
89   sstrncpy (vl.type, data->type, sizeof (vl.type));
90   sstrncpy (vl.type_instance, data->type_instance,
91       sizeof (vl.type_instance));
92
93   vl.interval = data->interval;
94   plugin_dispatch_values (&vl);
95
96   if (match_value->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
97   {
98     match_value->value.gauge = NAN;
99     match_value->values_num = 0;
100   }
101
102   return (0);
103 } /* int simple_submit_match */
104
105 static int tail_callback (void *data, char *buf,
106     int __attribute__((unused)) buflen)
107 {
108   cu_tail_match_t *obj = (cu_tail_match_t *) data;
109   size_t i;
110
111   for (i = 0; i < obj->matches_num; i++)
112     match_apply (obj->matches[i].match, buf);
113
114   return (0);
115 } /* int tail_callback */
116
117 /*
118  * Public functions
119  */
120 cu_tail_match_t *tail_match_create (const char *filename)
121 {
122   cu_tail_match_t *obj;
123
124   obj = (cu_tail_match_t *) malloc (sizeof (cu_tail_match_t));
125   if (obj == NULL)
126     return (NULL);
127   memset (obj, '\0', sizeof (cu_tail_match_t));
128
129   obj->tail = cu_tail_create (filename);
130   if (obj->tail == NULL)
131   {
132     sfree (obj);
133     return (NULL);
134   }
135
136   return (obj);
137 } /* cu_tail_match_t *tail_match_create */
138
139 void tail_match_destroy (cu_tail_match_t *obj)
140 {
141   size_t i;
142
143   if (obj == NULL)
144     return;
145
146   if (obj->tail != NULL)
147   {
148     cu_tail_destroy (obj->tail);
149     obj->tail = NULL;
150   }
151
152   for (i = 0; i < obj->matches_num; i++)
153   {
154     cu_tail_match_match_t *match = obj->matches + i;
155     if (match->match != NULL)
156     {
157       match_destroy (match->match);
158       match->match = NULL;
159     }
160
161     if ((match->user_data != NULL)
162         && (match->free != NULL))
163       (*match->free) (match->user_data);
164     match->user_data = NULL;
165   }
166
167   sfree (obj->matches);
168   sfree (obj);
169 } /* void tail_match_destroy */
170
171 int tail_match_add_match (cu_tail_match_t *obj, cu_match_t *match,
172     int (*submit_match) (cu_match_t *match, void *user_data),
173     void *user_data,
174     void (*free_user_data) (void *user_data))
175 {
176   cu_tail_match_match_t *temp;
177
178   temp = (cu_tail_match_match_t *) realloc (obj->matches,
179       sizeof (cu_tail_match_match_t) * (obj->matches_num + 1));
180   if (temp == NULL)
181     return (-1);
182
183   obj->matches = temp;
184   obj->matches_num++;
185
186   DEBUG ("tail_match_add_match interval %lf", CDTIME_T_TO_DOUBLE(((cu_tail_match_simple_t *)user_data)->interval));
187   temp = obj->matches + (obj->matches_num - 1);
188
189   temp->match = match;
190   temp->user_data = user_data;
191   temp->submit = submit_match;
192   temp->free = free_user_data;
193
194   return (0);
195 } /* int tail_match_add_match */
196
197 int tail_match_add_match_simple (cu_tail_match_t *obj,
198     const char *regex, const char *excluderegex, int ds_type,
199     const char *plugin, const char *plugin_instance,
200     const char *type, const char *type_instance, const cdtime_t interval)
201 {
202   cu_match_t *match;
203   cu_tail_match_simple_t *user_data;
204   int status;
205
206   match = match_create_simple (regex, excluderegex, ds_type);
207   if (match == NULL)
208     return (-1);
209
210   user_data = (cu_tail_match_simple_t *) malloc (sizeof (cu_tail_match_simple_t));
211   if (user_data == NULL)
212   {
213     match_destroy (match);
214     return (-1);
215   }
216   memset (user_data, '\0', sizeof (cu_tail_match_simple_t));
217
218   sstrncpy (user_data->plugin, plugin, sizeof (user_data->plugin));
219   if (plugin_instance != NULL)
220     sstrncpy (user_data->plugin_instance, plugin_instance,
221         sizeof (user_data->plugin_instance));
222
223   sstrncpy (user_data->type, type, sizeof (user_data->type));
224   if (type_instance != NULL)
225     sstrncpy (user_data->type_instance, type_instance,
226         sizeof (user_data->type_instance));
227
228   user_data->interval = interval;
229
230   status = tail_match_add_match (obj, match, simple_submit_match,
231       user_data, free);
232
233   if (status != 0)
234   {
235     match_destroy (match);
236     sfree (user_data);
237   }
238
239   return (status);
240 } /* int tail_match_add_match_simple */
241
242 int tail_match_read (cu_tail_match_t *obj)
243 {
244   char buffer[4096];
245   int status;
246   size_t i;
247
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   }
255
256   for (i = 0; i < obj->matches_num; i++)
257   {
258     cu_tail_match_match_t *lt_match = obj->matches + i;
259
260     if (lt_match->submit == NULL)
261       continue;
262
263     (*lt_match->submit) (lt_match->match, lt_match->user_data);
264   }
265
266   return (0);
267 } /* int tail_match_read */
268
269 /* vim: set sw=2 sts=2 ts=8 : */