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>,...]
44 # include <sys/select.h>
45 #endif /* HAVE_SYS_SELECT_H */
48 # include <sys/socket.h>
49 #endif /* HAVE_SYS_SOCKET_H */
51 /* *sigh* glibc does not define UNIX_PATH_MAX in sys/un.h ... */
53 # include <linux/un.h>
56 #endif /* HAVE_LINUX_UN_H | HAVE_SYS_UN_H */
60 #endif /* HAVE_GRP_H */
62 #define MODULE_NAME "email"
64 /* 256 bytes ought to be enough for anybody ;-) */
67 #define SOCK_PATH "/tmp/.collectd-email"
70 /* linked list of email and check types */
82 /* linked list of collector thread control information */
83 typedef struct collector {
86 /* socket to read data from */
89 /* buffer to read data to */
91 int idx; /* current position in buffer */
93 struct collector *next;
101 /* state of the plugin */
102 static int disabled = 0;
104 /* thread managing "client" connections */
105 static pthread_t connector;
107 /* tell the connector thread that a collector is available */
108 static pthread_cond_t collector_available = PTHREAD_COND_INITIALIZER;
110 /* collector threads that are in use */
111 static pthread_mutex_t active_mutex = PTHREAD_MUTEX_INITIALIZER;
112 static collector_list_t active;
114 /* collector threads that are available for use */
115 static pthread_mutex_t available_mutex = PTHREAD_MUTEX_INITIALIZER;
116 static collector_list_t available;
118 #define COUNT_FILE "email/email-%s.rrd"
119 static char *count_ds_def[] =
121 "DS:count:GAUGE:"COLLECTD_HEARTBEAT":0:U",
124 static int count_ds_num = 1;
126 static pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
127 static type_list_t count;
129 #define SIZE_FILE "email/email_size-%s.rrd"
130 static char *size_ds_def[] =
132 "DS:size:GAUGE:"COLLECTD_HEARTBEAT":0:U",
135 static int size_ds_num = 1;
137 static pthread_mutex_t size_mutex = PTHREAD_MUTEX_INITIALIZER;
138 static type_list_t size;
140 #define SCORE_FILE "email/spam_score.rrd"
141 static char *score_ds_def[] =
143 "DS:score:GAUGE:"COLLECTD_HEARTBEAT":U:U",
146 static int score_ds_num = 1;
148 static pthread_mutex_t score_mutex = PTHREAD_MUTEX_INITIALIZER;
150 static int score_count;
152 #define CHECK_FILE "email/spam_check-%s.rrd"
153 static char *check_ds_def[] =
155 "DS:hits:GAUGE:"COLLECTD_HEARTBEAT":0:U",
158 static int check_ds_num = 1;
160 static pthread_mutex_t check_mutex = PTHREAD_MUTEX_INITIALIZER;
161 static type_list_t check;
163 /* Increment the value of the given name in the given list by incr. */
164 static void type_list_incr (type_list_t *list, char *name, int incr)
166 if (NULL == list->head) {
167 list->head = (type_t *)smalloc (sizeof (type_t));
169 list->head->name = sstrdup (name);
170 list->head->value = incr;
171 list->head->next = NULL;
173 list->tail = list->head;
178 for (ptr = list->head; NULL != ptr; ptr = ptr->next) {
179 if (0 == strcmp (name, ptr->name))
184 list->tail->next = (type_t *)smalloc (sizeof (type_t));
185 list->tail = list->tail->next;
187 list->tail->name = sstrdup (name);
188 list->tail->value = incr;
189 list->tail->next = NULL;
196 } /* static void type_list_incr (type_list_t *, char *) */
198 /* Read a single character from the socket. If an error occurs or end-of-file
199 * is reached return '\0'. */
200 char read_char (collector_t *src)
207 FD_SET (src->socket, &fdset);
209 if (-1 == select (src->socket + 1, &fdset, NULL, NULL, NULL)) {
210 syslog (LOG_ERR, "select() failed: %s", strerror (errno));
214 assert (FD_ISSET (src->socket, &fdset));
220 if (0 > (len = read (src->socket, (void *)&ret, 1))) {
221 if (EINTR != errno) {
222 syslog (LOG_ERR, "read() failed: %s", strerror (errno));
229 } while (EINTR == errno);
231 } /* char read_char (collector_t *) */
233 /* Read a single line (terminated by '\n') from the the socket.
235 * The return value is zero terminated and does not contain any newline
236 * characters. In case that no complete line is available (non-blocking mode
237 * should be enabled) an empty string is returned.
239 * If an error occurs or end-of-file is reached return NULL.
241 * IMPORTANT NOTE: If there is no newline character found in BUFSIZE
242 * characters of the input stream, the line will will be ignored! By
243 * definition we should not get any longer input lines, thus this is
244 * acceptable in this case ;-) */
245 char *read_line (collector_t *src)
250 assert (BUFSIZE > src->idx);
252 for (i = 0; i < src->idx; ++i) {
253 if ('\n' == src->buffer[i])
257 if ('\n' != src->buffer[i]) {
263 FD_SET (src->socket, &fdset);
265 if (-1 == select (src->socket + 1, &fdset, NULL, NULL, NULL)) {
266 syslog (LOG_ERR, "select() failed: %s", strerror (errno));
270 assert (FD_ISSET (src->socket, &fdset));
274 if (0 > (len = read (src->socket,
275 (void *)(&(src->buffer[0]) + src->idx),
276 BUFSIZE - src->idx))) {
277 if (EINTR != errno) {
278 syslog (LOG_ERR, "read() failed: %s", strerror (errno));
285 } while (EINTR == errno);
289 for (i = src->idx - len; i < src->idx; ++i) {
290 if ('\n' == src->buffer[i])
294 if ('\n' != src->buffer[i]) {
295 ret = (char *)smalloc (1);
299 if (BUFSIZE == src->idx) { /* no space left in buffer */
300 while ('\n' != read_char (src))
301 /* ignore complete line */;
309 ret = (char *)smalloc (i + 1);
310 memcpy (ret, &(src->buffer[0]), i + 1);
316 src->buffer[0] = '\0';
318 memmove (&(src->buffer[0]), &(src->buffer[i + 1]), src->idx);
320 } /* char *read_line (collector_t *) */
322 static void *collect (void *arg)
324 collector_t *this = (collector_t *)arg;
328 { /* put the socket in non-blocking mode */
332 if (-1 == fcntl (this->socket, F_GETFL, &flags)) {
333 syslog (LOG_ERR, "fcntl() failed: %s", strerror (errno));
338 if (-1 == fcntl (this->socket, F_SETFL, flags | O_NONBLOCK)) {
339 syslog (LOG_ERR, "fcntl() failed: %s", strerror (errno));
345 char *line = read_line (this);
352 if ('\0' == line[0]) {
357 if (':' != line[1]) {
358 syslog (LOG_ERR, "email: syntax error in line '%s'", line);
363 if ('e' == line[0]) { /* e:<type>:<bytes> */
364 char *type = strtok (line + 2, ":");
365 char *tmp = strtok (NULL, ":");
369 syslog (LOG_ERR, "email: syntax error in line '%s'", line);
376 pthread_mutex_lock (&count_mutex);
377 type_list_incr (&count, type, 1);
378 pthread_mutex_unlock (&count_mutex);
380 pthread_mutex_lock (&size_mutex);
381 type_list_incr (&size, type, bytes);
382 pthread_mutex_unlock (&size_mutex);
384 else if ('s' == line[0]) { /* s:<value> */
385 pthread_mutex_lock (&score_mutex);
386 score = (score * (double)score_count + atof (line + 2))
387 / (double)(score_count + 1);
389 pthread_mutex_unlock (&score_mutex);
391 else if ('c' == line[0]) { /* c:<type1>[,<type2>,...] */
392 char *type = strtok (line + 2, ",");
395 pthread_mutex_lock (&check_mutex);
396 type_list_incr (&check, type, 1);
397 pthread_mutex_unlock (&check_mutex);
398 } while (NULL != (type = strtok (NULL, ",")));
401 syslog (LOG_ERR, "email: unknown type '%c'", line[0]);
407 /* put this thread back into the available list */
408 pthread_mutex_lock (&active_mutex);
415 for (ptr = active.head; NULL != ptr; last = ptr, ptr = ptr->next) {
416 if (0 != pthread_equal (ptr->thread, this->thread))
420 /* the current thread _has_ to be in the active list */
421 assert (NULL != ptr);
424 active.head = ptr->next;
427 last->next = ptr->next;
429 if (NULL == last->next) {
434 pthread_mutex_unlock (&active_mutex);
438 pthread_mutex_lock (&available_mutex);
440 if (NULL == available.head) {
441 available.head = this;
442 available.tail = this;
445 available.tail->next = this;
446 available.tail = this;
449 pthread_mutex_unlock (&available_mutex);
451 pthread_cond_signal (&collector_available);
452 pthread_exit ((void *)0);
453 } /* void *collect (void *) */
455 static void *open_connection (void *arg)
459 struct sockaddr_un addr;
461 /* create UNIX socket */
463 if (-1 == (local = socket (PF_UNIX, SOCK_STREAM, 0))) {
465 syslog (LOG_ERR, "socket() failed: %s", strerror (errno));
466 pthread_exit ((void *)1);
469 addr.sun_family = AF_UNIX;
471 strncpy (addr.sun_path, SOCK_PATH, (size_t)(UNIX_PATH_MAX - 1));
472 addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
473 unlink (addr.sun_path);
476 if (-1 == bind (local, (struct sockaddr *)&addr,
477 offsetof (struct sockaddr_un, sun_path)
478 + strlen(addr.sun_path))) {
480 syslog (LOG_ERR, "bind() failed: %s", strerror (errno));
481 pthread_exit ((void *)1);
485 if (-1 == listen (local, 5)) {
487 syslog (LOG_ERR, "listen() failed: %s", strerror (errno));
488 pthread_exit ((void *)1);
491 if ((uid_t)0 == geteuid ()) {
495 if (NULL != (grp = getgrnam (COLLECTD_GRP_NAME))) {
497 if (0 != chown (SOCK_PATH, (uid_t)-1, grp->gr_gid)) {
498 syslog (LOG_WARNING, "chown() failed: %s", strerror (errno));
502 syslog (LOG_WARNING, "getgrnam() failed: %s", strerror (errno));
506 syslog (LOG_WARNING, "not running as root");
510 if (0 != chmod (SOCK_PATH, S_IRWXU | S_IRWXG)) {
511 syslog (LOG_WARNING, "chmod() failed: %s", strerror (errno));
514 { /* initialize queue of available threads */
522 available.head = (collector_t *)smalloc (sizeof (collector_t));
523 available.tail = available.head;
524 available.tail->next = NULL;
526 last = available.head;
528 for (i = 1; i < MAX_CONNS; ++i) {
529 last->next = (collector_t *)smalloc (sizeof (collector_t));
531 available.tail = last;
532 available.tail->next = NULL;
540 collector_t *collector;
542 pthread_attr_t ptattr;
544 pthread_mutex_lock (&available_mutex);
545 while (NULL == available.head) {
546 pthread_cond_wait (&collector_available, &available_mutex);
548 pthread_mutex_unlock (&available_mutex);
552 if (-1 == (remote = accept (local, NULL, NULL))) {
553 if (EINTR != errno) {
555 syslog (LOG_ERR, "accept() failed: %s", strerror (errno));
556 pthread_exit ((void *)1);
559 } while (EINTR == errno);
561 /* assign connection to next available thread */
562 pthread_mutex_lock (&available_mutex);
564 collector = available.head;
565 collector->socket = remote;
567 if (available.head == available.tail) {
568 available.head = NULL;
569 available.tail = NULL;
572 available.head = available.head->next;
575 pthread_mutex_unlock (&available_mutex);
578 collector->next = NULL;
580 pthread_attr_init (&ptattr);
581 pthread_attr_setdetachstate (&ptattr, PTHREAD_CREATE_DETACHED);
583 if (0 == (err = pthread_create (&collector->thread, &ptattr, collect,
584 (void *)collector))) {
585 pthread_mutex_lock (&active_mutex);
587 if (NULL == active.head) {
588 active.head = collector;
589 active.tail = collector;
592 active.tail->next = collector;
593 active.tail = collector;
596 pthread_mutex_unlock (&active_mutex);
599 pthread_mutex_lock (&available_mutex);
601 if (NULL == available.head) {
602 available.head = collector;
603 available.tail = collector;
606 available.tail->next = collector;
607 available.tail = collector;
610 pthread_mutex_unlock (&available_mutex);
613 syslog (LOG_ERR, "pthread_create() failed: %s", strerror (err));
616 pthread_attr_destroy (&ptattr);
618 pthread_exit ((void *)0);
619 } /* void *open_connection (void *) */
621 static void email_init (void)
625 if (0 != (err = pthread_create (&connector, NULL,
626 open_connection, NULL))) {
628 syslog (LOG_ERR, "pthread_create() failed: %s", strerror (err));
632 } /* static void email_init (void) */
634 static void count_write (char *host, char *inst, char *val)
636 char file[BUFSIZE] = "";
639 len = snprintf (file, BUFSIZE, COUNT_FILE, inst);
640 if ((len < 0) || (len >= BUFSIZE))
643 rrd_update_file (host, file, val, count_ds_def, count_ds_num);
645 } /* static void email_write (char *host, char *inst, char *val) */
647 static void size_write (char *host, char *inst, char *val)
649 char file[BUFSIZE] = "";
652 len = snprintf (file, BUFSIZE, SIZE_FILE, inst);
653 if ((len < 0) || (len >= BUFSIZE))
656 rrd_update_file (host, file, val, size_ds_def, size_ds_num);
658 } /* static void size_write (char *host, char *inst, char *val) */
660 static void score_write (char *host, char *inst, char *val)
662 rrd_update_file (host, SCORE_FILE, val, score_ds_def, score_ds_num);
664 } /* static void score_write (char *host, char *inst, char *val) */
666 static void check_write (char *host, char *inst, char *val)
668 char file[BUFSIZE] = "";
671 len = snprintf (file, BUFSIZE, CHECK_FILE, inst);
672 if ((len < 0) || (len >= BUFSIZE))
675 rrd_update_file (host, file, val, check_ds_def, check_ds_num);
677 } /* static void check_write (char *host, char *inst, char *val) */
679 static void type_submit (char *plugin, char *inst, int value)
681 char buf[BUFSIZE] = "";
687 len = snprintf (buf, BUFSIZE, "%u:%i", (unsigned int)curtime, value);
688 if ((len < 0) || (len >= BUFSIZE))
691 plugin_submit (plugin, inst, buf);
693 } /* static void type_submit (char *, char *, int) */
695 static void score_submit (double value)
697 char buf[BUFSIZE] = "";
703 len = snprintf (buf, BUFSIZE, "%u:%.2f", (unsigned int)curtime, value);
704 if ((len < 0) || (len >= BUFSIZE))
707 plugin_submit ("email_spam_score", NULL, buf);
711 static void email_read (void)
718 pthread_mutex_lock (&count_mutex);
720 for (ptr = count.head; NULL != ptr; ptr = ptr->next) {
721 type_submit ("email_count", ptr->name, ptr->value);
725 pthread_mutex_unlock (&count_mutex);
727 pthread_mutex_lock (&size_mutex);
729 for (ptr = size.head; NULL != ptr; ptr = ptr->next) {
730 type_submit ("email_size", ptr->name, ptr->value);
734 pthread_mutex_unlock (&size_mutex);
736 pthread_mutex_lock (&score_mutex);
738 score_submit (score);
742 pthread_mutex_unlock (&score_mutex);
744 pthread_mutex_lock (&check_mutex);
746 for (ptr = check.head; NULL != ptr; ptr = ptr->next) {
747 type_submit ("email_spam_check", ptr->name, ptr->value);
751 pthread_mutex_unlock (&check_mutex);
753 } /* static void read (void) */
755 void module_register (void)
757 plugin_register (MODULE_NAME, email_init, email_read, NULL);
758 plugin_register ("email_count", NULL, NULL, count_write);
759 plugin_register ("email_size", NULL, NULL, size_write);
760 plugin_register ("email_spam_score", NULL, NULL, score_write);
761 plugin_register ("email_spam_check", NULL, NULL, check_write);
763 } /* void module_register (void) */
765 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */