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