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