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. */
40 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG && defined(__GNUC__) && __GNUC__
42 # pragma GCC poison sprintf
45 typedef struct lua_script_s {
49 struct lua_script_s *next;
52 struct clua_read_function_s
55 char *lua_function_name;
57 typedef struct clua_read_function_s clua_read_function_t;
59 struct lua_c_functions_s
64 typedef struct lua_c_functions_s lua_c_functions_t;
66 static char base_path[PATH_MAX + 1] = "";
67 static lua_script_t *scripts = NULL;
69 static int clua_read (user_data_t *ud) /* {{{ */
71 clua_read_function_t *rf = ud->data;
74 /* Load the function to the stack */
75 lua_pushstring (rf->lua_state, rf->lua_function_name);
76 lua_gettable (rf->lua_state, LUA_REGISTRYINDEX);
78 if (!lua_isfunction (rf->lua_state, /* stack pos = */ -1))
80 ERROR ("lua plugin: Unable to lookup the read function \"%s\".",
81 rf->lua_function_name);
82 /* pop the value again */
83 lua_settop (rf->lua_state, -2);
87 lua_call (rf->lua_state, /* nargs = */ 0, /* nresults = */ 1);
89 if (lua_isnumber (rf->lua_state, -1))
91 status = (int) lua_tonumber (rf->lua_state, -1);
95 ERROR ("lua plugin: Read function \"%s\" did not return a numeric status.",
96 rf->lua_function_name);
100 lua_settop (rf->lua_state, -2);
103 } /* }}} int clua_read */
105 /* Cleans up the stack, pushes the return value as a number onto the stack and
106 * returns the number of values returned (1). */
107 #define RETURN_LUA(l,status) do { \
108 lua_State *_l_state = (l); \
109 lua_settop (_l_state, 0); \
110 lua_pushnumber (_l_state, (lua_Number) (status)); \
117 static int lua_cb_log (lua_State *l) /* {{{ */
119 int nargs = lua_gettop (l); /* number of arguments */
125 WARNING ("lua plugin: collectd_log() called with an invalid number of arguments (%i).",
130 if (!lua_isnumber (l, 1))
132 WARNING ("lua plugin: The first argument to collectd_log() must be a number.");
136 if (!lua_isstring (l, 2))
138 WARNING ("lua plugin: The second argument to collectd_log() must be a string.");
142 severity = (int) lua_tonumber (l, /* stack pos = */ 1);
143 if ((severity != LOG_ERR)
144 && (severity != LOG_WARNING)
145 && (severity != LOG_NOTICE)
146 && (severity != LOG_INFO)
147 && (severity != LOG_DEBUG))
150 msg = lua_tostring (l, 2);
153 ERROR ("lua plugin: lua_tostring failed.");
157 plugin_log (severity, "%s", msg);
160 } /* }}} int lua_cb_log */
162 static int lua_cb_register_read (lua_State *l) /* {{{ */
164 static int count = 0;
165 int nargs = lua_gettop (l); /* number of arguments */
166 clua_read_function_t *rf;
168 int num = count++; /* XXX FIXME: Not thread-safe! */
169 char function_name[64];
173 WARNING ("lua plugin: collectd_register_read() called with an invalid "
174 "number of arguments (%i).", nargs);
178 /* If the argument is a string, assume it's a global function and try to look
180 if (lua_isstring (l, /* stack pos = */ 1))
181 lua_gettable (l, LUA_GLOBALSINDEX);
183 if (!lua_isfunction (l, /* stack pos = */ 1))
185 WARNING ("lua plugin: The first argument of collectd_register_read() "
186 "must be a function.");
190 ssnprintf (function_name, sizeof (function_name), "collectd.read_func_%i", num);
192 /* Push the name of the global variable */
193 lua_pushstring (l, function_name);
194 /* Push the name down to the first position */
196 /* Now set the global variable called "collectd". */
197 lua_settable (l, LUA_REGISTRYINDEX);
199 rf = malloc (sizeof (*rf));
202 ERROR ("lua plugin: malloc failed.");
207 user_data_t ud = { rf, /* free func */ NULL /* FIXME */ };
208 char cb_name[DATA_MAX_NAME_LEN];
210 memset (rf, 0, sizeof (*rf));
212 rf->lua_function_name = strdup (function_name);
214 ssnprintf (cb_name, sizeof (cb_name), "lua/read_func_%i", num);
216 plugin_register_complex_read (/* group = */ "lua",
217 /* name = */ cb_name,
218 /* callback = */ clua_read,
219 /* interval = */ NULL,
220 /* user_data = */ &ud);
223 DEBUG ("lua plugin: Successful call to lua_cb_register_read().");
226 } /* }}} int lua_cb_register_read */
228 static lua_c_functions_t lua_c_functions[] =
230 { "collectd_log", lua_cb_log },
231 { "collectd_register_read", lua_cb_register_read }
234 static void lua_script_free (lua_script_t *script) /* {{{ */
243 if (script->lua_state != NULL)
245 lua_close (script->lua_state);
246 script->lua_state = NULL;
249 sfree (script->script_path);
252 lua_script_free (next);
253 } /* }}} void lua_script_free */
255 static int lua_script_init (lua_script_t *script) /* {{{ */
259 memset (script, 0, sizeof (*script));
260 script->script_path = NULL;
263 /* initialize the lua context */
264 script->lua_state = lua_open();
265 if (script->lua_state == NULL)
267 ERROR ("lua plugin: lua_open failed.");
271 /* Open up all the standard Lua libraries. */
272 luaL_openlibs (script->lua_state);
274 /* Register all the functions we implement in C */
275 for (i = 0; i < STATIC_ARRAY_SIZE (lua_c_functions); i++)
276 lua_register (script->lua_state,
277 lua_c_functions[i].name, lua_c_functions[i].func);
280 } /* }}} int lua_script_init */
282 static int lua_script_load (const char *script_path) /* {{{ */
284 lua_script_t *script;
287 script = malloc (sizeof (*script));
290 ERROR ("lua plugin: malloc failed.");
294 status = lua_script_init (script);
297 lua_script_free (script);
301 script->script_path = strdup (script_path);
302 if (script->script_path == NULL)
304 ERROR ("lua plugin: strdup failed.");
305 lua_script_free (script);
309 status = luaL_dofile (script->lua_state, script->script_path);
316 case LUA_ERRSYNTAX: errmsg = "Syntax error"; break;
317 case LUA_ERRFILE: errmsg = "File I/O error"; break;
318 case LUA_ERRMEM: errmsg = "Memory allocation error"; break;
319 default: errmsg = "Unexpected error";
322 ERROR ("lua plugin: Loading script \"%s\" failed: %s",
323 script->script_path, errmsg);
325 lua_script_free (script);
329 /* Append this script to the global list of scripts. */
339 while (last->next != NULL)
346 } /* }}} int lua_script_load */
348 static int lua_config_base_path (const oconfig_item_t *ci) /* {{{ */
353 status = cf_util_get_string_buffer (ci, base_path, sizeof (base_path));
357 len = strlen (base_path);
358 while ((len > 0) && (base_path[len - 1] == '/'))
364 DEBUG ("lua plugin: base_path = \"%s\";", base_path);
367 } /* }}} int lua_config_base_path */
369 static int lua_config_script (const oconfig_item_t *ci) /* {{{ */
371 char rel_path[PATH_MAX + 1];
372 char abs_path[PATH_MAX + 1];
375 status = cf_util_get_string_buffer (ci, rel_path, sizeof (rel_path));
379 if (base_path[0] == 0)
380 sstrncpy (abs_path, rel_path, sizeof (abs_path));
382 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", base_path, rel_path);
384 DEBUG ("lua plugin: abs_path = \"%s\";", abs_path);
386 status = lua_script_load (abs_path);
390 INFO("lua plugin: File \"%s\" loaded succesfully", abs_path);
393 } /* }}} int lua_config_script */
398 * Script "script1.lua"
399 * Script "script2.lua"
402 static int lua_config (oconfig_item_t *ci) /* {{{ */
406 for (i = 0; i < ci->children_num; i++)
408 oconfig_item_t *child = ci->children + i;
410 if (strcasecmp ("BasePath", child->key) == 0) {
411 lua_config_base_path(child);
413 else if (strcasecmp ("Script", child->key) == 0){
414 lua_config_script(child);
418 WARNING ("network plugin: Option `%s' is not allowed here.",
424 } /* }}} int lua_config */
426 static int lua_shutdown (void) /* {{{ */
428 lua_script_free (scripts);
432 } /* }}} int lua_shutdown */
434 void module_register()
436 plugin_register_complex_config("lua", lua_config);
437 plugin_register_shutdown("lua", lua_shutdown);
440 /* vim: set sw=2 sts=2 et fdm=marker : */