3 * Copyright (C) 2010 Julien Ammous
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation; only version 2.1 of the License is
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "configfile.h"
27 #include "utils_cache.h"
29 /* Include the Lua API header files. */
34 typedef struct lua_script_s {
38 struct lua_script_s *next;
41 struct lua_c_functions_s
46 typedef struct lua_c_functions_s lua_c_functions_t;
48 static char base_path[PATH_MAX + 1] = "";
49 static lua_script_t *scripts = NULL;
51 /* Cleans up the stack, pushes the return value as a number onto the stack and
52 * returns the number of values returned (1). */
53 #define RETURN_LUA(l,status) do { \
54 lua_State *_l_state = (l); \
55 lua_settop (_l_state, 0); \
56 lua_pushnumber (_l_state, (lua_Number) (status)); \
63 static int lua_cb_log (lua_State *l) /* {{{ */
65 int nargs = lua_gettop (l); /* number of arguments */
71 WARNING ("lua plugin: collectd_log() called with an invalid number of arguments (%i).",
76 if (!lua_isnumber (l, 1))
78 WARNING ("lua plugin: The first argument to collectd_log() must be a number.");
82 if (!lua_isstring (l, 2))
84 WARNING ("lua plugin: The second argument to collectd_log() must be a string.");
88 severity = (int) lua_tonumber (l, /* stack pos = */ 1);
89 if ((severity != LOG_ERR)
90 && (severity != LOG_WARNING)
91 && (severity != LOG_NOTICE)
92 && (severity != LOG_INFO)
93 && (severity != LOG_DEBUG))
96 msg = lua_tostring (l, 2);
99 ERROR ("lua plugin: lua_tostring failed.");
103 plugin_log (severity, "%s", msg);
106 } /* }}} int lua_cb_log */
108 static lua_c_functions_t lua_c_functions[] =
110 { "collectd_log", lua_cb_log }
113 /* Declare the Lua libraries we wish to use.
114 * Note: If you are opening and running a file containing Lua code using
115 * 'lua_dofile(l, "myfile.lua") - you must delcare all the libraries used in
116 * that file here also. */
117 static const luaL_reg lua_load_libs[] =
119 { LUA_COLIBNAME, luaopen_base },
120 /* { "luaopen_loadlib", luaopen_loadlib }, */
122 { LUA_DBLIBNAME, luaopen_debug },
124 { LUA_TABLIBNAME, luaopen_table },
125 { LUA_IOLIBNAME, luaopen_io },
126 { LUA_STRLIBNAME, luaopen_string },
127 { LUA_MATHLIBNAME, luaopen_math }
130 static void lua_script_free (lua_script_t *script) /* {{{ */
139 if (script->lua_state != NULL)
141 lua_close (script->lua_state);
142 script->lua_state = NULL;
145 sfree (script->script_path);
148 lua_script_free (next);
149 } /* }}} void lua_script_free */
151 static int lua_script_init (lua_script_t *script) /* {{{ */
155 memset (script, 0, sizeof (*script));
156 script->script_path = NULL;
159 /* initialize the lua context */
160 script->lua_state = lua_open();
161 if (script->lua_state == NULL)
163 ERROR ("lua plugin: lua_open failed.");
167 /* Open up all the Lua libraries declared above. */
168 for (i = 0; i < STATIC_ARRAY_SIZE (lua_load_libs); i++)
172 status = (*lua_load_libs[i].func) (script->lua_state);
174 WARNING ("lua plugin: Loading library \"%s\" failed.",
175 lua_load_libs[i].name);
178 /* Register all the functions we implement in C */
179 for (i = 0; i < STATIC_ARRAY_SIZE (lua_c_functions); i++)
180 lua_register (script->lua_state,
181 lua_c_functions[i].name, lua_c_functions[i].func);
184 } /* }}} int lua_script_init */
186 static int lua_script_load (const char *script_path) /* {{{ */
188 lua_script_t *script;
191 script = malloc (sizeof (*script));
194 ERROR ("lua plugin: malloc failed.");
198 status = lua_script_init (script);
201 lua_script_free (script);
205 script->script_path = strdup (script_path);
206 if (script->script_path == NULL)
208 ERROR ("lua plugin: strdup failed.");
209 lua_script_free (script);
213 status = lua_dofile (script->lua_state, script->script_path);
220 case LUA_ERRSYNTAX: errmsg = "Syntax error"; break;
221 case LUA_ERRFILE: errmsg = "File I/O error"; break;
222 case LUA_ERRMEM: errmsg = "Memory allocation error"; break;
223 default: errmsg = "Unexpected error";
226 ERROR ("lua plugin: Loading script \"%s\" failed: %s",
227 script->script_path, errmsg);
229 lua_script_free (script);
233 /* Append this script to the global list of scripts. */
243 while (last->next != NULL)
250 } /* }}} int lua_script_load */
252 static int lua_config_base_path (const oconfig_item_t *ci) /* {{{ */
257 status = cf_util_get_string_buffer (ci, base_path, sizeof (base_path));
261 len = strlen (base_path);
262 while ((len > 0) && (base_path[len - 1] == '/'))
268 DEBUG ("lua plugin: base_path = \"%s\";", base_path);
271 } /* }}} int lua_config_base_path */
273 static int lua_config_script (const oconfig_item_t *ci) /* {{{ */
275 char rel_path[PATH_MAX + 1];
276 char abs_path[PATH_MAX + 1];
279 status = cf_util_get_string_buffer (ci, rel_path, sizeof (rel_path));
283 if (base_path[0] == 0)
284 sstrncpy (abs_path, rel_path, sizeof (abs_path));
286 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", base_path, rel_path);
288 DEBUG ("lua plugin: abs_path = \"%s\";", abs_path);
290 status = lua_script_load (abs_path);
294 INFO("lua plugin: File \"%s\" loaded succesfully", abs_path);
297 } /* }}} int lua_config_script */
302 * Script "script1.lua"
303 * Script "script2.lua"
306 static int lua_config (oconfig_item_t *ci) /* {{{ */
310 for (i = 0; i < ci->children_num; i++)
312 oconfig_item_t *child = ci->children + i;
314 if (strcasecmp ("BasePath", child->key) == 0) {
315 lua_config_base_path(child);
317 else if (strcasecmp ("Script", child->key) == 0){
318 lua_config_script(child);
322 WARNING ("network plugin: Option `%s' is not allowed here.",
328 } /* }}} int lua_config */
330 static int lua_shutdown (void) /* {{{ */
332 lua_script_free (scripts);
336 } /* }}} int lua_shutdown */
338 void module_register()
340 plugin_register_complex_config("lua", lua_config);
341 plugin_register_shutdown("lua", lua_shutdown);
344 /* vim: set sw=2 sts=2 et fdm=marker : */