Merge pull request #3329 from efuss/fix-3311
[collectd.git] / src / utils_tail_match.h
1 /*
2  * collectd - src/utils_tail_match.h
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  * Authors:
25  *   Luke Heberling <lukeh at c-ware.com>
26  *   Florian Forster <octo at collectd.org>
27  *
28  * Description:
29  *   `tail_match' uses `utils_tail' and `utils_match' to tail a file and try to
30  *   match it using several regular expressions. Matches are then passed to
31  *   user-provided callback functions or default handlers. This should keep all
32  *   of the parsing logic out of the actual plugin, which only operate with
33  *   regular expressions.
34  */
35
36 #ifndef UTILS_TAIL_MATCH_H
37 #define UTILS_TAIL_MATCH_H 1
38
39 #include "utils/latency/latency_config.h"
40 #include "utils/match/match.h"
41
42 struct cu_tail_match_s;
43 typedef struct cu_tail_match_s cu_tail_match_t;
44
45 /*
46  * NAME
47  *   tail_match_create
48  *
49  * DESCRIPTION
50  *   Allocates, initializes and returns a new `cu_tail_match_t' object.
51  *
52  * PARAMETERS
53  *   `filename'  The name to read data from.
54  *
55  * RETURN VALUE
56  *   Returns NULL upon failure, non-NULL otherwise.
57  */
58 cu_tail_match_t *tail_match_create(const char *filename);
59
60 /*
61  * NAME
62  *   tail_match_destroy
63  *
64  * DESCRIPTION
65  *   Releases resources used by the `cu_tail_match_t' object.
66  *
67  * PARAMETERS
68  *   The object to destroy.
69  */
70 void tail_match_destroy(cu_tail_match_t *obj);
71
72 /*
73  * NAME
74  *   tail_match_add_match
75  *
76  * DESCRIPTION
77  *   Adds a match, in form of a `cu_match_t' object, to the object.
78  *   After data has been read from the logfile (using utils_tail) the callback
79  *   function `submit_match' is called with the match object and the user
80  *   supplied data.
81  *   Please note that his function is called regardless whether this match
82  *   matched any lines recently or not.
83  *   When `tail_match_destroy' is called the `user_data' pointer is freed using
84  *   the `free_user_data' callback - if it is not NULL.
85  *   When using this interface the `tail_match' module doesn't dispatch any
86  *   values itself - all that has to happen in either the match-callbacks or
87  *   the submit_match callback.
88  *
89  * RETURN VALUE
90  *   Zero upon success, non-zero otherwise.
91  */
92 int tail_match_add_match(cu_tail_match_t *obj, cu_match_t *match,
93                          int (*submit_match)(cu_match_t *match,
94                                              void *user_data),
95                          void *user_data,
96                          void (*free_user_data)(void *user_data));
97
98 /*
99  * NAME
100  *  tail_match_add_match_simple
101  *
102  * DESCRIPTION
103  *  A simplified version of `tail_match_add_match'. The regular expression
104  *  `regex' must match a number, which is then dispatched according to
105  * `ds_type'.
106  *  See the `match_create_simple' function in utils_match.h for a description
107  *  how this flag effects calculation of a new value.
108  *  The values gathered are dispatched by the tail_match module in this case.
109  *  The passed `plugin', `plugin_instance', `type', and `type_instance' are
110  *  directly used when submitting these values.
111  *  With excluderegex it is possible to exlude lines from the match.
112  *  The `latency_cfg' specifies configuration for submitting latency.
113  *
114  * RETURN VALUE
115  *   Zero upon success, non-zero otherwise.
116  */
117 int tail_match_add_match_simple(cu_tail_match_t *obj, const char *regex,
118                                 const char *excluderegex, int ds_type,
119                                 const char *plugin, const char *plugin_instance,
120                                 const char *type, const char *type_instance,
121                                 const latency_config_t latency_cfg);
122
123 /*
124  * NAME
125  *   tail_match_read
126  *
127  * DESCRIPTION
128  *   This function should be called periodically by plugins. It reads new lines
129  *   from the logfile using `utils_tail' and tries to match them using all
130  *   added `utils_match' objects.
131  *   After all lines have been read and processed, the submit_match callback is
132  *   called or, in case of tail_match_add_match_simple, the data is dispatched
133  *   to the daemon directly.
134  *
135  * RETURN VALUE
136  *   Zero on success, nonzero on failure.
137  */
138 int tail_match_read(cu_tail_match_t *obj);
139
140 #endif /* UTILS_TAIL_MATCH_H */