2 * collectd - src/erlang.c
3 * Copyright (C) 2009 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
33 #include <erl_interface.h>
37 * Private data structures
39 struct ce_connection_info_s
44 typedef struct ce_connection_info_s ce_connection_info_t;
49 static pthread_t listen_thread_id;
50 static _Bool listen_thread_running = false;
52 static const char *config_keys[] =
59 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
61 static char conf_node[NI_MAXHOST] = "";
62 static char conf_service[NI_MAXSERV] = "29157";
63 static char conf_cookie[256] = "ceisaequ";
64 static char conf_hostname[256] = "alyja";
65 static char conf_nodename[256] = "collectd";
66 static char conf_fullname[256] = "collectd@alyja.office.noris.de";
71 static int send_atom (int fd, ETERM *to, const char *atom) /* {{{ */
76 reply = erl_mk_atom (atom);
80 status = erl_send (fd, to, reply);
81 erl_free_term (reply);
87 } /* }}} int send_atom */
89 static int send_error (int fd, ETERM *to, const char *message) /* {{{ */
94 DEBUG ("erlang plugin: send_error: message = %s.", message);
95 reply = erl_format ("{~a,~s}", "error", message);
97 status = erl_send (fd, to, reply);
103 erl_free_term (reply);
106 } /* }}} int send_error */
108 static int eterm_to_int (const ETERM *term, int *ret_int) /* {{{ */
110 if ((term == NULL) || (ret_int == NULL))
113 switch (ERL_TYPE (term))
116 *ret_int = (int) ERL_INT_VALUE (term);
120 *ret_int = (int) ERL_INT_UVALUE (term);
124 *ret_int = (int) (ERL_FLOAT_VALUE (term) + .5);
129 *ret_int = (int) ERL_LL_VALUE (term);
131 #endif /* ERL_LONGLONG */
133 #ifdef ERL_U_LONGLONG
135 *ret_int = (int) ERL_LL_UVALUE (term);
137 #endif /* ERL_U_LONGLONG */
140 ERROR ("erlang plugin: Don't know how to cast "
141 "erlang type %#x to int.", (unsigned int) ERL_TYPE (term));
143 } /* switch (ERL_TYPE (term)) */
146 } /* }}} int eterm_to_int */
148 static int eterm_to_time_t (const ETERM *term, time_t *ret_time) /* {{{ */
150 if ((term == NULL) || (ret_time == NULL))
153 if (ERL_IS_NIL (term)
154 || (ERL_IS_ATOM (term)
155 && ((strcmp ("now", ERL_ATOM_PTR (term)) == 0)
156 || (strcmp ("undefined", ERL_ATOM_PTR (term)) == 0))))
158 *ret_time = time (NULL);
162 switch (ERL_TYPE (term))
165 *ret_time = (time_t) ERL_INT_VALUE (term);
169 *ret_time = (time_t) ERL_INT_UVALUE (term);
173 if ((strcmp ("now", ERL_ATOM_PTR (term)) == 0)
174 || (strcmp ("undefined", ERL_ATOM_PTR (term)) == 0))
176 *ret_time = time (NULL);
180 ERROR ("erlang plugin: Invalid atom for time: %s.",
181 ERL_ATOM_PTR (term));
187 *ret_time = (time_t) (ERL_FLOAT_VALUE (term) + .5);
192 *ret_time = (time_t) ERL_LL_VALUE (term);
194 #endif /* ERL_LONGLONG */
196 #ifdef ERL_U_LONGLONG
198 *ret_time = (time_t) ERL_LL_UVALUE (term);
200 #endif /* ERL_U_LONGLONG */
203 ERROR ("erlang plugin: Don't know how to cast "
204 "erlang type %#x to time_t.", (unsigned int) ERL_TYPE (term));
206 } /* switch (ERL_TYPE (term)) */
209 } /* }}} int eterm_to_time_t */
211 static int eterm_to_string (const ETERM *term, char *buffer, size_t buffer_size) /* {{{ */
215 if ((term == NULL) || (buffer == NULL) || (buffer_size <= 0))
218 memset (buffer, 0, buffer_size);
220 if (ERL_IS_EMPTY_LIST (term)
222 || (ERL_IS_ATOM (term)
223 && (strcmp ("undefined", ERL_ATOM_PTR (term)) == 0)))
229 if (!ERL_IS_LIST (term))
232 tmp = erl_iolist_to_string (term);
236 strncpy (buffer, tmp, buffer_size - 1);
240 } /* }}} int eterm_to_string */
242 static int eterm_to_value (const ETERM *term, int ds_type, /* {{{ */
245 if ((term == NULL) || (value == NULL))
248 switch (ERL_TYPE (term))
252 int v = ERL_INT_VALUE (term);
255 case DS_TYPE_COUNTER: value->counter = (counter_t) v; break;
256 case DS_TYPE_GAUGE: value->gauge = (gauge_t) v; break;
257 case DS_TYPE_DERIVE: value->derive = (derive_t) v; break;
258 case DS_TYPE_ABSOLUTE: value->absolute = (absolute_t) v; break;
265 unsigned int v = ERL_INT_UVALUE (term);
268 case DS_TYPE_COUNTER: value->counter = (counter_t) v; break;
269 case DS_TYPE_GAUGE: value->gauge = (gauge_t) v; break;
270 case DS_TYPE_DERIVE: value->derive = (derive_t) v; break;
271 case DS_TYPE_ABSOLUTE: value->absolute = (absolute_t) v; break;
278 double v = ERL_FLOAT_VALUE (term);
281 case DS_TYPE_COUNTER: value->counter = (counter_t) v; break;
282 case DS_TYPE_GAUGE: value->gauge = (gauge_t) v; break;
283 case DS_TYPE_DERIVE: value->derive = (derive_t) v; break;
284 case DS_TYPE_ABSOLUTE: value->absolute = (absolute_t) v; break;
292 long long v = ERL_LL_VALUE (term);
295 case DS_TYPE_COUNTER: value->counter = (counter_t) v; break;
296 case DS_TYPE_GAUGE: value->gauge = (gauge_t) v; break;
297 case DS_TYPE_DERIVE: value->derive = (derive_t) v; break;
298 case DS_TYPE_ABSOLUTE: value->absolute = (absolute_t) v; break;
302 #endif /* ERL_LONGLONG */
304 #ifdef ERL_U_LONGLONG
307 unsigned long long v = ERL_LL_UVALUE (term);
310 case DS_TYPE_COUNTER: value->counter = (counter_t) v; break;
311 case DS_TYPE_GAUGE: value->gauge = (gauge_t) v; break;
312 case DS_TYPE_DERIVE: value->derive = (derive_t) v; break;
313 case DS_TYPE_ABSOLUTE: value->absolute = (absolute_t) v; break;
317 #endif /* ERL_U_LONGLONG */
320 ERROR ("erlang plugin: Don't know how to cast "
321 "erlang type %#x to value_t.", (unsigned int) ERL_TYPE (term));
323 } /* switch (ERL_TYPE (term)) */
326 } /* }}} int eterm_to_value */
328 static int eterm_to_values (const ETERM *term, const data_set_t *ds, /* {{{ */
334 if ((term == NULL) || (ds == NULL) || (vl == NULL))
337 if (!ERL_IS_LIST (term))
344 while (!ERL_IS_EMPTY_LIST (term))
346 const ETERM *eterm_value;
349 if (ds_index >= ds->ds_num)
351 ds_index = ds->ds_num + 1;
356 tmp = realloc (vl->values, sizeof (*tmp) * (vl->values_len + 1));
364 eterm_value = ERL_CONS_HEAD (term);
365 term = ERL_CONS_TAIL (term);
367 status = eterm_to_value (eterm_value, ds->ds[ds_index].type,
368 vl->values + vl->values_len);
376 if ((status == 0) && (ds_index != ds->ds_num))
377 NOTICE ("erlang plugin: Incorrect number of values received for type %s: "
378 "Expected %i, got %i.", ds->type, ds->ds_num, ds_index);
380 if ((status != 0) || (ds_index != ds->ds_num))
389 } /* }}} int eterm_to_values */
391 static int eterm_to_value_list (const ETERM *term, value_list_t *vl) /* {{{ */
395 const data_set_t *ds;
397 if ((term == NULL) || (vl == NULL))
400 if (!ERL_IS_TUPLE (term) || (ERL_TUPLE_SIZE (term) != 9))
403 tmp = erl_element (1, term);
404 if (!ERL_IS_ATOM (tmp)
405 || (strcmp ("value_list", ERL_ATOM_PTR (tmp)) != 0))
415 #define TUPLE_ELEM_TO_CHAR_ARRAY(idx,buf) \
416 tmp = erl_element ((idx), term); \
417 status = eterm_to_string (tmp, (buf), sizeof (buf)); \
418 erl_free_term (tmp); \
422 TUPLE_ELEM_TO_CHAR_ARRAY (2, vl->host);
423 TUPLE_ELEM_TO_CHAR_ARRAY (3, vl->plugin);
424 TUPLE_ELEM_TO_CHAR_ARRAY (4, vl->plugin_instance);
425 TUPLE_ELEM_TO_CHAR_ARRAY (5, vl->type);
426 TUPLE_ELEM_TO_CHAR_ARRAY (6, vl->type_instance);
428 ds = plugin_get_ds (vl->type);
435 tmp = erl_element (7, term);
436 status = eterm_to_time_t (tmp, &vl->time);
441 tmp = erl_element (8, term);
442 status = eterm_to_int (tmp, &vl->interval);
446 if (vl->interval < 1)
447 vl->interval = interval_g;
449 tmp = erl_element (9, term);
450 status = eterm_to_values (tmp, ds, vl);
455 #undef TUPLE_ELEM_TO_CHAR_ARRAY
461 /* validate the struct */
462 if ((vl->host[0] == 0) || (vl->plugin[0] == 0) || (vl->type[0] == 0))
465 if (ds->ds_num != vl->values_len)
469 } /* }}} int eterm_to_value_list */
471 /* Returns non-zero only if the request could not be handled gracefully. */
472 static int handle_dispatch_values (ce_connection_info_t *cinfo, /* {{{ */
473 const ErlMessage *req)
479 memset (&vl, 0, sizeof (vl));
482 eterm_vl = erl_element (2, req->msg);
483 status = eterm_to_value_list (eterm_vl, &vl);
484 erl_free_term (eterm_vl);
489 send_error (cinfo->fd, req->from, "Cannot parse argument as value list.");
493 status = plugin_dispatch_values (&vl);
497 send_error (cinfo->fd, req->from, "plugin_dispatch_values failed.");
502 send_atom (cinfo->fd, req->from, "success");
505 } /* }}} int handle_dispatch_values */
507 static void *handle_client_thread (void *arg) /* {{{ */
509 ce_connection_info_t *cinfo;
511 unsigned char buffer[4096];
515 DEBUG ("erlang plugin: handle_client_thread[%i]: Handling client %s.",
516 cinfo->fd, cinfo->conn.nodename);
526 erl_free_term (emsg.from);
528 erl_free_term (emsg.to);
530 erl_free_term (emsg.msg);
533 status = erl_receive_msg (cinfo->fd, buffer, sizeof (buffer), &emsg);
534 if (status == ERL_TICK)
537 if (status == ERL_ERROR)
540 if (emsg.type == ERL_REG_SEND)
545 if (!ERL_IS_TUPLE (emsg.msg))
547 ERROR ("erlang plugin: Message is not a tuple.");
548 send_atom (cinfo->fd, emsg.from, "error");
552 func = erl_element (1, emsg.msg);
553 if (!ERL_IS_ATOM (func))
555 ERROR ("erlang plugin: First element is not an atom!");
556 send_atom (cinfo->fd, emsg.from, "error");
557 erl_free_term (func);
561 DEBUG ("erlang plugin: Wanted function is: %s.", ERL_ATOM_PTR (func));
563 if (strcmp ("dispatch_values", ERL_ATOM_PTR (func)) == 0)
564 status = handle_dispatch_values (cinfo, &emsg);
567 ERROR ("erlang plugin: Received request for invalid function `%s'.",
568 ERL_ATOM_PTR (func));
569 send_atom (cinfo->fd, emsg.from, "error");
573 /* Check for fatal errors in the callback functions. */
576 ERROR ("erlang plugin: Handling request for `%s' failed.",
577 ERL_ATOM_PTR (func));
578 erl_free_term (func);
582 erl_free_term (func);
584 else if (emsg.type == ERL_EXIT)
586 DEBUG ("erlang plugin: handle_client_thread[%i]: "
587 "Received exit message.", cinfo->fd);
592 ERROR ("erlang plugin: Message type not handled: %i.", emsg.type);
596 erl_free_term (emsg.from);
598 erl_free_term (emsg.to);
600 erl_free_term (emsg.msg);
603 DEBUG ("erlang plugin: handle_client_thread[%i]: Exiting.", cinfo->fd);
608 pthread_exit ((void *) 0);
610 } /* }}} void *handle_client_thread */
612 static int create_listen_socket (void) /* {{{ */
614 struct addrinfo ai_hints;
615 struct addrinfo *ai_list;
616 struct addrinfo *ai_ptr;
623 memset (&ai_hints, 0, sizeof (ai_hints));
624 /* AI_PASSIVE => returns INADDR_ANY */
625 ai_hints.ai_flags = AI_PASSIVE;
627 ai_hints.ai_flags |= AI_ADDRCONFIG;
630 ai_hints.ai_family = AF_INET;
631 ai_hints.ai_socktype = SOCK_STREAM;
634 status = getaddrinfo (/* node = */ NULL, /* service = */ conf_service,
635 &ai_hints, &ai_list);
638 ERROR ("erlang plugin: getaddrinfo failed: %s", gai_strerror (status));
642 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
644 struct sockaddr_in *sa_in;
645 struct in_addr *sin_addr;
648 assert (ai_ptr->ai_family == AF_INET);
649 sa_in = (struct sockaddr_in *) ai_ptr->ai_addr;
650 sin_addr = &sa_in->sin_addr;
651 numeric_serv = (int) ntohs (sa_in->sin_port);
653 /* Dunno if calling this multiple times is legal. Since it wants to have
654 * the sin_addr for some reason this is the best place to call this,
656 status = erl_connect_xinit (/* host name = */ conf_hostname,
657 /* plain node name = */ conf_nodename,
658 /* full node name = */ conf_fullname,
659 /* our address = */ sin_addr,
660 /* secret cookie = */ conf_cookie,
661 /* instance number = */ 0);
664 ERROR ("erlang plugin: erl_connect_xinit failed with status %i.",
669 sock_descr = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
670 ai_ptr->ai_protocol);
673 ERROR ("erlang plugin: socket(2) failed.");
678 status = setsockopt (sock_descr, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
681 ERROR ("erlang plugin: setsockopt(2) failed.");
687 status = bind (sock_descr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
690 ERROR ("erlang plugin: bind(2) failed.");
696 status = listen (sock_descr, /* backlog = */ 10);
699 ERROR ("erlang plugin: listen(2) failed.");
706 } /* for (ai_list) */
708 freeaddrinfo (ai_list);
712 status = erl_publish (numeric_serv);
715 ERROR ("erlang plugin: erl_publish (%i) failed with status %i.", numeric_serv, status);
723 } /* }}} int create_listen_socket */
725 void *listen_thread (void *arg) /* {{{ */
732 /* I have no fucking idea what this does, nor what the arguments are. Didn't
733 * find any comprehensive docs yet. */
734 erl_init (/* void *x = */ NULL, /* long y = */ 0);
736 listen = create_listen_socket ();
743 pthread_attr_t tattr;
744 ce_connection_info_t *arg;
746 fd = erl_accept (listen, &conn);
749 ERROR ("erlang plugin: erl_accept failed with status %i.", fd);
753 DEBUG ("erlang plugin: Got connection from %s on fd %i.",
756 pthread_attr_init (&tattr);
757 pthread_attr_setdetachstate (&tattr, PTHREAD_CREATE_DETACHED);
759 arg = malloc (sizeof (*arg));
762 ERROR ("erlang plugin: malloc failed.");
766 memset (arg, 0, sizeof (*arg));
769 memcpy (&arg->conn, &conn, sizeof (conn));
771 pthread_create (&tid, &tattr, handle_client_thread, arg);
774 pthread_exit ((void *) 0);
776 } /* }}} void *listen_thread */
778 static int ce_init (void) /* {{{ */
780 if (!listen_thread_running)
784 status = pthread_create (&listen_thread_id,
789 listen_thread_running = true;
793 } /* }}} int ce_init */
795 static int ce_config (const char *key, const char *value) /* {{{ */
797 if (strcasecmp ("BindTo", key) == 0)
799 sstrncpy (conf_node, value, sizeof (conf_node));
801 else if (strcasecmp ("BindPort", key) == 0)
803 sstrncpy (conf_service, value, sizeof (conf_service));
805 else if (strcasecmp ("Cookie", key) == 0)
807 sstrncpy (conf_cookie, value, sizeof (conf_cookie));
809 else if (strcasecmp ("NodeName", key) == 0)
813 host = strchr (value, '@');
816 sstrncpy (conf_nodename, value, sizeof (conf_nodename));
817 sstrncpy (conf_hostname, hostname_g, sizeof (conf_hostname));
818 ssnprintf (conf_fullname, sizeof (conf_fullname), "%s@%s",
819 conf_nodename, conf_hostname);
821 else /* if (host != NULL) */
825 sstrncpy (conf_nodename, value, sizeof (conf_nodename));
826 sstrncpy (conf_hostname, host + 1, sizeof (conf_hostname));
827 sstrncpy (conf_fullname, value, sizeof (conf_fullname));
829 tmp = strchr (conf_nodename, '@');
840 } /* }}} int ce_config */
842 void module_register (void)
844 plugin_register_config ("erlang", ce_config, config_keys, config_keys_num);
845 plugin_register_init ("erlang", ce_init);
848 /* vim: set sw=2 ts=2 noet fdm=marker : */