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 */
58 #define MODULE_NAME "email"
60 /* 256 bytes ought to be enough for anybody ;-) */
63 #define SOCK_PATH "/tmp/.collectd-email"
66 /* linked list of email and check types */
78 /* linked list of collector thread control information */
79 typedef struct collector {
82 /* socket to read data from */
85 /* buffer to read data to */
87 int idx; /* current position in buffer */
89 struct collector *next;
97 /* state of the plugin */
98 static int disabled = 0;
100 /* thread managing "client" connections */
101 static pthread_t connector;
103 /* tell the connector thread that a collector is available */
104 static pthread_cond_t collector_available = PTHREAD_COND_INITIALIZER;
106 /* collector threads that are in use */
107 static pthread_mutex_t active_mutex = PTHREAD_MUTEX_INITIALIZER;
108 static collector_list_t active;
110 /* collector threads that are available for use */
111 static pthread_mutex_t available_mutex = PTHREAD_MUTEX_INITIALIZER;
112 static collector_list_t available;
114 #define COUNT_FILE "email/email-%s.rrd"
115 static char *count_ds_def[] =
117 "DS:count:GAUGE:"COLLECTD_HEARTBEAT":0:U",
120 static int count_ds_num = 1;
122 static pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
123 static type_list_t count;
125 #define SIZE_FILE "email/email_size-%s.rrd"
126 static char *size_ds_def[] =
128 "DS:size:GAUGE:"COLLECTD_HEARTBEAT":0:U",
131 static int size_ds_num = 1;
133 static pthread_mutex_t size_mutex = PTHREAD_MUTEX_INITIALIZER;
134 static type_list_t size;
136 #define SCORE_FILE "email/spam_score.rrd"
137 static char *score_ds_def[] =
139 "DS:score:GAUGE:"COLLECTD_HEARTBEAT":U:U",
142 static int score_ds_num = 1;
144 static pthread_mutex_t score_mutex = PTHREAD_MUTEX_INITIALIZER;
146 static int score_count;
148 #define CHECK_FILE "email/spam_check-%s.rrd"
149 static char *check_ds_def[] =
151 "DS:hits:GAUGE:"COLLECTD_HEARTBEAT":0:U",
154 static int check_ds_num = 1;
156 static pthread_mutex_t check_mutex = PTHREAD_MUTEX_INITIALIZER;
157 static type_list_t check;
159 /* Increment the value of the given name in the given list by incr. */
160 static void type_list_incr (type_list_t *list, char *name, int incr)
162 if (NULL == list->head) {
163 list->head = (type_t *)smalloc (sizeof (type_t));
165 list->head->name = sstrdup (name);
166 list->head->value = incr;
167 list->head->next = NULL;
169 list->tail = list->head;
174 for (ptr = list->head; NULL != ptr; ptr = ptr->next) {
175 if (0 == strcmp (name, ptr->name))
180 list->tail->next = (type_t *)smalloc (sizeof (type_t));
181 list->tail = list->tail->next;
183 list->tail->name = sstrdup (name);
184 list->tail->value = incr;
185 list->tail->next = NULL;
192 } /* static void type_list_incr (type_list_t *, char *) */
194 /* Read a single character from the socket. If an error occurs or end-of-file
195 * is reached return '\0'. */
196 char read_char (collector_t *src)
203 FD_SET (src->socket, &fdset);
205 if (-1 == select (src->socket + 1, &fdset, NULL, NULL, NULL)) {
206 syslog (LOG_ERR, "select() failed: %s", strerror (errno));
210 assert (FD_ISSET (src->socket, &fdset));
216 if (0 > (len = read (src->socket, (void *)&ret, 1))) {
217 if (EINTR != errno) {
218 syslog (LOG_ERR, "read() failed: %s", strerror (errno));
225 } while (EINTR == errno);
227 } /* char read_char (collector_t *) */
229 /* Read a single line (terminated by '\n') from the the socket.
231 * The return value is zero terminated and does not contain any newline
232 * characters. In case that no complete line is available (non-blocking mode
233 * should be enabled) an empty string is returned.
235 * If an error occurs or end-of-file is reached return NULL.
237 * IMPORTANT NOTE: If there is no newline character found in BUFSIZE
238 * characters of the input stream, the line will will be ignored! By
239 * definition we should not get any longer input lines, thus this is
240 * acceptable in this case ;-) */
241 char *read_line (collector_t *src)
246 assert (BUFSIZE > src->idx);
248 for (i = 0; i < src->idx; ++i) {
249 if ('\n' == src->buffer[i])
253 if ('\n' != src->buffer[i]) {
259 FD_SET (src->socket, &fdset);
261 if (-1 == select (src->socket + 1, &fdset, NULL, NULL, NULL)) {
262 syslog (LOG_ERR, "select() failed: %s", strerror (errno));
266 assert (FD_ISSET (src->socket, &fdset));
270 if (0 > (len = read (src->socket,
271 (void *)(&(src->buffer[0]) + src->idx),
272 BUFSIZE - src->idx))) {
273 if (EINTR != errno) {
274 syslog (LOG_ERR, "read() failed: %s", strerror (errno));
281 } while (EINTR == errno);
285 for (i = src->idx - len; i < src->idx; ++i) {
286 if ('\n' == src->buffer[i])
290 if ('\n' != src->buffer[i]) {
291 ret = (char *)smalloc (1);
295 if (BUFSIZE == src->idx) { /* no space left in buffer */
296 while ('\n' != read_char (src))
297 /* ignore complete line */;
305 ret = (char *)smalloc (i + 1);
306 memcpy (ret, &(src->buffer[0]), i + 1);
312 src->buffer[0] = '\0';
314 memmove (&(src->buffer[0]), &(src->buffer[i + 1]), src->idx);
316 } /* char *read_line (collector_t *) */
318 static void *collect (void *arg)
320 collector_t *this = (collector_t *)arg;
324 { /* put the socket in non-blocking mode */
328 if (-1 == fcntl (this->socket, F_GETFL, &flags)) {
329 syslog (LOG_ERR, "fcntl() failed: %s", strerror (errno));
334 if (-1 == fcntl (this->socket, F_SETFL, flags | O_NONBLOCK)) {
335 syslog (LOG_ERR, "fcntl() failed: %s", strerror (errno));
341 char *line = read_line (this);
348 if ('\0' == line[0]) {
353 if (':' != line[1]) {
354 syslog (LOG_ERR, "email: syntax error in line '%s'", line);
359 if ('e' == line[0]) { /* e:<type>:<bytes> */
360 char *type = strtok (line + 2, ":");
361 char *tmp = strtok (NULL, ":");
365 syslog (LOG_ERR, "email: syntax error in line '%s'", line);
372 pthread_mutex_lock (&count_mutex);
373 type_list_incr (&count, type, 1);
374 pthread_mutex_unlock (&count_mutex);
376 pthread_mutex_lock (&size_mutex);
377 type_list_incr (&size, type, bytes);
378 pthread_mutex_unlock (&size_mutex);
380 else if ('s' == line[0]) { /* s:<value> */
381 pthread_mutex_lock (&score_mutex);
382 score = (score * (double)score_count + atof (line + 2))
383 / (double)(score_count + 1);
385 pthread_mutex_unlock (&score_mutex);
387 else if ('c' == line[0]) { /* c:<type1>[,<type2>,...] */
388 char *type = strtok (line + 2, ",");
391 pthread_mutex_lock (&check_mutex);
392 type_list_incr (&check, type, 1);
393 pthread_mutex_unlock (&check_mutex);
394 } while (NULL != (type = strtok (NULL, ",")));
397 syslog (LOG_ERR, "email: unknown type '%c'", line[0]);
403 /* put this thread back into the available list */
404 pthread_mutex_lock (&active_mutex);
411 for (ptr = active.head; NULL != ptr; last = ptr, ptr = ptr->next) {
412 if (0 != pthread_equal (ptr->thread, this->thread))
416 /* the current thread _has_ to be in the active list */
417 assert (NULL != ptr);
420 active.head = ptr->next;
423 last->next = ptr->next;
425 if (NULL == last->next) {
430 pthread_mutex_unlock (&active_mutex);
434 pthread_mutex_lock (&available_mutex);
436 if (NULL == available.head) {
437 available.head = this;
438 available.tail = this;
441 available.tail->next = this;
442 available.tail = this;
445 pthread_mutex_unlock (&available_mutex);
447 pthread_cond_signal (&collector_available);
448 pthread_exit ((void *)0);
449 } /* void *collect (void *) */
451 static void *open_connection (void *arg)
455 struct sockaddr_un addr;
457 /* create UNIX socket */
459 if (-1 == (local = socket (PF_UNIX, SOCK_STREAM, 0))) {
461 syslog (LOG_ERR, "socket() failed: %s", strerror (errno));
462 pthread_exit ((void *)1);
465 addr.sun_family = AF_UNIX;
467 strncpy (addr.sun_path, SOCK_PATH, (size_t)(UNIX_PATH_MAX - 1));
468 addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
469 unlink (addr.sun_path);
472 if (-1 == bind (local, (struct sockaddr *)&addr,
473 offsetof (struct sockaddr_un, sun_path)
474 + strlen(addr.sun_path))) {
476 syslog (LOG_ERR, "bind() failed: %s", strerror (errno));
477 pthread_exit ((void *)1);
481 if (-1 == listen (local, 5)) {
483 syslog (LOG_ERR, "listen() failed: %s", strerror (errno));
484 pthread_exit ((void *)1);
487 { /* initialize queue of available threads */
495 available.head = (collector_t *)smalloc (sizeof (collector_t));
496 available.tail = available.head;
497 available.tail->next = NULL;
499 last = available.head;
501 for (i = 1; i < MAX_CONNS; ++i) {
502 last->next = (collector_t *)smalloc (sizeof (collector_t));
504 available.tail = last;
505 available.tail->next = NULL;
513 collector_t *collector;
515 pthread_attr_t ptattr;
517 pthread_mutex_lock (&available_mutex);
518 while (NULL == available.head) {
519 pthread_cond_wait (&collector_available, &available_mutex);
521 pthread_mutex_unlock (&available_mutex);
525 if (-1 == (remote = accept (local, NULL, NULL))) {
526 if (EINTR != errno) {
528 syslog (LOG_ERR, "accept() failed: %s", strerror (errno));
529 pthread_exit ((void *)1);
532 } while (EINTR == errno);
534 /* assign connection to next available thread */
535 pthread_mutex_lock (&available_mutex);
537 collector = available.head;
538 collector->socket = remote;
540 if (available.head == available.tail) {
541 available.head = NULL;
542 available.tail = NULL;
545 available.head = available.head->next;
548 pthread_mutex_unlock (&available_mutex);
551 collector->next = NULL;
553 pthread_attr_init (&ptattr);
554 pthread_attr_setdetachstate (&ptattr, PTHREAD_CREATE_DETACHED);
556 if (0 == (err = pthread_create (&collector->thread, &ptattr, collect,
557 (void *)collector))) {
558 pthread_mutex_lock (&active_mutex);
560 if (NULL == active.head) {
561 active.head = collector;
562 active.tail = collector;
565 active.tail->next = collector;
566 active.tail = collector;
569 pthread_mutex_unlock (&active_mutex);
572 pthread_mutex_lock (&available_mutex);
574 if (NULL == available.head) {
575 available.head = collector;
576 available.tail = collector;
579 available.tail->next = collector;
580 available.tail = collector;
583 pthread_mutex_unlock (&available_mutex);
586 syslog (LOG_ERR, "pthread_create() failed: %s", strerror (err));
589 pthread_attr_destroy (&ptattr);
591 pthread_exit ((void *)0);
592 } /* void *open_connection (void *) */
594 static void email_init (void)
598 if (0 != (err = pthread_create (&connector, NULL,
599 open_connection, NULL))) {
601 syslog (LOG_ERR, "pthread_create() failed: %s", strerror (err));
605 } /* static void email_init (void) */
607 static void count_write (char *host, char *inst, char *val)
609 char file[BUFSIZE] = "";
612 len = snprintf (file, BUFSIZE, COUNT_FILE, inst);
613 if ((len < 0) || (len >= BUFSIZE))
616 rrd_update_file (host, file, val, count_ds_def, count_ds_num);
618 } /* static void email_write (char *host, char *inst, char *val) */
620 static void size_write (char *host, char *inst, char *val)
622 char file[BUFSIZE] = "";
625 len = snprintf (file, BUFSIZE, SIZE_FILE, inst);
626 if ((len < 0) || (len >= BUFSIZE))
629 rrd_update_file (host, file, val, size_ds_def, size_ds_num);
631 } /* static void size_write (char *host, char *inst, char *val) */
633 static void score_write (char *host, char *inst, char *val)
635 rrd_update_file (host, SCORE_FILE, val, score_ds_def, score_ds_num);
637 } /* static void score_write (char *host, char *inst, char *val) */
639 static void check_write (char *host, char *inst, char *val)
641 char file[BUFSIZE] = "";
644 len = snprintf (file, BUFSIZE, CHECK_FILE, inst);
645 if ((len < 0) || (len >= BUFSIZE))
648 rrd_update_file (host, file, val, check_ds_def, check_ds_num);
650 } /* static void check_write (char *host, char *inst, char *val) */
652 static void type_submit (char *plugin, char *inst, int value)
654 char buf[BUFSIZE] = "";
660 len = snprintf (buf, BUFSIZE, "%u:%i", (unsigned int)curtime, value);
661 if ((len < 0) || (len >= BUFSIZE))
664 plugin_submit (plugin, inst, buf);
666 } /* static void type_submit (char *, char *, int) */
668 static void score_submit (double value)
670 char buf[BUFSIZE] = "";
676 len = snprintf (buf, BUFSIZE, "%u:%.2f", (unsigned int)curtime, value);
677 if ((len < 0) || (len >= BUFSIZE))
680 plugin_submit ("email_spam_score", NULL, buf);
684 static void email_read (void)
691 pthread_mutex_lock (&count_mutex);
693 for (ptr = count.head; NULL != ptr; ptr = ptr->next) {
694 type_submit ("email_count", ptr->name, ptr->value);
698 pthread_mutex_unlock (&count_mutex);
700 pthread_mutex_lock (&size_mutex);
702 for (ptr = size.head; NULL != ptr; ptr = ptr->next) {
703 type_submit ("email_size", ptr->name, ptr->value);
707 pthread_mutex_unlock (&size_mutex);
709 pthread_mutex_lock (&score_mutex);
711 score_submit (score);
715 pthread_mutex_unlock (&score_mutex);
717 pthread_mutex_lock (&check_mutex);
719 for (ptr = check.head; NULL != ptr; ptr = ptr->next) {
720 type_submit ("email_spam_check", ptr->name, ptr->value);
724 pthread_mutex_unlock (&check_mutex);
726 } /* static void read (void) */
728 void module_register (void)
730 plugin_register (MODULE_NAME, email_init, email_read, NULL);
731 plugin_register ("email_count", NULL, NULL, count_write);
732 plugin_register ("email_size", NULL, NULL, size_write);
733 plugin_register ("email_spam_score", NULL, NULL, score_write);
734 plugin_register ("email_spam_check", NULL, NULL, check_write);
736 } /* void module_register (void) */
738 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */