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 lua_call (cb->lua_state, /* nargs = */ 0, /* nresults = */ 1); /* +1 */
156 if (lua_isnumber (cb->lua_state, /* idx = */ -1))
158 status = (int) lua_tointeger (cb->lua_state, /* idx = */ -1);
162 ERROR ("lua plugin: Read function \"%s\" (id %i) did not return a numeric status.",
163 cb->lua_function_name, cb->callback_id);
166 /* pop return value and function */
167 lua_settop (cb->lua_state, /* idx = */ -2); /* -2 */
170 } /* }}} int clua_read */
172 static int clua_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
175 clua_callback_data_t *cb = ud->data;
178 pthread_mutex_lock (&cb->lock);
180 status = clua_load_callback (cb->lua_state, cb->callback_id);
183 ERROR ("lua plugin: Unable to load callback \"%s\" (id %i).",
184 cb->lua_function_name, cb->callback_id);
185 pthread_mutex_unlock (&cb->lock);
190 status = luaC_pushvaluelist (cb->lua_state, ds, vl);
193 lua_pop (cb->lua_state, /* nelems = */ 1); /* -1 = 0 */
194 pthread_mutex_unlock (&cb->lock);
195 ERROR ("lua plugin: luaC_pushvaluelist failed.");
200 status = lua_pcall (cb->lua_state,
203 /* errfunc = */ 0); /* -2+1 = 1 */
206 const char *errmsg = lua_tostring (cb->lua_state, /* idx = */ -1);
208 ERROR ("lua plugin: Calling the write callback failed. "
209 "In addition, retrieving the error message failed.");
211 ERROR ("lua plugin: Calling the write callback failed:\n%s", errmsg);
212 lua_pop (cb->lua_state, /* nelems = */ 1); /* -1 = 0 */
213 pthread_mutex_unlock (&cb->lock);
217 if (!lua_isnumber (cb->lua_state, /* idx = */ -1))
219 ERROR ("lua plugin: Write function \"%s\" (id %i) did not return a numeric value.",
220 cb->lua_function_name, cb->callback_id);
225 status = (int) lua_tointeger (cb->lua_state, /* idx = */ -1);
228 lua_pop (cb->lua_state, /* nelems = */ 1); /* -1 = 0 */
229 pthread_mutex_unlock (&cb->lock);
231 } /* }}} int clua_write */
233 /* Cleans up the stack, pushes the return value as a number onto the stack and
234 * returns the number of values returned (1). */
235 #define RETURN_LUA(l,status) do { \
236 lua_State *_l_state = (l); \
237 lua_settop (_l_state, 0); \
238 lua_pushnumber (_l_state, (lua_Number) (status)); \
245 static int lua_cb_log (lua_State *l) /* {{{ */
247 int nargs = lua_gettop (l); /* number of arguments */
253 WARNING ("lua plugin: collectd_log() called with an invalid number of arguments (%i).",
258 if (!lua_isnumber (l, 1))
260 WARNING ("lua plugin: The first argument to collectd_log() must be a number.");
264 if (!lua_isstring (l, 2))
266 WARNING ("lua plugin: The second argument to collectd_log() must be a string.");
270 severity = (int) lua_tonumber (l, /* stack pos = */ 1);
271 if ((severity != LOG_ERR)
272 && (severity != LOG_WARNING)
273 && (severity != LOG_NOTICE)
274 && (severity != LOG_INFO)
275 && (severity != LOG_DEBUG))
278 msg = lua_tostring (l, 2);
281 ERROR ("lua plugin: lua_tostring failed.");
285 plugin_log (severity, "%s", msg);
288 } /* }}} int lua_cb_log */
290 static int lua_cb_dispatch_values (lua_State *l) /* {{{ */
293 int nargs = lua_gettop (l); /* number of arguments */
294 char identifier[6 * DATA_MAX_NAME_LEN];
298 WARNING ("lua plugin: collectd_dispatch_values() called "
299 "with an invalid number of arguments (%i).", nargs);
303 if (!lua_istable (l, 1))
305 WARNING ("lua plugin: The first argument to collectd_dispatch_values() "
306 "must be a \"value list\" (i.e. a table).");
310 vl = luaC_tovaluelist (l, /* idx = */ -1);
313 WARNING ("lua plugin: ltoc_value_list failed.");
317 FORMAT_VL (identifier, sizeof (identifier), vl);
319 DEBUG ("lua plugin: collectd_dispatch_values: Received value list \"%s\", time %.3f, interval %.3f.",
320 identifier, CDTIME_T_TO_DOUBLE (vl->time), CDTIME_T_TO_DOUBLE (vl->interval));
325 } /* }}} lua_cb_dispatch_values */
327 static int lua_cb_register_read (lua_State *l) /* {{{ */
329 int nargs = lua_gettop (l); /* number of arguments */
330 clua_callback_data_t *cb;
334 char function_name[DATA_MAX_NAME_LEN] = "";
338 WARNING ("lua plugin: collectd_register_read() called with an invalid "
339 "number of arguments (%i).", nargs);
343 if (lua_isstring (l, /* stack pos = */ 1))
345 const char *tmp = lua_tostring (l, /* idx = */ 1);
346 ssnprintf (function_name, sizeof (function_name), "lua/%s", tmp);
349 callback_id = clua_store_callback (l, /* idx = */ 1);
352 ERROR ("lua plugin: Storing callback function failed.");
356 if (function_name[0] == 0)
357 ssnprintf (function_name, sizeof (function_name), "lua/callback_%i", callback_id);
359 cb = malloc (sizeof (*cb));
362 ERROR ("lua plugin: malloc failed.");
366 memset (cb, 0, sizeof (*cb));
368 cb->callback_id = callback_id;
369 cb->lua_function_name = strdup (function_name);
372 ud.free_func = NULL; /* FIXME */
374 plugin_register_complex_read (/* group = */ "lua",
375 /* name = */ function_name,
376 /* callback = */ clua_read,
377 /* interval = */ NULL,
378 /* user_data = */ &ud);
380 DEBUG ("lua plugin: Successful call to lua_cb_register_read().");
383 } /* }}} int lua_cb_register_read */
385 static int lua_cb_register_write (lua_State *l) /* {{{ */
387 int nargs = lua_gettop (l); /* number of arguments */
388 clua_callback_data_t *cb;
392 char function_name[DATA_MAX_NAME_LEN] = "";
396 WARNING ("lua plugin: collectd_register_read() called with an invalid "
397 "number of arguments (%i).", nargs);
401 if (lua_isstring (l, /* stack pos = */ 1))
403 const char *tmp = lua_tostring (l, /* idx = */ 1);
404 ssnprintf (function_name, sizeof (function_name), "lua/%s", tmp);
407 callback_id = clua_store_callback (l, /* idx = */ 1);
410 ERROR ("lua plugin: Storing callback function failed.");
414 if (function_name[0] == 0)
415 ssnprintf (function_name, sizeof (function_name), "lua/callback_%i", callback_id);
417 cb = malloc (sizeof (*cb));
420 ERROR ("lua plugin: malloc failed.");
424 memset (cb, 0, sizeof (*cb));
426 cb->callback_id = callback_id;
427 cb->lua_function_name = strdup (function_name);
428 pthread_mutex_init (&cb->lock, /* attr = */ NULL);
431 ud.free_func = NULL; /* FIXME */
433 plugin_register_write (/* name = */ function_name,
434 /* callback = */ clua_write,
435 /* user_data = */ &ud);
437 DEBUG ("lua plugin: Successful call to lua_cb_register_write().");
440 } /* }}} int lua_cb_register_write */
442 static lua_c_functions_t lua_c_functions[] =
444 { "collectd_log", lua_cb_log },
445 { "collectd_dispatch_values", lua_cb_dispatch_values },
446 { "collectd_register_read", lua_cb_register_read },
447 { "collectd_register_write", lua_cb_register_write }
450 static void lua_script_free (lua_script_t *script) /* {{{ */
459 if (script->lua_state != NULL)
461 lua_close (script->lua_state);
462 script->lua_state = NULL;
465 sfree (script->script_path);
468 lua_script_free (next);
469 } /* }}} void lua_script_free */
471 static int lua_script_init (lua_script_t *script) /* {{{ */
475 memset (script, 0, sizeof (*script));
476 script->script_path = NULL;
479 /* initialize the lua context */
480 script->lua_state = lua_open();
481 if (script->lua_state == NULL)
483 ERROR ("lua plugin: lua_open failed.");
487 /* Open up all the standard Lua libraries. */
488 luaL_openlibs (script->lua_state);
490 /* Register all the functions we implement in C */
491 for (i = 0; i < STATIC_ARRAY_SIZE (lua_c_functions); i++)
492 lua_register (script->lua_state,
493 lua_c_functions[i].name, lua_c_functions[i].func);
496 } /* }}} int lua_script_init */
498 static int lua_script_load (const char *script_path) /* {{{ */
500 lua_script_t *script;
503 script = malloc (sizeof (*script));
506 ERROR ("lua plugin: malloc failed.");
510 status = lua_script_init (script);
513 lua_script_free (script);
517 script->script_path = strdup (script_path);
518 if (script->script_path == NULL)
520 ERROR ("lua plugin: strdup failed.");
521 lua_script_free (script);
525 status = luaL_loadfile (script->lua_state, script->script_path);
528 ERROR ("lua plugin: luaL_loadfile failed with status %i", status);
529 lua_script_free (script);
533 status = lua_pcall (script->lua_state,
535 /* nresults = */ LUA_MULTRET,
541 errmsg = lua_tostring (script->lua_state, /* stack pos = */ -1);
544 ERROR ("lua plugin: lua_pcall failed with status %i. "
545 "In addition, no error message could be retrieved from the stack.",
548 ERROR ("lua plugin: Executing script \"%s\" failed:\n%s",
549 script->script_path, errmsg);
551 lua_script_free (script);
555 /* Append this script to the global list of scripts. */
565 while (last->next != NULL)
572 } /* }}} int lua_script_load */
574 static int lua_config_base_path (const oconfig_item_t *ci) /* {{{ */
579 status = cf_util_get_string_buffer (ci, base_path, sizeof (base_path));
583 len = strlen (base_path);
584 while ((len > 0) && (base_path[len - 1] == '/'))
590 DEBUG ("lua plugin: base_path = \"%s\";", base_path);
593 } /* }}} int lua_config_base_path */
595 static int lua_config_script (const oconfig_item_t *ci) /* {{{ */
597 char rel_path[PATH_MAX + 1];
598 char abs_path[PATH_MAX + 1];
601 status = cf_util_get_string_buffer (ci, rel_path, sizeof (rel_path));
605 if (base_path[0] == 0)
606 sstrncpy (abs_path, rel_path, sizeof (abs_path));
608 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", base_path, rel_path);
610 DEBUG ("lua plugin: abs_path = \"%s\";", abs_path);
612 status = lua_script_load (abs_path);
616 INFO("lua plugin: File \"%s\" loaded succesfully", abs_path);
619 } /* }}} int lua_config_script */
624 * Script "script1.lua"
625 * Script "script2.lua"
628 static int lua_config (oconfig_item_t *ci) /* {{{ */
632 for (i = 0; i < ci->children_num; i++)
634 oconfig_item_t *child = ci->children + i;
636 if (strcasecmp ("BasePath", child->key) == 0) {
637 lua_config_base_path(child);
639 else if (strcasecmp ("Script", child->key) == 0){
640 lua_config_script(child);
644 WARNING ("network plugin: Option `%s' is not allowed here.",
650 } /* }}} int lua_config */
652 static int lua_shutdown (void) /* {{{ */
654 lua_script_free (scripts);
658 } /* }}} int lua_shutdown */
660 void module_register()
662 plugin_register_complex_config("lua", lua_config);
663 plugin_register_shutdown("lua", lua_shutdown);
666 /* vim: set sw=2 sts=2 et fdm=marker : */