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 static int clua_read (user_data_t *ud) /* {{{ */
142 clua_callback_data_t *cb = ud->data;
145 status = clua_load_callback (cb->lua_state, cb->callback_id);
148 ERROR ("lua plugin: Unable to load callback \"%s\" (id %i).",
149 cb->lua_function_name, cb->callback_id);
154 status = lua_pcall (cb->lua_state,
157 /* errfunc = */ 0); /* -1+1 = 1 */
160 const char *errmsg = lua_tostring (cb->lua_state, /* idx = */ -1);
162 ERROR ("lua plugin: Calling a read callback failed. "
163 "In addition, retrieving the error message failed.");
165 ERROR ("lua plugin: Calling a read callback failed: %s", errmsg);
166 lua_pop (cb->lua_state, /* nelems = */ 1); /* -1 = 0 */
170 if (!lua_isnumber (cb->lua_state, /* idx = */ -1))
172 ERROR ("lua plugin: Read function \"%s\" (id %i) did not return a numeric status.",
173 cb->lua_function_name, cb->callback_id);
178 status = (int) lua_tointeger (cb->lua_state, /* idx = */ -1);
181 /* pop return value and function */
182 lua_pop (cb->lua_state, /* nelems = */ 1); /* -1 = 0 */
185 } /* }}} int clua_read */
187 static int clua_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
190 clua_callback_data_t *cb = ud->data;
193 pthread_mutex_lock (&cb->lock);
195 status = clua_load_callback (cb->lua_state, cb->callback_id);
198 ERROR ("lua plugin: Unable to load callback \"%s\" (id %i).",
199 cb->lua_function_name, cb->callback_id);
200 pthread_mutex_unlock (&cb->lock);
205 status = luaC_pushvaluelist (cb->lua_state, ds, vl);
208 lua_pop (cb->lua_state, /* nelems = */ 1); /* -1 = 0 */
209 pthread_mutex_unlock (&cb->lock);
210 ERROR ("lua plugin: luaC_pushvaluelist failed.");
215 status = lua_pcall (cb->lua_state,
218 /* errfunc = */ 0); /* -2+1 = 1 */
221 const char *errmsg = lua_tostring (cb->lua_state, /* idx = */ -1);
223 ERROR ("lua plugin: Calling the write callback failed. "
224 "In addition, retrieving the error message failed.");
226 ERROR ("lua plugin: Calling the write callback failed:\n%s", errmsg);
227 lua_pop (cb->lua_state, /* nelems = */ 1); /* -1 = 0 */
228 pthread_mutex_unlock (&cb->lock);
232 if (!lua_isnumber (cb->lua_state, /* idx = */ -1))
234 ERROR ("lua plugin: Write function \"%s\" (id %i) did not return a numeric value.",
235 cb->lua_function_name, cb->callback_id);
240 status = (int) lua_tointeger (cb->lua_state, /* idx = */ -1);
243 lua_pop (cb->lua_state, /* nelems = */ 1); /* -1 = 0 */
244 pthread_mutex_unlock (&cb->lock);
246 } /* }}} int clua_write */
248 /* Cleans up the stack, pushes the return value as a number onto the stack and
249 * returns the number of values returned (1). */
250 #define RETURN_LUA(l,status) do { \
251 lua_State *_l_state = (l); \
252 lua_settop (_l_state, 0); \
253 lua_pushnumber (_l_state, (lua_Number) (status)); \
260 static int lua_cb_log (lua_State *l) /* {{{ */
262 int nargs = lua_gettop (l); /* number of arguments */
268 WARNING ("lua plugin: collectd_log() called with an invalid number of arguments (%i).",
273 if (!lua_isnumber (l, 1))
275 WARNING ("lua plugin: The first argument to collectd_log() must be a number.");
279 if (!lua_isstring (l, 2))
281 WARNING ("lua plugin: The second argument to collectd_log() must be a string.");
285 severity = (int) lua_tonumber (l, /* stack pos = */ 1);
286 if ((severity != LOG_ERR)
287 && (severity != LOG_WARNING)
288 && (severity != LOG_NOTICE)
289 && (severity != LOG_INFO)
290 && (severity != LOG_DEBUG))
293 msg = lua_tostring (l, 2);
296 ERROR ("lua plugin: lua_tostring failed.");
300 plugin_log (severity, "%s", msg);
303 } /* }}} int lua_cb_log */
305 static int lua_cb_dispatch_values (lua_State *l) /* {{{ */
308 int nargs = lua_gettop (l); /* number of arguments */
309 char identifier[6 * DATA_MAX_NAME_LEN];
313 WARNING ("lua plugin: collectd_dispatch_values() called "
314 "with an invalid number of arguments (%i).", nargs);
318 if (!lua_istable (l, 1))
320 WARNING ("lua plugin: The first argument to collectd_dispatch_values() "
321 "must be a \"value list\" (i.e. a table).");
325 vl = luaC_tovaluelist (l, /* idx = */ -1);
328 WARNING ("lua plugin: luaC_tovaluelist failed.");
332 FORMAT_VL (identifier, sizeof (identifier), vl);
334 DEBUG ("lua plugin: collectd_dispatch_values: Received value list \"%s\", time %.3f, interval %.3f.",
335 identifier, CDTIME_T_TO_DOUBLE (vl->time), CDTIME_T_TO_DOUBLE (vl->interval));
340 } /* }}} lua_cb_dispatch_values */
342 static int lua_cb_register_read (lua_State *l) /* {{{ */
344 int nargs = lua_gettop (l); /* number of arguments */
345 clua_callback_data_t *cb;
349 char function_name[DATA_MAX_NAME_LEN] = "";
353 WARNING ("lua plugin: collectd_register_read() called with an invalid "
354 "number of arguments (%i).", nargs);
358 if (lua_isstring (l, /* stack pos = */ 1))
360 const char *tmp = lua_tostring (l, /* idx = */ 1);
361 ssnprintf (function_name, sizeof (function_name), "lua/%s", tmp);
364 callback_id = clua_store_callback (l, /* idx = */ 1);
367 ERROR ("lua plugin: Storing callback function failed.");
371 if (function_name[0] == 0)
372 ssnprintf (function_name, sizeof (function_name), "lua/callback_%i", callback_id);
374 cb = malloc (sizeof (*cb));
377 ERROR ("lua plugin: malloc failed.");
381 memset (cb, 0, sizeof (*cb));
383 cb->callback_id = callback_id;
384 cb->lua_function_name = strdup (function_name);
387 ud.free_func = NULL; /* FIXME */
389 plugin_register_complex_read (/* group = */ "lua",
390 /* name = */ function_name,
391 /* callback = */ clua_read,
392 /* interval = */ NULL,
393 /* user_data = */ &ud);
395 DEBUG ("lua plugin: Successful call to lua_cb_register_read().");
398 } /* }}} int lua_cb_register_read */
400 static int lua_cb_register_write (lua_State *l) /* {{{ */
402 int nargs = lua_gettop (l); /* number of arguments */
403 clua_callback_data_t *cb;
407 char function_name[DATA_MAX_NAME_LEN] = "";
411 WARNING ("lua plugin: collectd_register_read() called with an invalid "
412 "number of arguments (%i).", nargs);
416 if (lua_isstring (l, /* stack pos = */ 1))
418 const char *tmp = lua_tostring (l, /* idx = */ 1);
419 ssnprintf (function_name, sizeof (function_name), "lua/%s", tmp);
422 callback_id = clua_store_callback (l, /* idx = */ 1);
425 ERROR ("lua plugin: Storing callback function failed.");
429 if (function_name[0] == 0)
430 ssnprintf (function_name, sizeof (function_name), "lua/callback_%i", callback_id);
432 cb = malloc (sizeof (*cb));
435 ERROR ("lua plugin: malloc failed.");
439 memset (cb, 0, sizeof (*cb));
441 cb->callback_id = callback_id;
442 cb->lua_function_name = strdup (function_name);
443 pthread_mutex_init (&cb->lock, /* attr = */ NULL);
446 ud.free_func = NULL; /* FIXME */
448 plugin_register_write (/* name = */ function_name,
449 /* callback = */ clua_write,
450 /* user_data = */ &ud);
452 DEBUG ("lua plugin: Successful call to lua_cb_register_write().");
455 } /* }}} int lua_cb_register_write */
457 static lua_c_functions_t lua_c_functions[] =
459 { "collectd_log", lua_cb_log },
460 { "collectd_dispatch_values", lua_cb_dispatch_values },
461 { "collectd_register_read", lua_cb_register_read },
462 { "collectd_register_write", lua_cb_register_write }
465 static void lua_script_free (lua_script_t *script) /* {{{ */
474 if (script->lua_state != NULL)
476 lua_close (script->lua_state);
477 script->lua_state = NULL;
480 sfree (script->script_path);
483 lua_script_free (next);
484 } /* }}} void lua_script_free */
486 static int lua_script_init (lua_script_t *script) /* {{{ */
490 memset (script, 0, sizeof (*script));
491 script->script_path = NULL;
494 /* initialize the lua context */
495 script->lua_state = lua_open();
496 if (script->lua_state == NULL)
498 ERROR ("lua plugin: lua_open failed.");
502 /* Open up all the standard Lua libraries. */
503 luaL_openlibs (script->lua_state);
505 /* Register all the functions we implement in C */
506 for (i = 0; i < STATIC_ARRAY_SIZE (lua_c_functions); i++)
507 lua_register (script->lua_state,
508 lua_c_functions[i].name, lua_c_functions[i].func);
511 } /* }}} int lua_script_init */
513 static int lua_script_load (const char *script_path) /* {{{ */
515 lua_script_t *script;
518 script = malloc (sizeof (*script));
521 ERROR ("lua plugin: malloc failed.");
525 status = lua_script_init (script);
528 lua_script_free (script);
532 script->script_path = strdup (script_path);
533 if (script->script_path == NULL)
535 ERROR ("lua plugin: strdup failed.");
536 lua_script_free (script);
540 status = luaL_loadfile (script->lua_state, script->script_path);
543 ERROR ("lua plugin: luaL_loadfile failed with status %i", status);
544 lua_script_free (script);
548 status = lua_pcall (script->lua_state,
550 /* nresults = */ LUA_MULTRET,
556 errmsg = lua_tostring (script->lua_state, /* stack pos = */ -1);
559 ERROR ("lua plugin: lua_pcall failed with status %i. "
560 "In addition, no error message could be retrieved from the stack.",
563 ERROR ("lua plugin: Executing script \"%s\" failed:\n%s",
564 script->script_path, errmsg);
566 lua_script_free (script);
570 /* Append this script to the global list of scripts. */
580 while (last->next != NULL)
587 } /* }}} int lua_script_load */
589 static int lua_config_base_path (const oconfig_item_t *ci) /* {{{ */
594 status = cf_util_get_string_buffer (ci, base_path, sizeof (base_path));
598 len = strlen (base_path);
599 while ((len > 0) && (base_path[len - 1] == '/'))
605 DEBUG ("lua plugin: base_path = \"%s\";", base_path);
608 } /* }}} int lua_config_base_path */
610 static int lua_config_script (const oconfig_item_t *ci) /* {{{ */
612 char rel_path[PATH_MAX + 1];
613 char abs_path[PATH_MAX + 1];
616 status = cf_util_get_string_buffer (ci, rel_path, sizeof (rel_path));
620 if (base_path[0] == 0)
621 sstrncpy (abs_path, rel_path, sizeof (abs_path));
623 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", base_path, rel_path);
625 DEBUG ("lua plugin: abs_path = \"%s\";", abs_path);
627 status = lua_script_load (abs_path);
631 INFO("lua plugin: File \"%s\" loaded succesfully", abs_path);
634 } /* }}} int lua_config_script */
639 * Script "script1.lua"
640 * Script "script2.lua"
643 static int lua_config (oconfig_item_t *ci) /* {{{ */
647 for (i = 0; i < ci->children_num; i++)
649 oconfig_item_t *child = ci->children + i;
651 if (strcasecmp ("BasePath", child->key) == 0) {
652 lua_config_base_path(child);
654 else if (strcasecmp ("Script", child->key) == 0){
655 lua_config_script(child);
659 WARNING ("network plugin: Option `%s' is not allowed here.",
665 } /* }}} int lua_config */
667 static int lua_shutdown (void) /* {{{ */
669 lua_script_free (scripts);
673 } /* }}} int lua_shutdown */
675 void module_register()
677 plugin_register_complex_config("lua", lua_config);
678 plugin_register_shutdown("lua", lua_shutdown);
681 /* vim: set sw=2 sts=2 et fdm=marker : */