208258228b0c6da4d32384719da68f6790077466
[collectd.git] / src / snort.c
1 /**
2  * collectd - src/snort.c
3  * Copyright (C) 2013 Kris Nielander
4  *
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.
8  *
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.
13  *
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
17  *
18  * Authors:
19  *   Kris Nielander <nielander@fox-it.com>
20  *
21  * This plugin is based on the snmp plugin by Florian octo Forster.
22  *
23  **/
24
25 #include "collectd.h"
26 #include "plugin.h" /* plugin_register_*, plugin_dispatch_values */
27 #include "common.h" /* auxiliary functions */
28 #include <sys/mman.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 struct metric_definition_s {
35     char *name;
36     char *type;
37     int data_source_type;
38     int index;
39     struct metric_definition_s *next;
40 };
41 typedef struct metric_definition_s metric_definition_t;
42
43 struct instance_definition_s {
44     char *name;
45     char *path;
46     metric_definition_t **metric_list;
47     int metric_list_len;
48     cdtime_t last;
49     cdtime_t interval;
50     struct instance_definition_s *next;
51 };
52 typedef struct instance_definition_s instance_definition_t;
53
54 /* Private */
55 static metric_definition_t *metric_head = NULL;
56
57 static int snort_read_submit(instance_definition_t *id, metric_definition_t *md,
58     const char *buf){
59
60     /* Registration variables */
61     value_t value;
62     value_list_t vl = VALUE_LIST_INIT;
63
64     DEBUG("snort plugin: plugin_instance=%s type=%s value=%s", id->name,
65         md->type, buf);
66
67     if (buf == NULL)
68         return (-1);
69
70     /* Parse value */
71     parse_value(buf, &value, md->data_source_type);
72
73     /* Register */
74     vl.values_len = 1;
75     vl.values = &value;
76
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));
81
82     vl.time = id->last;
83     vl.interval = id->interval;
84
85     DEBUG("snort plugin: -> plugin_dispatch_values (&vl);");
86     plugin_dispatch_values(&vl);
87
88     return (0);
89 }
90
91 static int snort_read_buffer (instance_definition_t *id,
92         char const *buffer, size_t buffer_size)
93 {
94     int i;
95
96     char **metrics;
97     int metrics_num;
98
99     char *buf, *buf_ptr;
100
101     /* mmap, char pointers */
102     char const *p_end;
103
104     /* Set the start value count. */
105     metrics_num = 1;
106
107     /* Set the pointer to the last line of the file and count the fields.
108      (Skip the last two characters of the buffer: `\n' and `\0') */
109     for (p_end = (buffer + buffer_size) - 2; p_end > buffer; --p_end){
110         if (*p_end == ','){
111             ++metrics_num;
112         } else if (*p_end == '\n'){
113             ++p_end;
114             break;
115         }
116     }
117
118     if (metrics_num == 1){
119         ERROR("snort plugin: last line of `%s' does not contain enough values.", id->path);
120         return (-1);
121     }
122
123     if (*p_end == '#'){
124         ERROR("snort plugin: last line of `%s' is a comment.", id->path);
125         return (-1);
126     }
127
128     /* Copy the line to the buffer */
129     buf = strdup(p_end);
130
131     /* Create a list of all values */
132     metrics = calloc (metrics_num, sizeof (*metrics));
133     if (metrics == NULL) {
134         ERROR ("snort plugin: calloc failed.");
135         sfree (buf);
136         return (ENOMEM);
137     }
138
139     buf_ptr = buf;
140     i = 0;
141     while (buf_ptr != NULL) {
142         char *next = strchr (buf_ptr, ',');
143         if (next != NULL) {
144             *next = 0;
145             next++;
146         }
147         metrics[i] = buf_ptr;
148         buf_ptr = next;
149         i++;
150     }
151     assert (i == metrics_num);
152
153     /* Set last time */
154     id->last = TIME_T_TO_CDTIME_T(strtol(*metrics, NULL, 0));
155
156     /* Register values */
157     for (i = 0; i < id->metric_list_len; ++i){
158         metric_definition_t *md = id->metric_list[i];
159
160         if (md->index >= metrics_num) {
161             ERROR ("snort plugin: Metric \"%s\": Request for index %i when "
162                     "only %i fields are available.",
163                     md->name, md->index, metrics_num);
164             continue;
165         }
166
167         snort_read_submit(id, md, metrics[md->index]);
168     }
169
170     /* Free up resources */
171     free(metrics);
172     free(buf);
173     return (0);
174 }
175
176 static int snort_read(user_data_t *ud){
177     instance_definition_t *id;
178
179     int fd;
180
181     struct stat sb;
182
183     /* mmap, char pointers */
184     char *p_start;
185
186     id = ud->data;
187     DEBUG("snort plugin: snort_read (instance = %s)", id->name);
188
189     fd = open(id->path, O_RDONLY);
190     if (fd == -1){
191         ERROR("snort plugin: Unable to open `%s'.", id->path);
192         return (-1);
193     }
194
195     if ((fstat(fd, &sb) != 0) || (!S_ISREG(sb.st_mode))){
196         ERROR("snort plugin: `%s' is not a file.", id->path);
197         close (fd);
198         return (-1);
199     }
200
201     if (sb.st_size == 0){
202         ERROR("snort plugin: `%s' is empty.", id->path);
203         close (fd);
204         return (-1);
205     }
206
207     p_start = mmap(/* addr = */ NULL, sb.st_size, PROT_READ, MAP_SHARED, fd,
208         /* offset = */ 0);
209     if (p_start == MAP_FAILED){
210         ERROR("snort plugin: mmap error");
211         close (fd);
212         return (-1);
213     }
214
215     snort_read_buffer (id, p_start, (size_t) sb.st_size);
216
217     /* Done with mmap and file pointer */
218     close(fd);
219     munmap(p_start, sb.st_size);
220     return (0);
221 }
222
223 static void snort_metric_definition_destroy(void *arg){
224     metric_definition_t *md;
225
226     md = arg;
227     if (md == NULL)
228         return;
229
230     if (md->name != NULL)
231         DEBUG("snort plugin: Destroying metric definition `%s'.", md->name);
232
233     sfree(md->name);
234     sfree(md->type);
235     sfree(md);
236 }
237
238 static int snort_config_add_metric_index(metric_definition_t *md, oconfig_item_t *ci){
239     if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)){
240         WARNING("snort plugin: `Index' needs exactly one integer argument.");
241         return (-1);
242     }
243
244     md->index = (int)ci->values[0].value.number;
245     if (md->index <= 0){
246         WARNING("snort plugin: `Index' must be higher than 0.");
247         return (-1);
248     }
249
250     return (0);
251 }
252
253 /* Parse metric  */
254 static int snort_config_add_metric(oconfig_item_t *ci){
255     metric_definition_t *md;
256     const data_set_t *ds;
257     int status = 0;
258     int i;
259
260     if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)){
261         WARNING("snort plugin: The `Metric' config option needs exactly one string argument.");
262         return (-1);
263     }
264
265     md = (metric_definition_t *)malloc(sizeof(*md));
266     if (md == NULL)
267         return (-1);
268     memset(md, 0, sizeof(*md));
269
270     md->name = strdup(ci->values[0].value.string);
271     if (md->name == NULL){
272         free(md);
273         return (-1);
274     }
275
276     for (i = 0; i < ci->children_num; ++i){
277         oconfig_item_t *option = ci->children + i;
278         status = 0;
279
280         if (strcasecmp("Type", option->key) == 0)
281             status = cf_util_get_string(option, &md->type);
282         else if (strcasecmp("Index", option->key) == 0)
283             status = snort_config_add_metric_index(md, option);
284         else {
285             WARNING("snort plugin: Option `%s' not allowed here.", option->key);
286             status = -1;
287         }
288
289         if (status != 0)
290             break;
291     }
292
293     if (status != 0){
294         snort_metric_definition_destroy(md);
295         return (-1);
296     }
297
298     /* Verify all necessary options have been set. */
299     if (md->type == NULL){
300         WARNING("snort plugin: Option `Type' must be set.");
301         status = -1;
302     } else if (md->index == 0){
303         WARNING("snort plugin: Option `Index' must be set.");
304         status = -1;
305     }
306
307     if (status != 0){
308         snort_metric_definition_destroy(md);
309         return (-1);
310     }
311
312     /* Retrieve the data source type from the types db. */
313     ds = plugin_get_ds(md->type);
314     if (ds == NULL){
315         WARNING("snort plugin: `Type' must be defined in `types.db'.");
316         snort_metric_definition_destroy(md);
317         return (-1);
318     } else {
319         md->data_source_type = ds->ds->type;
320     }
321
322     DEBUG("snort plugin: md = { name = %s, type = %s, data_source_type = %d, index = %d }",
323         md->name, md->type, md->data_source_type, md->index);
324
325     if (metric_head == NULL)
326         metric_head = md;
327     else {
328         metric_definition_t *last;
329         last = metric_head;
330         while (last->next != NULL)
331             last = last->next;
332         last->next = md;
333     }
334
335     return (0);
336 }
337
338 static void snort_instance_definition_destroy(void *arg){
339     instance_definition_t *id;
340
341     id = arg;
342     if (id == NULL)
343         return;
344
345     if (id->name != NULL)
346         DEBUG("snort plugin: Destroying instance definition `%s'.", id->name);
347
348     sfree(id->name);
349     sfree(id->path);
350     sfree(id->metric_list);
351     sfree(id);
352 }
353
354 static int snort_config_add_instance_collect(instance_definition_t *id, oconfig_item_t *ci){
355     metric_definition_t *metric;
356     int i;
357
358     if (ci->values_num < 1){
359         WARNING("snort plugin: The `Collect' config option needs at least one argument.");
360         return (-1);
361     }
362
363     /* Verify string arguments */
364     for (i = 0; i < ci->values_num; ++i)
365         if (ci->values[i].type != OCONFIG_TYPE_STRING){
366             WARNING("snort plugin: All arguments to `Collect' must be strings.");
367             return (-1);
368         }
369
370     id->metric_list = (metric_definition_t **)malloc(sizeof(metric_definition_t *) * ci->values_num);
371     if (id->metric_list == NULL)
372         return (-1);
373
374     for (i = 0; i < ci->values_num; ++i){
375         for (metric = metric_head; metric != NULL; metric = metric->next)
376             if (strcasecmp(ci->values[i].value.string, metric->name) == 0)
377                 break;
378
379         if (metric == NULL){
380             WARNING("snort plugin: `Collect' argument not found `%s'.", ci->values[i].value.string);
381             return (-1);
382         }
383
384         DEBUG("snort plugin: id { name=%s md->name=%s }", id->name, metric->name);
385
386         id->metric_list[i] = metric;
387         id->metric_list_len++;
388     }
389
390     return (0);
391 }
392
393 /* Parse instance  */
394 static int snort_config_add_instance(oconfig_item_t *ci){
395
396     instance_definition_t* id;
397     int status = 0;
398     int i;
399
400     /* Registration variables */
401     char cb_name[DATA_MAX_NAME_LEN];
402     user_data_t cb_data;
403     struct timespec cb_interval;
404
405     if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)){
406         WARNING("snort plugin: The `Instance' config option needs exactly one string argument.");
407         return (-1);
408     }
409
410     id = (instance_definition_t *)malloc(sizeof(*id));
411     if (id == NULL)
412         return (-1);
413     memset(id, 0, sizeof(*id));
414
415     id->name = strdup(ci->values[0].value.string);
416     if (id->name == NULL){
417         free(id);
418         return (-1);
419     }
420
421     /* Use default interval. */
422     id->interval = plugin_get_interval();
423
424     for (i = 0; i < ci->children_num; ++i){
425         oconfig_item_t *option = ci->children + i;
426         status = 0;
427
428         if (strcasecmp("Path", option->key) == 0)
429             status = cf_util_get_string(option, &id->path);
430         else if (strcasecmp("Collect", option->key) == 0)
431             status = snort_config_add_instance_collect(id, option);
432         else if (strcasecmp("Interval", option->key) == 0)
433             cf_util_get_cdtime(option, &id->interval);
434         else {
435             WARNING("snort plugin: Option `%s' not allowed here.", option->key);
436             status = -1;
437         }
438
439         if (status != 0)
440             break;
441     }
442
443     if (status != 0){
444         snort_instance_definition_destroy(id);
445         return (-1);
446     }
447
448     /* Verify all necessary options have been set. */
449     if (id->path == NULL){
450         WARNING("snort plugin: Option `Path' must be set.");
451         status = -1;
452     } else if (id->metric_list == NULL){
453         WARNING("snort plugin: Option `Collect' must be set.");
454         status = -1;
455    }
456
457     if (status != 0){
458         snort_instance_definition_destroy(id);
459         return (-1);
460     }
461
462     DEBUG("snort plugin: id = { name = %s, path = %s }", id->name, id->path);
463
464     ssnprintf (cb_name, sizeof (cb_name), "snort-%s", id->name);
465     memset(&cb_data, 0, sizeof(cb_data));
466     cb_data.data = id;
467     cb_data.free_func = snort_instance_definition_destroy;
468     CDTIME_T_TO_TIMESPEC(id->interval, &cb_interval);
469     status = plugin_register_complex_read(NULL, cb_name, snort_read, &cb_interval, &cb_data);
470
471     if (status != 0){
472         ERROR("snort plugin: Registering complex read function failed.");
473         snort_instance_definition_destroy(id);
474         return (-1);
475     }
476
477     return (0);
478 }
479
480 /* Parse blocks */
481 static int snort_config(oconfig_item_t *ci){
482     int i;
483     for (i = 0; i < ci->children_num; ++i){
484         oconfig_item_t *child = ci->children + i;
485         if (strcasecmp("Metric", child->key) == 0)
486             snort_config_add_metric(child);
487         else if (strcasecmp("Instance", child->key) == 0)
488             snort_config_add_instance(child);
489         else
490             WARNING("snort plugin: Ignore unknown config option `%s'.", child->key);
491     }
492
493     return (0);
494 } /* int snort_config */
495
496 static int snort_shutdown(void){
497     metric_definition_t *metric_this;
498     metric_definition_t *metric_next;
499
500     metric_this = metric_head;
501     metric_head = NULL;
502
503     while (metric_this != NULL){
504         metric_next = metric_this->next;
505         snort_metric_definition_destroy(metric_this);
506         metric_this = metric_next;
507     }
508
509     return (0);
510 }
511
512 void module_register(void){
513     plugin_register_complex_config("snort", snort_config);
514     plugin_register_shutdown("snort", snort_shutdown);
515 }
516