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)); \
114 static int ltoc_string_buffer (lua_State *l, /* {{{ */
115 char *buffer, size_t buffer_size,
120 str = lua_tostring (l, /* stack pos = */ -1);
122 sstrncpy (buffer, str, buffer_size);
125 lua_pop (l, /* nelem = */ 1);
127 return ((str != NULL) ? 0 : -1);
128 } /* }}} int ltoc_string_buffer */
130 static int ltoc_table_string_buffer (lua_State *l, /* {{{ */
131 const char *table_key,
132 char *buffer, size_t buffer_size)
134 lua_pushstring (l, table_key);
135 lua_gettable (l, -2);
137 if (!lua_isstring (l, /* stack pos = */ -1))
139 lua_pop (l, /* nelem = */ 1);
143 return (ltoc_string_buffer (l, buffer, buffer_size, /* consume = */ 1));
144 } /* }}} int ltoc_table_string_buffer */
146 static int ltoc_table_cdtime (lua_State *l, /* {{{ */
147 const char *table_key, cdtime_t *ret_time)
151 lua_pushstring (l, table_key);
152 lua_gettable (l, -2);
154 if (!lua_isnumber (l, /* stack pos = */ -1))
156 lua_pop (l, /* nelem = */ 1);
160 d = (double) lua_tonumber (l, -1);
161 lua_pop (l, /* nelem = */ 1);
163 *ret_time = DOUBLE_TO_CDTIME_T (d);
165 } /* }}} int ltoc_table_cdtime */
167 static value_list_t *ltoc_value_list (lua_State *l) /* {{{ */
172 vl = malloc (sizeof (*vl));
175 memset (vl, 0, sizeof (*vl));
179 #define COPY_FIELD(field,def) do { \
180 status = ltoc_table_string_buffer (l, #field, vl->field, sizeof (vl->field)); \
185 sstrncpy (vl->field, (def), sizeof (vl->field)); \
189 COPY_FIELD (host, hostname_g);
190 COPY_FIELD (plugin, NULL);
191 COPY_FIELD (plugin_instance, "");
192 COPY_FIELD (type, NULL);
193 COPY_FIELD (type_instance, "");
197 status = ltoc_table_cdtime (l, "time", &vl->time);
199 vl->time = cdtime ();
201 status = ltoc_table_cdtime (l, "interval", &vl->interval);
203 vl->interval = interval_g;
208 } /* }}} value_list_t *ltoc_value_list */
213 static int lua_cb_log (lua_State *l) /* {{{ */
215 int nargs = lua_gettop (l); /* number of arguments */
221 WARNING ("lua plugin: collectd_log() called with an invalid number of arguments (%i).",
226 if (!lua_isnumber (l, 1))
228 WARNING ("lua plugin: The first argument to collectd_log() must be a number.");
232 if (!lua_isstring (l, 2))
234 WARNING ("lua plugin: The second argument to collectd_log() must be a string.");
238 severity = (int) lua_tonumber (l, /* stack pos = */ 1);
239 if ((severity != LOG_ERR)
240 && (severity != LOG_WARNING)
241 && (severity != LOG_NOTICE)
242 && (severity != LOG_INFO)
243 && (severity != LOG_DEBUG))
246 msg = lua_tostring (l, 2);
249 ERROR ("lua plugin: lua_tostring failed.");
253 plugin_log (severity, "%s", msg);
256 } /* }}} int lua_cb_log */
258 static int lua_cb_dispatch_values (lua_State *l) /* {{{ */
261 int nargs = lua_gettop (l); /* number of arguments */
262 char identifier[6 * DATA_MAX_NAME_LEN];
266 WARNING ("lua plugin: collectd_dispatch_values() called "
267 "with an invalid number of arguments (%i).", nargs);
271 if (!lua_istable (l, 1))
273 WARNING ("lua plugin: The first argument to collectd_dispatch_values() "
278 vl = ltoc_value_list (l);
281 WARNING ("lua plugin: ltoc_value_list failed.");
285 FORMAT_VL (identifier, sizeof (identifier), vl);
287 DEBUG ("lua plugin: collectd_dispatch_values: Received value list \"%s\", time %.3f, interval %.3f.",
288 identifier, CDTIME_T_TO_DOUBLE (vl->time), CDTIME_T_TO_DOUBLE (vl->interval));
292 } /* }}} lua_cb_dispatch_values */
294 static int lua_cb_register_read (lua_State *l) /* {{{ */
296 static int count = 0;
297 int nargs = lua_gettop (l); /* number of arguments */
298 clua_read_function_t *rf;
300 int num = count++; /* XXX FIXME: Not thread-safe! */
301 char function_name[64];
305 WARNING ("lua plugin: collectd_register_read() called with an invalid "
306 "number of arguments (%i).", nargs);
310 /* If the argument is a string, assume it's a global function and try to look
312 if (lua_isstring (l, /* stack pos = */ 1))
313 lua_gettable (l, LUA_GLOBALSINDEX);
315 if (!lua_isfunction (l, /* stack pos = */ 1))
317 WARNING ("lua plugin: The first argument of collectd_register_read() "
318 "must be a function.");
322 ssnprintf (function_name, sizeof (function_name), "collectd.read_func_%i", num);
324 /* Push the name of the global variable */
325 lua_pushstring (l, function_name);
326 /* Push the name down to the first position */
328 /* Now set the global variable called "collectd". */
329 lua_settable (l, LUA_REGISTRYINDEX);
331 rf = malloc (sizeof (*rf));
334 ERROR ("lua plugin: malloc failed.");
339 user_data_t ud = { rf, /* free func */ NULL /* FIXME */ };
340 char cb_name[DATA_MAX_NAME_LEN];
342 memset (rf, 0, sizeof (*rf));
344 rf->lua_function_name = strdup (function_name);
346 ssnprintf (cb_name, sizeof (cb_name), "lua/read_func_%i", num);
348 plugin_register_complex_read (/* group = */ "lua",
349 /* name = */ cb_name,
350 /* callback = */ clua_read,
351 /* interval = */ NULL,
352 /* user_data = */ &ud);
355 DEBUG ("lua plugin: Successful call to lua_cb_register_read().");
358 } /* }}} int lua_cb_register_read */
360 static lua_c_functions_t lua_c_functions[] =
362 { "collectd_log", lua_cb_log },
363 { "collectd_dispatch_values", lua_cb_dispatch_values },
364 { "collectd_register_read", lua_cb_register_read }
367 static void lua_script_free (lua_script_t *script) /* {{{ */
376 if (script->lua_state != NULL)
378 lua_close (script->lua_state);
379 script->lua_state = NULL;
382 sfree (script->script_path);
385 lua_script_free (next);
386 } /* }}} void lua_script_free */
388 static int lua_script_init (lua_script_t *script) /* {{{ */
392 memset (script, 0, sizeof (*script));
393 script->script_path = NULL;
396 /* initialize the lua context */
397 script->lua_state = lua_open();
398 if (script->lua_state == NULL)
400 ERROR ("lua plugin: lua_open failed.");
404 /* Open up all the standard Lua libraries. */
405 luaL_openlibs (script->lua_state);
407 /* Register all the functions we implement in C */
408 for (i = 0; i < STATIC_ARRAY_SIZE (lua_c_functions); i++)
409 lua_register (script->lua_state,
410 lua_c_functions[i].name, lua_c_functions[i].func);
413 } /* }}} int lua_script_init */
415 static int lua_script_load (const char *script_path) /* {{{ */
417 lua_script_t *script;
420 script = malloc (sizeof (*script));
423 ERROR ("lua plugin: malloc failed.");
427 status = lua_script_init (script);
430 lua_script_free (script);
434 script->script_path = strdup (script_path);
435 if (script->script_path == NULL)
437 ERROR ("lua plugin: strdup failed.");
438 lua_script_free (script);
442 status = luaL_loadfile (script->lua_state, script->script_path);
445 ERROR ("lua plugin: luaL_loadfile failed with status %i", status);
446 lua_script_free (script);
450 status = lua_pcall (script->lua_state,
452 /* nresults = */ LUA_MULTRET,
458 errmsg = lua_tostring (script->lua_state, /* stack pos = */ -1);
461 ERROR ("lua plugin: lua_pcall failed with status %i. "
462 "In addition, no error message could be retrieved from the stack.",
465 ERROR ("lua plugin: Executing script \"%s\" failed:\n%s",
466 script->script_path, errmsg);
468 lua_script_free (script);
472 /* Append this script to the global list of scripts. */
482 while (last->next != NULL)
489 } /* }}} int lua_script_load */
491 static int lua_config_base_path (const oconfig_item_t *ci) /* {{{ */
496 status = cf_util_get_string_buffer (ci, base_path, sizeof (base_path));
500 len = strlen (base_path);
501 while ((len > 0) && (base_path[len - 1] == '/'))
507 DEBUG ("lua plugin: base_path = \"%s\";", base_path);
510 } /* }}} int lua_config_base_path */
512 static int lua_config_script (const oconfig_item_t *ci) /* {{{ */
514 char rel_path[PATH_MAX + 1];
515 char abs_path[PATH_MAX + 1];
518 status = cf_util_get_string_buffer (ci, rel_path, sizeof (rel_path));
522 if (base_path[0] == 0)
523 sstrncpy (abs_path, rel_path, sizeof (abs_path));
525 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", base_path, rel_path);
527 DEBUG ("lua plugin: abs_path = \"%s\";", abs_path);
529 status = lua_script_load (abs_path);
533 INFO("lua plugin: File \"%s\" loaded succesfully", abs_path);
536 } /* }}} int lua_config_script */
541 * Script "script1.lua"
542 * Script "script2.lua"
545 static int lua_config (oconfig_item_t *ci) /* {{{ */
549 for (i = 0; i < ci->children_num; i++)
551 oconfig_item_t *child = ci->children + i;
553 if (strcasecmp ("BasePath", child->key) == 0) {
554 lua_config_base_path(child);
556 else if (strcasecmp ("Script", child->key) == 0){
557 lua_config_script(child);
561 WARNING ("network plugin: Option `%s' is not allowed here.",
567 } /* }}} int lua_config */
569 static int lua_shutdown (void) /* {{{ */
571 lua_script_free (scripts);
575 } /* }}} int lua_shutdown */
577 void module_register()
579 plugin_register_complex_config("lua", lua_config);
580 plugin_register_shutdown("lua", lua_shutdown);
583 /* vim: set sw=2 sts=2 et fdm=marker : */