src/utils_logtail.[ch]: Implement a module that parses logfiles using the `utils_tail...
[collectd.git] / src / utils_logtail.c
1 /*
2  * collectd - src/utils_logtail.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_logtail.h"
33
34 struct cu_logtail_match_s
35 {
36   cu_match_t *match;
37   void *user_data;
38   int (*submit) (cu_match_t *match, void *user_data);
39   void (*free) (void *user_data);
40 };
41 typedef struct cu_logtail_match_s cu_logtail_match_t;
42
43 struct cu_logtail_s
44 {
45   int flags;
46   cu_tail_t *tail;
47
48   cu_logtail_match_t *matches;
49   size_t matches_num;
50 };
51
52 /*
53  * Private functions
54  */
55 static int simple_submit_match (cu_match_t *match, void *user_data)
56 {
57   cu_logtail_simple_t *data = (cu_logtail_simple_t *) user_data;
58   cu_match_value_t *match_value;
59   value_list_t vl = VALUE_LIST_INIT;
60   value_t values[1];
61
62   match_value = (cu_match_value_t *) match_get_user_data (match);
63   if (match_value == NULL)
64     return (-1);
65
66   values[0] = match_value->value;
67
68   vl.values = values;
69   vl.values_len = 1;
70   vl.time = time (NULL);
71   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
72   sstrncpy (vl.plugin, data->plugin, sizeof (vl.plugin));
73   sstrncpy (vl.plugin_instance, data->plugin_instance,
74       sizeof (vl.plugin_instance));
75   sstrncpy (vl.type_instance, data->type_instance,
76       sizeof (vl.type_instance));
77
78   plugin_dispatch_values (data->type, &vl);
79
80   return (0);
81 } /* int simple_submit_match */
82
83 static int tail_callback (void *data, char *buf, int buflen)
84 {
85   cu_logtail_t *obj = (cu_logtail_t *) data;
86   int i;
87
88   for (i = 0; i < obj->matches_num; i++)
89     match_apply (obj->matches[i].match, buf);
90
91   return (0);
92 } /* int tail_callback */
93
94 /*
95  * Public functions
96  */
97 cu_logtail_t *logtail_create (const char *filename)
98 {
99   cu_logtail_t *obj;
100
101   obj = (cu_logtail_t *) malloc (sizeof (cu_logtail_t));
102   if (obj == NULL)
103     return (NULL);
104   memset (obj, '\0', sizeof (cu_logtail_t));
105
106   obj->tail = cu_tail_create (filename);
107   if (obj->tail == NULL)
108   {
109     sfree (obj);
110     return (NULL);
111   }
112
113   return (obj);
114 } /* cu_logtail_t *logtail_create */
115
116 void logtail_destroy (cu_logtail_t *obj)
117 {
118   int i;
119
120   if (obj == NULL)
121     return;
122
123   if (obj->tail != NULL)
124   {
125     cu_tail_destroy (obj->tail);
126     obj->tail = NULL;
127   }
128
129   for (i = 0; i < obj->matches_num; i++)
130   {
131     cu_logtail_match_t *match = obj->matches + i;
132     if (match->match != NULL)
133     {
134       match_destroy (match->match);
135       match->match = NULL;
136     }
137
138     if ((match->user_data != NULL)
139         && (match->free != NULL))
140       (*match->free) (match->user_data);
141     match->user_data = NULL;
142   }
143
144   sfree (obj->matches);
145   sfree (obj);
146 } /* void logtail_destroy */
147
148 int logtail_add_match (cu_logtail_t *obj, cu_match_t *match,
149     int (*submit_match) (cu_match_t *match, void *user_data),
150     void *user_data,
151     void (*free_user_data) (void *user_data))
152 {
153   cu_logtail_match_t *temp;
154
155   temp = (cu_logtail_match_t *) realloc (obj->matches,
156       sizeof (cu_logtail_match_t) * (obj->matches_num + 1));
157   if (temp == NULL)
158     return (-1);
159
160   obj->matches = temp;
161   obj->matches_num++;
162
163   temp = obj->matches + (obj->matches_num - 1);
164
165   temp->match = match;
166   temp->user_data = user_data;
167   temp->submit = submit_match;
168   temp->free = free_user_data;
169
170   return (0);
171 } /* int logtail_add_match */
172
173 int logtail_add_match_simple (cu_logtail_t *obj,
174     const char *regex, int ds_type,
175     const char *plugin, const char *plugin_instance,
176     const char *type, const char *type_instance)
177 {
178   cu_match_t *match;
179   cu_logtail_simple_t *user_data;
180   int status;
181
182   match = match_create_simple (regex, ds_type);
183   if (match == NULL)
184     return (-1);
185
186   user_data = (cu_logtail_simple_t *) malloc (sizeof (cu_logtail_simple_t));
187   if (user_data == NULL)
188   {
189     match_destroy (match);
190     return (-1);
191   }
192
193   sstrncpy (user_data->plugin, plugin, sizeof (user_data->plugin));
194   sstrncpy (user_data->plugin_instance, plugin_instance,
195       sizeof (user_data->plugin_instance));
196   sstrncpy (user_data->type, type, sizeof (user_data->type));
197   sstrncpy (user_data->type_instance, type_instance,
198       sizeof (user_data->type_instance));
199
200   status = logtail_add_match (obj, match, simple_submit_match,
201       user_data, free);
202
203   if (status != 0)
204   {
205     match_destroy (match);
206     sfree (user_data);
207   }
208
209   return (status);
210 } /* int logtail_add_match_simple */
211
212 int logtail_read (cu_logtail_t *obj)
213 {
214   char buffer[4096];
215   int status;
216   int i;
217
218   status = cu_tail_read (obj->tail, buffer, sizeof (buffer), tail_callback,
219       (void *) obj);
220   if (status != 0)
221     return (status);
222
223   for (i = 0; i < obj->matches_num; i++)
224   {
225     cu_logtail_match_t *lt_match = obj->matches + i;
226
227     (*lt_match->submit) (lt_match->match, lt_match->user_data);
228   }
229
230   return (0);
231 } /* int logtail_read */
232
233 /* vim: set sw=2 sts=2 ts=8 : */