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_value (lua_State *l, /* {{{ */
115 int ds_type, value_t *ret_value)
117 if (!lua_isnumber (l, /* stack pos = */ -1))
120 if (ds_type == DS_TYPE_GAUGE)
121 ret_value->gauge = (gauge_t) lua_tonumber (l, /* stack pos = */ -1);
122 else if (ds_type == DS_TYPE_DERIVE)
123 ret_value->derive = (derive_t) lua_tointeger (l, /* stack pos = */ -1);
124 else if (ds_type == DS_TYPE_COUNTER)
125 ret_value->counter = (counter_t) lua_tointeger (l, /* stack pos = */ -1);
126 else if (ds_type == DS_TYPE_ABSOLUTE)
127 ret_value->absolute = (absolute_t) lua_tointeger (l, /* stack pos = */ -1);
132 } /* }}} int ltoc_value */
134 static int ltoc_values (lua_State *l, /* {{{ */
135 const data_set_t *ds,
141 if (!lua_istable (l, -1))
143 lua_pop (l, /* nelem = */ 1);
147 /* Push initial key */
149 for (i = 0; i < ((size_t) ds->ds_num); i++)
151 /* Pops old key and pushed new key and value. */
152 status = lua_next (l, -2);
153 if (status == 0) /* no more elements */
156 status = ltoc_value (l, ds->ds[i].type, ret_values + i);
159 DEBUG ("ltoc_value failed.");
164 lua_pop (l, /* nelems = */ 1);
166 /* Pop the key and the table itself */
167 lua_pop (l, /* nelems = */ 2);
170 } /* }}} int ltoc_values */
172 static int ltoc_table_values (lua_State *l, /* {{{ */
173 const data_set_t *ds, value_list_t *vl)
177 lua_pushstring (l, "values");
178 lua_gettable (l, -2);
180 if (!lua_istable (l, -1))
182 NOTICE ("lua plugin: ltoc_table_values: The \"values\" member is not a table.");
183 lua_pop (l, /* nelem = */ 1);
187 vl->values_len = ds->ds_num;
188 vl->values = calloc ((size_t) vl->values_len, sizeof (*vl->values));
189 if (vl->values == NULL)
191 ERROR ("lua plugin: calloc failed.");
193 lua_pop (l, /* nelem = */ 1);
197 status = ltoc_values (l, ds, vl->values);
199 lua_pop (l, /* nelem = */ 1);
208 } /* }}} int ltoc_table_values */
210 static int ltoc_string_buffer (lua_State *l, /* {{{ */
211 char *buffer, size_t buffer_size,
216 str = lua_tostring (l, /* stack pos = */ -1);
218 sstrncpy (buffer, str, buffer_size);
221 lua_pop (l, /* nelem = */ 1);
223 return ((str != NULL) ? 0 : -1);
224 } /* }}} int ltoc_string_buffer */
226 static int ltoc_table_string_buffer (lua_State *l, /* {{{ */
227 const char *table_key,
228 char *buffer, size_t buffer_size)
230 lua_pushstring (l, table_key);
231 lua_gettable (l, -2);
233 if (!lua_isstring (l, /* stack pos = */ -1))
235 lua_pop (l, /* nelem = */ 1);
239 return (ltoc_string_buffer (l, buffer, buffer_size, /* consume = */ 1));
240 } /* }}} int ltoc_table_string_buffer */
242 static int ltoc_table_cdtime (lua_State *l, /* {{{ */
243 const char *table_key, cdtime_t *ret_time)
247 lua_pushstring (l, table_key);
248 lua_gettable (l, -2);
250 if (!lua_isnumber (l, /* stack pos = */ -1))
252 lua_pop (l, /* nelem = */ 1);
256 d = (double) lua_tonumber (l, -1);
257 lua_pop (l, /* nelem = */ 1);
259 *ret_time = DOUBLE_TO_CDTIME_T (d);
261 } /* }}} int ltoc_table_cdtime */
263 static value_list_t *ltoc_value_list (lua_State *l) /* {{{ */
265 const data_set_t *ds;
269 vl = malloc (sizeof (*vl));
272 memset (vl, 0, sizeof (*vl));
276 #define COPY_FIELD(field,def) do { \
277 status = ltoc_table_string_buffer (l, #field, vl->field, sizeof (vl->field)); \
282 sstrncpy (vl->field, (def), sizeof (vl->field)); \
286 COPY_FIELD (host, hostname_g);
287 COPY_FIELD (plugin, NULL);
288 COPY_FIELD (plugin_instance, "");
289 COPY_FIELD (type, NULL);
290 COPY_FIELD (type_instance, "");
294 ds = plugin_get_ds (vl->type);
297 INFO ("lua plugin: Unable to lookup type \"%s\".", vl->type);
302 status = ltoc_table_cdtime (l, "time", &vl->time);
304 vl->time = cdtime ();
306 status = ltoc_table_cdtime (l, "interval", &vl->interval);
308 vl->interval = interval_g;
310 status = ltoc_table_values (l, ds, vl);
313 WARNING ("lua plugin: ltoc_table_values failed.");
319 } /* }}} value_list_t *ltoc_value_list */
324 static int lua_cb_log (lua_State *l) /* {{{ */
326 int nargs = lua_gettop (l); /* number of arguments */
332 WARNING ("lua plugin: collectd_log() called with an invalid number of arguments (%i).",
337 if (!lua_isnumber (l, 1))
339 WARNING ("lua plugin: The first argument to collectd_log() must be a number.");
343 if (!lua_isstring (l, 2))
345 WARNING ("lua plugin: The second argument to collectd_log() must be a string.");
349 severity = (int) lua_tonumber (l, /* stack pos = */ 1);
350 if ((severity != LOG_ERR)
351 && (severity != LOG_WARNING)
352 && (severity != LOG_NOTICE)
353 && (severity != LOG_INFO)
354 && (severity != LOG_DEBUG))
357 msg = lua_tostring (l, 2);
360 ERROR ("lua plugin: lua_tostring failed.");
364 plugin_log (severity, "%s", msg);
367 } /* }}} int lua_cb_log */
369 static int lua_cb_dispatch_values (lua_State *l) /* {{{ */
372 int nargs = lua_gettop (l); /* number of arguments */
373 char identifier[6 * DATA_MAX_NAME_LEN];
377 WARNING ("lua plugin: collectd_dispatch_values() called "
378 "with an invalid number of arguments (%i).", nargs);
382 if (!lua_istable (l, 1))
384 WARNING ("lua plugin: The first argument to collectd_dispatch_values() "
389 vl = ltoc_value_list (l);
392 WARNING ("lua plugin: ltoc_value_list failed.");
396 FORMAT_VL (identifier, sizeof (identifier), vl);
398 DEBUG ("lua plugin: collectd_dispatch_values: Received value list \"%s\", time %.3f, interval %.3f.",
399 identifier, CDTIME_T_TO_DOUBLE (vl->time), CDTIME_T_TO_DOUBLE (vl->interval));
404 } /* }}} lua_cb_dispatch_values */
406 static int lua_cb_register_read (lua_State *l) /* {{{ */
408 static int count = 0;
409 int nargs = lua_gettop (l); /* number of arguments */
410 clua_read_function_t *rf;
412 int num = count++; /* XXX FIXME: Not thread-safe! */
413 char function_name[64];
417 WARNING ("lua plugin: collectd_register_read() called with an invalid "
418 "number of arguments (%i).", nargs);
422 /* If the argument is a string, assume it's a global function and try to look
424 if (lua_isstring (l, /* stack pos = */ 1))
425 lua_gettable (l, LUA_GLOBALSINDEX);
427 if (!lua_isfunction (l, /* stack pos = */ 1))
429 WARNING ("lua plugin: The first argument of collectd_register_read() "
430 "must be a function.");
434 ssnprintf (function_name, sizeof (function_name), "collectd.read_func_%i", num);
436 /* Push the name of the global variable */
437 lua_pushstring (l, function_name);
438 /* Push the name down to the first position */
440 /* Now set the global variable called "collectd". */
441 lua_settable (l, LUA_REGISTRYINDEX);
443 rf = malloc (sizeof (*rf));
446 ERROR ("lua plugin: malloc failed.");
451 user_data_t ud = { rf, /* free func */ NULL /* FIXME */ };
452 char cb_name[DATA_MAX_NAME_LEN];
454 memset (rf, 0, sizeof (*rf));
456 rf->lua_function_name = strdup (function_name);
458 ssnprintf (cb_name, sizeof (cb_name), "lua/read_func_%i", num);
460 plugin_register_complex_read (/* group = */ "lua",
461 /* name = */ cb_name,
462 /* callback = */ clua_read,
463 /* interval = */ NULL,
464 /* user_data = */ &ud);
467 DEBUG ("lua plugin: Successful call to lua_cb_register_read().");
470 } /* }}} int lua_cb_register_read */
472 static lua_c_functions_t lua_c_functions[] =
474 { "collectd_log", lua_cb_log },
475 { "collectd_dispatch_values", lua_cb_dispatch_values },
476 { "collectd_register_read", lua_cb_register_read }
479 static void lua_script_free (lua_script_t *script) /* {{{ */
488 if (script->lua_state != NULL)
490 lua_close (script->lua_state);
491 script->lua_state = NULL;
494 sfree (script->script_path);
497 lua_script_free (next);
498 } /* }}} void lua_script_free */
500 static int lua_script_init (lua_script_t *script) /* {{{ */
504 memset (script, 0, sizeof (*script));
505 script->script_path = NULL;
508 /* initialize the lua context */
509 script->lua_state = lua_open();
510 if (script->lua_state == NULL)
512 ERROR ("lua plugin: lua_open failed.");
516 /* Open up all the standard Lua libraries. */
517 luaL_openlibs (script->lua_state);
519 /* Register all the functions we implement in C */
520 for (i = 0; i < STATIC_ARRAY_SIZE (lua_c_functions); i++)
521 lua_register (script->lua_state,
522 lua_c_functions[i].name, lua_c_functions[i].func);
525 } /* }}} int lua_script_init */
527 static int lua_script_load (const char *script_path) /* {{{ */
529 lua_script_t *script;
532 script = malloc (sizeof (*script));
535 ERROR ("lua plugin: malloc failed.");
539 status = lua_script_init (script);
542 lua_script_free (script);
546 script->script_path = strdup (script_path);
547 if (script->script_path == NULL)
549 ERROR ("lua plugin: strdup failed.");
550 lua_script_free (script);
554 status = luaL_loadfile (script->lua_state, script->script_path);
557 ERROR ("lua plugin: luaL_loadfile failed with status %i", status);
558 lua_script_free (script);
562 status = lua_pcall (script->lua_state,
564 /* nresults = */ LUA_MULTRET,
570 errmsg = lua_tostring (script->lua_state, /* stack pos = */ -1);
573 ERROR ("lua plugin: lua_pcall failed with status %i. "
574 "In addition, no error message could be retrieved from the stack.",
577 ERROR ("lua plugin: Executing script \"%s\" failed:\n%s",
578 script->script_path, errmsg);
580 lua_script_free (script);
584 /* Append this script to the global list of scripts. */
594 while (last->next != NULL)
601 } /* }}} int lua_script_load */
603 static int lua_config_base_path (const oconfig_item_t *ci) /* {{{ */
608 status = cf_util_get_string_buffer (ci, base_path, sizeof (base_path));
612 len = strlen (base_path);
613 while ((len > 0) && (base_path[len - 1] == '/'))
619 DEBUG ("lua plugin: base_path = \"%s\";", base_path);
622 } /* }}} int lua_config_base_path */
624 static int lua_config_script (const oconfig_item_t *ci) /* {{{ */
626 char rel_path[PATH_MAX + 1];
627 char abs_path[PATH_MAX + 1];
630 status = cf_util_get_string_buffer (ci, rel_path, sizeof (rel_path));
634 if (base_path[0] == 0)
635 sstrncpy (abs_path, rel_path, sizeof (abs_path));
637 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", base_path, rel_path);
639 DEBUG ("lua plugin: abs_path = \"%s\";", abs_path);
641 status = lua_script_load (abs_path);
645 INFO("lua plugin: File \"%s\" loaded succesfully", abs_path);
648 } /* }}} int lua_config_script */
653 * Script "script1.lua"
654 * Script "script2.lua"
657 static int lua_config (oconfig_item_t *ci) /* {{{ */
661 for (i = 0; i < ci->children_num; i++)
663 oconfig_item_t *child = ci->children + i;
665 if (strcasecmp ("BasePath", child->key) == 0) {
666 lua_config_base_path(child);
668 else if (strcasecmp ("Script", child->key) == 0){
669 lua_config_script(child);
673 WARNING ("network plugin: Option `%s' is not allowed here.",
679 } /* }}} int lua_config */
681 static int lua_shutdown (void) /* {{{ */
683 lua_script_free (scripts);
687 } /* }}} int lua_shutdown */
689 void module_register()
691 plugin_register_complex_config("lua", lua_config);
692 plugin_register_shutdown("lua", lua_shutdown);
695 /* vim: set sw=2 sts=2 et fdm=marker : */