snort plugin: Split the snort_read() function.
[collectd.git] / src / snort.c
index a49cb5f..2082582 100644 (file)
@@ -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;