b71e205932a57e1ff57c08feb259eb3c83cd09b7
[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 verplant.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 };
41 typedef struct cu_tail_match_simple_s cu_tail_match_simple_t;
42
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;
51
52 struct cu_tail_match_s
53 {
54   int flags;
55   cu_tail_t *tail;
56
57   cu_tail_match_match_t *matches;
58   size_t matches_num;
59 };
60
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];
70
71   match_value = (cu_match_value_t *) match_get_user_data (match);
72   if (match_value == NULL)
73     return (-1);
74
75   values[0] = match_value->value;
76
77   vl.values = values;
78   vl.values_len = 1;
79   vl.time = time (NULL);
80   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
81   sstrncpy (vl.plugin, data->plugin, sizeof (vl.plugin));
82   sstrncpy (vl.plugin_instance, data->plugin_instance,
83       sizeof (vl.plugin_instance));
84   sstrncpy (vl.type_instance, data->type_instance,
85       sizeof (vl.type_instance));
86
87   plugin_dispatch_values (data->type, &vl);
88
89   return (0);
90 } /* int simple_submit_match */
91
92 static int tail_callback (void *data, char *buf, int buflen)
93 {
94   cu_tail_match_t *obj = (cu_tail_match_t *) data;
95   int i;
96
97   for (i = 0; i < obj->matches_num; i++)
98     match_apply (obj->matches[i].match, buf);
99
100   return (0);
101 } /* int tail_callback */
102
103 /*
104  * Public functions
105  */
106 cu_tail_match_t *tail_match_create (const char *filename)
107 {
108   cu_tail_match_t *obj;
109
110   obj = (cu_tail_match_t *) malloc (sizeof (cu_tail_match_t));
111   if (obj == NULL)
112     return (NULL);
113   memset (obj, '\0', sizeof (cu_tail_match_t));
114
115   obj->tail = cu_tail_create (filename);
116   if (obj->tail == NULL)
117   {
118     sfree (obj);
119     return (NULL);
120   }
121
122   return (obj);
123 } /* cu_tail_match_t *tail_match_create */
124
125 void tail_match_destroy (cu_tail_match_t *obj)
126 {
127   int i;
128
129   if (obj == NULL)
130     return;
131
132   if (obj->tail != NULL)
133   {
134     cu_tail_destroy (obj->tail);
135     obj->tail = NULL;
136   }
137
138   for (i = 0; i < obj->matches_num; i++)
139   {
140     cu_tail_match_match_t *match = obj->matches + i;
141     if (match->match != NULL)
142     {
143       match_destroy (match->match);
144       match->match = NULL;
145     }
146
147     if ((match->user_data != NULL)
148         && (match->free != NULL))
149       (*match->free) (match->user_data);
150     match->user_data = NULL;
151   }
152
153   sfree (obj->matches);
154   sfree (obj);
155 } /* void tail_match_destroy */
156
157 int tail_match_add_match (cu_tail_match_t *obj, cu_match_t *match,
158     int (*submit_match) (cu_match_t *match, void *user_data),
159     void *user_data,
160     void (*free_user_data) (void *user_data))
161 {
162   cu_tail_match_match_t *temp;
163
164   temp = (cu_tail_match_match_t *) realloc (obj->matches,
165       sizeof (cu_tail_match_match_t) * (obj->matches_num + 1));
166   if (temp == NULL)
167     return (-1);
168
169   obj->matches = temp;
170   obj->matches_num++;
171
172   temp = obj->matches + (obj->matches_num - 1);
173
174   temp->match = match;
175   temp->user_data = user_data;
176   temp->submit = submit_match;
177   temp->free = free_user_data;
178
179   return (0);
180 } /* int tail_match_add_match */
181
182 int tail_match_add_match_simple (cu_tail_match_t *obj,
183     const char *regex, int ds_type,
184     const char *plugin, const char *plugin_instance,
185     const char *type, const char *type_instance)
186 {
187   cu_match_t *match;
188   cu_tail_match_simple_t *user_data;
189   int status;
190
191   match = match_create_simple (regex, ds_type);
192   if (match == NULL)
193     return (-1);
194
195   user_data = (cu_tail_match_simple_t *) malloc (sizeof (cu_tail_match_simple_t));
196   if (user_data == NULL)
197   {
198     match_destroy (match);
199     return (-1);
200   }
201   memset (user_data, '\0', sizeof (cu_tail_match_simple_t));
202
203   sstrncpy (user_data->plugin, plugin, sizeof (user_data->plugin));
204   if (plugin_instance != NULL)
205     sstrncpy (user_data->plugin_instance, plugin_instance,
206         sizeof (user_data->plugin_instance));
207
208   sstrncpy (user_data->type, type, sizeof (user_data->type));
209   if (type_instance != NULL)
210     sstrncpy (user_data->type_instance, type_instance,
211         sizeof (user_data->type_instance));
212
213   status = tail_match_add_match (obj, match, simple_submit_match,
214       user_data, free);
215
216   if (status != 0)
217   {
218     match_destroy (match);
219     sfree (user_data);
220   }
221
222   return (status);
223 } /* int tail_match_add_match_simple */
224
225 int tail_match_read (cu_tail_match_t *obj)
226 {
227   char buffer[4096];
228   int status;
229   int i;
230
231   status = cu_tail_read (obj->tail, buffer, sizeof (buffer), tail_callback,
232       (void *) obj);
233   if (status != 0)
234   {
235     ERROR ("tail_match: cu_tail_read failed.");
236     return (status);
237   }
238
239   for (i = 0; i < obj->matches_num; i++)
240   {
241     cu_tail_match_match_t *lt_match = obj->matches + i;
242
243     if (lt_match->submit == NULL)
244       continue;
245
246     (*lt_match->submit) (lt_match->match, lt_match->user_data);
247   }
248
249   return (0);
250 } /* int tail_match_read */
251
252 /* vim: set sw=2 sts=2 ts=8 : */