2 * collectd - src/tail.c
3 * Copyright (C) 2008 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
31 #include "utils_latency_config.h"
32 #include "utils_tail_match.h"
36 * <File "/var/log/exim4/mainlog">
41 * Regex "S=([1-9][0-9]*)"
42 * ExcludeRegex "U=root.*S="
51 struct ctail_config_match_s {
57 latency_config_t latency;
59 typedef struct ctail_config_match_s ctail_config_match_t;
61 static size_t tail_file_num;
63 static int ctail_read(user_data_t *ud);
65 static void ctail_match_free(void *arg) {
66 tail_match_destroy((cu_tail_match_t *)arg);
67 } /* void ctail_match_free */
69 static int ctail_config_add_match_dstype(ctail_config_match_t *cm,
71 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
72 WARNING("tail plugin: `DSType' needs exactly one string argument.");
76 char const *ds_type = ci->values[0].value.string;
77 if (strncasecmp("Gauge", ds_type, strlen("Gauge")) == 0) {
78 cm->flags = UTILS_MATCH_DS_TYPE_GAUGE;
79 if (strcasecmp("GaugeAverage", ds_type) == 0)
80 cm->flags |= UTILS_MATCH_CF_GAUGE_AVERAGE;
81 else if (strcasecmp("GaugeMin", ds_type) == 0)
82 cm->flags |= UTILS_MATCH_CF_GAUGE_MIN;
83 else if (strcasecmp("GaugeMax", ds_type) == 0)
84 cm->flags |= UTILS_MATCH_CF_GAUGE_MAX;
85 else if (strcasecmp("GaugeLast", ds_type) == 0)
86 cm->flags |= UTILS_MATCH_CF_GAUGE_LAST;
87 else if (strcasecmp("GaugeInc", ds_type) == 0)
88 cm->flags |= UTILS_MATCH_CF_GAUGE_INC;
89 else if (strcasecmp("GaugeAdd", ds_type) == 0)
90 cm->flags |= UTILS_MATCH_CF_GAUGE_ADD;
91 else if (strcasecmp("GaugePersist", ci->values[0].value.string) == 0)
92 cm->flags |= UTILS_MATCH_CF_GAUGE_PERSIST;
95 } else if (strcasecmp("Distribution", ds_type) == 0) {
96 cm->flags = UTILS_MATCH_DS_TYPE_GAUGE | UTILS_MATCH_CF_GAUGE_DIST;
98 int status = latency_config(&cm->latency, ci);
101 } else if (strncasecmp("Counter", ds_type, strlen("Counter")) == 0) {
102 cm->flags = UTILS_MATCH_DS_TYPE_COUNTER;
103 if (strcasecmp("CounterSet", ds_type) == 0)
104 cm->flags |= UTILS_MATCH_CF_COUNTER_SET;
105 else if (strcasecmp("CounterAdd", ds_type) == 0)
106 cm->flags |= UTILS_MATCH_CF_COUNTER_ADD;
107 else if (strcasecmp("CounterInc", ds_type) == 0)
108 cm->flags |= UTILS_MATCH_CF_COUNTER_INC;
111 } else if (strncasecmp("Derive", ds_type, strlen("Derive")) == 0) {
112 cm->flags = UTILS_MATCH_DS_TYPE_DERIVE;
113 if (strcasecmp("DeriveSet", ds_type) == 0)
114 cm->flags |= UTILS_MATCH_CF_DERIVE_SET;
115 else if (strcasecmp("DeriveAdd", ds_type) == 0)
116 cm->flags |= UTILS_MATCH_CF_DERIVE_ADD;
117 else if (strcasecmp("DeriveInc", ds_type) == 0)
118 cm->flags |= UTILS_MATCH_CF_DERIVE_INC;
121 } else if (strncasecmp("Absolute", ds_type, strlen("Absolute")) == 0) {
122 cm->flags = UTILS_MATCH_DS_TYPE_ABSOLUTE;
123 if (strcasecmp("AbsoluteSet", ds_type) == 0)
124 cm->flags |= UTILS_MATCH_CF_ABSOLUTE_SET;
131 if (cm->flags == 0) {
132 WARNING("tail plugin: `%s' is not a valid argument to `DSType'.",
133 ci->values[0].value.string);
138 } /* int ctail_config_add_match_dstype */
140 static int ctail_config_add_match(cu_tail_match_t *tm, const char *plugin_name,
141 const char *plugin_instance,
142 oconfig_item_t *ci) {
143 ctail_config_match_t cm = {0};
146 if (ci->values_num != 0) {
147 WARNING("tail plugin: Ignoring arguments for the `Match' block.");
151 for (int i = 0; i < ci->children_num; i++) {
152 oconfig_item_t *option = ci->children + i;
154 if (strcasecmp("Regex", option->key) == 0)
155 status = cf_util_get_string(option, &cm.regex);
156 else if (strcasecmp("ExcludeRegex", option->key) == 0)
157 status = cf_util_get_string(option, &cm.excluderegex);
158 else if (strcasecmp("DSType", option->key) == 0)
159 status = ctail_config_add_match_dstype(&cm, option);
160 else if (strcasecmp("Type", option->key) == 0)
161 status = cf_util_get_string(option, &cm.type);
162 else if (strcasecmp("Instance", option->key) == 0)
163 status = cf_util_get_string(option, &cm.type_instance);
165 WARNING("tail plugin: Option `%s' not allowed here.", option->key);
171 } /* for (i = 0; i < ci->children_num; i++) */
173 while (status == 0) {
174 if (cm.regex == NULL) {
175 WARNING("tail plugin: `Regex' missing in `Match' block.");
180 if (cm.type == NULL) {
181 WARNING("tail plugin: `Type' missing in `Match' block.");
187 WARNING("tail plugin: `DSType' missing in `Match' block.");
193 } /* while (status == 0) */
196 // TODO(octo): there's nothing "simple" about the latency stuff …
197 status = tail_match_add_match_simple(
198 tm, cm.regex, cm.excluderegex, cm.flags,
199 (plugin_name != NULL) ? plugin_name : "tail", plugin_instance, cm.type,
200 cm.type_instance, cm.latency);
203 ERROR("tail plugin: tail_match_add_match_simple failed.");
207 sfree(cm.excluderegex);
209 sfree(cm.type_instance);
210 latency_config_free(cm.latency);
213 } /* int ctail_config_add_match */
215 static int ctail_config_add_file(oconfig_item_t *ci) {
217 cdtime_t interval = 0;
218 char *plugin_name = NULL;
219 char *plugin_instance = NULL;
222 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
223 WARNING("tail plugin: `File' needs exactly one string argument.");
227 tm = tail_match_create(ci->values[0].value.string);
229 ERROR("tail plugin: tail_match_create (%s) failed.",
230 ci->values[0].value.string);
234 for (int i = 0; i < ci->children_num; i++) {
235 oconfig_item_t *option = ci->children + i;
238 if (strcasecmp("Plugin", option->key) == 0)
239 status = cf_util_get_string(option, &plugin_name);
240 else if (strcasecmp("Instance", option->key) == 0)
241 status = cf_util_get_string(option, &plugin_instance);
242 else if (strcasecmp("Interval", option->key) == 0)
243 cf_util_get_cdtime(option, &interval);
244 else if (strcasecmp("Match", option->key) == 0) {
245 status = ctail_config_add_match(tm, plugin_name, plugin_instance, option);
248 /* Be mild with failed matches.. */
256 } /* for (i = 0; i < ci->children_num; i++) */
259 sfree(plugin_instance);
261 if (num_matches == 0) {
262 ERROR("tail plugin: No (valid) matches found for file `%s'.",
263 ci->values[0].value.string);
264 tail_match_destroy(tm);
269 snprintf(str, sizeof(str), "tail-%zu", tail_file_num++);
271 plugin_register_complex_read(
272 NULL, str, ctail_read, interval,
273 &(user_data_t){.data = tm, .free_func = ctail_match_free});
276 } /* int ctail_config_add_file */
278 static int ctail_config(oconfig_item_t *ci) {
279 for (int i = 0; i < ci->children_num; i++) {
280 oconfig_item_t *option = ci->children + i;
282 if (strcasecmp("File", option->key) == 0)
283 ctail_config_add_file(option);
285 WARNING("tail plugin: Option `%s' not allowed here.", option->key);
287 } /* for (i = 0; i < ci->children_num; i++) */
290 } /* int ctail_config */
292 static int ctail_read(user_data_t *ud) {
295 status = tail_match_read((cu_tail_match_t *)ud->data);
297 ERROR("tail plugin: tail_match_read failed.");
302 } /* int ctail_read */
304 void module_register(void) {
305 plugin_register_complex_config("tail", ctail_config);
306 } /* void module_register */