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>,...]
43 # define EMAIL_HAVE_READ 1
45 # define EMAIL_HAVE_READ 0
49 # include <sys/select.h>
50 #endif /* HAVE_SYS_SELECT_H */
53 # include <sys/socket.h>
54 #endif /* HAVE_SYS_SOCKET_H */
56 /* *sigh* glibc does not define UNIX_PATH_MAX in sys/un.h ... */
58 # include <linux/un.h>
61 #endif /* HAVE_LINUX_UN_H | HAVE_SYS_UN_H */
65 #endif /* HAVE_GRP_H */
67 #define MODULE_NAME "email"
69 /* 256 bytes ought to be enough for anybody ;-) */
72 #define SOCK_PATH "/tmp/.collectd-email"
76 * Private data structures
79 /* linked list of email and check types */
91 /* linked list of collector thread control information */
92 typedef struct collector {
95 /* socket to read data from */
98 /* buffer to read data to */
100 int idx; /* current position in buffer */
102 struct collector *next;
109 #endif /* EMAIL_HAVE_READ */
115 /* state of the plugin */
116 static int disabled = 0;
118 /* thread managing "client" connections */
119 static pthread_t connector;
121 /* tell the connector thread that a collector is available */
122 static pthread_cond_t collector_available = PTHREAD_COND_INITIALIZER;
124 /* collector threads that are in use */
125 static pthread_mutex_t active_mutex = PTHREAD_MUTEX_INITIALIZER;
126 static collector_list_t active;
128 /* collector threads that are available for use */
129 static pthread_mutex_t available_mutex = PTHREAD_MUTEX_INITIALIZER;
130 static collector_list_t available;
132 static pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
133 static type_list_t count;
135 static pthread_mutex_t size_mutex = PTHREAD_MUTEX_INITIALIZER;
136 static type_list_t size;
138 static pthread_mutex_t score_mutex = PTHREAD_MUTEX_INITIALIZER;
140 static int score_count;
142 static pthread_mutex_t check_mutex = PTHREAD_MUTEX_INITIALIZER;
143 static type_list_t check;
144 #endif /* EMAIL_HAVE_READ */
146 #define COUNT_FILE "email/email-%s.rrd"
147 static char *count_ds_def[] =
149 "DS:count:GAUGE:"COLLECTD_HEARTBEAT":0:U",
152 static int count_ds_num = 1;
154 #define SIZE_FILE "email/email_size-%s.rrd"
155 static char *size_ds_def[] =
157 "DS:size:GAUGE:"COLLECTD_HEARTBEAT":0:U",
160 static int size_ds_num = 1;
162 #define SCORE_FILE "email/spam_score.rrd"
163 static char *score_ds_def[] =
165 "DS:score:GAUGE:"COLLECTD_HEARTBEAT":U:U",
168 static int score_ds_num = 1;
170 #define CHECK_FILE "email/spam_check-%s.rrd"
171 static char *check_ds_def[] =
173 "DS:hits:GAUGE:"COLLECTD_HEARTBEAT":0:U",
176 static int check_ds_num = 1;
179 /* Increment the value of the given name in the given list by incr. */
180 static void type_list_incr (type_list_t *list, char *name, int incr)
182 if (NULL == list->head) {
183 list->head = (type_t *)smalloc (sizeof (type_t));
185 list->head->name = sstrdup (name);
186 list->head->value = incr;
187 list->head->next = NULL;
189 list->tail = list->head;
194 for (ptr = list->head; NULL != ptr; ptr = ptr->next) {
195 if (0 == strcmp (name, ptr->name))
200 list->tail->next = (type_t *)smalloc (sizeof (type_t));
201 list->tail = list->tail->next;
203 list->tail->name = sstrdup (name);
204 list->tail->value = incr;
205 list->tail->next = NULL;
212 } /* static void type_list_incr (type_list_t *, char *) */
214 /* Read a single character from the socket. If an error occurs or end-of-file
215 * is reached return '\0'. */
216 char read_char (collector_t *src)
223 FD_SET (src->socket, &fdset);
225 if (-1 == select (src->socket + 1, &fdset, NULL, NULL, NULL)) {
226 syslog (LOG_ERR, "select() failed: %s", strerror (errno));
230 assert (FD_ISSET (src->socket, &fdset));
236 if (0 > (len = read (src->socket, (void *)&ret, 1))) {
237 if (EINTR != errno) {
238 syslog (LOG_ERR, "read() failed: %s", strerror (errno));
245 } while (EINTR == errno);
247 } /* char read_char (collector_t *) */
249 /* Read a single line (terminated by '\n') from the the socket.
251 * The return value is zero terminated and does not contain any newline
252 * characters. In case that no complete line is available (non-blocking mode
253 * should be enabled) an empty string is returned.
255 * If an error occurs or end-of-file is reached return NULL.
257 * IMPORTANT NOTE: If there is no newline character found in BUFSIZE
258 * characters of the input stream, the line will will be ignored! By
259 * definition we should not get any longer input lines, thus this is
260 * acceptable in this case ;-) */
261 char *read_line (collector_t *src)
266 assert (BUFSIZE > src->idx);
268 for (i = 0; i < src->idx; ++i) {
269 if ('\n' == src->buffer[i])
273 if ('\n' != src->buffer[i]) {
279 FD_SET (src->socket, &fdset);
281 if (-1 == select (src->socket + 1, &fdset, NULL, NULL, NULL)) {
282 syslog (LOG_ERR, "select() failed: %s", strerror (errno));
286 assert (FD_ISSET (src->socket, &fdset));
290 if (0 > (len = read (src->socket,
291 (void *)(&(src->buffer[0]) + src->idx),
292 BUFSIZE - src->idx))) {
293 if (EINTR != errno) {
294 syslog (LOG_ERR, "read() failed: %s", strerror (errno));
301 } while (EINTR == errno);
305 for (i = src->idx - len; i < src->idx; ++i) {
306 if ('\n' == src->buffer[i])
310 if ('\n' != src->buffer[i]) {
311 ret = (char *)smalloc (1);
315 if (BUFSIZE == src->idx) { /* no space left in buffer */
316 while ('\n' != read_char (src))
317 /* ignore complete line */;
325 ret = (char *)smalloc (i + 1);
326 memcpy (ret, &(src->buffer[0]), i + 1);
332 src->buffer[0] = '\0';
334 memmove (&(src->buffer[0]), &(src->buffer[i + 1]), src->idx);
336 } /* char *read_line (collector_t *) */
338 static void *collect (void *arg)
340 collector_t *this = (collector_t *)arg;
344 { /* put the socket in non-blocking mode */
348 if (-1 == fcntl (this->socket, F_GETFL, &flags)) {
349 syslog (LOG_ERR, "fcntl() failed: %s", strerror (errno));
354 if (-1 == fcntl (this->socket, F_SETFL, flags | O_NONBLOCK)) {
355 syslog (LOG_ERR, "fcntl() failed: %s", strerror (errno));
361 char *line = read_line (this);
368 if ('\0' == line[0]) {
373 if (':' != line[1]) {
374 syslog (LOG_ERR, "email: syntax error in line '%s'", line);
379 if ('e' == line[0]) { /* e:<type>:<bytes> */
381 char *type = strtok_r (line + 2, ":", &ptr);
382 char *tmp = strtok_r (NULL, ":", &ptr);
386 syslog (LOG_ERR, "email: syntax error in line '%s'", line);
393 pthread_mutex_lock (&count_mutex);
394 type_list_incr (&count, type, 1);
395 pthread_mutex_unlock (&count_mutex);
397 pthread_mutex_lock (&size_mutex);
398 type_list_incr (&size, type, bytes);
399 pthread_mutex_unlock (&size_mutex);
401 else if ('s' == line[0]) { /* s:<value> */
402 pthread_mutex_lock (&score_mutex);
403 score = (score * (double)score_count + atof (line + 2))
404 / (double)(score_count + 1);
406 pthread_mutex_unlock (&score_mutex);
408 else if ('c' == line[0]) { /* c:<type1>[,<type2>,...] */
410 char *type = strtok_r (line + 2, ",", &ptr);
413 pthread_mutex_lock (&check_mutex);
414 type_list_incr (&check, type, 1);
415 pthread_mutex_unlock (&check_mutex);
416 } while (NULL != (type = strtok_r (NULL, ",", &ptr)));
419 syslog (LOG_ERR, "email: unknown type '%c'", line[0]);
425 /* put this thread back into the available list */
426 pthread_mutex_lock (&active_mutex);
433 for (ptr = active.head; NULL != ptr; last = ptr, ptr = ptr->next) {
434 if (0 != pthread_equal (ptr->thread, this->thread))
438 /* the current thread _has_ to be in the active list */
439 assert (NULL != ptr);
442 active.head = ptr->next;
445 last->next = ptr->next;
447 if (NULL == last->next) {
452 pthread_mutex_unlock (&active_mutex);
456 pthread_mutex_lock (&available_mutex);
458 if (NULL == available.head) {
459 available.head = this;
460 available.tail = this;
463 available.tail->next = this;
464 available.tail = this;
467 pthread_mutex_unlock (&available_mutex);
469 pthread_cond_signal (&collector_available);
470 pthread_exit ((void *)0);
471 } /* void *collect (void *) */
473 static void *open_connection (void *arg)
477 struct sockaddr_un addr;
479 /* create UNIX socket */
481 if (-1 == (local = socket (PF_UNIX, SOCK_STREAM, 0))) {
483 syslog (LOG_ERR, "socket() failed: %s", strerror (errno));
484 pthread_exit ((void *)1);
487 addr.sun_family = AF_UNIX;
489 strncpy (addr.sun_path, SOCK_PATH, (size_t)(UNIX_PATH_MAX - 1));
490 addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
491 unlink (addr.sun_path);
494 if (-1 == bind (local, (struct sockaddr *)&addr,
495 offsetof (struct sockaddr_un, sun_path)
496 + strlen(addr.sun_path))) {
498 syslog (LOG_ERR, "bind() failed: %s", strerror (errno));
499 pthread_exit ((void *)1);
503 if (-1 == listen (local, 5)) {
505 syslog (LOG_ERR, "listen() failed: %s", strerror (errno));
506 pthread_exit ((void *)1);
509 if ((uid_t)0 == geteuid ()) {
513 if (NULL != (grp = getgrnam (COLLECTD_GRP_NAME))) {
515 if (0 != chown (SOCK_PATH, (uid_t)-1, grp->gr_gid)) {
516 syslog (LOG_WARNING, "chown() failed: %s", strerror (errno));
520 syslog (LOG_WARNING, "getgrnam() failed: %s", strerror (errno));
524 syslog (LOG_WARNING, "not running as root");
528 if (0 != chmod (SOCK_PATH, S_IRWXU | S_IRWXG)) {
529 syslog (LOG_WARNING, "chmod() failed: %s", strerror (errno));
532 { /* initialize queue of available threads */
540 available.head = (collector_t *)smalloc (sizeof (collector_t));
541 available.tail = available.head;
542 available.tail->next = NULL;
544 last = available.head;
546 for (i = 1; i < MAX_CONNS; ++i) {
547 last->next = (collector_t *)smalloc (sizeof (collector_t));
549 available.tail = last;
550 available.tail->next = NULL;
558 collector_t *collector;
560 pthread_attr_t ptattr;
562 pthread_mutex_lock (&available_mutex);
563 while (NULL == available.head) {
564 pthread_cond_wait (&collector_available, &available_mutex);
566 pthread_mutex_unlock (&available_mutex);
570 if (-1 == (remote = accept (local, NULL, NULL))) {
571 if (EINTR != errno) {
573 syslog (LOG_ERR, "accept() failed: %s", strerror (errno));
574 pthread_exit ((void *)1);
577 } while (EINTR == errno);
579 /* assign connection to next available thread */
580 pthread_mutex_lock (&available_mutex);
582 collector = available.head;
583 collector->socket = remote;
585 if (available.head == available.tail) {
586 available.head = NULL;
587 available.tail = NULL;
590 available.head = available.head->next;
593 pthread_mutex_unlock (&available_mutex);
596 collector->next = NULL;
598 pthread_attr_init (&ptattr);
599 pthread_attr_setdetachstate (&ptattr, PTHREAD_CREATE_DETACHED);
601 if (0 == (err = pthread_create (&collector->thread, &ptattr, collect,
602 (void *)collector))) {
603 pthread_mutex_lock (&active_mutex);
605 if (NULL == active.head) {
606 active.head = collector;
607 active.tail = collector;
610 active.tail->next = collector;
611 active.tail = collector;
614 pthread_mutex_unlock (&active_mutex);
617 pthread_mutex_lock (&available_mutex);
619 if (NULL == available.head) {
620 available.head = collector;
621 available.tail = collector;
624 available.tail->next = collector;
625 available.tail = collector;
628 pthread_mutex_unlock (&available_mutex);
631 syslog (LOG_ERR, "pthread_create() failed: %s", strerror (err));
634 pthread_attr_destroy (&ptattr);
636 pthread_exit ((void *)0);
637 } /* void *open_connection (void *) */
638 #endif /* EMAIL_HAVE_READ */
640 static void email_init (void)
645 if (0 != (err = pthread_create (&connector, NULL,
646 open_connection, NULL))) {
648 syslog (LOG_ERR, "pthread_create() failed: %s", strerror (err));
651 #endif /* EMAIL_HAVE_READ */
653 } /* static void email_init (void) */
655 static void count_write (char *host, char *inst, char *val)
657 char file[BUFSIZE] = "";
660 len = snprintf (file, BUFSIZE, COUNT_FILE, inst);
661 if ((len < 0) || (len >= BUFSIZE))
664 rrd_update_file (host, file, val, count_ds_def, count_ds_num);
666 } /* static void email_write (char *host, char *inst, char *val) */
668 static void size_write (char *host, char *inst, char *val)
670 char file[BUFSIZE] = "";
673 len = snprintf (file, BUFSIZE, SIZE_FILE, inst);
674 if ((len < 0) || (len >= BUFSIZE))
677 rrd_update_file (host, file, val, size_ds_def, size_ds_num);
679 } /* static void size_write (char *host, char *inst, char *val) */
681 static void score_write (char *host, char *inst, char *val)
683 rrd_update_file (host, SCORE_FILE, val, score_ds_def, score_ds_num);
685 } /* static void score_write (char *host, char *inst, char *val) */
687 static void check_write (char *host, char *inst, char *val)
689 char file[BUFSIZE] = "";
692 len = snprintf (file, BUFSIZE, CHECK_FILE, inst);
693 if ((len < 0) || (len >= BUFSIZE))
696 rrd_update_file (host, file, val, check_ds_def, check_ds_num);
698 } /* static void check_write (char *host, char *inst, char *val) */
701 static void type_submit (char *plugin, char *inst, int value)
703 char buf[BUFSIZE] = "";
709 len = snprintf (buf, BUFSIZE, "%u:%i", (unsigned int)curtime, value);
710 if ((len < 0) || (len >= BUFSIZE))
713 plugin_submit (plugin, inst, buf);
715 } /* static void type_submit (char *, char *, int) */
717 static void score_submit (double value)
719 char buf[BUFSIZE] = "";
725 len = snprintf (buf, BUFSIZE, "%u:%.2f", (unsigned int)curtime, value);
726 if ((len < 0) || (len >= BUFSIZE))
729 plugin_submit ("email_spam_score", NULL, buf);
733 static void email_read (void)
740 pthread_mutex_lock (&count_mutex);
742 for (ptr = count.head; NULL != ptr; ptr = ptr->next) {
743 type_submit ("email_count", ptr->name, ptr->value);
747 pthread_mutex_unlock (&count_mutex);
749 pthread_mutex_lock (&size_mutex);
751 for (ptr = size.head; NULL != ptr; ptr = ptr->next) {
752 type_submit ("email_size", ptr->name, ptr->value);
756 pthread_mutex_unlock (&size_mutex);
758 pthread_mutex_lock (&score_mutex);
760 score_submit (score);
764 pthread_mutex_unlock (&score_mutex);
766 pthread_mutex_lock (&check_mutex);
768 for (ptr = check.head; NULL != ptr; ptr = ptr->next) {
769 type_submit ("email_spam_check", ptr->name, ptr->value);
773 pthread_mutex_unlock (&check_mutex);
775 } /* static void read (void) */
776 #else /* if !EMAIL_HAVE_READ */
777 # define email_read NULL
780 void module_register (void)
782 plugin_register (MODULE_NAME, email_init, email_read, NULL);
783 plugin_register ("email_count", NULL, NULL, count_write);
784 plugin_register ("email_size", NULL, NULL, size_write);
785 plugin_register ("email_spam_score", NULL, NULL, score_write);
786 plugin_register ("email_spam_check", NULL, NULL, check_write);
788 } /* void module_register (void) */
790 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */