1 /**
2 * collectd - src/email.c
3 * Copyright (C) 2006-2008 Sebastian Harl
4 *
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; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Author:
19 * Sebastian Harl <sh at tokkee.org>
20 **/
22 /*
23 * This plugin communicates with a spam filter, a virus scanner or similar
24 * software using a UNIX socket and a very simple protocol:
25 *
26 * e-mail type (e.g. ham, spam, virus, ...) and size
27 * e:<type>:<bytes>
28 *
29 * spam score
30 * s:<value>
31 *
32 * successful spam checks
33 * c:<type1>[,<type2>,...]
34 */
36 #include "collectd.h"
37 #include "common.h"
38 #include "plugin.h"
40 #include "configfile.h"
42 #include <stddef.h>
44 #if HAVE_LIBPTHREAD
45 # include <pthread.h>
46 #endif
48 #include <sys/socket.h>
49 #include <sys/un.h>
50 #include <sys/select.h>
52 /* some systems (e.g. Darwin) seem to not define UNIX_PATH_MAX at all */
53 #ifndef UNIX_PATH_MAX
54 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
55 #endif /* UNIX_PATH_MAX */
57 #if HAVE_GRP_H
58 # include <grp.h>
59 #endif /* HAVE_GRP_H */
61 #define SOCK_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-email"
62 #define MAX_CONNS 5
63 #define MAX_CONNS_LIMIT 16384
65 #define log_debug(...) DEBUG ("email: "__VA_ARGS__)
66 #define log_err(...) ERROR ("email: "__VA_ARGS__)
67 #define log_warn(...) WARNING ("email: "__VA_ARGS__)
69 /*
70 * Private data structures
71 */
72 /* linked list of email and check types */
73 typedef struct type {
74 char *name;
75 int value;
76 struct type *next;
77 } type_t;
79 typedef struct {
80 type_t *head;
81 type_t *tail;
82 } type_list_t;
84 /* collector thread control information */
85 typedef struct collector {
86 pthread_t thread;
88 /* socket descriptor of the current/last connection */
89 FILE *socket;
90 } collector_t;
92 /* linked list of pending connections */
93 typedef struct conn {
94 /* socket to read data from */
95 FILE *socket;
97 /* linked list of connections */
98 struct conn *next;
99 } conn_t;
101 typedef struct {
102 conn_t *head;
103 conn_t *tail;
104 } conn_list_t;
106 /*
107 * Private variables
108 */
109 /* valid configuration file keys */
110 static const char *config_keys[] =
111 {
112 "SocketFile",
113 "SocketGroup",
114 "SocketPerms",
115 "MaxConns"
116 };
117 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
119 /* socket configuration */
120 static char *sock_file = NULL;
121 static char *sock_group = NULL;
122 static int sock_perms = S_IRWXU | S_IRWXG;
123 static int max_conns = MAX_CONNS;
125 /* state of the plugin */
126 static int disabled = 0;
128 /* thread managing "client" connections */
129 static pthread_t connector = (pthread_t) 0;
130 static int connector_socket = -1;
132 /* tell the collector threads that a new connection is available */
133 static pthread_cond_t conn_available = PTHREAD_COND_INITIALIZER;
135 /* connections that are waiting to be processed */
136 static pthread_mutex_t conns_mutex = PTHREAD_MUTEX_INITIALIZER;
137 static conn_list_t conns;
139 /* tell the connector thread that a collector is available */
140 static pthread_cond_t collector_available = PTHREAD_COND_INITIALIZER;
142 /* collector threads */
143 static collector_t **collectors = NULL;
145 static pthread_mutex_t available_mutex = PTHREAD_MUTEX_INITIALIZER;
146 static int available_collectors;
148 static pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
149 static type_list_t list_count;
150 static type_list_t list_count_copy;
152 static pthread_mutex_t size_mutex = PTHREAD_MUTEX_INITIALIZER;
153 static type_list_t list_size;
154 static type_list_t list_size_copy;
156 static pthread_mutex_t score_mutex = PTHREAD_MUTEX_INITIALIZER;
157 static double score;
158 static int score_count;
160 static pthread_mutex_t check_mutex = PTHREAD_MUTEX_INITIALIZER;
161 static type_list_t list_check;
162 static type_list_t list_check_copy;
164 /*
165 * Private functions
166 */
167 static int email_config (const char *key, const char *value)
168 {
169 if (0 == strcasecmp (key, "SocketFile")) {
170 if (NULL != sock_file)
171 free (sock_file);
172 sock_file = sstrdup (value);
173 }
174 else if (0 == strcasecmp (key, "SocketGroup")) {
175 if (NULL != sock_group)
176 free (sock_group);
177 sock_group = sstrdup (value);
178 }
179 else if (0 == strcasecmp (key, "SocketPerms")) {
180 /* the user is responsible for providing reasonable values */
181 sock_perms = (int)strtol (value, NULL, 8);
182 }
183 else if (0 == strcasecmp (key, "MaxConns")) {
184 long int tmp = strtol (value, NULL, 0);
186 if (tmp < 1) {
187 fprintf (stderr, "email plugin: `MaxConns' was set to invalid "
188 "value %li, will use default %i.\n",
189 tmp, MAX_CONNS);
190 ERROR ("email plugin: `MaxConns' was set to invalid "
191 "value %li, will use default %i.\n",
192 tmp, MAX_CONNS);
193 max_conns = MAX_CONNS;
194 }
195 else if (tmp > MAX_CONNS_LIMIT) {
196 fprintf (stderr, "email plugin: `MaxConns' was set to invalid "
197 "value %li, will use hardcoded limit %i.\n",
198 tmp, MAX_CONNS_LIMIT);
199 ERROR ("email plugin: `MaxConns' was set to invalid "
200 "value %li, will use hardcoded limit %i.\n",
201 tmp, MAX_CONNS_LIMIT);
202 max_conns = MAX_CONNS_LIMIT;
203 }
204 else {
205 max_conns = (int)tmp;
206 }
207 }
208 else {
209 return -1;
210 }
211 return 0;
212 } /* static int email_config (char *, char *) */
214 /* Increment the value of the given name in the given list by incr. */
215 static void type_list_incr (type_list_t *list, char *name, int incr)
216 {
217 if (NULL == list->head) {
218 list->head = (type_t *)smalloc (sizeof (type_t));
220 list->head->name = sstrdup (name);
221 list->head->value = incr;
222 list->head->next = NULL;
224 list->tail = list->head;
225 }
226 else {
227 type_t *ptr;
229 for (ptr = list->head; NULL != ptr; ptr = ptr->next) {
230 if (0 == strcmp (name, ptr->name))
231 break;
232 }
234 if (NULL == ptr) {
235 list->tail->next = (type_t *)smalloc (sizeof (type_t));
236 list->tail = list->tail->next;
238 list->tail->name = sstrdup (name);
239 list->tail->value = incr;
240 list->tail->next = NULL;
241 }
242 else {
243 ptr->value += incr;
244 }
245 }
246 return;
247 } /* static void type_list_incr (type_list_t *, char *) */
249 static void *collect (void *arg)
250 {
251 collector_t *this = (collector_t *)arg;
253 while (1) {
254 int loop = 1;
256 conn_t *connection;
258 pthread_mutex_lock (&conns_mutex);
260 while (NULL == conns.head) {
261 pthread_cond_wait (&conn_available, &conns_mutex);
262 }
264 connection = conns.head;
265 conns.head = conns.head->next;
267 if (NULL == conns.head) {
268 conns.tail = NULL;
269 }
271 pthread_mutex_unlock (&conns_mutex);
273 /* make the socket available to the global
274 * thread and connection management */
275 this->socket = connection->socket;
277 log_debug ("collect: handling connection on fd #%i",
278 fileno (this->socket));
280 while (loop) {
281 /* 256 bytes ought to be enough for anybody ;-) */
282 char line[256 + 1]; /* line + '\0' */
283 int len = 0;
285 errno = 0;
286 if (NULL == fgets (line, sizeof (line), this->socket)) {
287 loop = 0;
289 if (0 != errno) {
290 char errbuf[1024];
291 log_err ("collect: reading from socket (fd #%i) "
292 "failed: %s", fileno (this->socket),
293 sstrerror (errno, errbuf, sizeof (errbuf)));
294 }
295 break;
296 }
298 len = strlen (line);
299 if (('\n' != line[len - 1]) && ('\r' != line[len - 1])) {
300 log_warn ("collect: line too long (> %zu characters): "
301 "'%s' (truncated)", sizeof (line) - 1, line);
303 while (NULL != fgets (line, sizeof (line), this->socket))
304 if (('\n' == line[len - 1]) || ('\r' == line[len - 1]))
305 break;
306 continue;
307 }
309 line[len - 1] = '\0';
311 log_debug ("collect: line = '%s'", line);
313 if (':' != line[1]) {
314 log_err ("collect: syntax error in line '%s'", line);
315 continue;
316 }
318 if ('e' == line[0]) { /* e:<type>:<bytes> */
319 char *ptr = NULL;
320 char *type = strtok_r (line + 2, ":", &ptr);
321 char *tmp = strtok_r (NULL, ":", &ptr);
322 int bytes = 0;
324 if (NULL == tmp) {
325 log_err ("collect: syntax error in line '%s'", line);
326 continue;
327 }
329 bytes = atoi (tmp);
331 pthread_mutex_lock (&count_mutex);
332 type_list_incr (&list_count, type, 1);
333 pthread_mutex_unlock (&count_mutex);
335 if (bytes > 0) {
336 pthread_mutex_lock (&size_mutex);
337 type_list_incr (&list_size, type, bytes);
338 pthread_mutex_unlock (&size_mutex);
339 }
340 }
341 else if ('s' == line[0]) { /* s:<value> */
342 pthread_mutex_lock (&score_mutex);
343 score = (score * (double)score_count + atof (line + 2))
344 / (double)(score_count + 1);
345 ++score_count;
346 pthread_mutex_unlock (&score_mutex);
347 }
348 else if ('c' == line[0]) { /* c:<type1>[,<type2>,...] */
349 char *ptr = NULL;
350 char *type = strtok_r (line + 2, ",", &ptr);
352 do {
353 pthread_mutex_lock (&check_mutex);
354 type_list_incr (&list_check, type, 1);
355 pthread_mutex_unlock (&check_mutex);
356 } while (NULL != (type = strtok_r (NULL, ",", &ptr)));
357 }
358 else {
359 log_err ("collect: unknown type '%c'", line[0]);
360 }
361 } /* while (loop) */
363 log_debug ("[thread #%5lu] shutting down connection on fd #%i",
364 pthread_self (), fileno (this->socket));
366 fclose (connection->socket);
367 free (connection);
369 this->socket = NULL;
371 pthread_mutex_lock (&available_mutex);
372 ++available_collectors;
373 pthread_mutex_unlock (&available_mutex);
375 pthread_cond_signal (&collector_available);
376 } /* while (1) */
378 pthread_exit ((void *)0);
379 } /* static void *collect (void *) */
381 static void *open_connection (void *arg)
382 {
383 struct sockaddr_un addr;
385 char *path = (NULL == sock_file) ? SOCK_PATH : sock_file;
386 char *group = (NULL == sock_group) ? COLLECTD_GRP_NAME : sock_group;
388 /* create UNIX socket */
389 errno = 0;
390 if (-1 == (connector_socket = socket (PF_UNIX, SOCK_STREAM, 0))) {
391 char errbuf[1024];
392 disabled = 1;
393 log_err ("socket() failed: %s",
394 sstrerror (errno, errbuf, sizeof (errbuf)));
395 pthread_exit ((void *)1);
396 }
398 addr.sun_family = AF_UNIX;
400 strncpy (addr.sun_path, path, (size_t)(UNIX_PATH_MAX - 1));
401 addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
403 errno = 0;
404 if (-1 == bind (connector_socket, (struct sockaddr *)&addr,
405 offsetof (struct sockaddr_un, sun_path)
406 + strlen(addr.sun_path))) {
407 char errbuf[1024];
408 disabled = 1;
409 close (connector_socket);
410 connector_socket = -1;
411 log_err ("bind() failed: %s",
412 sstrerror (errno, errbuf, sizeof (errbuf)));
413 pthread_exit ((void *)1);
414 }
416 errno = 0;
417 if (-1 == listen (connector_socket, 5)) {
418 char errbuf[1024];
419 disabled = 1;
420 close (connector_socket);
421 connector_socket = -1;
422 log_err ("listen() failed: %s",
423 sstrerror (errno, errbuf, sizeof (errbuf)));
424 pthread_exit ((void *)1);
425 }
427 {
428 struct group sg;
429 struct group *grp;
430 char grbuf[2048];
431 int status;
433 grp = NULL;
434 status = getgrnam_r (group, &sg, grbuf, sizeof (grbuf), &grp);
435 if (status != 0)
436 {
437 char errbuf[1024];
438 log_warn ("getgrnam_r (%s) failed: %s", group,
439 sstrerror (errno, errbuf, sizeof (errbuf)));
440 }
441 else if (grp == NULL)
442 {
443 log_warn ("No such group: `%s'", group);
444 }
445 else
446 {
447 status = chown (path, (uid_t) -1, grp->gr_gid);
448 if (status != 0)
449 {
450 char errbuf[1024];
451 log_warn ("chown (%s, -1, %i) failed: %s",
452 path, (int) grp->gr_gid,
453 sstrerror (errno, errbuf, sizeof (errbuf)));
454 }
455 }
456 }
458 errno = 0;
459 if (0 != chmod (path, sock_perms)) {
460 char errbuf[1024];
461 log_warn ("chmod() failed: %s",
462 sstrerror (errno, errbuf, sizeof (errbuf)));
463 }
465 { /* initialize collector threads */
466 int i = 0;
467 int err = 0;
469 pthread_attr_t ptattr;
471 conns.head = NULL;
472 conns.tail = NULL;
474 pthread_attr_init (&ptattr);
475 pthread_attr_setdetachstate (&ptattr, PTHREAD_CREATE_DETACHED);
477 available_collectors = max_conns;
479 collectors =
480 (collector_t **)smalloc (max_conns * sizeof (collector_t *));
482 for (i = 0; i < max_conns; ++i) {
483 collectors[i] = (collector_t *)smalloc (sizeof (collector_t));
484 collectors[i]->socket = NULL;
486 if (0 != (err = pthread_create (&collectors[i]->thread, &ptattr,
487 collect, collectors[i]))) {
488 char errbuf[1024];
489 log_err ("pthread_create() failed: %s",
490 sstrerror (errno, errbuf, sizeof (errbuf)));
491 collectors[i]->thread = (pthread_t) 0;
492 }
493 }
495 pthread_attr_destroy (&ptattr);
496 }
498 while (1) {
499 int remote = 0;
501 conn_t *connection;
503 pthread_mutex_lock (&available_mutex);
505 while (0 == available_collectors) {
506 pthread_cond_wait (&collector_available, &available_mutex);
507 }
509 --available_collectors;
511 pthread_mutex_unlock (&available_mutex);
513 do {
514 errno = 0;
515 if (-1 == (remote = accept (connector_socket, NULL, NULL))) {
516 if (EINTR != errno) {
517 char errbuf[1024];
518 disabled = 1;
519 close (connector_socket);
520 connector_socket = -1;
521 log_err ("accept() failed: %s",
522 sstrerror (errno, errbuf, sizeof (errbuf)));
523 pthread_exit ((void *)1);
524 }
525 }
526 } while (EINTR == errno);
528 connection = (conn_t *)smalloc (sizeof (conn_t));
530 connection->socket = fdopen (remote, "r");
531 connection->next = NULL;
533 if (NULL == connection->socket) {
534 close (remote);
535 continue;
536 }
538 pthread_mutex_lock (&conns_mutex);
540 if (NULL == conns.head) {
541 conns.head = connection;
542 conns.tail = connection;
543 }
544 else {
545 conns.tail->next = connection;
546 conns.tail = conns.tail->next;
547 }
549 pthread_mutex_unlock (&conns_mutex);
551 pthread_cond_signal (&conn_available);
552 }
553 pthread_exit ((void *)0);
554 } /* static void *open_connection (void *) */
556 static int email_init (void)
557 {
558 int err = 0;
560 if (0 != (err = pthread_create (&connector, NULL,
561 open_connection, NULL))) {
562 char errbuf[1024];
563 disabled = 1;
564 log_err ("pthread_create() failed: %s",
565 sstrerror (errno, errbuf, sizeof (errbuf)));
566 return (-1);
567 }
569 return (0);
570 } /* int email_init */
572 static int email_shutdown (void)
573 {
574 type_t *ptr = NULL;
576 int i = 0;
578 if (connector != ((pthread_t) 0)) {
579 pthread_kill (connector, SIGTERM);
580 connector = (pthread_t) 0;
581 }
583 if (connector_socket >= 0) {
584 close (connector_socket);
585 connector_socket = -1;
586 }
588 /* don't allow any more connections to be processed */
589 pthread_mutex_lock (&conns_mutex);
591 available_collectors = 0;
593 if (collectors != NULL) {
594 for (i = 0; i < max_conns; ++i) {
595 if (collectors[i] == NULL)
596 continue;
598 if (collectors[i]->thread != ((pthread_t) 0)) {
599 pthread_kill (collectors[i]->thread, SIGTERM);
600 collectors[i]->thread = (pthread_t) 0;
601 }
603 if (collectors[i]->socket != NULL) {
604 fclose (collectors[i]->socket);
605 collectors[i]->socket = NULL;
606 }
608 sfree (collectors[i]);
609 }
610 sfree (collectors);
611 } /* if (collectors != NULL) */
613 pthread_mutex_unlock (&conns_mutex);
615 for (ptr = list_count.head; NULL != ptr; ptr = ptr->next) {
616 free (ptr->name);
617 free (ptr);
618 }
620 for (ptr = list_count_copy.head; NULL != ptr; ptr = ptr->next) {
621 free (ptr->name);
622 free (ptr);
623 }
625 for (ptr = list_size.head; NULL != ptr; ptr = ptr->next) {
626 free (ptr->name);
627 free (ptr);
628 }
630 for (ptr = list_size_copy.head; NULL != ptr; ptr = ptr->next) {
631 free (ptr->name);
632 free (ptr);
633 }
635 for (ptr = list_check.head; NULL != ptr; ptr = ptr->next) {
636 free (ptr->name);
637 free (ptr);
638 }
640 for (ptr = list_check_copy.head; NULL != ptr; ptr = ptr->next) {
641 free (ptr->name);
642 free (ptr);
643 }
645 unlink ((NULL == sock_file) ? SOCK_PATH : sock_file);
647 sfree (sock_file);
648 sfree (sock_group);
649 return (0);
650 } /* static void email_shutdown (void) */
652 static void email_submit (const char *type, const char *type_instance, gauge_t value)
653 {
654 value_t values[1];
655 value_list_t vl = VALUE_LIST_INIT;
657 values[0].gauge = value;
659 vl.values = values;
660 vl.values_len = 1;
661 vl.time = time (NULL);
662 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
663 sstrncpy (vl.plugin, "email", sizeof (vl.plugin));
664 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
666 plugin_dispatch_values (type, &vl);
667 } /* void email_submit */
669 /* Copy list l1 to list l2. l2 may partly exist already, but it is assumed
670 * that neither the order nor the name of any element of either list is
671 * changed and no elements are deleted. The values of l1 are reset to zero
672 * after they have been copied to l2. */
673 static void copy_type_list (type_list_t *l1, type_list_t *l2)
674 {
675 type_t *ptr1;
676 type_t *ptr2;
678 type_t *last = NULL;
680 for (ptr1 = l1->head, ptr2 = l2->head; NULL != ptr1;
681 ptr1 = ptr1->next, last = ptr2, ptr2 = ptr2->next) {
682 if (NULL == ptr2) {
683 ptr2 = (type_t *)smalloc (sizeof (type_t));
684 ptr2->name = NULL;
685 ptr2->next = NULL;
687 if (NULL == last) {
688 l2->head = ptr2;
689 }
690 else {
691 last->next = ptr2;
692 }
694 l2->tail = ptr2;
695 }
697 if (NULL == ptr2->name) {
698 ptr2->name = sstrdup (ptr1->name);
699 }
701 ptr2->value = ptr1->value;
702 ptr1->value = 0;
703 }
704 return;
705 }
707 static int email_read (void)
708 {
709 type_t *ptr;
711 double score_old;
712 int score_count_old;
714 if (disabled)
715 return (-1);
717 /* email count */
718 pthread_mutex_lock (&count_mutex);
720 copy_type_list (&list_count, &list_count_copy);
722 pthread_mutex_unlock (&count_mutex);
724 for (ptr = list_count_copy.head; NULL != ptr; ptr = ptr->next) {
725 email_submit ("email_count", ptr->name, ptr->value);
726 }
728 /* email size */
729 pthread_mutex_lock (&size_mutex);
731 copy_type_list (&list_size, &list_size_copy);
733 pthread_mutex_unlock (&size_mutex);
735 for (ptr = list_size_copy.head; NULL != ptr; ptr = ptr->next) {
736 email_submit ("email_size", ptr->name, ptr->value);
737 }
739 /* spam score */
740 pthread_mutex_lock (&score_mutex);
742 score_old = score;
743 score_count_old = score_count;
744 score = 0.0;
745 score_count = 0;
747 pthread_mutex_unlock (&score_mutex);
749 if (score_count_old > 0)
750 email_submit ("spam_score", "", score_old);
752 /* spam checks */
753 pthread_mutex_lock (&check_mutex);
755 copy_type_list (&list_check, &list_check_copy);
757 pthread_mutex_unlock (&check_mutex);
759 for (ptr = list_check_copy.head; NULL != ptr; ptr = ptr->next)
760 email_submit ("spam_check", ptr->name, ptr->value);
762 return (0);
763 } /* int email_read */
765 void module_register (void)
766 {
767 plugin_register_config ("email", email_config, config_keys, config_keys_num);
768 plugin_register_init ("email", email_init);
769 plugin_register_read ("email", email_read);
770 plugin_register_shutdown ("email", email_shutdown);
771 } /* void module_register */
773 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */