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>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
32 #include <erl_interface.h>
36 * Private data structures
38 struct ce_connection_info_s
43 typedef struct ce_connection_info_s ce_connection_info_t;
48 static pthread_t listen_thread_id;
49 static _Bool listen_thread_running = false;
51 static char conf_service[NI_MAXSERV] = "29157";
52 static char conf_cookie[256] = "ceisaequ";
57 static int send_atom (int fd, ETERM *to, const char *atom) /* {{{ */
62 reply = erl_mk_atom (atom);
66 status = erl_send (fd, to, reply);
67 erl_free_term (reply);
73 } /* }}} int send_atom */
75 static int send_error (int fd, ETERM *to, const char *message) /* {{{ */
80 DEBUG ("erlang plugin: send_error: message = %s.", message);
81 reply = erl_format ("{~a,~s}", "error", message);
83 status = erl_send (fd, to, reply);
89 erl_free_term (reply);
92 } /* }}} int send_error */
94 static int eterm_to_int (const ETERM *term, int *ret_int) /* {{{ */
96 if ((term == NULL) || (ret_int == NULL))
99 switch (ERL_TYPE (term))
102 *ret_int = (int) ERL_INT_VALUE (term);
106 *ret_int = (int) ERL_INT_UVALUE (term);
110 *ret_int = (int) (ERL_FLOAT_VALUE (term) + .5);
115 *ret_int = (int) ERL_LL_VALUE (term);
117 #endif /* ERL_LONGLONG */
119 #ifdef ERL_U_LONGLONG
121 *ret_int = (int) ERL_LL_UVALUE (term);
123 #endif /* ERL_U_LONGLONG */
126 ERROR ("erlang plugin: Don't know how to cast "
127 "erlang type %#x to int.", (unsigned int) ERL_TYPE (term));
129 } /* switch (ERL_TYPE (term)) */
132 } /* }}} int eterm_to_int */
134 static int eterm_to_time_t (const ETERM *term, time_t *ret_time) /* {{{ */
136 if ((term == NULL) || (ret_time == NULL))
139 if (ERL_IS_NIL (term)
140 || (ERL_IS_ATOM (term)
141 && ((strcmp ("now", ERL_ATOM_PTR (term)) == 0)
142 || (strcmp ("undefined", ERL_ATOM_PTR (term)) == 0))))
144 *ret_time = time (NULL);
148 switch (ERL_TYPE (term))
151 *ret_time = (time_t) ERL_INT_VALUE (term);
155 *ret_time = (time_t) ERL_INT_UVALUE (term);
159 if ((strcmp ("now", ERL_ATOM_PTR (term)) == 0)
160 || (strcmp ("undefined", ERL_ATOM_PTR (term)) == 0))
162 *ret_time = time (NULL);
166 ERROR ("erlang plugin: Invalid atom for time: %s.",
167 ERL_ATOM_PTR (term));
173 *ret_time = (time_t) (ERL_FLOAT_VALUE (term) + .5);
178 *ret_time = (time_t) ERL_LL_VALUE (term);
180 #endif /* ERL_LONGLONG */
182 #ifdef ERL_U_LONGLONG
184 *ret_time = (time_t) ERL_LL_UVALUE (term);
186 #endif /* ERL_U_LONGLONG */
189 ERROR ("erlang plugin: Don't know how to cast "
190 "erlang type %#x to time_t.", (unsigned int) ERL_TYPE (term));
192 } /* switch (ERL_TYPE (term)) */
195 } /* }}} int eterm_to_time_t */
197 static int eterm_to_string (const ETERM *term, char *buffer, size_t buffer_size) /* {{{ */
201 if ((term == NULL) || (buffer == NULL) || (buffer_size <= 0))
204 memset (buffer, 0, buffer_size);
206 if (ERL_IS_EMPTY_LIST (term)
208 || (ERL_IS_ATOM (term)
209 && (strcmp ("undefined", ERL_ATOM_PTR (term)) == 0)))
215 if (!ERL_IS_LIST (term))
218 tmp = erl_iolist_to_string (term);
222 strncpy (buffer, tmp, buffer_size - 1);
226 } /* }}} int eterm_to_string */
228 static int eterm_to_value (const ETERM *term, int ds_type, /* {{{ */
231 if ((term == NULL) || (value == NULL))
234 switch (ERL_TYPE (term))
238 int v = ERL_INT_VALUE (term);
241 case DS_TYPE_COUNTER: value->counter = (counter_t) v; break;
242 case DS_TYPE_GAUGE: value->gauge = (gauge_t) v; break;
243 case DS_TYPE_DERIVE: value->derive = (derive_t) v; break;
244 case DS_TYPE_ABSOLUTE: value->absolute = (absolute_t) v; break;
251 unsigned int v = ERL_INT_UVALUE (term);
254 case DS_TYPE_COUNTER: value->counter = (counter_t) v; break;
255 case DS_TYPE_GAUGE: value->gauge = (gauge_t) v; break;
256 case DS_TYPE_DERIVE: value->derive = (derive_t) v; break;
257 case DS_TYPE_ABSOLUTE: value->absolute = (absolute_t) v; break;
264 double v = ERL_FLOAT_VALUE (term);
267 case DS_TYPE_COUNTER: value->counter = (counter_t) v; break;
268 case DS_TYPE_GAUGE: value->gauge = (gauge_t) v; break;
269 case DS_TYPE_DERIVE: value->derive = (derive_t) v; break;
270 case DS_TYPE_ABSOLUTE: value->absolute = (absolute_t) v; break;
278 long long v = ERL_LL_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;
288 #endif /* ERL_LONGLONG */
290 #ifdef ERL_U_LONGLONG
293 unsigned long long v = ERL_LL_UVALUE (term);
296 case DS_TYPE_COUNTER: value->counter = (counter_t) v; break;
297 case DS_TYPE_GAUGE: value->gauge = (gauge_t) v; break;
298 case DS_TYPE_DERIVE: value->derive = (derive_t) v; break;
299 case DS_TYPE_ABSOLUTE: value->absolute = (absolute_t) v; break;
303 #endif /* ERL_U_LONGLONG */
306 ERROR ("erlang plugin: Don't know how to cast "
307 "erlang type %#x to value_t.", (unsigned int) ERL_TYPE (term));
309 } /* switch (ERL_TYPE (term)) */
312 } /* }}} int eterm_to_value */
314 static int eterm_to_values (const ETERM *term, const data_set_t *ds, /* {{{ */
320 if ((term == NULL) || (ds == NULL) || (vl == NULL))
323 if (!ERL_IS_LIST (term))
330 while (!ERL_IS_EMPTY_LIST (term))
332 const ETERM *eterm_value;
335 if (ds_index >= ds->ds_num)
337 ds_index = ds->ds_num + 1;
342 tmp = realloc (vl->values, sizeof (*tmp) * (vl->values_len + 1));
350 eterm_value = ERL_CONS_HEAD (term);
351 term = ERL_CONS_TAIL (term);
353 status = eterm_to_value (eterm_value, ds->ds[ds_index].type,
354 vl->values + vl->values_len);
362 if ((status == 0) && (ds_index != ds->ds_num))
363 NOTICE ("erlang plugin: Incorrect number of values received for type %s: "
364 "Expected %i, got %i.", ds->type, ds->ds_num, ds_index);
366 if ((status != 0) || (ds_index != ds->ds_num))
375 } /* }}} int eterm_to_values */
377 static int eterm_to_value_list (const ETERM *term, value_list_t *vl) /* {{{ */
381 const data_set_t *ds;
383 if ((term == NULL) || (vl == NULL))
386 if (!ERL_IS_TUPLE (term) || (ERL_TUPLE_SIZE (term) != 9))
389 tmp = erl_element (1, term);
390 if (!ERL_IS_ATOM (tmp)
391 || (strcmp ("value_list", ERL_ATOM_PTR (tmp)) != 0))
401 #define TUPLE_ELEM_TO_CHAR_ARRAY(idx,buf) \
402 tmp = erl_element ((idx), term); \
403 status = eterm_to_string (tmp, (buf), sizeof (buf)); \
404 erl_free_term (tmp); \
408 TUPLE_ELEM_TO_CHAR_ARRAY (2, vl->host);
409 TUPLE_ELEM_TO_CHAR_ARRAY (3, vl->plugin);
410 TUPLE_ELEM_TO_CHAR_ARRAY (4, vl->plugin_instance);
411 TUPLE_ELEM_TO_CHAR_ARRAY (5, vl->type);
412 TUPLE_ELEM_TO_CHAR_ARRAY (6, vl->type_instance);
414 ds = plugin_get_ds (vl->type);
421 tmp = erl_element (7, term);
422 status = eterm_to_time_t (tmp, &vl->time);
427 tmp = erl_element (8, term);
428 status = eterm_to_int (tmp, &vl->interval);
432 if (vl->interval < 1)
433 vl->interval = interval_g;
435 tmp = erl_element (9, term);
436 status = eterm_to_values (tmp, ds, vl);
441 #undef TUPLE_ELEM_TO_CHAR_ARRAY
447 /* validate the struct */
448 if ((vl->host[0] == 0) || (vl->plugin[0] == 0) || (vl->type[0] == 0))
451 if (ds->ds_num != vl->values_len)
455 } /* }}} int eterm_to_value_list */
457 /* Returns non-zero only if the request could not be handled gracefully. */
458 static int handle_dispatch_values (ce_connection_info_t *cinfo, /* {{{ */
459 const ErlMessage *req)
465 memset (&vl, 0, sizeof (vl));
468 eterm_vl = erl_element (2, req->msg);
469 status = eterm_to_value_list (eterm_vl, &vl);
470 erl_free_term (eterm_vl);
475 send_error (cinfo->fd, req->from, "Cannot parse argument as value list.");
479 status = plugin_dispatch_values (&vl);
483 send_error (cinfo->fd, req->from, "plugin_dispatch_values failed.");
488 send_atom (cinfo->fd, req->from, "success");
491 } /* }}} int handle_dispatch_values */
493 static void *handle_client_thread (void *arg) /* {{{ */
495 ce_connection_info_t *cinfo;
497 unsigned char buffer[4096];
501 DEBUG ("erlang plugin: handle_client_thread[%i]: Handling client %s.",
502 cinfo->fd, cinfo->conn.nodename);
512 erl_free_term (emsg.from);
514 erl_free_term (emsg.to);
516 erl_free_term (emsg.msg);
519 status = erl_receive_msg (cinfo->fd, buffer, sizeof (buffer), &emsg);
520 if (status == ERL_TICK)
523 if (status == ERL_ERROR)
526 if (emsg.type == ERL_REG_SEND)
531 if (!ERL_IS_TUPLE (emsg.msg))
533 ERROR ("erlang plugin: Message is not a tuple.");
534 send_atom (cinfo->fd, emsg.from, "error");
538 func = erl_element (1, emsg.msg);
539 if (!ERL_IS_ATOM (func))
541 ERROR ("erlang plugin: First element is not an atom!");
542 send_atom (cinfo->fd, emsg.from, "error");
543 erl_free_term (func);
547 DEBUG ("erlang plugin: Wanted function is: %s.", ERL_ATOM_PTR (func));
549 if (strcmp ("dispatch_values", ERL_ATOM_PTR (func)) == 0)
550 status = handle_dispatch_values (cinfo, &emsg);
553 ERROR ("erlang plugin: Received request for invalid function `%s'.",
554 ERL_ATOM_PTR (func));
555 send_atom (cinfo->fd, emsg.from, "error");
559 /* Check for fatal errors in the callback functions. */
562 ERROR ("erlang plugin: Handling request for `%s' failed.",
563 ERL_ATOM_PTR (func));
564 erl_free_term (func);
568 erl_free_term (func);
570 else if (emsg.type == ERL_EXIT)
572 DEBUG ("erlang plugin: handle_client_thread[%i]: "
573 "Received exit message.", cinfo->fd);
578 ERROR ("erlang plugin: Message type not handled: %i.", emsg.type);
582 erl_free_term (emsg.from);
584 erl_free_term (emsg.to);
586 erl_free_term (emsg.msg);
589 DEBUG ("erlang plugin: handle_client_thread[%i]: Exiting.", cinfo->fd);
594 pthread_exit ((void *) 0);
596 } /* }}} void *handle_client_thread */
598 static int create_listen_socket (void) /* {{{ */
600 struct addrinfo ai_hints;
601 struct addrinfo *ai_list;
602 struct addrinfo *ai_ptr;
609 memset (&ai_hints, 0, sizeof (ai_hints));
610 /* AI_PASSIVE => returns INADDR_ANY */
611 ai_hints.ai_flags = AI_PASSIVE;
613 ai_hints.ai_flags |= AI_ADDRCONFIG;
616 ai_hints.ai_family = AF_INET;
617 ai_hints.ai_socktype = SOCK_STREAM;
620 status = getaddrinfo (/* node = */ NULL, /* service = */ conf_service,
621 &ai_hints, &ai_list);
624 ERROR ("erlang plugin: getaddrinfo failed: %s", gai_strerror (status));
628 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
630 struct sockaddr_in *sa_in;
631 struct in_addr *sin_addr;
634 assert (ai_ptr->ai_family == AF_INET);
635 sa_in = (struct sockaddr_in *) ai_ptr->ai_addr;
636 sin_addr = &sa_in->sin_addr;
637 numeric_serv = (int) ntohs (sa_in->sin_port);
639 /* Dunno if calling this multiple times is legal. Since it wants to have
640 * the sin_addr for some reason this is the best place to call this,
642 status = erl_connect_xinit (/* host name = */ "leeloo",
643 /* plain node name = */ "collectd",
644 /* full node name = */ "collectd@leeloo.lan.home.verplant.org",
645 /* our address = */ sin_addr,
646 /* secret cookie = */ conf_cookie,
647 /* instance number = */ 0);
650 ERROR ("erlang plugin: erl_connect_xinit failed with status %i.",
655 sock_descr = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
656 ai_ptr->ai_protocol);
659 ERROR ("erlang plugin: socket(2) failed.");
664 status = setsockopt (sock_descr, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
667 ERROR ("erlang plugin: setsockopt(2) failed.");
673 status = bind (sock_descr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
676 ERROR ("erlang plugin: bind(2) failed.");
682 status = listen (sock_descr, /* backlog = */ 10);
685 ERROR ("erlang plugin: listen(2) failed.");
692 } /* for (ai_list) */
694 freeaddrinfo (ai_list);
698 status = erl_publish (numeric_serv);
701 ERROR ("erlang plugin: erl_publish (%i) failed with status %i.", numeric_serv, status);
709 } /* }}} int create_listen_socket */
711 void *listen_thread (void *arg) /* {{{ */
718 /* I have no fucking idea what this does, nor what the arguments are. Didn't
719 * find any comprehensive docs yet. */
720 erl_init (/* void *x = */ NULL, /* long y = */ 0);
722 listen = create_listen_socket ();
729 pthread_attr_t tattr;
730 ce_connection_info_t *arg;
732 fd = erl_accept (listen, &conn);
735 ERROR ("erlang plugin: erl_accept failed with status %i.", fd);
739 DEBUG ("erlang plugin: Got connection from %s on fd %i.",
742 pthread_attr_init (&tattr);
743 pthread_attr_setdetachstate (&tattr, PTHREAD_CREATE_DETACHED);
745 arg = malloc (sizeof (*arg));
748 ERROR ("erlang plugin: malloc failed.");
752 memset (arg, 0, sizeof (*arg));
755 memcpy (&arg->conn, &conn, sizeof (conn));
757 pthread_create (&tid, &tattr, handle_client_thread, arg);
760 pthread_exit ((void *) 0);
762 } /* }}} void *listen_thread */
764 static int ce_init (void) /* {{{ */
766 if (!listen_thread_running)
770 status = pthread_create (&listen_thread_id,
775 listen_thread_running = true;
779 } /* }}} int ce_init */
781 void module_register (void)
783 plugin_register_init ("erlang", ce_init);
786 /* vim: set sw=2 ts=2 noet fdm=marker : */