#include "common.h"
#include "plugin.h"
+#include "configfile.h"
+
#if HAVE_LIBPTHREAD
# include <pthread.h>
# define EMAIL_HAVE_READ 1
/* 256 bytes ought to be enough for anybody ;-) */
#define BUFSIZE 256
-#define SOCK_PATH "/tmp/.collectd-email"
+#ifndef COLLECTD_SOCKET_PREFIX
+# define COLLECTD_SOCKET_PREFIX "/tmp/.collectd-"
+#endif /* COLLECTD_SOCKET_PREFIX */
+
+#define SOCK_PATH COLLECTD_SOCKET_PREFIX"email"
#define MAX_CONNS 5
+#define MAX_CONNS_LIMIT 16384
/*
* Private data structures
* Private variables
*/
#if EMAIL_HAVE_READ
+/* valid configuration file keys */
+static char *config_keys[] =
+{
+ "SocketGroup",
+ "SocketPerms",
+ "MaxConns",
+ NULL
+};
+static int config_keys_num = 3;
+
+/* socket configuration */
+static char *sock_group = COLLECTD_GRP_NAME;
+static int sock_perms = S_IRWXU | S_IRWXG;
+static int max_conns = MAX_CONNS;
+
/* state of the plugin */
static int disabled = 0;
/* thread managing "client" connections */
static pthread_t connector;
+static int connector_socket;
/* tell the connector thread that a collector is available */
static pthread_cond_t collector_available = PTHREAD_COND_INITIALIZER;
static int check_ds_num = 1;
#if EMAIL_HAVE_READ
+static int email_config (char *key, char *value)
+{
+ if (0 == strcasecmp (key, "SocketGroup")) {
+ 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);
+ }
+ else if (0 == strcasecmp (key, "MaxConns")) {
+ long int tmp = strtol (value, NULL, 0);
+
+ if (tmp < 1) {
+ fprintf (stderr, "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);
+ max_conns = MAX_CONNS_LIMIT;
+ }
+ else {
+ max_conns = (int)tmp;
+ }
+ }
+ else {
+ return -1;
+ }
+ return 0;
+} /* static int email_config (char *, char *) */
+
/* Increment the value of the given name in the given list by incr. */
static void type_list_incr (type_list_t *list, char *name, int incr)
{
static void *open_connection (void *arg)
{
- int local = 0;
-
struct sockaddr_un addr;
/* create UNIX socket */
errno = 0;
- if (-1 == (local = socket (PF_UNIX, SOCK_STREAM, 0))) {
+ if (-1 == (connector_socket = socket (PF_UNIX, SOCK_STREAM, 0))) {
disabled = 1;
syslog (LOG_ERR, "socket() failed: %s", strerror (errno));
pthread_exit ((void *)1);
unlink (addr.sun_path);
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))) {
disabled = 1;
}
errno = 0;
- if (-1 == listen (local, 5)) {
+ if (-1 == listen (connector_socket, 5)) {
disabled = 1;
syslog (LOG_ERR, "listen() failed: %s", strerror (errno));
pthread_exit ((void *)1);
struct group *grp;
errno = 0;
- if (NULL != (grp = getgrnam (COLLECTD_GRP_NAME))) {
+ 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));
}
errno = 0;
- if (0 != chmod (SOCK_PATH, S_IRWXU | S_IRWXG)) {
+ if (0 != chmod (SOCK_PATH, sock_perms)) {
syslog (LOG_WARNING, "chmod() failed: %s", strerror (errno));
}
last = available.head;
- for (i = 1; i < MAX_CONNS; ++i) {
+ for (i = 1; i < max_conns; ++i) {
last->next = (collector_t *)smalloc (sizeof (collector_t));
last = last->next;
available.tail = last;
do {
errno = 0;
- if (-1 == (remote = accept (local, NULL, NULL))) {
+ if (-1 == (remote = accept (connector_socket, NULL, NULL))) {
if (EINTR != errno) {
disabled = 1;
syslog (LOG_ERR, "accept() failed: %s", strerror (errno));
return;
} /* static void email_init (void) */
+#if EMAIL_HAVE_READ
+static void email_shutdown (void)
+{
+ collector_t *ptr;
+
+ if (disabled)
+ return;
+
+ close (connector_socket);
+ pthread_kill (connector, SIGTERM);
+
+ pthread_mutex_lock (&active_mutex);
+
+ for (ptr = active.head; NULL != ptr; ptr = ptr->next) {
+ close (ptr->socket);
+ pthread_kill (ptr->thread, SIGTERM);
+ }
+
+ pthread_mutex_unlock (&active_mutex);
+
+ unlink (SOCK_PATH);
+ return;
+} /* static void email_shutdown (void) */
+#endif /* EMAIL_HAVE_READ */
+
static void count_write (char *host, char *inst, char *val)
{
char file[BUFSIZE] = "";
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
+ plugin_register_shutdown (MODULE_NAME, email_shutdown);
+ cf_register (MODULE_NAME, email_config, config_keys, config_keys_num);
+#endif /* EMAIL_HAVE_READ */
return;
} /* void module_register (void) */