Merge branch 'ls/shutdown'
[collectd.git] / src / email.c
1 /**
2  * collectd - src/email.c
3  * Copyright (C) 2006  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; either version 2 of the License, or (at your
8  * option) any later version.
9  *
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.
14  *
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
18  *
19  * Author:
20  *   Sebastian Harl <sh at tokkee.org>
21  **/
22
23 /*
24  * This plugin communicates with a spam filter, a virus scanner or similar
25  * software using a UNIX socket and a very simple protocol:
26  *
27  * e-mail type (e.g. ham, spam, virus, ...) and size
28  * e:<type>:<bytes>
29  *
30  * spam score
31  * s:<value>
32  *
33  * successful spam checks
34  * c:<type1>[,<type2>,...]
35  */
36
37 #include "collectd.h"
38 #include "common.h"
39 #include "plugin.h"
40
41 #include "configfile.h"
42
43 #if HAVE_LIBPTHREAD
44 # include <pthread.h>
45 # define EMAIL_HAVE_READ 1
46 #else
47 # define EMAIL_HAVE_READ 0
48 #endif
49
50 #if HAVE_SYS_SELECT_H
51 #       include <sys/select.h>
52 #endif /* HAVE_SYS_SELECT_H */
53
54 #if HAVE_SYS_SOCKET_H
55 #       include <sys/socket.h>
56 #endif /* HAVE_SYS_SOCKET_H */
57
58 /* *sigh* glibc does not define UNIX_PATH_MAX in sys/un.h ... */
59 #if HAVE_LINUX_UN_H
60 #       include <linux/un.h>
61 #elif HAVE_SYS_UN_H
62 #       include <sys/un.h>
63 #endif /* HAVE_LINUX_UN_H | HAVE_SYS_UN_H */
64
65 #if HAVE_GRP_H
66 #       include <grp.h>
67 #endif /* HAVE_GRP_H */
68
69 #define MODULE_NAME "email"
70
71 /* 256 bytes ought to be enough for anybody ;-) */
72 #define BUFSIZE 256
73
74 #ifndef COLLECTD_SOCKET_PREFIX
75 # define COLLECTD_SOCKET_PREFIX "/tmp/.collectd-"
76 #endif /* COLLECTD_SOCKET_PREFIX */
77
78 #define SOCK_PATH COLLECTD_SOCKET_PREFIX"email"
79 #define MAX_CONNS 5
80 #define MAX_CONNS_LIMIT 16384
81
82 /*
83  * Private data structures
84  */
85 #if EMAIL_HAVE_READ
86 /* linked list of email and check types */
87 typedef struct type {
88         char        *name;
89         int         value;
90         struct type *next;
91 } type_t;
92
93 typedef struct {
94         type_t *head;
95         type_t *tail;
96 } type_list_t;
97
98 /* linked list of collector thread control information */
99 typedef struct collector {
100         pthread_t thread;
101
102         /* socket to read data from */
103         int socket;
104
105         /* buffer to read data to */
106         char buffer[BUFSIZE];
107         int  idx; /* current position in buffer */
108
109         struct collector *next;
110 } collector_t;
111
112 typedef struct {
113         collector_t *head;
114         collector_t *tail;
115 } collector_list_t;
116 #endif /* EMAIL_HAVE_READ */
117
118 /*
119  * Private variables
120  */
121 #if EMAIL_HAVE_READ
122 /* valid configuration file keys */
123 static char *config_keys[] =
124 {
125         "SocketGroup",
126         "SocketPerms",
127         "MaxConns",
128         NULL
129 };
130 static int config_keys_num = 3;
131
132 /* socket configuration */
133 static char *sock_group = COLLECTD_GRP_NAME;
134 static int  sock_perms  = S_IRWXU | S_IRWXG;
135 static int  max_conns   = MAX_CONNS;
136
137 /* state of the plugin */
138 static int disabled = 0;
139
140 /* thread managing "client" connections */
141 static pthread_t connector;
142 static int connector_socket;
143
144 /* tell the connector thread that a collector is available */
145 static pthread_cond_t collector_available = PTHREAD_COND_INITIALIZER;
146
147 /* collector threads that are in use */
148 static pthread_mutex_t active_mutex = PTHREAD_MUTEX_INITIALIZER;
149 static collector_list_t active;
150
151 /* collector threads that are available for use */
152 static pthread_mutex_t available_mutex = PTHREAD_MUTEX_INITIALIZER;
153 static collector_list_t available;
154
155 static pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
156 static type_list_t count;
157
158 static pthread_mutex_t size_mutex = PTHREAD_MUTEX_INITIALIZER;
159 static type_list_t size;
160
161 static pthread_mutex_t score_mutex = PTHREAD_MUTEX_INITIALIZER;
162 static double score;
163 static int score_count;
164
165 static pthread_mutex_t check_mutex = PTHREAD_MUTEX_INITIALIZER;
166 static type_list_t check;
167 #endif /* EMAIL_HAVE_READ */
168
169 #define COUNT_FILE "email/email-%s.rrd"
170 static char *count_ds_def[] =
171 {
172         "DS:count:GAUGE:"COLLECTD_HEARTBEAT":0:U",
173         NULL
174 };
175 static int count_ds_num = 1;
176
177 #define SIZE_FILE  "email/email_size-%s.rrd"
178 static char *size_ds_def[] =
179 {
180         "DS:size:GAUGE:"COLLECTD_HEARTBEAT":0:U",
181         NULL
182 };
183 static int size_ds_num = 1;
184
185 #define SCORE_FILE "email/spam_score.rrd"
186 static char *score_ds_def[] =
187 {
188         "DS:score:GAUGE:"COLLECTD_HEARTBEAT":U:U",
189         NULL
190 };
191 static int score_ds_num = 1;
192
193 #define CHECK_FILE "email/spam_check-%s.rrd"
194 static char *check_ds_def[] =
195 {
196         "DS:hits:GAUGE:"COLLECTD_HEARTBEAT":0:U",
197         NULL
198 };
199 static int check_ds_num = 1;
200
201 #if EMAIL_HAVE_READ
202 static int email_config (char *key, char *value)
203 {
204         if (0 == strcasecmp (key, "SocketGroup")) {
205                 sock_group = sstrdup (value);
206         }
207         else if (0 == strcasecmp (key, "SocketPerms")) {
208                 /* the user is responsible for providing reasonable values */
209                 sock_perms = (int)strtol (value, NULL, 0);
210         }
211         else if (0 == strcasecmp (key, "MaxConns")) {
212                 long int tmp = strtol (value, NULL, 0);
213
214                 if (tmp < 1) {
215                         fprintf (stderr, "email plugin: `MaxConns' was set to invalid "
216                                         "value %li, will use default %i.\n",
217                                         tmp, MAX_CONNS);
218                         max_conns = MAX_CONNS;
219                 }
220                 else if (tmp > MAX_CONNS_LIMIT) {
221                         fprintf (stderr, "email plugin: `MaxConns' was set to invalid "
222                                         "value %li, will use hardcoded limit %i.\n",
223                                         tmp, MAX_CONNS_LIMIT);
224                         max_conns = MAX_CONNS_LIMIT;
225                 }
226                 else {
227                         max_conns = (int)tmp;
228                 }
229         }
230         else {
231                 return -1;
232         }
233         return 0;
234 } /* static int email_config (char *, char *) */
235
236 /* Increment the value of the given name in the given list by incr. */
237 static void type_list_incr (type_list_t *list, char *name, int incr)
238 {
239         if (NULL == list->head) {
240                 list->head = (type_t *)smalloc (sizeof (type_t));
241
242                 list->head->name  = sstrdup (name);
243                 list->head->value = incr;
244                 list->head->next  = NULL;
245
246                 list->tail = list->head;
247         }
248         else {
249                 type_t *ptr;
250
251                 for (ptr = list->head; NULL != ptr; ptr = ptr->next) {
252                         if (0 == strcmp (name, ptr->name))
253                                 break;
254                 }
255
256                 if (NULL == ptr) {
257                         list->tail->next = (type_t *)smalloc (sizeof (type_t));
258                         list->tail = list->tail->next;
259
260                         list->tail->name  = sstrdup (name);
261                         list->tail->value = incr;
262                         list->tail->next  = NULL;
263                 }
264                 else {
265                         ptr->value += incr;
266                 }
267         }
268         return;
269 } /* static void type_list_incr (type_list_t *, char *) */
270
271 /* Read a single character from the socket. If an error occurs or end-of-file
272  * is reached return '\0'. */
273 char read_char (collector_t *src)
274 {
275         char ret = '\0';
276
277         fd_set fdset;
278
279         FD_ZERO (&fdset);
280         FD_SET (src->socket, &fdset);
281
282         if (-1 == select (src->socket + 1, &fdset, NULL, NULL, NULL)) {
283                 syslog (LOG_ERR, "select() failed: %s", strerror (errno));
284                 return '\0';
285         }
286
287         assert (FD_ISSET (src->socket, &fdset));
288
289         do {
290                 ssize_t len = 0;
291
292                 errno = 0;
293                 if (0 > (len = read (src->socket, (void *)&ret, 1))) {
294                         if (EINTR != errno) {
295                                 syslog (LOG_ERR, "read() failed: %s", strerror (errno));
296                                 return '\0';
297                         }
298                 }
299
300                 if (0 == len)
301                         return '\0';
302         } while (EINTR == errno);
303         return ret;
304 } /* char read_char (collector_t *) */
305
306 /* Read a single line (terminated by '\n') from the the socket.
307  *
308  * The return value is zero terminated and does not contain any newline
309  * characters. In case that no complete line is available (non-blocking mode
310  * should be enabled) an empty string is returned.
311  *
312  * If an error occurs or end-of-file is reached return NULL.
313  *
314  * IMPORTANT NOTE: If there is no newline character found in BUFSIZE
315  * characters of the input stream, the line will will be ignored! By
316  * definition we should not get any longer input lines, thus this is
317  * acceptable in this case ;-) */
318 char *read_line (collector_t *src)
319 {
320         int  i = 0;
321         char *ret;
322
323         assert (BUFSIZE > src->idx);
324
325         for (i = 0; i < src->idx; ++i) {
326                 if ('\n' == src->buffer[i])
327                         break;
328         }
329
330         if ('\n' != src->buffer[i]) {
331                 fd_set fdset;
332         
333                 ssize_t len = 0;
334
335                 FD_ZERO (&fdset);
336                 FD_SET (src->socket, &fdset);
337
338                 if (-1 == select (src->socket + 1, &fdset, NULL, NULL, NULL)) {
339                         syslog (LOG_ERR, "select() failed: %s", strerror (errno));
340                         return NULL;
341                 }
342
343                 assert (FD_ISSET (src->socket, &fdset));
344
345                 do {
346                         errno = 0;
347                         if (0 > (len = read (src->socket,
348                                                         (void *)(&(src->buffer[0]) + src->idx),
349                                                         BUFSIZE - src->idx))) {
350                                 if (EINTR != errno) {
351                                         syslog (LOG_ERR, "read() failed: %s", strerror (errno));
352                                         return NULL;
353                                 }
354                         }
355
356                         if (0 == len)
357                                 return NULL;
358                 } while (EINTR == errno);
359
360                 src->idx += len;
361
362                 for (i = src->idx - len; i < src->idx; ++i) {
363                         if ('\n' == src->buffer[i])
364                                 break;
365                 }
366
367                 if ('\n' != src->buffer[i]) {
368                         ret = (char *)smalloc (1);
369
370                         ret[0] = '\0';
371
372                         if (BUFSIZE == src->idx) { /* no space left in buffer */
373                                 while ('\n' != read_char (src))
374                                         /* ignore complete line */;
375
376                                 src->idx = 0;
377                         }
378                         return ret;
379                 }
380         }
381
382         ret = (char *)smalloc (i + 1);
383         memcpy (ret, &(src->buffer[0]), i + 1);
384         ret[i] = '\0';
385
386         src->idx -= (i + 1);
387
388         if (0 == src->idx)
389                 src->buffer[0] = '\0';
390         else
391                 memmove (&(src->buffer[0]), &(src->buffer[i + 1]), src->idx);
392         return ret;
393 } /* char *read_line (collector_t *) */
394
395 static void *collect (void *arg)
396 {
397         collector_t *this = (collector_t *)arg;
398         
399         int loop = 1;
400
401         { /* put the socket in non-blocking mode */
402                 int flags = 0;
403
404                 errno = 0;
405                 if (-1 == fcntl (this->socket, F_GETFL, &flags)) {
406                         syslog (LOG_ERR, "fcntl() failed: %s", strerror (errno));
407                         loop = 0;
408                 }
409
410                 errno = 0;
411                 if (-1 == fcntl (this->socket, F_SETFL, flags | O_NONBLOCK)) {
412                         syslog (LOG_ERR, "fcntl() failed: %s", strerror (errno));
413                         loop = 0;
414                 }
415         }
416
417         while (loop) {
418                 char *line = read_line (this);
419
420                 if (NULL == line) {
421                         loop = 0;
422                         break;
423                 }
424
425                 if ('\0' == line[0]) {
426                         free (line);
427                         continue;
428                 }
429
430                 if (':' != line[1]) {
431                         syslog (LOG_ERR, "email: syntax error in line '%s'", line);
432                         free (line);
433                         continue;
434                 }
435
436                 if ('e' == line[0]) { /* e:<type>:<bytes> */
437                         char *ptr  = NULL;
438                         char *type = strtok_r (line + 2, ":", &ptr);
439                         char *tmp  = strtok_r (NULL, ":", &ptr);
440                         int  bytes = 0;
441
442                         if (NULL == tmp) {
443                                 syslog (LOG_ERR, "email: syntax error in line '%s'", line);
444                                 free (line);
445                                 continue;
446                         }
447
448                         bytes = atoi (tmp);
449
450                         pthread_mutex_lock (&count_mutex);
451                         type_list_incr (&count, type, 1);
452                         pthread_mutex_unlock (&count_mutex);
453
454                         pthread_mutex_lock (&size_mutex);
455                         type_list_incr (&size, type, bytes);
456                         pthread_mutex_unlock (&size_mutex);
457                 }
458                 else if ('s' == line[0]) { /* s:<value> */
459                         pthread_mutex_lock (&score_mutex);
460                         score = (score * (double)score_count + atof (line + 2))
461                                         / (double)(score_count + 1);
462                         ++score_count;
463                         pthread_mutex_unlock (&score_mutex);
464                 }
465                 else if ('c' == line[0]) { /* c:<type1>[,<type2>,...] */
466                         char *ptr  = NULL;
467                         char *type = strtok_r (line + 2, ",", &ptr);
468
469                         do {
470                                 pthread_mutex_lock (&check_mutex);
471                                 type_list_incr (&check, type, 1);
472                                 pthread_mutex_unlock (&check_mutex);
473                         } while (NULL != (type = strtok_r (NULL, ",", &ptr)));
474                 }
475                 else {
476                         syslog (LOG_ERR, "email: unknown type '%c'", line[0]);
477                 }
478
479                 free (line);
480         }
481
482         /* put this thread back into the available list */
483         pthread_mutex_lock (&active_mutex);
484         {
485                 collector_t *last;
486                 collector_t *ptr;
487
488                 last = NULL;
489
490                 for (ptr = active.head; NULL != ptr; last = ptr, ptr = ptr->next) {
491                         if (0 != pthread_equal (ptr->thread, this->thread))
492                                 break;
493                 }
494
495                 /* the current thread _has_ to be in the active list */
496                 assert (NULL != ptr);
497
498                 if (NULL == last) {
499                         active.head = ptr->next;
500                 }
501                 else {
502                         last->next = ptr->next;
503
504                         if (NULL == last->next) {
505                                 active.tail = last;
506                         }
507                 }
508         }
509         pthread_mutex_unlock (&active_mutex);
510
511         this->next = NULL;
512
513         pthread_mutex_lock (&available_mutex);
514
515         if (NULL == available.head) {
516                 available.head = this;
517                 available.tail = this;
518         }
519         else {
520                 available.tail->next = this;
521                 available.tail = this;
522         }
523
524         pthread_mutex_unlock (&available_mutex);
525
526         pthread_cond_signal (&collector_available);
527         pthread_exit ((void *)0);
528 } /* void *collect (void *) */
529
530 static void *open_connection (void *arg)
531 {
532         struct sockaddr_un addr;
533
534         /* create UNIX socket */
535         errno = 0;
536         if (-1 == (connector_socket = socket (PF_UNIX, SOCK_STREAM, 0))) {
537                 disabled = 1;
538                 syslog (LOG_ERR, "socket() failed: %s", strerror (errno));
539                 pthread_exit ((void *)1);
540         }
541
542         addr.sun_family = AF_UNIX;
543
544         strncpy (addr.sun_path, SOCK_PATH, (size_t)(UNIX_PATH_MAX - 1));
545         addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
546         unlink (addr.sun_path);
547
548         errno = 0;
549         if (-1 == bind (connector_socket, (struct sockaddr *)&addr,
550                                 offsetof (struct sockaddr_un, sun_path)
551                                         + strlen(addr.sun_path))) {
552                 disabled = 1;
553                 syslog (LOG_ERR, "bind() failed: %s", strerror (errno));
554                 pthread_exit ((void *)1);
555         }
556
557         errno = 0;
558         if (-1 == listen (connector_socket, 5)) {
559                 disabled = 1;
560                 syslog (LOG_ERR, "listen() failed: %s", strerror (errno));
561                 pthread_exit ((void *)1);
562         }
563
564         if ((uid_t)0 == geteuid ()) {
565                 struct group *grp;
566
567                 errno = 0;
568                 if (NULL != (grp = getgrnam (sock_group))) {
569                         errno = 0;
570                         if (0 != chown (SOCK_PATH, (uid_t)-1, grp->gr_gid)) {
571                                 syslog (LOG_WARNING, "chown() failed: %s", strerror (errno));
572                         }
573                 }
574                 else {
575                         syslog (LOG_WARNING, "getgrnam() failed: %s", strerror (errno));
576                 }
577         }
578         else {
579                 syslog (LOG_WARNING, "not running as root");
580         }
581
582         errno = 0;
583         if (0 != chmod (SOCK_PATH, sock_perms)) {
584                 syslog (LOG_WARNING, "chmod() failed: %s", strerror (errno));
585         }
586
587         { /* initialize queue of available threads */
588                 int i = 0;
589
590                 collector_t *last;
591
592                 active.head = NULL;
593                 active.tail = NULL;
594
595                 available.head = (collector_t *)smalloc (sizeof (collector_t));
596                 available.tail = available.head;
597                 available.tail->next = NULL;
598
599                 last = available.head;
600
601                 for (i = 1; i < max_conns; ++i) {
602                         last->next = (collector_t *)smalloc (sizeof (collector_t));
603                         last = last->next;
604                         available.tail = last;
605                         available.tail->next = NULL;
606                 }
607         }
608
609         while (1) {
610                 int remote = 0;
611                 int err    = 0;
612
613                 collector_t *collector;
614
615                 pthread_attr_t ptattr;
616
617                 pthread_mutex_lock (&available_mutex);
618                 while (NULL == available.head) {
619                         pthread_cond_wait (&collector_available, &available_mutex);
620                 }
621                 pthread_mutex_unlock (&available_mutex);
622
623                 do {
624                         errno = 0;
625                         if (-1 == (remote = accept (connector_socket, NULL, NULL))) {
626                                 if (EINTR != errno) {
627                                         disabled = 1;
628                                         syslog (LOG_ERR, "accept() failed: %s", strerror (errno));
629                                         pthread_exit ((void *)1);
630                                 }
631                         }
632                 } while (EINTR == errno);
633
634                 /* assign connection to next available thread */
635                 pthread_mutex_lock (&available_mutex);
636
637                 collector = available.head;
638                 collector->socket = remote;
639
640                 if (available.head == available.tail) {
641                         available.head = NULL;
642                         available.tail = NULL;
643                 }
644                 else {
645                         available.head = available.head->next;
646                 }
647
648                 pthread_mutex_unlock (&available_mutex);
649
650                 collector->idx  = 0;
651                 collector->next = NULL;
652
653                 pthread_attr_init (&ptattr);
654                 pthread_attr_setdetachstate (&ptattr, PTHREAD_CREATE_DETACHED);
655
656                 if (0 == (err = pthread_create (&collector->thread, &ptattr, collect,
657                                 (void *)collector))) {
658                         pthread_mutex_lock (&active_mutex);
659
660                         if (NULL == active.head) {
661                                 active.head = collector;
662                                 active.tail = collector;
663                         }
664                         else {
665                                 active.tail->next = collector;
666                                 active.tail = collector;
667                         }
668
669                         pthread_mutex_unlock (&active_mutex);
670                 }
671                 else {
672                         pthread_mutex_lock (&available_mutex);
673
674                         if (NULL == available.head) {
675                                 available.head = collector;
676                                 available.tail = collector;
677                         }
678                         else {
679                                 available.tail->next = collector;
680                                 available.tail = collector;
681                         }
682
683                         pthread_mutex_unlock (&available_mutex);
684
685                         close (remote);
686                         syslog (LOG_ERR, "pthread_create() failed: %s", strerror (err));
687                 }
688
689                 pthread_attr_destroy (&ptattr);
690         }
691         pthread_exit ((void *)0);
692 } /* void *open_connection (void *) */
693 #endif /* EMAIL_HAVE_READ */
694
695 static void email_init (void)
696 {
697 #if EMAIL_HAVE_READ
698         int err = 0;
699
700         if (0 != (err = pthread_create (&connector, NULL,
701                                 open_connection, NULL))) {
702                 disabled = 1;
703                 syslog (LOG_ERR, "pthread_create() failed: %s", strerror (err));
704                 return;
705         }
706 #endif /* EMAIL_HAVE_READ */
707         return;
708 } /* static void email_init (void) */
709
710 #if EMAIL_HAVE_READ
711 static void email_shutdown (void)
712 {
713         collector_t *ptr;
714
715         if (disabled)
716                 return;
717
718         close (connector_socket);
719         pthread_kill (connector, SIGTERM);
720
721         pthread_mutex_lock (&active_mutex);
722
723         for (ptr = active.head; NULL != ptr; ptr = ptr->next) {
724                 close (ptr->socket);
725                 pthread_kill (ptr->thread, SIGTERM);
726         }
727
728         pthread_mutex_unlock (&active_mutex);
729
730         unlink (SOCK_PATH);
731         return;
732 } /* static void email_shutdown (void) */
733 #endif /* EMAIL_HAVE_READ */
734
735 static void count_write (char *host, char *inst, char *val)
736 {
737         char file[BUFSIZE] = "";
738         int  len           = 0;
739
740         len = snprintf (file, BUFSIZE, COUNT_FILE, inst);
741         if ((len < 0) || (len >= BUFSIZE))
742                 return;
743
744         rrd_update_file (host, file, val, count_ds_def, count_ds_num);
745         return;
746 } /* static void email_write (char *host, char *inst, char *val) */
747
748 static void size_write (char *host, char *inst, char *val)
749 {
750         char file[BUFSIZE] = "";
751         int  len           = 0;
752
753         len = snprintf (file, BUFSIZE, SIZE_FILE, inst);
754         if ((len < 0) || (len >= BUFSIZE))
755                 return;
756
757         rrd_update_file (host, file, val, size_ds_def, size_ds_num);
758         return;
759 } /* static void size_write (char *host, char *inst, char *val) */
760
761 static void score_write (char *host, char *inst, char *val)
762 {
763         rrd_update_file (host, SCORE_FILE, val, score_ds_def, score_ds_num);
764         return;
765 } /* static void score_write (char *host, char *inst, char *val) */
766
767 static void check_write (char *host, char *inst, char *val)
768 {
769         char file[BUFSIZE] = "";
770         int  len           = 0;
771
772         len = snprintf (file, BUFSIZE, CHECK_FILE, inst);
773         if ((len < 0) || (len >= BUFSIZE))
774                 return;
775
776         rrd_update_file (host, file, val, check_ds_def, check_ds_num);
777         return;
778 } /* static void check_write (char *host, char *inst, char *val) */
779
780 #if EMAIL_HAVE_READ
781 static void type_submit (char *plugin, char *inst, int value)
782 {
783         char buf[BUFSIZE] = "";
784         int  len          = 0;
785
786         if (0 == value)
787                 return;
788
789         len = snprintf (buf, BUFSIZE, "%u:%i", (unsigned int)curtime, value);
790         if ((len < 0) || (len >= BUFSIZE))
791                 return;
792
793         plugin_submit (plugin, inst, buf);
794         return;
795 } /* static void type_submit (char *, char *, int) */
796
797 static void score_submit (double value)
798 {
799         char buf[BUFSIZE] = "";
800         int  len          = 0;
801
802         if (0.0 == value)
803                 return;
804
805         len = snprintf (buf, BUFSIZE, "%u:%.2f", (unsigned int)curtime, value);
806         if ((len < 0) || (len >= BUFSIZE))
807                 return;
808
809         plugin_submit ("email_spam_score", NULL, buf);
810         return;
811 }
812
813 static void email_read (void)
814 {
815         type_t *ptr;
816
817         if (disabled)
818                 return;
819
820         pthread_mutex_lock (&count_mutex);
821
822         for (ptr = count.head; NULL != ptr; ptr = ptr->next) {
823                 type_submit ("email_count", ptr->name, ptr->value);
824                 ptr->value = 0;
825         }
826
827         pthread_mutex_unlock (&count_mutex);
828
829         pthread_mutex_lock (&size_mutex);
830
831         for (ptr = size.head; NULL != ptr; ptr = ptr->next) {
832                 type_submit ("email_size", ptr->name, ptr->value);
833                 ptr->value = 0;
834         }
835
836         pthread_mutex_unlock (&size_mutex);
837
838         pthread_mutex_lock (&score_mutex);
839
840         score_submit (score);
841         score = 0.0;
842         score_count = 0;
843
844         pthread_mutex_unlock (&score_mutex);
845
846         pthread_mutex_lock (&check_mutex);
847
848         for (ptr = check.head; NULL != ptr; ptr = ptr->next) {
849                 type_submit ("email_spam_check", ptr->name, ptr->value);
850                 ptr->value = 0;
851         }
852
853         pthread_mutex_unlock (&check_mutex);
854         return;
855 } /* static void read (void) */
856 #else /* if !EMAIL_HAVE_READ */
857 # define email_read NULL
858 #endif
859
860 void module_register (void)
861 {
862         plugin_register (MODULE_NAME, email_init, email_read, NULL);
863         plugin_register ("email_count", NULL, NULL, count_write);
864         plugin_register ("email_size", NULL, NULL, size_write);
865         plugin_register ("email_spam_score", NULL, NULL, score_write);
866         plugin_register ("email_spam_check", NULL, NULL, check_write);
867 #if EMAIL_HAVE_READ
868         plugin_register_shutdown (MODULE_NAME, email_shutdown);
869         cf_register (MODULE_NAME, email_config, config_keys, config_keys_num);
870 #endif /* EMAIL_HAVE_READ */
871         return;
872 } /* void module_register (void) */
873
874 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
875