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"
41 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG && defined(__GNUC__) && __GNUC__
43 # pragma GCC poison sprintf
46 typedef struct lua_script_s {
50 struct lua_script_s *next;
53 struct clua_read_function_s
56 char *lua_function_name;
58 typedef struct clua_read_function_s clua_read_function_t;
60 struct lua_c_functions_s
65 typedef struct lua_c_functions_s lua_c_functions_t;
67 static char base_path[PATH_MAX + 1] = "";
68 static lua_script_t *scripts = NULL;
70 static int clua_read (user_data_t *ud) /* {{{ */
72 clua_read_function_t *rf = ud->data;
75 /* Load the function to the stack */
76 lua_pushstring (rf->lua_state, rf->lua_function_name);
77 lua_gettable (rf->lua_state, LUA_REGISTRYINDEX);
79 if (!lua_isfunction (rf->lua_state, /* stack pos = */ -1))
81 ERROR ("lua plugin: Unable to lookup the read function \"%s\".",
82 rf->lua_function_name);
83 /* pop the value again */
84 lua_settop (rf->lua_state, -2);
88 lua_call (rf->lua_state, /* nargs = */ 0, /* nresults = */ 1);
90 if (lua_isnumber (rf->lua_state, -1))
92 status = (int) lua_tonumber (rf->lua_state, -1);
96 ERROR ("lua plugin: Read function \"%s\" did not return a numeric status.",
97 rf->lua_function_name);
101 lua_settop (rf->lua_state, -2);
104 } /* }}} int clua_read */
106 /* Cleans up the stack, pushes the return value as a number onto the stack and
107 * returns the number of values returned (1). */
108 #define RETURN_LUA(l,status) do { \
109 lua_State *_l_state = (l); \
110 lua_settop (_l_state, 0); \
111 lua_pushnumber (_l_state, (lua_Number) (status)); \
118 static int lua_cb_log (lua_State *l) /* {{{ */
120 int nargs = lua_gettop (l); /* number of arguments */
126 WARNING ("lua plugin: collectd_log() called with an invalid number of arguments (%i).",
131 if (!lua_isnumber (l, 1))
133 WARNING ("lua plugin: The first argument to collectd_log() must be a number.");
137 if (!lua_isstring (l, 2))
139 WARNING ("lua plugin: The second argument to collectd_log() must be a string.");
143 severity = (int) lua_tonumber (l, /* stack pos = */ 1);
144 if ((severity != LOG_ERR)
145 && (severity != LOG_WARNING)
146 && (severity != LOG_NOTICE)
147 && (severity != LOG_INFO)
148 && (severity != LOG_DEBUG))
151 msg = lua_tostring (l, 2);
154 ERROR ("lua plugin: lua_tostring failed.");
158 plugin_log (severity, "%s", msg);
161 } /* }}} int lua_cb_log */
163 static int lua_cb_dispatch_values (lua_State *l) /* {{{ */
166 int nargs = lua_gettop (l); /* number of arguments */
167 char identifier[6 * DATA_MAX_NAME_LEN];
171 WARNING ("lua plugin: collectd_dispatch_values() called "
172 "with an invalid number of arguments (%i).", nargs);
176 if (!lua_istable (l, 1))
178 WARNING ("lua plugin: The first argument to collectd_dispatch_values() "
179 "must be a \"value list\" (i.e. a table).");
183 vl = luaC_tovaluelist (l, /* idx = */ -1);
186 WARNING ("lua plugin: ltoc_value_list failed.");
190 FORMAT_VL (identifier, sizeof (identifier), vl);
192 DEBUG ("lua plugin: collectd_dispatch_values: Received value list \"%s\", time %.3f, interval %.3f.",
193 identifier, CDTIME_T_TO_DOUBLE (vl->time), CDTIME_T_TO_DOUBLE (vl->interval));
198 } /* }}} lua_cb_dispatch_values */
200 static int lua_cb_register_read (lua_State *l) /* {{{ */
202 static int count = 0;
203 int nargs = lua_gettop (l); /* number of arguments */
204 clua_read_function_t *rf;
206 int num = count++; /* XXX FIXME: Not thread-safe! */
207 char function_name[64];
211 WARNING ("lua plugin: collectd_register_read() called with an invalid "
212 "number of arguments (%i).", nargs);
216 /* If the argument is a string, assume it's a global function and try to look
218 if (lua_isstring (l, /* stack pos = */ 1))
219 lua_gettable (l, LUA_GLOBALSINDEX);
221 if (!lua_isfunction (l, /* stack pos = */ 1))
223 WARNING ("lua plugin: The first argument of collectd_register_read() "
224 "must be a function.");
228 ssnprintf (function_name, sizeof (function_name), "collectd.read_func_%i", num);
230 /* Push the name of the global variable */
231 lua_pushstring (l, function_name);
232 /* Push the name down to the first position */
234 /* Now set the global variable called "collectd". */
235 lua_settable (l, LUA_REGISTRYINDEX);
237 rf = malloc (sizeof (*rf));
240 ERROR ("lua plugin: malloc failed.");
245 user_data_t ud = { rf, /* free func */ NULL /* FIXME */ };
246 char cb_name[DATA_MAX_NAME_LEN];
248 memset (rf, 0, sizeof (*rf));
250 rf->lua_function_name = strdup (function_name);
252 ssnprintf (cb_name, sizeof (cb_name), "lua/read_func_%i", num);
254 plugin_register_complex_read (/* group = */ "lua",
255 /* name = */ cb_name,
256 /* callback = */ clua_read,
257 /* interval = */ NULL,
258 /* user_data = */ &ud);
261 DEBUG ("lua plugin: Successful call to lua_cb_register_read().");
264 } /* }}} int lua_cb_register_read */
266 static lua_c_functions_t lua_c_functions[] =
268 { "collectd_log", lua_cb_log },
269 { "collectd_dispatch_values", lua_cb_dispatch_values },
270 { "collectd_register_read", lua_cb_register_read }
273 static void lua_script_free (lua_script_t *script) /* {{{ */
282 if (script->lua_state != NULL)
284 lua_close (script->lua_state);
285 script->lua_state = NULL;
288 sfree (script->script_path);
291 lua_script_free (next);
292 } /* }}} void lua_script_free */
294 static int lua_script_init (lua_script_t *script) /* {{{ */
298 memset (script, 0, sizeof (*script));
299 script->script_path = NULL;
302 /* initialize the lua context */
303 script->lua_state = lua_open();
304 if (script->lua_state == NULL)
306 ERROR ("lua plugin: lua_open failed.");
310 /* Open up all the standard Lua libraries. */
311 luaL_openlibs (script->lua_state);
313 /* Register all the functions we implement in C */
314 for (i = 0; i < STATIC_ARRAY_SIZE (lua_c_functions); i++)
315 lua_register (script->lua_state,
316 lua_c_functions[i].name, lua_c_functions[i].func);
319 } /* }}} int lua_script_init */
321 static int lua_script_load (const char *script_path) /* {{{ */
323 lua_script_t *script;
326 script = malloc (sizeof (*script));
329 ERROR ("lua plugin: malloc failed.");
333 status = lua_script_init (script);
336 lua_script_free (script);
340 script->script_path = strdup (script_path);
341 if (script->script_path == NULL)
343 ERROR ("lua plugin: strdup failed.");
344 lua_script_free (script);
348 status = luaL_loadfile (script->lua_state, script->script_path);
351 ERROR ("lua plugin: luaL_loadfile failed with status %i", status);
352 lua_script_free (script);
356 status = lua_pcall (script->lua_state,
358 /* nresults = */ LUA_MULTRET,
364 errmsg = lua_tostring (script->lua_state, /* stack pos = */ -1);
367 ERROR ("lua plugin: lua_pcall failed with status %i. "
368 "In addition, no error message could be retrieved from the stack.",
371 ERROR ("lua plugin: Executing script \"%s\" failed:\n%s",
372 script->script_path, errmsg);
374 lua_script_free (script);
378 /* Append this script to the global list of scripts. */
388 while (last->next != NULL)
395 } /* }}} int lua_script_load */
397 static int lua_config_base_path (const oconfig_item_t *ci) /* {{{ */
402 status = cf_util_get_string_buffer (ci, base_path, sizeof (base_path));
406 len = strlen (base_path);
407 while ((len > 0) && (base_path[len - 1] == '/'))
413 DEBUG ("lua plugin: base_path = \"%s\";", base_path);
416 } /* }}} int lua_config_base_path */
418 static int lua_config_script (const oconfig_item_t *ci) /* {{{ */
420 char rel_path[PATH_MAX + 1];
421 char abs_path[PATH_MAX + 1];
424 status = cf_util_get_string_buffer (ci, rel_path, sizeof (rel_path));
428 if (base_path[0] == 0)
429 sstrncpy (abs_path, rel_path, sizeof (abs_path));
431 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", base_path, rel_path);
433 DEBUG ("lua plugin: abs_path = \"%s\";", abs_path);
435 status = lua_script_load (abs_path);
439 INFO("lua plugin: File \"%s\" loaded succesfully", abs_path);
442 } /* }}} int lua_config_script */
447 * Script "script1.lua"
448 * Script "script2.lua"
451 static int lua_config (oconfig_item_t *ci) /* {{{ */
455 for (i = 0; i < ci->children_num; i++)
457 oconfig_item_t *child = ci->children + i;
459 if (strcasecmp ("BasePath", child->key) == 0) {
460 lua_config_base_path(child);
462 else if (strcasecmp ("Script", child->key) == 0){
463 lua_config_script(child);
467 WARNING ("network plugin: Option `%s' is not allowed here.",
473 } /* }}} int lua_config */
475 static int lua_shutdown (void) /* {{{ */
477 lua_script_free (scripts);
481 } /* }}} int lua_shutdown */
483 void module_register()
485 plugin_register_complex_config("lua", lua_config);
486 plugin_register_shutdown("lua", lua_shutdown);
489 /* vim: set sw=2 sts=2 et fdm=marker : */