email plugin: Don't unlink the socket file on startup.
[collectd.git] / src / email.c
index 7738a39..c78d761 100644 (file)
@@ -4,8 +4,7 @@
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2 of the License, or (at your
- * option) any later version.
+ * Free Software Foundation; only version 2 of the License is applicable.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
 
 #include "configfile.h"
 
+#include <stddef.h>
+
 #if HAVE_LIBPTHREAD
 # include <pthread.h>
 #endif
 
-#if HAVE_SYS_SELECT_H
-#      include <sys/select.h>
-#endif /* HAVE_SYS_SELECT_H */
-
-#if HAVE_SYS_SOCKET_H
-#      include <sys/socket.h>
-#endif /* HAVE_SYS_SOCKET_H */
-
-/* *sigh* glibc does not define UNIX_PATH_MAX in sys/un.h ... */
-#if HAVE_LINUX_UN_H
-#      include <linux/un.h>
-#elif HAVE_SYS_UN_H
-#      include <sys/un.h>
-#endif /* HAVE_LINUX_UN_H | HAVE_SYS_UN_H */
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/select.h>
 
 /* some systems (e.g. Darwin) seem to not define UNIX_PATH_MAX at all */
 #ifndef UNIX_PATH_MAX
 /* 256 bytes ought to be enough for anybody ;-) */
 #define BUFSIZE 256
 
-#ifndef COLLECTD_SOCKET_PREFIX
-# define COLLECTD_SOCKET_PREFIX "/tmp/.collectd-"
-#endif /* COLLECTD_SOCKET_PREFIX */
-
-#define SOCK_PATH COLLECTD_SOCKET_PREFIX"email"
+#define SOCK_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-email"
 #define MAX_CONNS 5
 #define MAX_CONNS_LIMIT 16384
 
@@ -131,43 +117,15 @@ typedef struct {
 /* valid configuration file keys */
 static const char *config_keys[] =
 {
+       "SocketFile",
        "SocketGroup",
        "SocketPerms",
        "MaxConns"
 };
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
-static data_source_t gauge_dsrc[1] =
-{
-       {"value", DS_TYPE_GAUGE, 0.0, NAN}
-};
-
-static data_set_t email_count_ds =
-{
-       "email_count", 1, gauge_dsrc
-};
-
-static data_set_t email_size_ds =
-{
-       "email_size", 1, gauge_dsrc
-};
-
-static data_set_t spam_check_ds =
-{
-       "spam_check", 1, gauge_dsrc
-};
-
-static data_source_t spam_score_dsrc[1] =
-{
-       {"score", DS_TYPE_GAUGE, NAN, NAN}
-};
-
-static data_set_t spam_score_ds =
-{
-       "spam_score", 1, spam_score_dsrc
-};
-
 /* socket configuration */
+static char *sock_file  = SOCK_PATH;
 static char *sock_group = COLLECTD_GRP_NAME;
 static int  sock_perms  = S_IRWXU | S_IRWXG;
 static int  max_conns   = MAX_CONNS;
@@ -213,7 +171,10 @@ static type_list_t check;
  */
 static int email_config (const char *key, const char *value)
 {
-       if (0 == strcasecmp (key, "SocketGroup")) {
+       if (0 == strcasecmp (key, "SocketFile")) {
+               sock_file = sstrdup (value);
+       }
+       else if (0 == strcasecmp (key, "SocketGroup")) {
                sock_group = sstrdup (value);
        }
        else if (0 == strcasecmp (key, "SocketPerms")) {
@@ -550,9 +511,8 @@ static void *open_connection (void *arg)
 
        addr.sun_family = AF_UNIX;
 
-       strncpy (addr.sun_path, SOCK_PATH, (size_t)(UNIX_PATH_MAX - 1));
+       strncpy (addr.sun_path, sock_file, (size_t)(UNIX_PATH_MAX - 1));
        addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
-       unlink (addr.sun_path);
 
        errno = 0;
        if (-1 == bind (connector_socket, (struct sockaddr *)&addr,
@@ -597,12 +557,12 @@ static void *open_connection (void *arg)
                }
                else
                {
-                       status = chown (SOCK_PATH, (uid_t) -1, grp->gr_gid);
+                       status = chown (sock_file, (uid_t) -1, grp->gr_gid);
                        if (status != 0)
                        {
                                char errbuf[1024];
                                log_warn ("chown (%s, -1, %i) failed: %s",
-                                               SOCK_PATH, (int) grp->gr_gid,
+                                               sock_file, (int) grp->gr_gid,
                                                sstrerror (errno, errbuf, sizeof (errbuf)));
                        }
                }
@@ -613,7 +573,7 @@ static void *open_connection (void *arg)
        }
 
        errno = 0;
-       if (0 != chmod (SOCK_PATH, sock_perms)) {
+       if (0 != chmod (sock_file, sock_perms)) {
                char errbuf[1024];
                log_warn ("chmod() failed: %s",
                                sstrerror (errno, errbuf, sizeof (errbuf)));
@@ -756,7 +716,7 @@ static int email_shutdown (void)
 
        pthread_mutex_unlock (&conns_mutex);
 
-       unlink (SOCK_PATH);
+       unlink (sock_file);
        errno = 0;
 
        return (0);
@@ -772,8 +732,8 @@ static void email_submit (const char *type, const char *type_instance, gauge_t v
        vl.values = values;
        vl.values_len = 1;
        vl.time = time (NULL);
-       strcpy (vl.host, hostname_g);
-       strcpy (vl.plugin, "email");
+       sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+       sstrncpy (vl.plugin, "email", sizeof (vl.plugin));
        strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
        plugin_dispatch_values (type, &vl);
@@ -821,7 +781,8 @@ static int email_read (void)
 {
        type_t *ptr;
 
-       double sc;
+       double score_old;
+       int score_count_old;
 
        static type_list_t *cnt;
        static type_list_t *sz;
@@ -870,13 +831,15 @@ static int email_read (void)
        /* spam score */
        pthread_mutex_lock (&score_mutex);
 
-       sc = score;
+       score_old = score;
+       score_count_old = score_count;
        score = 0.0;
        score_count = 0;
 
        pthread_mutex_unlock (&score_mutex);
 
-       email_submit ("spam_score", "", sc);
+       if (score_count_old > 0)
+               email_submit ("spam_score", "", score_old);
 
        /* spam checks */
        pthread_mutex_lock (&check_mutex);
@@ -891,22 +854,11 @@ static int email_read (void)
        return (0);
 } /* int email_read */
 
-void module_register (modreg_e load)
+void module_register (void)
 {
-       if (load & MR_DATASETS)
-       {
-               plugin_register_data_set (&email_count_ds);
-               plugin_register_data_set (&email_size_ds);
-               plugin_register_data_set (&spam_check_ds);
-               plugin_register_data_set (&spam_score_ds);
-       }
-
-       if (load & MR_READ)
-       {
-               plugin_register_config ("email", email_config, config_keys, config_keys_num);
-               plugin_register_init ("email", email_init);
-               plugin_register_read ("email", email_read);
-       }
+       plugin_register_config ("email", email_config, config_keys, config_keys_num);
+       plugin_register_init ("email", email_init);
+       plugin_register_read ("email", email_read);
        plugin_register_shutdown ("email", email_shutdown);
 } /* void module_register */