2 * collectd - src/snort.c
3 * Copyright (C) 2013 Kris Nielander
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Kris Nielander <nielander@fox-it.com>
21 * This plugin is based on the snmp plugin by Florian octo Forster.
26 #include "plugin.h" /* plugin_register_*, plugin_dispatch_values */
27 #include "common.h" /* auxiliary functions */
34 struct metric_definition_s {
39 struct metric_definition_s *next;
41 typedef struct metric_definition_s metric_definition_t;
43 struct instance_definition_s {
46 metric_definition_t **metric_list;
50 struct instance_definition_s *next;
52 typedef struct instance_definition_s instance_definition_t;
55 static metric_definition_t *metric_head = NULL;
57 static int snort_read_submit(instance_definition_t *id, metric_definition_t *md,
60 /* Registration variables */
62 value_list_t vl = VALUE_LIST_INIT;
64 DEBUG("snort plugin: plugin_instance=%s type=%s value=%s", id->name,
71 parse_value(buf, &value, md->data_source_type);
77 sstrncpy(vl.host, hostname_g, sizeof (vl.host));
78 sstrncpy(vl.plugin, "snort", sizeof(vl.plugin));
79 sstrncpy(vl.plugin_instance, id->name, sizeof(vl.plugin_instance));
80 sstrncpy(vl.type, md->type, sizeof(vl.type));
83 vl.interval = id->interval;
85 DEBUG("snort plugin: -> plugin_dispatch_values (&vl);");
86 plugin_dispatch_values(&vl);
91 static int snort_read(user_data_t *ud){
92 instance_definition_t *id;
93 metric_definition_t *md;
105 /* mmap, char pointers */
110 DEBUG("snort plugin: snort_read (instance = %s)", id->name);
112 fd = open(id->path, O_RDONLY);
114 ERROR("snort plugin: Unable to open `%s'.", id->path);
118 if ((fstat(fd, &sb) != 0) || (!S_ISREG(sb.st_mode))){
119 ERROR("snort plugin: `%s' is not a file.", id->path);
123 if (sb.st_size == 0){
124 ERROR("snort plugin: `%s' is empty.", id->path);
128 p_start = mmap(/* addr = */ NULL, sb.st_size, PROT_READ, MAP_SHARED, fd,
130 if (p_start == MAP_FAILED){
131 ERROR("snort plugin: mmap error");
135 /* Set the start value count. */
138 /* Set the pointer to the last line of the file and count the fields.
139 (Skip the last two characters of the buffer: `\n' and `\0') */
140 for (p_end = (p_start + sb.st_size) - 2; p_end > p_start; --p_end){
143 } else if (*p_end == '\n'){
150 ERROR("snort plugin: last line of `%s' does not contain enough values.", id->path);
155 ERROR("snort plugin: last line of `%s' is a comment.", id->path);
159 /* Copy the line to the buffer */
160 buf_t = buf = strdup(p_end);
162 /* Done with mmap and file pointer */
164 munmap(p_start, sb.st_size);
166 /* Create a list of all values */
167 metrics = (char **)calloc(count, sizeof(char *));
168 if (metrics == NULL){
172 for (metrics_t = metrics; (*metrics_t = strsep(&buf_t, ",")) != NULL;)
173 if (**metrics_t != '\0')
174 if (++metrics_t >= &metrics[count])
178 id->last = TIME_T_TO_CDTIME_T(strtol(*metrics, NULL, 0));
180 /* Register values */
181 for (i = 0; i < id->metric_list_len; ++i){
182 md = id->metric_list[i];
183 snort_read_submit(id, md, metrics[md->index]);
186 /* Free up resources */
192 static void snort_metric_definition_destroy(void *arg){
193 metric_definition_t *md;
199 if (md->name != NULL)
200 DEBUG("snort plugin: Destroying metric definition `%s'.", md->name);
207 static int snort_config_add_metric_index(metric_definition_t *md, oconfig_item_t *ci){
208 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)){
209 WARNING("snort plugin: `Index' needs exactly one integer argument.");
213 md->index = (int)ci->values[0].value.number;
215 WARNING("snort plugin: `Index' must be higher than 0.");
223 static int snort_config_add_metric(oconfig_item_t *ci){
224 metric_definition_t *md;
225 const data_set_t *ds;
229 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)){
230 WARNING("snort plugin: The `Metric' config option needs exactly one string argument.");
234 md = (metric_definition_t *)malloc(sizeof(*md));
237 memset(md, 0, sizeof(*md));
239 md->name = strdup(ci->values[0].value.string);
240 if (md->name == NULL){
245 for (i = 0; i < ci->children_num; ++i){
246 oconfig_item_t *option = ci->children + i;
249 if (strcasecmp("Type", option->key) == 0)
250 status = cf_util_get_string(option, &md->type);
251 else if (strcasecmp("Index", option->key) == 0)
252 status = snort_config_add_metric_index(md, option);
254 WARNING("snort plugin: Option `%s' not allowed here.", option->key);
263 snort_metric_definition_destroy(md);
267 /* Verify all necessary options have been set. */
268 if (md->type == NULL){
269 WARNING("snort plugin: Option `Type' must be set.");
271 } else if (md->index == 0){
272 WARNING("snort plugin: Option `Index' must be set.");
277 snort_metric_definition_destroy(md);
281 /* Retrieve the data source type from the types db. */
282 ds = plugin_get_ds(md->type);
284 WARNING("snort plugin: `Type' must be defined in `types.db'.");
285 snort_metric_definition_destroy(md);
288 md->data_source_type = ds->ds->type;
291 DEBUG("snort plugin: md = { name = %s, type = %s, data_source_type = %d, index = %d }",
292 md->name, md->type, md->data_source_type, md->index);
294 if (metric_head == NULL)
297 metric_definition_t *last;
299 while (last->next != NULL)
307 static void snort_instance_definition_destroy(void *arg){
308 instance_definition_t *id;
314 if (id->name != NULL)
315 DEBUG("snort plugin: Destroying instance definition `%s'.", id->name);
319 sfree(id->metric_list);
323 static int snort_config_add_instance_collect(instance_definition_t *id, oconfig_item_t *ci){
324 metric_definition_t *metric;
327 if (ci->values_num < 1){
328 WARNING("snort plugin: The `Collect' config option needs at least one argument.");
332 /* Verify string arguments */
333 for (i = 0; i < ci->values_num; ++i)
334 if (ci->values[i].type != OCONFIG_TYPE_STRING){
335 WARNING("snort plugin: All arguments to `Collect' must be strings.");
339 id->metric_list = (metric_definition_t **)malloc(sizeof(metric_definition_t *) * ci->values_num);
340 if (id->metric_list == NULL)
343 for (i = 0; i < ci->values_num; ++i){
344 for (metric = metric_head; metric != NULL; metric = metric->next)
345 if (strcasecmp(ci->values[i].value.string, metric->name) == 0)
349 WARNING("snort plugin: `Collect' argument not found `%s'.", ci->values[i].value.string);
353 DEBUG("snort plugin: id { name=%s md->name=%s }", id->name, metric->name);
355 id->metric_list[i] = metric;
356 id->metric_list_len++;
363 static int snort_config_add_instance(oconfig_item_t *ci){
365 instance_definition_t* id;
369 /* Registration variables */
370 char cb_name[DATA_MAX_NAME_LEN];
372 struct timespec cb_interval;
374 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)){
375 WARNING("snort plugin: The `Instance' config option needs exactly one string argument.");
379 id = (instance_definition_t *)malloc(sizeof(*id));
382 memset(id, 0, sizeof(*id));
384 id->name = strdup(ci->values[0].value.string);
385 if (id->name == NULL){
390 /* Use default interval. */
391 id->interval = plugin_get_interval();
393 for (i = 0; i < ci->children_num; ++i){
394 oconfig_item_t *option = ci->children + i;
397 if (strcasecmp("Path", option->key) == 0)
398 status = cf_util_get_string(option, &id->path);
399 else if (strcasecmp("Collect", option->key) == 0)
400 status = snort_config_add_instance_collect(id, option);
401 else if (strcasecmp("Interval", option->key) == 0)
402 cf_util_get_cdtime(option, &id->interval);
404 WARNING("snort plugin: Option `%s' not allowed here.", option->key);
413 snort_instance_definition_destroy(id);
417 /* Verify all necessary options have been set. */
418 if (id->path == NULL){
419 WARNING("snort plugin: Option `Path' must be set.");
421 } else if (id->metric_list == NULL){
422 WARNING("snort plugin: Option `Collect' must be set.");
427 snort_instance_definition_destroy(id);
431 DEBUG("snort plugin: id = { name = %s, path = %s }", id->name, id->path);
433 ssnprintf (cb_name, sizeof (cb_name), "snort-%s", id->name);
434 memset(&cb_data, 0, sizeof(cb_data));
436 cb_data.free_func = snort_instance_definition_destroy;
437 CDTIME_T_TO_TIMESPEC(id->interval, &cb_interval);
438 status = plugin_register_complex_read(NULL, cb_name, snort_read, &cb_interval, &cb_data);
441 ERROR("snort plugin: Registering complex read function failed.");
442 snort_instance_definition_destroy(id);
450 static int snort_config(oconfig_item_t *ci){
452 for (i = 0; i < ci->children_num; ++i){
453 oconfig_item_t *child = ci->children + i;
454 if (strcasecmp("Metric", child->key) == 0)
455 snort_config_add_metric(child);
456 else if (strcasecmp("Instance", child->key) == 0)
457 snort_config_add_instance(child);
459 WARNING("snort plugin: Ignore unknown config option `%s'.", child->key);
463 } /* int snort_config */
465 static int snort_shutdown(void){
466 metric_definition_t *metric_this;
467 metric_definition_t *metric_next;
469 metric_this = metric_head;
472 while (metric_this != NULL){
473 metric_next = metric_this->next;
474 snort_metric_definition_destroy(metric_this);
475 metric_this = metric_next;
481 void module_register(void){
482 plugin_register_complex_config("snort", snort_config);
483 plugin_register_shutdown("snort", snort_shutdown);