/* * Lua bindings for RRDtool * * This software is licensed to the public under the Free Software * Foundation's GNU GPL, version 2 or later. You may obtain a copy * of the GPL by visiting the Free Software Foundations web site at * www.fsf.org, and a copy is included in this distribution. * * Copyright 2008 Fidelis Assis, all rights reserved. * */ #include #include #include #include #include #include #include #include #include #include #include "lua.h" #include "lauxlib.h" #include "lualib.h" #include "../../src/rrd_tool.h" #ifdef LUA50 #ifdef HAVE_COMPAT51 #include "compat-5.1.h" #else #include "compat-5.1r5/compat-5.1.h" #endif #endif extern void rrd_freemem(void *mem); extern int luaopen_rrd (lua_State * L); typedef int (*RRD_FUNCTION)(int, char **); typedef rrd_info_t *(RRD_FUNCTION_V)(int, char **); /**********************************************************/ static void reset_rrd_state(void) { optind = 0; opterr = 0; rrd_clear_error(); } static char **make_argv(const char *cmd, lua_State * L) { char **argv; int i; int argc = lua_gettop(L) + 1; if (!(argv = calloc(argc, sizeof (char *)))) /* raise an error and never return */ luaL_error(L, "Can't allocate memory for arguments array", cmd); /* fprintf(stderr, "Args:\n"); */ argv[0] = (char *) cmd; /* Dummy arg. Cast to (char *) because rrd */ /* functions don't expect (const * char) */ /* fprintf(stderr, "%s\n", argv[0]); */ for (i=1; ikey); switch (data->type) { case RD_I_CNT: if (isnan(data->value.u_val)) { lua_pushnil(L); } else { lua_pushnumber(L, (lua_Number) data->value.u_val); } lua_rawset(L, -3); break; case RD_I_VAL: lua_pushnumber(L, (lua_Number) data->value.u_val); lua_rawset(L, -3); break; case RD_I_STR: lua_pushstring(L, data->value.u_str); lua_rawset(L, -3); break; case RD_I_BLO: lua_pushlstring(L, (const char *) data->value.u_blo.ptr, data->value.u_blo.size); lua_rawset(L, -3); break; default: rrd_info_free(p); return luaL_error(L, "Wrong data type to info call"); break; } data = data->next; } rrd_info_free(p); return 1; } #endif /**********************************************************/ static int lua_rrd_create (lua_State * L) { rrd_common_call(L, "create", rrd_create); return 0; } static int lua_rrd_dump (lua_State * L) { rrd_common_call(L, "dump", rrd_dump); return 0; } static int lua_rrd_resize (lua_State * L) { rrd_common_call(L, "resize", rrd_resize); return 0; } #ifdef HAVE_RRD_RESTORE static int lua_rrd_restore (lua_State * L) { rrd_common_call(L, "restore", rrd_restore); return 0; } #endif static int lua_rrd_tune (lua_State * L) { rrd_common_call(L, "tune", rrd_tune); return 0; } static int lua_rrd_update (lua_State * L) { rrd_common_call(L, "update", rrd_update); return 0; } static int lua_rrd_fetch (lua_State * L) { int argc = lua_gettop(L) + 1; char **argv = make_argv("fetch", L); unsigned long i, j, step, ds_cnt; rrd_value_t *data, *p; char **names; time_t t, start, end; reset_rrd_state(); rrd_fetch(argc, argv, &start, &end, &step, &ds_cnt, &names, &data); free(argv); if (rrd_test_error()) luaL_error(L, rrd_get_error()); lua_pushnumber(L, (lua_Number) start); lua_pushnumber(L, (lua_Number) step); /* fprintf(stderr, "%lu, %lu, %lu, %lu\n", start, end, step, num_points); */ /* create the ds names array */ lua_newtable(L); for (i=0; i