erlang plugin: Hint towards epmd if erl_publish fails.
[collectd.git] / src / erlang.c
index 8317d82..5b84675 100644 (file)
@@ -20,6 +20,7 @@
  **/
 
 #include "collectd.h"
+#include "common.h"
 #include "plugin.h"
 
 #include <sys/types.h>
@@ -48,8 +49,21 @@ typedef struct ce_connection_info_s ce_connection_info_t;
 static pthread_t listen_thread_id;
 static _Bool     listen_thread_running = false;
 
+static const char *config_keys[] =
+{
+       "BindTo",
+       "BindPort",
+       "Cookie",
+       "NodeName"
+};
+static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
+
+static char conf_node[NI_MAXHOST] = "";
 static char conf_service[NI_MAXSERV] = "29157";
 static char conf_cookie[256] = "ceisaequ";
+static char conf_hostname[256] = "alyja";
+static char conf_nodename[256] = "collectd";
+static char conf_fullname[256] = "collectd@alyja.office.noris.de";
 
 /*
  * Private functions
@@ -110,13 +124,17 @@ static int eterm_to_int (const ETERM *term, int *ret_int) /* {{{ */
                        *ret_int = (int) (ERL_FLOAT_VALUE (term) + .5);
                        break;
 
+#ifdef ERL_LONGLONG
                case ERL_LONGLONG:
                        *ret_int = (int) ERL_LL_VALUE (term);
                        break;
+#endif /* ERL_LONGLONG */
 
+#ifdef ERL_U_LONGLONG
                case ERL_U_LONGLONG:
                        *ret_int = (int) ERL_LL_UVALUE (term);
                        break;
+#endif /* ERL_U_LONGLONG */
 
                default:
                        ERROR ("erlang plugin: Don't know how to cast "
@@ -169,13 +187,17 @@ static int eterm_to_time_t (const ETERM *term, time_t *ret_time) /* {{{ */
                        *ret_time = (time_t) (ERL_FLOAT_VALUE (term) + .5);
                        break;
 
+#ifdef ERL_LONGLONG
                case ERL_LONGLONG:
                        *ret_time = (time_t) ERL_LL_VALUE (term);
                        break;
+#endif /* ERL_LONGLONG */
 
+#ifdef ERL_U_LONGLONG
                case ERL_U_LONGLONG:
                        *ret_time = (time_t) ERL_LL_UVALUE (term);
                        break;
+#endif /* ERL_U_LONGLONG */
 
                default:
                        ERROR ("erlang plugin: Don't know how to cast "
@@ -264,6 +286,7 @@ static int eterm_to_value (const ETERM *term, int ds_type, /* {{{ */
                        break;
                }
 
+#ifdef ERL_LONGLONG
                case ERL_LONGLONG:
                {
                        long long v = ERL_LL_VALUE (term);
@@ -276,7 +299,9 @@ static int eterm_to_value (const ETERM *term, int ds_type, /* {{{ */
                        }
                        break;
                }
+#endif /* ERL_LONGLONG */
 
+#ifdef ERL_U_LONGLONG
                case ERL_U_LONGLONG:
                {
                        unsigned long long v = ERL_LL_UVALUE (term);
@@ -289,6 +314,7 @@ static int eterm_to_value (const ETERM *term, int ds_type, /* {{{ */
                        }
                        break;
                }
+#endif /* ERL_U_LONGLONG */
 
                default:
                        ERROR ("erlang plugin: Don't know how to cast "
@@ -627,9 +653,9 @@ static int create_listen_socket (void) /* {{{ */
                /* Dunno if calling this multiple times is legal. Since it wants to have
                 * the sin_addr for some reason this is the best place to call this,
                 * though. -octo */
-               status = erl_connect_xinit (/* host name = */ "leeloo",
-                               /* plain node name = */ "collectd",
-                               /* full node name  = */ "collectd@leeloo.lan.home.verplant.org",
+               status = erl_connect_xinit (/* host name = */ conf_hostname,
+                               /* plain node name = */ conf_nodename,
+                               /* full node name  = */ conf_fullname,
                                /* our address     = */ sin_addr,
                                /* secret cookie   = */ conf_cookie,
                                /* instance number = */ 0);
@@ -686,7 +712,8 @@ static int create_listen_socket (void) /* {{{ */
                status = erl_publish (numeric_serv);
                if (status < 0)
                {
-                       ERROR ("erlang plugin: erl_publish (%i) failed with status %i.", numeric_serv, status);
+                       ERROR ("erlang plugin: erl_publish (%i) failed with status %i. "
+                                       "Is epmd running?", numeric_serv, status);
                        close (sock_descr);
                        sock_descr = -1;
                        return (-1);
@@ -766,8 +793,56 @@ static int ce_init (void) /* {{{ */
        return (0);
 } /* }}} int ce_init */
 
+static int ce_config (const char *key, const char *value) /* {{{ */
+{
+       if (strcasecmp ("BindTo", key) == 0)
+       {
+               sstrncpy (conf_node, value, sizeof (conf_node));
+       }
+       else if (strcasecmp ("BindPort", key) == 0)
+       {
+               sstrncpy (conf_service, value, sizeof (conf_service));
+       }
+       else if (strcasecmp ("Cookie", key) == 0)
+       {
+               sstrncpy (conf_cookie, value, sizeof (conf_cookie));
+       }
+       else if (strcasecmp ("NodeName", key) == 0)
+       {
+               const char *host;
+
+               host = strchr (value, '@');
+               if (host == NULL)
+               {
+                       sstrncpy (conf_nodename, value, sizeof (conf_nodename));
+                       sstrncpy (conf_hostname, hostname_g, sizeof (conf_hostname));
+                       ssnprintf (conf_fullname, sizeof (conf_fullname), "%s@%s",
+                                       conf_nodename, conf_hostname);
+               }
+               else /* if (host != NULL) */
+               {
+                       char *tmp;
+
+                       sstrncpy (conf_nodename, value, sizeof (conf_nodename));
+                       sstrncpy (conf_hostname, host + 1, sizeof (conf_hostname));
+                       sstrncpy (conf_fullname, value, sizeof (conf_fullname));
+
+                       tmp = strchr (conf_nodename, '@');
+                       if (tmp != NULL)
+                               *tmp = 0;
+               }
+       }
+       else
+       {
+               return (-1);
+       }
+
+       return (0);
+} /* }}} int ce_config */
+
 void module_register (void)
 {
+       plugin_register_config ("erlang", ce_config, config_keys, config_keys_num);
        plugin_register_init ("erlang", ce_init);
 }