Code

email plugin: Ignore size if it less than or equal to zero.
[collectd.git] / src / email.c
index 7c23e9b4fe2d478441efcce640afd291b54208c0..509eb2305532c9d1d199ed441640df470cf4316a 100644 (file)
@@ -290,7 +290,7 @@ static void type_list_incr (type_list_t *list, char *name, int incr)
 
 /* Read a single character from the socket. If an error occurs or end-of-file
  * is reached return '\0'. */
-char read_char (conn_t *src)
+static char read_char (conn_t *src)
 {
        char ret = '\0';
 
@@ -321,7 +321,7 @@ char read_char (conn_t *src)
                        return '\0';
        } while (EINTR == errno);
        return ret;
-} /* char read_char (conn_t *) */
+} /* static char read_char (conn_t *) */
 
 /* Read a single line (terminated by '\n') from the the socket.
  *
@@ -334,7 +334,7 @@ char read_char (conn_t *src)
  * characters of the input stream, the line will will be ignored! By
  * definition we should not get any longer input lines, thus this is
  * acceptable in this case ;-) */
-char *read_line (conn_t *src)
+static char *read_line (conn_t *src)
 {
        int i = 0;
 
@@ -406,7 +406,7 @@ char *read_line (conn_t *src)
        src->length    = i;
 
        return src->buffer;
-} /* char *read_line (conn_t *) */
+} /* static char *read_line (conn_t *) */
 
 static void *collect (void *arg)
 {
@@ -486,9 +486,11 @@ static void *collect (void *arg)
                                type_list_incr (&count, type, 1);
                                pthread_mutex_unlock (&count_mutex);
 
-                               pthread_mutex_lock (&size_mutex);
-                               type_list_incr (&size, type, bytes);
-                               pthread_mutex_unlock (&size_mutex);
+                               if (bytes > 0) {
+                                       pthread_mutex_lock (&size_mutex);
+                                       type_list_incr (&size, type, bytes);
+                                       pthread_mutex_unlock (&size_mutex);
+                               }
                        }
                        else if ('s' == line[0]) { /* s:<value> */
                                pthread_mutex_lock (&score_mutex);
@@ -525,7 +527,7 @@ static void *collect (void *arg)
 
        free (buffer);
        pthread_exit ((void *)0);
-} /* void *collect (void *) */
+} /* static void *collect (void *) */
 
 static void *open_connection (void *arg)
 {
@@ -661,7 +663,7 @@ static void *open_connection (void *arg)
                pthread_cond_signal (&conn_available);
        }
        pthread_exit ((void *)0);
-} /* void *open_connection (void *) */
+} /* static void *open_connection (void *) */
 #endif /* EMAIL_HAVE_READ */
 
 static void email_init (void)
@@ -756,9 +758,6 @@ static void type_submit (char *plugin, char *inst, int value)
        char buf[BUFSIZE] = "";
        int  len          = 0;
 
-       if (0 == value)
-               return;
-
        len = snprintf (buf, BUFSIZE, "%u:%i", (unsigned int)curtime, value);
        if ((len < 0) || (len >= BUFSIZE))
                return;
@@ -772,58 +771,123 @@ static void score_submit (double value)
        char buf[BUFSIZE] = "";
        int  len          = 0;
 
-       if (0.0 == value)
-               return;
-
        len = snprintf (buf, BUFSIZE, "%u:%.2f", (unsigned int)curtime, value);
        if ((len < 0) || (len >= BUFSIZE))
                return;
 
        plugin_submit ("email_spam_score", NULL, buf);
        return;
+} /* static void score_submit (double) */
+
+/* Copy list l1 to list l2. l2 may partly exist already, but it is assumed
+ * that neither the order nor the name of any element of either list is
+ * changed and no elements are deleted. The values of l1 are reset to zero
+ * after they have been copied to l2. */
+static void copy_type_list (type_list_t *l1, type_list_t *l2)
+{
+       type_t *ptr1;
+       type_t *ptr2;
+
+       type_t *last = NULL;
+
+       for (ptr1 = l1->head, ptr2 = l2->head; NULL != ptr1;
+                       ptr1 = ptr1->next, last = ptr2, ptr2 = ptr2->next) {
+               if (NULL == ptr2) {
+                       ptr2 = (type_t *)smalloc (sizeof (type_t));
+                       ptr2->name = NULL;
+                       ptr2->next = NULL;
+
+                       if (NULL == last) {
+                               l2->head = ptr2;
+                       }
+                       else {
+                               last->next = ptr2;
+                       }
+
+                       l2->tail = ptr2;
+               }
+
+               if (NULL == ptr2->name) {
+                       ptr2->name = sstrdup (ptr1->name);
+               }
+
+               ptr2->value = ptr1->value;
+               ptr1->value = 0;
+       }
+       return;
 }
 
 static void email_read (void)
 {
        type_t *ptr;
 
+       double sc;
+
+       static type_list_t *cnt;
+       static type_list_t *sz;
+       static type_list_t *chk;
+
        if (disabled)
                return;
 
-       pthread_mutex_lock (&count_mutex);
+       if (NULL == cnt) {
+               cnt = (type_list_t *)smalloc (sizeof (type_list_t));
+               cnt->head = NULL;
+       }
 
-       for (ptr = count.head; NULL != ptr; ptr = ptr->next) {
-               type_submit ("email_count", ptr->name, ptr->value);
-               ptr->value = 0;
+       if (NULL == sz) {
+               sz = (type_list_t *)smalloc (sizeof (type_list_t));
+               sz->head = NULL;
        }
 
+       if (NULL == chk) {
+               chk = (type_list_t *)smalloc (sizeof (type_list_t));
+               chk->head = NULL;
+       }
+
+       /* email count */
+       pthread_mutex_lock (&count_mutex);
+
+       copy_type_list (&count, cnt);
+
        pthread_mutex_unlock (&count_mutex);
 
+       for (ptr = cnt->head; NULL != ptr; ptr = ptr->next) {
+               type_submit ("email_count", ptr->name, ptr->value);
+       }
+
+       /* email size */
        pthread_mutex_lock (&size_mutex);
 
-       for (ptr = size.head; NULL != ptr; ptr = ptr->next) {
-               type_submit ("email_size", ptr->name, ptr->value);
-               ptr->value = 0;
-       }
+       copy_type_list (&size, sz);
 
        pthread_mutex_unlock (&size_mutex);
 
+       for (ptr = sz->head; NULL != ptr; ptr = ptr->next) {
+               type_submit ("email_size", ptr->name, ptr->value);
+       }
+
+       /* spam score */
        pthread_mutex_lock (&score_mutex);
 
-       score_submit (score);
+       sc = score;
        score = 0.0;
        score_count = 0;
 
        pthread_mutex_unlock (&score_mutex);
 
+       score_submit (sc);
+
+       /* spam checks */
        pthread_mutex_lock (&check_mutex);
 
-       for (ptr = check.head; NULL != ptr; ptr = ptr->next) {
-               type_submit ("email_spam_check", ptr->name, ptr->value);
-               ptr->value = 0;
-       }
+       copy_type_list (&check, chk);
 
        pthread_mutex_unlock (&check_mutex);
+
+       for (ptr = chk->head; NULL != ptr; ptr = ptr->next) {
+               type_submit ("email_spam_check", ptr->name, ptr->value);
+       }
        return;
 } /* static void read (void) */
 #else /* if !EMAIL_HAVE_READ */