X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fsnort.c;h=208258228b0c6da4d32384719da68f6790077466;hb=53d05e8592dc3277c88df320c26302c24c918c5b;hp=a49cb5fe52a0b1f4c7b061fba733d02a426a2a99;hpb=c97c8756a28969fea924a80bfc42c989dcd20ced;p=collectd.git diff --git a/src/snort.c b/src/snort.c index a49cb5fe..20825822 100644 --- a/src/snort.c +++ b/src/snort.c @@ -88,58 +88,25 @@ static int snort_read_submit(instance_definition_t *id, metric_definition_t *md, return (0); } -static int snort_read(user_data_t *ud){ - instance_definition_t *id; - metric_definition_t *md; - +static int snort_read_buffer (instance_definition_t *id, + char const *buffer, size_t buffer_size) +{ int i; - int fd; char **metrics; int metrics_num; - struct stat sb; char *buf, *buf_ptr; /* mmap, char pointers */ - char *p_start; - char *p_end; - - id = ud->data; - DEBUG("snort plugin: snort_read (instance = %s)", id->name); - - fd = open(id->path, O_RDONLY); - if (fd == -1){ - ERROR("snort plugin: Unable to open `%s'.", id->path); - return (-1); - } - - if ((fstat(fd, &sb) != 0) || (!S_ISREG(sb.st_mode))){ - ERROR("snort plugin: `%s' is not a file.", id->path); - close (fd); - return (-1); - } - - if (sb.st_size == 0){ - ERROR("snort plugin: `%s' is empty.", id->path); - close (fd); - return (-1); - } - - p_start = mmap(/* addr = */ NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, - /* offset = */ 0); - if (p_start == MAP_FAILED){ - ERROR("snort plugin: mmap error"); - close (fd); - return (-1); - } + char const *p_end; /* Set the start value count. */ metrics_num = 1; /* Set the pointer to the last line of the file and count the fields. (Skip the last two characters of the buffer: `\n' and `\0') */ - for (p_end = (p_start + sb.st_size) - 2; p_end > p_start; --p_end){ + for (p_end = (buffer + buffer_size) - 2; p_end > buffer; --p_end){ if (*p_end == ','){ ++metrics_num; } else if (*p_end == '\n'){ @@ -150,31 +117,23 @@ static int snort_read(user_data_t *ud){ if (metrics_num == 1){ ERROR("snort plugin: last line of `%s' does not contain enough values.", id->path); - close (fd); - munmap(p_start, sb.st_size); return (-1); } if (*p_end == '#'){ ERROR("snort plugin: last line of `%s' is a comment.", id->path); - close (fd); - munmap(p_start, sb.st_size); return (-1); } /* Copy the line to the buffer */ buf = strdup(p_end); - /* Done with mmap and file pointer */ - close(fd); - munmap(p_start, sb.st_size); - /* Create a list of all values */ metrics = calloc (metrics_num, sizeof (*metrics)); if (metrics == NULL) { ERROR ("snort plugin: calloc failed."); sfree (buf); - return (-1); + return (ENOMEM); } buf_ptr = buf; @@ -196,7 +155,7 @@ static int snort_read(user_data_t *ud){ /* Register values */ for (i = 0; i < id->metric_list_len; ++i){ - md = id->metric_list[i]; + metric_definition_t *md = id->metric_list[i]; if (md->index >= metrics_num) { ERROR ("snort plugin: Metric \"%s\": Request for index %i when " @@ -214,6 +173,53 @@ static int snort_read(user_data_t *ud){ return (0); } +static int snort_read(user_data_t *ud){ + instance_definition_t *id; + + int fd; + + struct stat sb; + + /* mmap, char pointers */ + char *p_start; + + id = ud->data; + DEBUG("snort plugin: snort_read (instance = %s)", id->name); + + fd = open(id->path, O_RDONLY); + if (fd == -1){ + ERROR("snort plugin: Unable to open `%s'.", id->path); + return (-1); + } + + if ((fstat(fd, &sb) != 0) || (!S_ISREG(sb.st_mode))){ + ERROR("snort plugin: `%s' is not a file.", id->path); + close (fd); + return (-1); + } + + if (sb.st_size == 0){ + ERROR("snort plugin: `%s' is empty.", id->path); + close (fd); + return (-1); + } + + p_start = mmap(/* addr = */ NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, + /* offset = */ 0); + if (p_start == MAP_FAILED){ + ERROR("snort plugin: mmap error"); + close (fd); + return (-1); + } + + snort_read_buffer (id, p_start, (size_t) sb.st_size); + + /* Done with mmap and file pointer */ + close(fd); + munmap(p_start, sb.st_size); + return (0); +} + static void snort_metric_definition_destroy(void *arg){ metric_definition_t *md;