Tree wide: Don't set vl.host to hostname_g in plugin code.
[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
34 #include "common.h"
35 #include "plugin.h"
36 #include "utils_match.h"
37 #include "utils_tail.h"
38 #include "utils_tail_match.h"
39
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;
49
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;
58
59 struct cu_tail_match_s
60 {
61   int flags;
62   cu_tail_t *tail;
63
64   cdtime_t interval;
65   cu_tail_match_match_t *matches;
66   size_t matches_num;
67 };
68
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];
78
79   match_value = (cu_match_value_t *) match_get_user_data (match);
80   if (match_value == NULL)
81     return (-1);
82
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;
88
89   vl.values = values;
90   vl.values_len = 1;
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
115   for (size_t i = 0; i < obj->matches_num; i++)
116     match_apply (obj->matches[i].match, buf);
117
118   return (0);
119 } /* int tail_callback */
120
121 /*
122  * Public functions
123  */
124 cu_tail_match_t *tail_match_create (const char *filename)
125 {
126   cu_tail_match_t *obj;
127
128   obj = calloc (1, sizeof (*obj));
129   if (obj == NULL)
130     return (NULL);
131
132   obj->tail = cu_tail_create (filename);
133   if (obj->tail == NULL)
134   {
135     sfree (obj);
136     return (NULL);
137   }
138
139   return (obj);
140 } /* cu_tail_match_t *tail_match_create */
141
142 void tail_match_destroy (cu_tail_match_t *obj)
143 {
144   if (obj == NULL)
145     return;
146
147   if (obj->tail != NULL)
148   {
149     cu_tail_destroy (obj->tail);
150     obj->tail = NULL;
151   }
152
153   for (size_t i = 0; i < obj->matches_num; i++)
154   {
155     cu_tail_match_match_t *match = obj->matches + i;
156     if (match->match != NULL)
157     {
158       match_destroy (match->match);
159       match->match = NULL;
160     }
161
162     if ((match->user_data != NULL)
163         && (match->free != NULL))
164       (*match->free) (match->user_data);
165     match->user_data = NULL;
166   }
167
168   sfree (obj->matches);
169   sfree (obj);
170 } /* void tail_match_destroy */
171
172 int tail_match_add_match (cu_tail_match_t *obj, cu_match_t *match,
173     int (*submit_match) (cu_match_t *match, void *user_data),
174     void *user_data,
175     void (*free_user_data) (void *user_data))
176 {
177   cu_tail_match_match_t *temp;
178
179   temp = realloc (obj->matches,
180       sizeof (cu_tail_match_match_t) * (obj->matches_num + 1));
181   if (temp == NULL)
182     return (-1);
183
184   obj->matches = temp;
185   obj->matches_num++;
186
187   DEBUG ("tail_match_add_match interval %lf", CDTIME_T_TO_DOUBLE(((cu_tail_match_simple_t *)user_data)->interval));
188   temp = obj->matches + (obj->matches_num - 1);
189
190   temp->match = match;
191   temp->user_data = user_data;
192   temp->submit = submit_match;
193   temp->free = free_user_data;
194
195   return (0);
196 } /* int tail_match_add_match */
197
198 int tail_match_add_match_simple (cu_tail_match_t *obj,
199     const char *regex, const char *excluderegex, int ds_type,
200     const char *plugin, const char *plugin_instance,
201     const char *type, const char *type_instance, const cdtime_t interval)
202 {
203   cu_match_t *match;
204   cu_tail_match_simple_t *user_data;
205   int status;
206
207   match = match_create_simple (regex, excluderegex, ds_type);
208   if (match == NULL)
209     return (-1);
210
211   user_data = calloc (1, sizeof (*user_data));
212   if (user_data == NULL)
213   {
214     match_destroy (match);
215     return (-1);
216   }
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
247   status = cu_tail_read (obj->tail, buffer, sizeof (buffer), tail_callback,
248       (void *) obj);
249   if (status != 0)
250   {
251     ERROR ("tail_match: cu_tail_read failed.");
252     return (status);
253   }
254
255   for (size_t i = 0; i < obj->matches_num; i++)
256   {
257     cu_tail_match_match_t *lt_match = obj->matches + i;
258
259     if (lt_match->submit == NULL)
260       continue;
261
262     (*lt_match->submit) (lt_match->match, lt_match->user_data);
263   }
264
265   return (0);
266 } /* int tail_match_read */
267
268 /* vim: set sw=2 sts=2 ts=8 : */