3 * Copyright (C) 2010 Julien Ammous
4 * Copyright (C) 2010 Florian Forster
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; only version 2.1 of the License is
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * Florian Forster <octo at collectd.org>
25 /* <lua5.1/luaconf.h> defines a macro using "sprintf". Although not used here,
26 * GCC will complain about the macro definition. */
27 #define DONT_POISON_SPRINTF_YET
32 #include "configfile.h"
33 #include "utils_cache.h"
35 /* Include the Lua API header files. */
39 #include "utils_lua.h"
43 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG && defined(__GNUC__) && __GNUC__
45 # pragma GCC poison sprintf
49 typedef struct lua_script_s lua_script_t;
58 struct clua_callback_data_s
61 char *lua_function_name;
65 typedef struct clua_callback_data_s clua_callback_data_t;
67 struct lua_c_functions_s
72 typedef struct lua_c_functions_s lua_c_functions_t;
74 static char base_path[PATH_MAX + 1] = "";
75 static lua_script_t *scripts = NULL;
77 static int clua_store_callback (lua_State *l, int idx) /* {{{ */
79 static int callback_num = 0;
82 /* XXX FIXME: Not thread-safe! */
83 callback_id = ++callback_num;
86 idx += lua_gettop (l) + 1;
88 lua_getfield (l, LUA_REGISTRYINDEX, "collectd_callbacks"); /* +1 = 1 */
89 if (!lua_istable (l, /* idx = */ -1))
91 lua_pop (l, /* nelems = */ 1); /* -1 = 0 */
92 lua_newtable (l); /* +1 = 1 */
93 lua_pushvalue (l, -1); /* +1 = 2 */
94 lua_setfield (l, LUA_REGISTRYINDEX, "collectd_callbacks"); /* -1 = 1 */
97 /* The table is now on top of the stack */
98 lua_pushinteger (l, (lua_Integer) callback_id); /* +1 = 2 */
100 /* Copy the function pointer */
101 lua_pushvalue (l, idx); /* +1 = 3 */
102 /* Lookup function if it's a string */
103 if (lua_isstring (l, /* idx = */ -1))
104 lua_gettable (l, LUA_GLOBALSINDEX); /* +-0 = 3 */
106 if (!lua_isfunction (l, /* idx = */ -1))
108 lua_pop (l, /* nelems = */ 3); /* -3 = 0 */
112 lua_settable (l, /* idx = */ -3); /* -2 = 1 */
113 lua_pop (l, /* nelems = */ 1); /* -1 = 0 */
114 return (callback_id);
115 } /* }}} int clua_store_callback */
117 static int clua_load_callback (lua_State *l, int callback_id) /* {{{ */
119 lua_getfield (l, LUA_REGISTRYINDEX, "collectd_callbacks"); /* +1 */
120 if (!lua_istable (l, /* idx = */ -1))
122 lua_pop (l, /* nelems = */ 1); /* -1 */
126 lua_pushinteger (l, (lua_Integer) callback_id); /* + 1 */
127 lua_gettable (l, /* idx = */ -2); /* +-0 */
129 if (!lua_isfunction (l, -1))
131 lua_pop (l, /* nelems = */ 2); /* -2 */
136 lua_remove (l, /* idx = */ -2); /* -1 */
138 } /* }}} int clua_load_callback */
140 /* Store the threads in a global variable so they are not cleaned up by the
141 * garbage collector. */
142 static int clua_store_thread (lua_State *l, int idx, int callback_id) /* {{{ */
145 idx += lua_gettop (l) + 1;
147 lua_getfield (l, LUA_REGISTRYINDEX, "collectd_threads"); /* +1 = 1 */
148 if (!lua_istable (l, /* idx = */ -1))
150 lua_pop (l, /* nelems = */ 1); /* -1 = 0 */
151 lua_newtable (l); /* +1 = 1 */
152 lua_pushvalue (l, -1); /* +1 = 2 */
153 lua_setfield (l, LUA_REGISTRYINDEX, "collectd_threads"); /* -1 = 1 */
156 /* The table is now on top of the stack */
157 lua_pushinteger (l, (lua_Integer) callback_id); /* +1 = 2 */
159 /* Copy the thread pointer */
160 lua_pushvalue (l, idx); /* +1 = 3 */
162 if (!lua_isthread (l, /* idx = */ -1))
164 lua_pop (l, /* nelems = */ 3); /* -3 = 0 */
168 lua_settable (l, /* idx = */ -3); /* -2 = 1 */
169 lua_pop (l, /* nelems = */ 1); /* -1 = 0 */
171 } /* }}} int clua_store_thread */
173 static int clua_read (user_data_t *ud) /* {{{ */
175 clua_callback_data_t *cb = ud->data;
179 pthread_mutex_lock (&cb->lock);
183 status = clua_load_callback (l, cb->callback_id);
186 ERROR ("lua plugin: Unable to load callback \"%s\" (id %i).",
187 cb->lua_function_name, cb->callback_id);
188 pthread_mutex_unlock (&cb->lock);
193 status = lua_pcall (l,
196 /* errfunc = */ 0); /* -1+1 = 1 */
199 const char *errmsg = lua_tostring (l, /* idx = */ -1);
201 ERROR ("lua plugin: Calling a read callback failed. "
202 "In addition, retrieving the error message failed.");
204 ERROR ("lua plugin: Calling a read callback failed: %s", errmsg);
205 lua_pop (l, /* nelems = */ 1); /* -1 = 0 */
206 pthread_mutex_unlock (&cb->lock);
210 if (!lua_isnumber (l, /* idx = */ -1))
212 ERROR ("lua plugin: Read function \"%s\" (id %i) did not return a numeric status.",
213 cb->lua_function_name, cb->callback_id);
218 status = (int) lua_tointeger (l, /* idx = */ -1);
221 /* pop return value and function */
222 lua_pop (l, /* nelems = */ 1); /* -1 = 0 */
224 pthread_mutex_unlock (&cb->lock);
226 } /* }}} int clua_read */
228 static int clua_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
231 clua_callback_data_t *cb = ud->data;
235 pthread_mutex_lock (&cb->lock);
239 status = clua_load_callback (l, cb->callback_id);
242 ERROR ("lua plugin: Unable to load callback \"%s\" (id %i).",
243 cb->lua_function_name, cb->callback_id);
244 pthread_mutex_unlock (&cb->lock);
249 status = luaC_pushvaluelist (l, ds, vl);
252 lua_pop (l, /* nelems = */ 1); /* -1 = 0 */
253 pthread_mutex_unlock (&cb->lock);
254 ERROR ("lua plugin: luaC_pushvaluelist failed.");
259 status = lua_pcall (l,
262 /* errfunc = */ 0); /* -2+1 = 1 */
265 const char *errmsg = lua_tostring (l, /* idx = */ -1);
267 ERROR ("lua plugin: Calling the write callback failed. "
268 "In addition, retrieving the error message failed.");
270 ERROR ("lua plugin: Calling the write callback failed:\n%s", errmsg);
271 lua_pop (l, /* nelems = */ 1); /* -1 = 0 */
272 pthread_mutex_unlock (&cb->lock);
276 if (!lua_isnumber (l, /* idx = */ -1))
278 ERROR ("lua plugin: Write function \"%s\" (id %i) did not return a numeric value.",
279 cb->lua_function_name, cb->callback_id);
284 status = (int) lua_tointeger (l, /* idx = */ -1);
287 lua_pop (l, /* nelems = */ 1); /* -1 = 0 */
288 pthread_mutex_unlock (&cb->lock);
290 } /* }}} int clua_write */
292 /* Cleans up the stack, pushes the return value as a number onto the stack and
293 * returns the number of values returned (1). */
294 #define RETURN_LUA(l,status) do { \
295 lua_State *_l_state = (l); \
296 lua_settop (_l_state, 0); \
297 lua_pushnumber (_l_state, (lua_Number) (status)); \
304 static int lua_cb_log (lua_State *l) /* {{{ */
306 int nargs = lua_gettop (l); /* number of arguments */
312 WARNING ("lua plugin: collectd_log() called with an invalid number of arguments (%i).",
317 if (!lua_isnumber (l, 1))
319 WARNING ("lua plugin: The first argument to collectd_log() must be a number.");
323 if (!lua_isstring (l, 2))
325 WARNING ("lua plugin: The second argument to collectd_log() must be a string.");
329 severity = (int) lua_tonumber (l, /* stack pos = */ 1);
330 if ((severity != LOG_ERR)
331 && (severity != LOG_WARNING)
332 && (severity != LOG_NOTICE)
333 && (severity != LOG_INFO)
334 && (severity != LOG_DEBUG))
337 msg = lua_tostring (l, 2);
340 ERROR ("lua plugin: lua_tostring failed.");
344 plugin_log (severity, "%s", msg);
347 } /* }}} int lua_cb_log */
349 static int lua_cb_dispatch_values (lua_State *l) /* {{{ */
352 int nargs = lua_gettop (l); /* number of arguments */
353 char identifier[6 * DATA_MAX_NAME_LEN];
357 WARNING ("lua plugin: collectd_dispatch_values() called "
358 "with an invalid number of arguments (%i).", nargs);
362 if (!lua_istable (l, 1))
364 WARNING ("lua plugin: The first argument to collectd_dispatch_values() "
365 "must be a \"value list\" (i.e. a table).");
369 vl = luaC_tovaluelist (l, /* idx = */ -1);
372 WARNING ("lua plugin: luaC_tovaluelist failed.");
376 FORMAT_VL (identifier, sizeof (identifier), vl);
378 DEBUG ("lua plugin: collectd_dispatch_values: Received value list \"%s\", time %.3f, interval %.3f.",
379 identifier, CDTIME_T_TO_DOUBLE (vl->time), CDTIME_T_TO_DOUBLE (vl->interval));
384 } /* }}} lua_cb_dispatch_values */
386 static int lua_cb_register_read (lua_State *l) /* {{{ */
388 int nargs = lua_gettop (l); /* number of arguments */
389 clua_callback_data_t *cb;
394 char function_name[DATA_MAX_NAME_LEN] = "";
398 WARNING ("lua plugin: collectd_register_read() called with an invalid "
399 "number of arguments (%i).", nargs);
403 if (lua_isstring (l, /* stack pos = */ 1))
405 const char *tmp = lua_tostring (l, /* idx = */ 1);
406 ssnprintf (function_name, sizeof (function_name), "lua/%s", tmp);
409 callback_id = clua_store_callback (l, /* idx = */ 1);
412 ERROR ("lua plugin: Storing callback function failed.");
416 thread = lua_newthread (l);
419 ERROR ("lua plugin: lua_newthread failed.");
422 clua_store_thread (l, /* idx = */ -1, callback_id);
423 lua_pop (l, /* nelems = */ 1);
425 if (function_name[0] == 0)
426 ssnprintf (function_name, sizeof (function_name), "lua/callback_%i", callback_id);
428 cb = malloc (sizeof (*cb));
431 ERROR ("lua plugin: malloc failed.");
435 memset (cb, 0, sizeof (*cb));
436 cb->lua_state = thread;
437 cb->callback_id = callback_id;
438 cb->lua_function_name = strdup (function_name);
439 pthread_mutex_init (&cb->lock, /* attr = */ NULL);
442 ud.free_func = NULL; /* FIXME */
444 plugin_register_complex_read (/* group = */ "lua",
445 /* name = */ function_name,
446 /* callback = */ clua_read,
447 /* interval = */ NULL,
448 /* user_data = */ &ud);
450 DEBUG ("lua plugin: Successful call to lua_cb_register_read().");
453 } /* }}} int lua_cb_register_read */
455 static int lua_cb_register_write (lua_State *l) /* {{{ */
457 int nargs = lua_gettop (l); /* number of arguments */
458 clua_callback_data_t *cb;
463 char function_name[DATA_MAX_NAME_LEN] = "";
467 WARNING ("lua plugin: collectd_register_read() called with an invalid "
468 "number of arguments (%i).", nargs);
472 if (lua_isstring (l, /* stack pos = */ 1))
474 const char *tmp = lua_tostring (l, /* idx = */ 1);
475 ssnprintf (function_name, sizeof (function_name), "lua/%s", tmp);
478 callback_id = clua_store_callback (l, /* idx = */ 1);
481 ERROR ("lua plugin: Storing callback function failed.");
485 thread = lua_newthread (l);
488 ERROR ("lua plugin: lua_newthread failed.");
491 clua_store_thread (l, /* idx = */ -1, callback_id);
492 lua_pop (l, /* nelems = */ 1);
494 if (function_name[0] == 0)
495 ssnprintf (function_name, sizeof (function_name), "lua/callback_%i", callback_id);
497 cb = malloc (sizeof (*cb));
500 ERROR ("lua plugin: malloc failed.");
504 memset (cb, 0, sizeof (*cb));
505 cb->lua_state = thread;
506 cb->callback_id = callback_id;
507 cb->lua_function_name = strdup (function_name);
508 pthread_mutex_init (&cb->lock, /* attr = */ NULL);
511 ud.free_func = NULL; /* FIXME */
513 plugin_register_write (/* name = */ function_name,
514 /* callback = */ clua_write,
515 /* user_data = */ &ud);
517 DEBUG ("lua plugin: Successful call to lua_cb_register_write().");
520 } /* }}} int lua_cb_register_write */
522 static lua_c_functions_t lua_c_functions[] =
524 { "collectd_log", lua_cb_log },
525 { "collectd_dispatch_values", lua_cb_dispatch_values },
526 { "collectd_register_read", lua_cb_register_read },
527 { "collectd_register_write", lua_cb_register_write }
530 static void lua_script_free (lua_script_t *script) /* {{{ */
539 if (script->lua_state != NULL)
541 lua_close (script->lua_state);
542 script->lua_state = NULL;
545 sfree (script->script_path);
548 lua_script_free (next);
549 } /* }}} void lua_script_free */
551 static int lua_script_init (lua_script_t *script) /* {{{ */
555 memset (script, 0, sizeof (*script));
556 script->script_path = NULL;
559 /* initialize the lua context */
560 script->lua_state = lua_open();
561 if (script->lua_state == NULL)
563 ERROR ("lua plugin: lua_open failed.");
567 /* Open up all the standard Lua libraries. */
568 luaL_openlibs (script->lua_state);
570 /* Register all the functions we implement in C */
571 for (i = 0; i < STATIC_ARRAY_SIZE (lua_c_functions); i++)
572 lua_register (script->lua_state,
573 lua_c_functions[i].name, lua_c_functions[i].func);
576 } /* }}} int lua_script_init */
578 static int lua_script_load (const char *script_path) /* {{{ */
580 lua_script_t *script;
583 script = malloc (sizeof (*script));
586 ERROR ("lua plugin: malloc failed.");
590 status = lua_script_init (script);
593 lua_script_free (script);
597 script->script_path = strdup (script_path);
598 if (script->script_path == NULL)
600 ERROR ("lua plugin: strdup failed.");
601 lua_script_free (script);
605 status = luaL_loadfile (script->lua_state, script->script_path);
608 ERROR ("lua plugin: luaL_loadfile failed with status %i", status);
609 lua_script_free (script);
613 status = lua_pcall (script->lua_state,
615 /* nresults = */ LUA_MULTRET,
621 errmsg = lua_tostring (script->lua_state, /* stack pos = */ -1);
624 ERROR ("lua plugin: lua_pcall failed with status %i. "
625 "In addition, no error message could be retrieved from the stack.",
628 ERROR ("lua plugin: Executing script \"%s\" failed:\n%s",
629 script->script_path, errmsg);
631 lua_script_free (script);
635 /* Append this script to the global list of scripts. */
645 while (last->next != NULL)
652 } /* }}} int lua_script_load */
654 static int lua_config_base_path (const oconfig_item_t *ci) /* {{{ */
659 status = cf_util_get_string_buffer (ci, base_path, sizeof (base_path));
663 len = strlen (base_path);
664 while ((len > 0) && (base_path[len - 1] == '/'))
670 DEBUG ("lua plugin: base_path = \"%s\";", base_path);
673 } /* }}} int lua_config_base_path */
675 static int lua_config_script (const oconfig_item_t *ci) /* {{{ */
677 char rel_path[PATH_MAX + 1];
678 char abs_path[PATH_MAX + 1];
681 status = cf_util_get_string_buffer (ci, rel_path, sizeof (rel_path));
685 if (base_path[0] == 0)
686 sstrncpy (abs_path, rel_path, sizeof (abs_path));
688 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", base_path, rel_path);
690 DEBUG ("lua plugin: abs_path = \"%s\";", abs_path);
692 status = lua_script_load (abs_path);
696 INFO("lua plugin: File \"%s\" loaded succesfully", abs_path);
699 } /* }}} int lua_config_script */
704 * Script "script1.lua"
705 * Script "script2.lua"
708 static int lua_config (oconfig_item_t *ci) /* {{{ */
712 for (i = 0; i < ci->children_num; i++)
714 oconfig_item_t *child = ci->children + i;
716 if (strcasecmp ("BasePath", child->key) == 0) {
717 lua_config_base_path(child);
719 else if (strcasecmp ("Script", child->key) == 0){
720 lua_config_script(child);
724 WARNING ("network plugin: Option `%s' is not allowed here.",
730 } /* }}} int lua_config */
732 static int lua_shutdown (void) /* {{{ */
734 lua_script_free (scripts);
738 } /* }}} int lua_shutdown */
740 void module_register()
742 plugin_register_complex_config("lua", lua_config);
743 plugin_register_shutdown("lua", lua_shutdown);
746 /* vim: set sw=2 sts=2 et fdm=marker : */