2 * collectd - src/email.c
3 * Copyright (C) 2006 Sebastian Harl
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Sebastian Harl <sh at tokkee.org>
24 * This plugin communicates with a spam filter, a virus scanner or similar
25 * software using a UNIX socket and a very simple protocol:
27 * e-mail type (e.g. ham, spam, virus, ...) and size
33 * successful spam checks
34 * c:<type1>[,<type2>,...]
41 #include "configfile.h"
45 # define EMAIL_HAVE_READ 1
47 # define EMAIL_HAVE_READ 0
51 # include <sys/select.h>
52 #endif /* HAVE_SYS_SELECT_H */
55 # include <sys/socket.h>
56 #endif /* HAVE_SYS_SOCKET_H */
58 /* *sigh* glibc does not define UNIX_PATH_MAX in sys/un.h ... */
60 # include <linux/un.h>
63 #endif /* HAVE_LINUX_UN_H | HAVE_SYS_UN_H */
67 #endif /* HAVE_GRP_H */
69 #define MODULE_NAME "email"
71 /* 256 bytes ought to be enough for anybody ;-) */
74 #define SOCK_PATH "/tmp/.collectd-email"
78 * Private data structures
81 /* linked list of email and check types */
93 /* linked list of collector thread control information */
94 typedef struct collector {
97 /* socket to read data from */
100 /* buffer to read data to */
101 char buffer[BUFSIZE];
102 int idx; /* current position in buffer */
104 struct collector *next;
111 #endif /* EMAIL_HAVE_READ */
117 /* valid configuration file keys */
118 static char *config_keys[] =
125 static int config_keys_num = 3;
127 /* socket configuration */
128 static char *sock_group = COLLECTD_GRP_NAME;
129 static int sock_perms = S_IRWXU | S_IRWXG;
130 static int max_conns = MAX_CONNS;
132 /* state of the plugin */
133 static int disabled = 0;
135 /* thread managing "client" connections */
136 static pthread_t connector;
138 /* tell the connector thread that a collector is available */
139 static pthread_cond_t collector_available = PTHREAD_COND_INITIALIZER;
141 /* collector threads that are in use */
142 static pthread_mutex_t active_mutex = PTHREAD_MUTEX_INITIALIZER;
143 static collector_list_t active;
145 /* collector threads that are available for use */
146 static pthread_mutex_t available_mutex = PTHREAD_MUTEX_INITIALIZER;
147 static collector_list_t available;
149 static pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
150 static type_list_t count;
152 static pthread_mutex_t size_mutex = PTHREAD_MUTEX_INITIALIZER;
153 static type_list_t size;
155 static pthread_mutex_t score_mutex = PTHREAD_MUTEX_INITIALIZER;
157 static int score_count;
159 static pthread_mutex_t check_mutex = PTHREAD_MUTEX_INITIALIZER;
160 static type_list_t check;
161 #endif /* EMAIL_HAVE_READ */
163 #define COUNT_FILE "email/email-%s.rrd"
164 static char *count_ds_def[] =
166 "DS:count:GAUGE:"COLLECTD_HEARTBEAT":0:U",
169 static int count_ds_num = 1;
171 #define SIZE_FILE "email/email_size-%s.rrd"
172 static char *size_ds_def[] =
174 "DS:size:GAUGE:"COLLECTD_HEARTBEAT":0:U",
177 static int size_ds_num = 1;
179 #define SCORE_FILE "email/spam_score.rrd"
180 static char *score_ds_def[] =
182 "DS:score:GAUGE:"COLLECTD_HEARTBEAT":U:U",
185 static int score_ds_num = 1;
187 #define CHECK_FILE "email/spam_check-%s.rrd"
188 static char *check_ds_def[] =
190 "DS:hits:GAUGE:"COLLECTD_HEARTBEAT":0:U",
193 static int check_ds_num = 1;
196 static int email_config (char *key, char *value)
198 if (0 == strcasecmp (key, "SocketGroup")) {
199 sock_group = sstrdup (value);
201 else if (0 == strcasecmp (key, "SocketPerms")) {
202 /* the user is responsible for providing reasonable values */
203 sock_perms = (int)strtol (value, NULL, 0);
205 else if (0 == strcasecmp (key, "MaxConns")) {
206 long int tmp = strtol (value, NULL, 0);
212 max_conns = (int)tmp;
219 } /* static int email_config (char *, char *) */
221 /* Increment the value of the given name in the given list by incr. */
222 static void type_list_incr (type_list_t *list, char *name, int incr)
224 if (NULL == list->head) {
225 list->head = (type_t *)smalloc (sizeof (type_t));
227 list->head->name = sstrdup (name);
228 list->head->value = incr;
229 list->head->next = NULL;
231 list->tail = list->head;
236 for (ptr = list->head; NULL != ptr; ptr = ptr->next) {
237 if (0 == strcmp (name, ptr->name))
242 list->tail->next = (type_t *)smalloc (sizeof (type_t));
243 list->tail = list->tail->next;
245 list->tail->name = sstrdup (name);
246 list->tail->value = incr;
247 list->tail->next = NULL;
254 } /* static void type_list_incr (type_list_t *, char *) */
256 /* Read a single character from the socket. If an error occurs or end-of-file
257 * is reached return '\0'. */
258 char read_char (collector_t *src)
265 FD_SET (src->socket, &fdset);
267 if (-1 == select (src->socket + 1, &fdset, NULL, NULL, NULL)) {
268 syslog (LOG_ERR, "select() failed: %s", strerror (errno));
272 assert (FD_ISSET (src->socket, &fdset));
278 if (0 > (len = read (src->socket, (void *)&ret, 1))) {
279 if (EINTR != errno) {
280 syslog (LOG_ERR, "read() failed: %s", strerror (errno));
287 } while (EINTR == errno);
289 } /* char read_char (collector_t *) */
291 /* Read a single line (terminated by '\n') from the the socket.
293 * The return value is zero terminated and does not contain any newline
294 * characters. In case that no complete line is available (non-blocking mode
295 * should be enabled) an empty string is returned.
297 * If an error occurs or end-of-file is reached return NULL.
299 * IMPORTANT NOTE: If there is no newline character found in BUFSIZE
300 * characters of the input stream, the line will will be ignored! By
301 * definition we should not get any longer input lines, thus this is
302 * acceptable in this case ;-) */
303 char *read_line (collector_t *src)
308 assert (BUFSIZE > src->idx);
310 for (i = 0; i < src->idx; ++i) {
311 if ('\n' == src->buffer[i])
315 if ('\n' != src->buffer[i]) {
321 FD_SET (src->socket, &fdset);
323 if (-1 == select (src->socket + 1, &fdset, NULL, NULL, NULL)) {
324 syslog (LOG_ERR, "select() failed: %s", strerror (errno));
328 assert (FD_ISSET (src->socket, &fdset));
332 if (0 > (len = read (src->socket,
333 (void *)(&(src->buffer[0]) + src->idx),
334 BUFSIZE - src->idx))) {
335 if (EINTR != errno) {
336 syslog (LOG_ERR, "read() failed: %s", strerror (errno));
343 } while (EINTR == errno);
347 for (i = src->idx - len; i < src->idx; ++i) {
348 if ('\n' == src->buffer[i])
352 if ('\n' != src->buffer[i]) {
353 ret = (char *)smalloc (1);
357 if (BUFSIZE == src->idx) { /* no space left in buffer */
358 while ('\n' != read_char (src))
359 /* ignore complete line */;
367 ret = (char *)smalloc (i + 1);
368 memcpy (ret, &(src->buffer[0]), i + 1);
374 src->buffer[0] = '\0';
376 memmove (&(src->buffer[0]), &(src->buffer[i + 1]), src->idx);
378 } /* char *read_line (collector_t *) */
380 static void *collect (void *arg)
382 collector_t *this = (collector_t *)arg;
386 { /* put the socket in non-blocking mode */
390 if (-1 == fcntl (this->socket, F_GETFL, &flags)) {
391 syslog (LOG_ERR, "fcntl() failed: %s", strerror (errno));
396 if (-1 == fcntl (this->socket, F_SETFL, flags | O_NONBLOCK)) {
397 syslog (LOG_ERR, "fcntl() failed: %s", strerror (errno));
403 char *line = read_line (this);
410 if ('\0' == line[0]) {
415 if (':' != line[1]) {
416 syslog (LOG_ERR, "email: syntax error in line '%s'", line);
421 if ('e' == line[0]) { /* e:<type>:<bytes> */
423 char *type = strtok_r (line + 2, ":", &ptr);
424 char *tmp = strtok_r (NULL, ":", &ptr);
428 syslog (LOG_ERR, "email: syntax error in line '%s'", line);
435 pthread_mutex_lock (&count_mutex);
436 type_list_incr (&count, type, 1);
437 pthread_mutex_unlock (&count_mutex);
439 pthread_mutex_lock (&size_mutex);
440 type_list_incr (&size, type, bytes);
441 pthread_mutex_unlock (&size_mutex);
443 else if ('s' == line[0]) { /* s:<value> */
444 pthread_mutex_lock (&score_mutex);
445 score = (score * (double)score_count + atof (line + 2))
446 / (double)(score_count + 1);
448 pthread_mutex_unlock (&score_mutex);
450 else if ('c' == line[0]) { /* c:<type1>[,<type2>,...] */
452 char *type = strtok_r (line + 2, ",", &ptr);
455 pthread_mutex_lock (&check_mutex);
456 type_list_incr (&check, type, 1);
457 pthread_mutex_unlock (&check_mutex);
458 } while (NULL != (type = strtok_r (NULL, ",", &ptr)));
461 syslog (LOG_ERR, "email: unknown type '%c'", line[0]);
467 /* put this thread back into the available list */
468 pthread_mutex_lock (&active_mutex);
475 for (ptr = active.head; NULL != ptr; last = ptr, ptr = ptr->next) {
476 if (0 != pthread_equal (ptr->thread, this->thread))
480 /* the current thread _has_ to be in the active list */
481 assert (NULL != ptr);
484 active.head = ptr->next;
487 last->next = ptr->next;
489 if (NULL == last->next) {
494 pthread_mutex_unlock (&active_mutex);
498 pthread_mutex_lock (&available_mutex);
500 if (NULL == available.head) {
501 available.head = this;
502 available.tail = this;
505 available.tail->next = this;
506 available.tail = this;
509 pthread_mutex_unlock (&available_mutex);
511 pthread_cond_signal (&collector_available);
512 pthread_exit ((void *)0);
513 } /* void *collect (void *) */
515 static void *open_connection (void *arg)
519 struct sockaddr_un addr;
521 /* create UNIX socket */
523 if (-1 == (local = socket (PF_UNIX, SOCK_STREAM, 0))) {
525 syslog (LOG_ERR, "socket() failed: %s", strerror (errno));
526 pthread_exit ((void *)1);
529 addr.sun_family = AF_UNIX;
531 strncpy (addr.sun_path, SOCK_PATH, (size_t)(UNIX_PATH_MAX - 1));
532 addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
533 unlink (addr.sun_path);
536 if (-1 == bind (local, (struct sockaddr *)&addr,
537 offsetof (struct sockaddr_un, sun_path)
538 + strlen(addr.sun_path))) {
540 syslog (LOG_ERR, "bind() failed: %s", strerror (errno));
541 pthread_exit ((void *)1);
545 if (-1 == listen (local, 5)) {
547 syslog (LOG_ERR, "listen() failed: %s", strerror (errno));
548 pthread_exit ((void *)1);
551 if ((uid_t)0 == geteuid ()) {
555 if (NULL != (grp = getgrnam (sock_group))) {
557 if (0 != chown (SOCK_PATH, (uid_t)-1, grp->gr_gid)) {
558 syslog (LOG_WARNING, "chown() failed: %s", strerror (errno));
562 syslog (LOG_WARNING, "getgrnam() failed: %s", strerror (errno));
566 syslog (LOG_WARNING, "not running as root");
570 if (0 != chmod (SOCK_PATH, sock_perms)) {
571 syslog (LOG_WARNING, "chmod() failed: %s", strerror (errno));
574 { /* initialize queue of available threads */
582 available.head = (collector_t *)smalloc (sizeof (collector_t));
583 available.tail = available.head;
584 available.tail->next = NULL;
586 last = available.head;
588 for (i = 1; i < max_conns; ++i) {
589 last->next = (collector_t *)smalloc (sizeof (collector_t));
591 available.tail = last;
592 available.tail->next = NULL;
600 collector_t *collector;
602 pthread_attr_t ptattr;
604 pthread_mutex_lock (&available_mutex);
605 while (NULL == available.head) {
606 pthread_cond_wait (&collector_available, &available_mutex);
608 pthread_mutex_unlock (&available_mutex);
612 if (-1 == (remote = accept (local, NULL, NULL))) {
613 if (EINTR != errno) {
615 syslog (LOG_ERR, "accept() failed: %s", strerror (errno));
616 pthread_exit ((void *)1);
619 } while (EINTR == errno);
621 /* assign connection to next available thread */
622 pthread_mutex_lock (&available_mutex);
624 collector = available.head;
625 collector->socket = remote;
627 if (available.head == available.tail) {
628 available.head = NULL;
629 available.tail = NULL;
632 available.head = available.head->next;
635 pthread_mutex_unlock (&available_mutex);
638 collector->next = NULL;
640 pthread_attr_init (&ptattr);
641 pthread_attr_setdetachstate (&ptattr, PTHREAD_CREATE_DETACHED);
643 if (0 == (err = pthread_create (&collector->thread, &ptattr, collect,
644 (void *)collector))) {
645 pthread_mutex_lock (&active_mutex);
647 if (NULL == active.head) {
648 active.head = collector;
649 active.tail = collector;
652 active.tail->next = collector;
653 active.tail = collector;
656 pthread_mutex_unlock (&active_mutex);
659 pthread_mutex_lock (&available_mutex);
661 if (NULL == available.head) {
662 available.head = collector;
663 available.tail = collector;
666 available.tail->next = collector;
667 available.tail = collector;
670 pthread_mutex_unlock (&available_mutex);
673 syslog (LOG_ERR, "pthread_create() failed: %s", strerror (err));
676 pthread_attr_destroy (&ptattr);
678 pthread_exit ((void *)0);
679 } /* void *open_connection (void *) */
680 #endif /* EMAIL_HAVE_READ */
682 static void email_init (void)
687 if (0 != (err = pthread_create (&connector, NULL,
688 open_connection, NULL))) {
690 syslog (LOG_ERR, "pthread_create() failed: %s", strerror (err));
693 #endif /* EMAIL_HAVE_READ */
695 } /* static void email_init (void) */
697 static void count_write (char *host, char *inst, char *val)
699 char file[BUFSIZE] = "";
702 len = snprintf (file, BUFSIZE, COUNT_FILE, inst);
703 if ((len < 0) || (len >= BUFSIZE))
706 rrd_update_file (host, file, val, count_ds_def, count_ds_num);
708 } /* static void email_write (char *host, char *inst, char *val) */
710 static void size_write (char *host, char *inst, char *val)
712 char file[BUFSIZE] = "";
715 len = snprintf (file, BUFSIZE, SIZE_FILE, inst);
716 if ((len < 0) || (len >= BUFSIZE))
719 rrd_update_file (host, file, val, size_ds_def, size_ds_num);
721 } /* static void size_write (char *host, char *inst, char *val) */
723 static void score_write (char *host, char *inst, char *val)
725 rrd_update_file (host, SCORE_FILE, val, score_ds_def, score_ds_num);
727 } /* static void score_write (char *host, char *inst, char *val) */
729 static void check_write (char *host, char *inst, char *val)
731 char file[BUFSIZE] = "";
734 len = snprintf (file, BUFSIZE, CHECK_FILE, inst);
735 if ((len < 0) || (len >= BUFSIZE))
738 rrd_update_file (host, file, val, check_ds_def, check_ds_num);
740 } /* static void check_write (char *host, char *inst, char *val) */
743 static void type_submit (char *plugin, char *inst, int value)
745 char buf[BUFSIZE] = "";
751 len = snprintf (buf, BUFSIZE, "%u:%i", (unsigned int)curtime, value);
752 if ((len < 0) || (len >= BUFSIZE))
755 plugin_submit (plugin, inst, buf);
757 } /* static void type_submit (char *, char *, int) */
759 static void score_submit (double value)
761 char buf[BUFSIZE] = "";
767 len = snprintf (buf, BUFSIZE, "%u:%.2f", (unsigned int)curtime, value);
768 if ((len < 0) || (len >= BUFSIZE))
771 plugin_submit ("email_spam_score", NULL, buf);
775 static void email_read (void)
782 pthread_mutex_lock (&count_mutex);
784 for (ptr = count.head; NULL != ptr; ptr = ptr->next) {
785 type_submit ("email_count", ptr->name, ptr->value);
789 pthread_mutex_unlock (&count_mutex);
791 pthread_mutex_lock (&size_mutex);
793 for (ptr = size.head; NULL != ptr; ptr = ptr->next) {
794 type_submit ("email_size", ptr->name, ptr->value);
798 pthread_mutex_unlock (&size_mutex);
800 pthread_mutex_lock (&score_mutex);
802 score_submit (score);
806 pthread_mutex_unlock (&score_mutex);
808 pthread_mutex_lock (&check_mutex);
810 for (ptr = check.head; NULL != ptr; ptr = ptr->next) {
811 type_submit ("email_spam_check", ptr->name, ptr->value);
815 pthread_mutex_unlock (&check_mutex);
817 } /* static void read (void) */
818 #else /* if !EMAIL_HAVE_READ */
819 # define email_read NULL
822 void module_register (void)
824 plugin_register (MODULE_NAME, email_init, email_read, NULL);
825 plugin_register ("email_count", NULL, NULL, count_write);
826 plugin_register ("email_size", NULL, NULL, size_write);
827 plugin_register ("email_spam_score", NULL, NULL, score_write);
828 plugin_register ("email_spam_check", NULL, NULL, check_write);
830 cf_register (MODULE_NAME, email_config, config_keys, config_keys_num);
831 #endif /* EMAIL_HAVE_READ */
833 } /* void module_register (void) */
835 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */