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