X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Femail.c;h=f5a544ac4ea0a2745eaa2b7648112859c904c67a;hb=de0fdb208de123fe753c5fcf03533833777a5b4a;hp=404a20a8951f6619b9a481f7051f9d4f10859918;hpb=33d409e0d68beac9d6d5439a9237b50f28fcd976;p=collectd.git diff --git a/src/email.c b/src/email.c index 404a20a8..8fc5509f 100644 --- a/src/email.c +++ b/src/email.c @@ -1,11 +1,10 @@ /** * collectd - src/email.c - * Copyright (C) 2006 Sebastian Harl + * Copyright (C) 2006-2008 Sebastian Harl * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. + * Free Software Foundation; only version 2 of the License is applicable. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of @@ -40,27 +39,15 @@ #include "configfile.h" +#include + #if HAVE_LIBPTHREAD # include -# define EMAIL_HAVE_READ 1 -#else -# define EMAIL_HAVE_READ 0 #endif -#if HAVE_SYS_SELECT_H -# include -#endif /* HAVE_SYS_SELECT_H */ - -#if HAVE_SYS_SOCKET_H -# include -#endif /* HAVE_SYS_SOCKET_H */ - -/* *sigh* glibc does not define UNIX_PATH_MAX in sys/un.h ... */ -#if HAVE_LINUX_UN_H -# include -#elif HAVE_SYS_UN_H -# include -#endif /* HAVE_LINUX_UN_H | HAVE_SYS_UN_H */ +#include +#include +#include /* some systems (e.g. Darwin) seem to not define UNIX_PATH_MAX at all */ #ifndef UNIX_PATH_MAX @@ -71,23 +58,17 @@ # include #endif /* HAVE_GRP_H */ -#define MODULE_NAME "email" - -/* 256 bytes ought to be enough for anybody ;-) */ -#define BUFSIZE 256 - -#ifndef COLLECTD_SOCKET_PREFIX -# define COLLECTD_SOCKET_PREFIX "/tmp/.collectd-" -#endif /* COLLECTD_SOCKET_PREFIX */ - -#define SOCK_PATH COLLECTD_SOCKET_PREFIX"email" +#define SOCK_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-email" #define MAX_CONNS 5 #define MAX_CONNS_LIMIT 16384 +#define log_debug(...) DEBUG ("email: "__VA_ARGS__) +#define log_err(...) ERROR ("email: "__VA_ARGS__) +#define log_warn(...) WARNING ("email: "__VA_ARGS__) + /* * Private data structures */ -#if EMAIL_HAVE_READ /* linked list of email and check types */ typedef struct type { char *name; @@ -100,42 +81,44 @@ typedef struct { type_t *tail; } type_list_t; -/* linked list of collector thread control information */ +/* collector thread control information */ typedef struct collector { pthread_t thread; - /* socket to read data from */ - int socket; + /* socket descriptor of the current/last connection */ + FILE *socket; +} collector_t; - /* buffer to read data to */ - char buffer[BUFSIZE]; - int idx; /* current position in buffer */ +/* linked list of pending connections */ +typedef struct conn { + /* socket to read data from */ + FILE *socket; - struct collector *next; -} collector_t; + /* linked list of connections */ + struct conn *next; +} conn_t; typedef struct { - collector_t *head; - collector_t *tail; -} collector_list_t; -#endif /* EMAIL_HAVE_READ */ + conn_t *head; + conn_t *tail; +} conn_list_t; /* * Private variables */ -#if EMAIL_HAVE_READ /* valid configuration file keys */ -static char *config_keys[] = +static const char *config_keys[] = { + "SocketFile", "SocketGroup", "SocketPerms", - "MaxConns", - NULL + "MaxConns" }; -static int config_keys_num = 3; +static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); /* socket configuration */ -static char *sock_group = COLLECTD_GRP_NAME; +static char *sock_file = NULL; +static char *sock_group = NULL; static int sock_perms = S_IRWXU | S_IRWXG; static int max_conns = MAX_CONNS; @@ -143,74 +126,59 @@ static int max_conns = MAX_CONNS; static int disabled = 0; /* thread managing "client" connections */ -static pthread_t connector; +static pthread_t connector = (pthread_t) 0; +static int connector_socket = -1; + +/* tell the collector threads that a new connection is available */ +static pthread_cond_t conn_available = PTHREAD_COND_INITIALIZER; + +/* connections that are waiting to be processed */ +static pthread_mutex_t conns_mutex = PTHREAD_MUTEX_INITIALIZER; +static conn_list_t conns; /* tell the connector thread that a collector is available */ static pthread_cond_t collector_available = PTHREAD_COND_INITIALIZER; -/* collector threads that are in use */ -static pthread_mutex_t active_mutex = PTHREAD_MUTEX_INITIALIZER; -static collector_list_t active; +/* collector threads */ +static collector_t **collectors = NULL; -/* collector threads that are available for use */ static pthread_mutex_t available_mutex = PTHREAD_MUTEX_INITIALIZER; -static collector_list_t available; +static int available_collectors; static pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; -static type_list_t count; +static type_list_t list_count; +static type_list_t list_count_copy; static pthread_mutex_t size_mutex = PTHREAD_MUTEX_INITIALIZER; -static type_list_t size; +static type_list_t list_size; +static type_list_t list_size_copy; static pthread_mutex_t score_mutex = PTHREAD_MUTEX_INITIALIZER; static double score; static int score_count; static pthread_mutex_t check_mutex = PTHREAD_MUTEX_INITIALIZER; -static type_list_t check; -#endif /* EMAIL_HAVE_READ */ +static type_list_t list_check; +static type_list_t list_check_copy; -#define COUNT_FILE "email/email-%s.rrd" -static char *count_ds_def[] = -{ - "DS:count:GAUGE:"COLLECTD_HEARTBEAT":0:U", - NULL -}; -static int count_ds_num = 1; - -#define SIZE_FILE "email/email_size-%s.rrd" -static char *size_ds_def[] = -{ - "DS:size:GAUGE:"COLLECTD_HEARTBEAT":0:U", - NULL -}; -static int size_ds_num = 1; - -#define SCORE_FILE "email/spam_score.rrd" -static char *score_ds_def[] = -{ - "DS:score:GAUGE:"COLLECTD_HEARTBEAT":U:U", - NULL -}; -static int score_ds_num = 1; - -#define CHECK_FILE "email/spam_check-%s.rrd" -static char *check_ds_def[] = -{ - "DS:hits:GAUGE:"COLLECTD_HEARTBEAT":0:U", - NULL -}; -static int check_ds_num = 1; - -#if EMAIL_HAVE_READ -static int email_config (char *key, char *value) +/* + * Private functions + */ +static int email_config (const char *key, const char *value) { - if (0 == strcasecmp (key, "SocketGroup")) { + if (0 == strcasecmp (key, "SocketFile")) { + if (NULL != sock_file) + free (sock_file); + sock_file = sstrdup (value); + } + else if (0 == strcasecmp (key, "SocketGroup")) { + if (NULL != sock_group) + free (sock_group); sock_group = sstrdup (value); } else if (0 == strcasecmp (key, "SocketPerms")) { /* the user is responsible for providing reasonable values */ - sock_perms = (int)strtol (value, NULL, 0); + sock_perms = (int)strtol (value, NULL, 8); } else if (0 == strcasecmp (key, "MaxConns")) { long int tmp = strtol (value, NULL, 0); @@ -219,12 +187,18 @@ static int email_config (char *key, char *value) fprintf (stderr, "email plugin: `MaxConns' was set to invalid " "value %li, will use default %i.\n", tmp, MAX_CONNS); + ERROR ("email plugin: `MaxConns' was set to invalid " + "value %li, will use default %i.\n", + tmp, MAX_CONNS); max_conns = MAX_CONNS; } else if (tmp > MAX_CONNS_LIMIT) { fprintf (stderr, "email plugin: `MaxConns' was set to invalid " "value %li, will use hardcoded limit %i.\n", tmp, MAX_CONNS_LIMIT); + ERROR ("email plugin: `MaxConns' was set to invalid " + "value %li, will use hardcoded limit %i.\n", + tmp, MAX_CONNS_LIMIT); max_conns = MAX_CONNS_LIMIT; } else { @@ -272,584 +246,526 @@ static void type_list_incr (type_list_t *list, char *name, int incr) return; } /* static void type_list_incr (type_list_t *, char *) */ -/* Read a single character from the socket. If an error occurs or end-of-file - * is reached return '\0'. */ -char read_char (collector_t *src) +static void *collect (void *arg) { - char ret = '\0'; - - fd_set fdset; - - FD_ZERO (&fdset); - FD_SET (src->socket, &fdset); + collector_t *this = (collector_t *)arg; - if (-1 == select (src->socket + 1, &fdset, NULL, NULL, NULL)) { - syslog (LOG_ERR, "select() failed: %s", strerror (errno)); - return '\0'; - } + while (1) { + int loop = 1; - assert (FD_ISSET (src->socket, &fdset)); + conn_t *connection; - do { - ssize_t len = 0; + pthread_mutex_lock (&conns_mutex); - errno = 0; - if (0 > (len = read (src->socket, (void *)&ret, 1))) { - if (EINTR != errno) { - syslog (LOG_ERR, "read() failed: %s", strerror (errno)); - return '\0'; - } + while (NULL == conns.head) { + pthread_cond_wait (&conn_available, &conns_mutex); } - if (0 == len) - return '\0'; - } while (EINTR == errno); - return ret; -} /* char read_char (collector_t *) */ + connection = conns.head; + conns.head = conns.head->next; -/* Read a single line (terminated by '\n') from the the socket. - * - * The return value is zero terminated and does not contain any newline - * characters. In case that no complete line is available (non-blocking mode - * should be enabled) an empty string is returned. - * - * If an error occurs or end-of-file is reached return NULL. - * - * IMPORTANT NOTE: If there is no newline character found in BUFSIZE - * 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 (collector_t *src) -{ - int i = 0; - char *ret; - - assert (BUFSIZE > src->idx); - - for (i = 0; i < src->idx; ++i) { - if ('\n' == src->buffer[i]) - break; - } + if (NULL == conns.head) { + conns.tail = NULL; + } - if ('\n' != src->buffer[i]) { - fd_set fdset; - - ssize_t len = 0; + pthread_mutex_unlock (&conns_mutex); - FD_ZERO (&fdset); - FD_SET (src->socket, &fdset); + /* make the socket available to the global + * thread and connection management */ + this->socket = connection->socket; - if (-1 == select (src->socket + 1, &fdset, NULL, NULL, NULL)) { - syslog (LOG_ERR, "select() failed: %s", strerror (errno)); - return NULL; - } + log_debug ("collect: handling connection on fd #%i", + fileno (this->socket)); - assert (FD_ISSET (src->socket, &fdset)); + while (loop) { + /* 256 bytes ought to be enough for anybody ;-) */ + char line[256 + 1]; /* line + '\0' */ + int len = 0; - do { errno = 0; - if (0 > (len = read (src->socket, - (void *)(&(src->buffer[0]) + src->idx), - BUFSIZE - src->idx))) { - if (EINTR != errno) { - syslog (LOG_ERR, "read() failed: %s", strerror (errno)); - return NULL; + if (NULL == fgets (line, sizeof (line), this->socket)) { + loop = 0; + + if (0 != errno) { + char errbuf[1024]; + log_err ("collect: reading from socket (fd #%i) " + "failed: %s", fileno (this->socket), + sstrerror (errno, errbuf, sizeof (errbuf))); } - } - - if (0 == len) - return NULL; - } while (EINTR == errno); - - src->idx += len; - - for (i = src->idx - len; i < src->idx; ++i) { - if ('\n' == src->buffer[i]) break; - } - - if ('\n' != src->buffer[i]) { - ret = (char *)smalloc (1); - - ret[0] = '\0'; - - if (BUFSIZE == src->idx) { /* no space left in buffer */ - while ('\n' != read_char (src)) - /* ignore complete line */; - - src->idx = 0; } - return ret; - } - } - - ret = (char *)smalloc (i + 1); - memcpy (ret, &(src->buffer[0]), i + 1); - ret[i] = '\0'; - - src->idx -= (i + 1); - - if (0 == src->idx) - src->buffer[0] = '\0'; - else - memmove (&(src->buffer[0]), &(src->buffer[i + 1]), src->idx); - return ret; -} /* char *read_line (collector_t *) */ - -static void *collect (void *arg) -{ - collector_t *this = (collector_t *)arg; - - int loop = 1; - - { /* put the socket in non-blocking mode */ - int flags = 0; - - errno = 0; - if (-1 == fcntl (this->socket, F_GETFL, &flags)) { - syslog (LOG_ERR, "fcntl() failed: %s", strerror (errno)); - loop = 0; - } - - errno = 0; - if (-1 == fcntl (this->socket, F_SETFL, flags | O_NONBLOCK)) { - syslog (LOG_ERR, "fcntl() failed: %s", strerror (errno)); - loop = 0; - } - } - - while (loop) { - char *line = read_line (this); - - if (NULL == line) { - loop = 0; - break; - } - - if ('\0' == line[0]) { - free (line); - continue; - } - if (':' != line[1]) { - syslog (LOG_ERR, "email: syntax error in line '%s'", line); - free (line); - continue; - } + len = strlen (line); + if (('\n' != line[len - 1]) && ('\r' != line[len - 1])) { + log_warn ("collect: line too long (> %zu characters): " + "'%s' (truncated)", sizeof (line) - 1, line); - if ('e' == line[0]) { /* e:: */ - char *ptr = NULL; - char *type = strtok_r (line + 2, ":", &ptr); - char *tmp = strtok_r (NULL, ":", &ptr); - int bytes = 0; - - if (NULL == tmp) { - syslog (LOG_ERR, "email: syntax error in line '%s'", line); - free (line); + while (NULL != fgets (line, sizeof (line), this->socket)) + if (('\n' == line[len - 1]) || ('\r' == line[len - 1])) + break; continue; } - bytes = atoi (tmp); - - pthread_mutex_lock (&count_mutex); - 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); - } - else if ('s' == line[0]) { /* s: */ - pthread_mutex_lock (&score_mutex); - score = (score * (double)score_count + atof (line + 2)) - / (double)(score_count + 1); - ++score_count; - pthread_mutex_unlock (&score_mutex); - } - else if ('c' == line[0]) { /* c:[,,...] */ - char *ptr = NULL; - char *type = strtok_r (line + 2, ",", &ptr); - - do { - pthread_mutex_lock (&check_mutex); - type_list_incr (&check, type, 1); - pthread_mutex_unlock (&check_mutex); - } while (NULL != (type = strtok_r (NULL, ",", &ptr))); - } - else { - syslog (LOG_ERR, "email: unknown type '%c'", line[0]); - } + line[len - 1] = '\0'; - free (line); - } + log_debug ("collect: line = '%s'", line); - /* put this thread back into the available list */ - pthread_mutex_lock (&active_mutex); - { - collector_t *last; - collector_t *ptr; + if (':' != line[1]) { + log_err ("collect: syntax error in line '%s'", line); + continue; + } - last = NULL; + if ('e' == line[0]) { /* e:: */ + char *ptr = NULL; + char *type = strtok_r (line + 2, ":", &ptr); + char *tmp = strtok_r (NULL, ":", &ptr); + int bytes = 0; - for (ptr = active.head; NULL != ptr; last = ptr, ptr = ptr->next) { - if (0 != pthread_equal (ptr->thread, this->thread)) - break; - } + if (NULL == tmp) { + log_err ("collect: syntax error in line '%s'", line); + continue; + } - /* the current thread _has_ to be in the active list */ - assert (NULL != ptr); + bytes = atoi (tmp); - if (NULL == last) { - active.head = ptr->next; - } - else { - last->next = ptr->next; + pthread_mutex_lock (&count_mutex); + type_list_incr (&list_count, type, 1); + pthread_mutex_unlock (&count_mutex); - if (NULL == last->next) { - active.tail = last; + if (bytes > 0) { + pthread_mutex_lock (&size_mutex); + type_list_incr (&list_size, type, bytes); + pthread_mutex_unlock (&size_mutex); + } } - } - } - pthread_mutex_unlock (&active_mutex); + else if ('s' == line[0]) { /* s: */ + pthread_mutex_lock (&score_mutex); + score = (score * (double)score_count + atof (line + 2)) + / (double)(score_count + 1); + ++score_count; + pthread_mutex_unlock (&score_mutex); + } + else if ('c' == line[0]) { /* c:[,,...] */ + char *ptr = NULL; + char *type = strtok_r (line + 2, ",", &ptr); + + do { + pthread_mutex_lock (&check_mutex); + type_list_incr (&list_check, type, 1); + pthread_mutex_unlock (&check_mutex); + } while (NULL != (type = strtok_r (NULL, ",", &ptr))); + } + else { + log_err ("collect: unknown type '%c'", line[0]); + } + } /* while (loop) */ - this->next = NULL; + log_debug ("Shutting down connection on fd #%i", + fileno (this->socket)); - pthread_mutex_lock (&available_mutex); + fclose (connection->socket); + free (connection); - if (NULL == available.head) { - available.head = this; - available.tail = this; - } - else { - available.tail->next = this; - available.tail = this; - } + this->socket = NULL; - pthread_mutex_unlock (&available_mutex); + pthread_mutex_lock (&available_mutex); + ++available_collectors; + pthread_mutex_unlock (&available_mutex); + + pthread_cond_signal (&collector_available); + } /* while (1) */ - pthread_cond_signal (&collector_available); pthread_exit ((void *)0); -} /* void *collect (void *) */ +} /* static void *collect (void *) */ -static void *open_connection (void *arg) +static void *open_connection (void __attribute__((unused)) *arg) { - int local = 0; - struct sockaddr_un addr; + char *path = (NULL == sock_file) ? SOCK_PATH : sock_file; + char *group = (NULL == sock_group) ? COLLECTD_GRP_NAME : sock_group; + /* create UNIX socket */ errno = 0; - if (-1 == (local = socket (PF_UNIX, SOCK_STREAM, 0))) { + if (-1 == (connector_socket = socket (PF_UNIX, SOCK_STREAM, 0))) { + char errbuf[1024]; disabled = 1; - syslog (LOG_ERR, "socket() failed: %s", strerror (errno)); + log_err ("socket() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); pthread_exit ((void *)1); } addr.sun_family = AF_UNIX; - - strncpy (addr.sun_path, SOCK_PATH, (size_t)(UNIX_PATH_MAX - 1)); - addr.sun_path[UNIX_PATH_MAX - 1] = '\0'; - unlink (addr.sun_path); + sstrncpy (addr.sun_path, path, (size_t)(UNIX_PATH_MAX - 1)); errno = 0; - if (-1 == bind (local, (struct sockaddr *)&addr, + if (-1 == bind (connector_socket, (struct sockaddr *)&addr, offsetof (struct sockaddr_un, sun_path) + strlen(addr.sun_path))) { + char errbuf[1024]; disabled = 1; - syslog (LOG_ERR, "bind() failed: %s", strerror (errno)); + close (connector_socket); + connector_socket = -1; + log_err ("bind() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); pthread_exit ((void *)1); } errno = 0; - if (-1 == listen (local, 5)) { + if (-1 == listen (connector_socket, 5)) { + char errbuf[1024]; disabled = 1; - syslog (LOG_ERR, "listen() failed: %s", strerror (errno)); + close (connector_socket); + connector_socket = -1; + log_err ("listen() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); pthread_exit ((void *)1); } - if ((uid_t)0 == geteuid ()) { + { + struct group sg; struct group *grp; - - errno = 0; - if (NULL != (grp = getgrnam (sock_group))) { - errno = 0; - if (0 != chown (SOCK_PATH, (uid_t)-1, grp->gr_gid)) { - syslog (LOG_WARNING, "chown() failed: %s", strerror (errno)); - } + char grbuf[2048]; + int status; + + grp = NULL; + status = getgrnam_r (group, &sg, grbuf, sizeof (grbuf), &grp); + if (status != 0) + { + char errbuf[1024]; + log_warn ("getgrnam_r (%s) failed: %s", group, + sstrerror (errno, errbuf, sizeof (errbuf))); } - else { - syslog (LOG_WARNING, "getgrnam() failed: %s", strerror (errno)); + else if (grp == NULL) + { + log_warn ("No such group: `%s'", group); + } + else + { + status = chown (path, (uid_t) -1, grp->gr_gid); + if (status != 0) + { + char errbuf[1024]; + log_warn ("chown (%s, -1, %i) failed: %s", + path, (int) grp->gr_gid, + sstrerror (errno, errbuf, sizeof (errbuf))); + } } - } - else { - syslog (LOG_WARNING, "not running as root"); } errno = 0; - if (0 != chmod (SOCK_PATH, sock_perms)) { - syslog (LOG_WARNING, "chmod() failed: %s", strerror (errno)); + if (0 != chmod (path, sock_perms)) { + char errbuf[1024]; + log_warn ("chmod() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); } - { /* initialize queue of available threads */ - int i = 0; + { /* initialize collector threads */ + int i = 0; + int err = 0; - collector_t *last; + pthread_attr_t ptattr; - active.head = NULL; - active.tail = NULL; + conns.head = NULL; + conns.tail = NULL; + + pthread_attr_init (&ptattr); + pthread_attr_setdetachstate (&ptattr, PTHREAD_CREATE_DETACHED); - available.head = (collector_t *)smalloc (sizeof (collector_t)); - available.tail = available.head; - available.tail->next = NULL; + available_collectors = max_conns; - last = available.head; + collectors = + (collector_t **)smalloc (max_conns * sizeof (collector_t *)); - for (i = 1; i < max_conns; ++i) { - last->next = (collector_t *)smalloc (sizeof (collector_t)); - last = last->next; - available.tail = last; - available.tail->next = NULL; + for (i = 0; i < max_conns; ++i) { + collectors[i] = (collector_t *)smalloc (sizeof (collector_t)); + collectors[i]->socket = NULL; + + if (0 != (err = pthread_create (&collectors[i]->thread, &ptattr, + collect, collectors[i]))) { + char errbuf[1024]; + log_err ("pthread_create() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + collectors[i]->thread = (pthread_t) 0; + } } + + pthread_attr_destroy (&ptattr); } while (1) { int remote = 0; - int err = 0; - - collector_t *collector; - pthread_attr_t ptattr; + conn_t *connection; pthread_mutex_lock (&available_mutex); - while (NULL == available.head) { + + while (0 == available_collectors) { pthread_cond_wait (&collector_available, &available_mutex); } + + --available_collectors; + pthread_mutex_unlock (&available_mutex); do { errno = 0; - if (-1 == (remote = accept (local, NULL, NULL))) { + if (-1 == (remote = accept (connector_socket, NULL, NULL))) { if (EINTR != errno) { + char errbuf[1024]; disabled = 1; - syslog (LOG_ERR, "accept() failed: %s", strerror (errno)); + close (connector_socket); + connector_socket = -1; + log_err ("accept() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); pthread_exit ((void *)1); } } } while (EINTR == errno); - /* assign connection to next available thread */ - pthread_mutex_lock (&available_mutex); + connection = (conn_t *)smalloc (sizeof (conn_t)); - collector = available.head; - collector->socket = remote; + connection->socket = fdopen (remote, "r"); + connection->next = NULL; - if (available.head == available.tail) { - available.head = NULL; - available.tail = NULL; - } - else { - available.head = available.head->next; + if (NULL == connection->socket) { + close (remote); + continue; } - pthread_mutex_unlock (&available_mutex); - - collector->idx = 0; - collector->next = NULL; - - pthread_attr_init (&ptattr); - pthread_attr_setdetachstate (&ptattr, PTHREAD_CREATE_DETACHED); - - if (0 == (err = pthread_create (&collector->thread, &ptattr, collect, - (void *)collector))) { - pthread_mutex_lock (&active_mutex); + pthread_mutex_lock (&conns_mutex); - if (NULL == active.head) { - active.head = collector; - active.tail = collector; - } - else { - active.tail->next = collector; - active.tail = collector; - } - - pthread_mutex_unlock (&active_mutex); + if (NULL == conns.head) { + conns.head = connection; + conns.tail = connection; } else { - pthread_mutex_lock (&available_mutex); - - if (NULL == available.head) { - available.head = collector; - available.tail = collector; - } - else { - available.tail->next = collector; - available.tail = collector; - } - - pthread_mutex_unlock (&available_mutex); - - close (remote); - syslog (LOG_ERR, "pthread_create() failed: %s", strerror (err)); + conns.tail->next = connection; + conns.tail = conns.tail->next; } - pthread_attr_destroy (&ptattr); + pthread_mutex_unlock (&conns_mutex); + + pthread_cond_signal (&conn_available); } pthread_exit ((void *)0); -} /* void *open_connection (void *) */ -#endif /* EMAIL_HAVE_READ */ +} /* static void *open_connection (void *) */ -static void email_init (void) +static int email_init (void) { -#if EMAIL_HAVE_READ int err = 0; if (0 != (err = pthread_create (&connector, NULL, open_connection, NULL))) { + char errbuf[1024]; disabled = 1; - syslog (LOG_ERR, "pthread_create() failed: %s", strerror (err)); - return; + log_err ("pthread_create() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + return (-1); } -#endif /* EMAIL_HAVE_READ */ - return; -} /* static void email_init (void) */ -static void count_write (char *host, char *inst, char *val) + return (0); +} /* int email_init */ + +static int email_shutdown (void) { - char file[BUFSIZE] = ""; - int len = 0; + type_t *ptr = NULL; - len = snprintf (file, BUFSIZE, COUNT_FILE, inst); - if ((len < 0) || (len >= BUFSIZE)) - return; + int i = 0; - rrd_update_file (host, file, val, count_ds_def, count_ds_num); - return; -} /* static void email_write (char *host, char *inst, char *val) */ + if (connector != ((pthread_t) 0)) { + pthread_kill (connector, SIGTERM); + connector = (pthread_t) 0; + } -static void size_write (char *host, char *inst, char *val) -{ - char file[BUFSIZE] = ""; - int len = 0; + if (connector_socket >= 0) { + close (connector_socket); + connector_socket = -1; + } - len = snprintf (file, BUFSIZE, SIZE_FILE, inst); - if ((len < 0) || (len >= BUFSIZE)) - return; + /* don't allow any more connections to be processed */ + pthread_mutex_lock (&conns_mutex); - rrd_update_file (host, file, val, size_ds_def, size_ds_num); - return; -} /* static void size_write (char *host, char *inst, char *val) */ + available_collectors = 0; -static void score_write (char *host, char *inst, char *val) -{ - rrd_update_file (host, SCORE_FILE, val, score_ds_def, score_ds_num); - return; -} /* static void score_write (char *host, char *inst, char *val) */ + if (collectors != NULL) { + for (i = 0; i < max_conns; ++i) { + if (collectors[i] == NULL) + continue; -static void check_write (char *host, char *inst, char *val) -{ - char file[BUFSIZE] = ""; - int len = 0; + if (collectors[i]->thread != ((pthread_t) 0)) { + pthread_kill (collectors[i]->thread, SIGTERM); + collectors[i]->thread = (pthread_t) 0; + } - len = snprintf (file, BUFSIZE, CHECK_FILE, inst); - if ((len < 0) || (len >= BUFSIZE)) - return; + if (collectors[i]->socket != NULL) { + fclose (collectors[i]->socket); + collectors[i]->socket = NULL; + } - rrd_update_file (host, file, val, check_ds_def, check_ds_num); - return; -} /* static void check_write (char *host, char *inst, char *val) */ + sfree (collectors[i]); + } + sfree (collectors); + } /* if (collectors != NULL) */ -#if EMAIL_HAVE_READ -static void type_submit (char *plugin, char *inst, int value) -{ - char buf[BUFSIZE] = ""; - int len = 0; + pthread_mutex_unlock (&conns_mutex); - if (0 == value) - return; + for (ptr = list_count.head; NULL != ptr; ptr = ptr->next) { + free (ptr->name); + free (ptr); + } - len = snprintf (buf, BUFSIZE, "%u:%i", (unsigned int)curtime, value); - if ((len < 0) || (len >= BUFSIZE)) - return; + for (ptr = list_count_copy.head; NULL != ptr; ptr = ptr->next) { + free (ptr->name); + free (ptr); + } - plugin_submit (plugin, inst, buf); - return; -} /* static void type_submit (char *, char *, int) */ + for (ptr = list_size.head; NULL != ptr; ptr = ptr->next) { + free (ptr->name); + free (ptr); + } + + for (ptr = list_size_copy.head; NULL != ptr; ptr = ptr->next) { + free (ptr->name); + free (ptr); + } + + for (ptr = list_check.head; NULL != ptr; ptr = ptr->next) { + free (ptr->name); + free (ptr); + } + + for (ptr = list_check_copy.head; NULL != ptr; ptr = ptr->next) { + free (ptr->name); + free (ptr); + } + + unlink ((NULL == sock_file) ? SOCK_PATH : sock_file); -static void score_submit (double value) + sfree (sock_file); + sfree (sock_group); + return (0); +} /* static void email_shutdown (void) */ + +static void email_submit (const char *type, const char *type_instance, gauge_t value) +{ + value_t values[1]; + value_list_t vl = VALUE_LIST_INIT; + + values[0].gauge = value; + + vl.values = values; + vl.values_len = 1; + sstrncpy (vl.host, hostname_g, sizeof (vl.host)); + sstrncpy (vl.plugin, "email", sizeof (vl.plugin)); + sstrncpy (vl.type, type, sizeof (vl.type)); + sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); + + plugin_dispatch_values (&vl); +} /* void email_submit */ + +/* 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) { - char buf[BUFSIZE] = ""; - int len = 0; + type_t *ptr1; + type_t *ptr2; - if (0.0 == value) - return; + type_t *last = NULL; - len = snprintf (buf, BUFSIZE, "%u:%.2f", (unsigned int)curtime, value); - if ((len < 0) || (len >= BUFSIZE)) - return; + 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; - plugin_submit ("email_spam_score", NULL, buf); + 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) +static int email_read (void) { type_t *ptr; + double score_old; + int score_count_old; + if (disabled) - return; + return (-1); + /* email count */ pthread_mutex_lock (&count_mutex); - for (ptr = count.head; NULL != ptr; ptr = ptr->next) { - type_submit ("email_count", ptr->name, ptr->value); - ptr->value = 0; - } + copy_type_list (&list_count, &list_count_copy); pthread_mutex_unlock (&count_mutex); + for (ptr = list_count_copy.head; NULL != ptr; ptr = ptr->next) { + email_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 (&list_size, &list_size_copy); pthread_mutex_unlock (&size_mutex); + for (ptr = list_size_copy.head; NULL != ptr; ptr = ptr->next) { + email_submit ("email_size", ptr->name, ptr->value); + } + + /* spam score */ pthread_mutex_lock (&score_mutex); - score_submit (score); + score_old = score; + score_count_old = score_count; score = 0.0; score_count = 0; pthread_mutex_unlock (&score_mutex); + if (score_count_old > 0) + email_submit ("spam_score", "", score_old); + + /* 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 (&list_check, &list_check_copy); pthread_mutex_unlock (&check_mutex); - return; -} /* static void read (void) */ -#else /* if !EMAIL_HAVE_READ */ -# define email_read NULL -#endif + + for (ptr = list_check_copy.head; NULL != ptr; ptr = ptr->next) + email_submit ("spam_check", ptr->name, ptr->value); + + return (0); +} /* int email_read */ void module_register (void) { - plugin_register (MODULE_NAME, email_init, email_read, NULL); - plugin_register ("email_count", NULL, NULL, count_write); - plugin_register ("email_size", NULL, NULL, size_write); - plugin_register ("email_spam_score", NULL, NULL, score_write); - plugin_register ("email_spam_check", NULL, NULL, check_write); -#if EMAIL_HAVE_READ - cf_register (MODULE_NAME, email_config, config_keys, config_keys_num); -#endif /* EMAIL_HAVE_READ */ - return; -} /* void module_register (void) */ + plugin_register_config ("email", email_config, config_keys, config_keys_num); + plugin_register_init ("email", email_init); + plugin_register_read ("email", email_read); + plugin_register_shutdown ("email", email_shutdown); +} /* void module_register */ /* vim: set sw=4 ts=4 tw=78 noexpandtab : */ -