2 * Lua bindings for RRDTool
4 * This software is licensed to the public under the Free Software
5 * Foundation's GNU GPL, version 2 or later. You may obtain a copy
6 * of the GPL by visiting the Free Software Foundations web site at
7 * www.fsf.org, and a copy is included in this distribution.
9 * Copyright 2008 Fidelis Assis, all rights reserved.
27 #include "../../src/rrd_tool.h"
31 #include "compat-5.1.h"
33 #include "compat-5.1r5/compat-5.1.h"
37 extern void rrd_freemem(void *mem);
39 extern int luaopen_rrd (lua_State * L);
40 typedef int (*RRD_FUNCTION)(int, char **);
41 typedef rrd_info_t *(RRD_FUNCTION_V)(int, char **);
43 /**********************************************************/
45 static void reset_rrd_state(void)
52 static char **make_argv(const char *cmd, lua_State * L)
56 int argc = lua_gettop(L) + 1;
58 if (!(argv = calloc(argc, sizeof (char *))))
59 /* raise an error and never return */
60 luaL_error(L, "Can't allocate memory for arguments array", cmd);
62 /* fprintf(stderr, "Args:\n"); */
63 argv[0] = (char *) cmd; /* Dummy arg. Cast to (char *) because rrd */
64 /* functions don't expect (const * char) */
65 /* fprintf(stderr, "%s\n", argv[0]); */
66 for (i=1; i<argc; i++) {
67 /* accepts string or number */
68 if (lua_isstring(L, i) || lua_isnumber(L, i)) {
69 if (!(argv[i] = strdup(lua_tostring (L, i)))) {
70 /* raise an error and never return */
71 luaL_error(L, "%s - error duplicating string area for arg #%d",
75 /* raise an error and never return */
76 luaL_error(L, "Invalid arg #%d to %s: args must be strings or numbers",
79 /* fprintf(stderr, "%s\n", argv[i]); */
85 rrd_common_call (lua_State *L, const char *cmd, RRD_FUNCTION rrd_function)
88 int argc = lua_gettop(L) + 1;
90 argv = make_argv(cmd, L);
92 rrd_function(argc, argv);
94 if (rrd_test_error()) luaL_error(L, rrd_get_error());
100 lua_rrd_infocall(lua_State *L, const char *cmd, RRD_FUNCTION_V rrd_function)
103 rrd_info_t *p, *data;
104 int argc = lua_gettop(L) + 1;
106 argv = make_argv(cmd, L);
108 data = rrd_function(argc, argv);
110 if (rrd_test_error()) luaL_error(L, rrd_get_error());
115 lua_pushstring(L, data->key);
116 switch (data->type) {
118 if (isnan(data->value.u_val)) {
121 lua_pushnumber(L, (lua_Number) data->value.u_val);
126 lua_pushnumber(L, (lua_Number) data->value.u_val);
130 lua_pushstring(L, data->value.u_str);
134 lua_pushlstring(L, (const char *) data->value.u_blo.ptr,
135 data->value.u_blo.size);
140 return luaL_error(L, "Wrong data type to info call");
150 /**********************************************************/
153 lua_rrd_create (lua_State * L)
155 rrd_common_call(L, "create", rrd_create);
160 lua_rrd_dump (lua_State * L)
162 rrd_common_call(L, "dump", rrd_dump);
167 lua_rrd_resize (lua_State * L)
169 rrd_common_call(L, "resize", rrd_resize);
174 lua_rrd_restore (lua_State * L)
176 rrd_common_call(L, "restore", rrd_restore);
181 lua_rrd_tune (lua_State * L)
183 rrd_common_call(L, "tune", rrd_tune);
188 lua_rrd_update (lua_State * L)
190 rrd_common_call(L, "update", rrd_update);
195 lua_rrd_fetch (lua_State * L)
197 int argc = lua_gettop(L) + 1;
198 char **argv = make_argv("fetch", L);
199 unsigned long i, j, step, ds_cnt;
200 rrd_value_t *data, *p;
202 time_t t, start, end;
205 rrd_fetch(argc, argv, &start, &end, &step, &ds_cnt, &names, &data);
207 if (rrd_test_error()) luaL_error(L, rrd_get_error());
209 lua_pushnumber(L, (lua_Number) start);
210 lua_pushnumber(L, (lua_Number) step);
211 /* fprintf(stderr, "%lu, %lu, %lu, %lu\n", start, end, step, num_points); */
213 /* create the ds names array */
215 for (i=0; i<ds_cnt; i++) {
216 lua_pushstring(L, names[i]);
217 lua_rawseti(L, -2, i+1);
218 rrd_freemem(names[i]);
222 /* create the data points array */
225 for (t=start, i=0; t<end; t+=step, i++) {
227 for (j=0; j<ds_cnt; j++) {
228 /*fprintf(stderr, "Point #%lu\n", j+1); */
229 lua_pushnumber(L, (lua_Number) *p++);
230 lua_rawseti(L, -2, j+1);
232 lua_rawseti(L, -2, i+1);
236 /* return the end as the last value */
237 lua_pushnumber(L, (lua_Number) end);
243 lua_rrd_first (lua_State * L)
246 int argc = lua_gettop(L) + 1;
247 char **argv = make_argv("first", L);
249 first = rrd_first(argc, argv);
251 if (rrd_test_error()) luaL_error(L, rrd_get_error());
252 lua_pushnumber(L, (lua_Number) first);
257 lua_rrd_last (lua_State * L)
260 int argc = lua_gettop(L) + 1;
261 char **argv = make_argv("last", L);
263 last = rrd_last(argc, argv);
265 if (rrd_test_error()) luaL_error(L, rrd_get_error());
266 lua_pushnumber(L, (lua_Number) last);
271 lua_rrd_graph (lua_State * L)
273 int argc = lua_gettop(L) + 1;
274 char **argv = make_argv("last", L);
280 rrd_graph(argc, argv, &calcpr, &xsize, &ysize, NULL, &ymin, &ymax);
282 if (rrd_test_error()) luaL_error(L, rrd_get_error());
283 lua_pushnumber(L, (lua_Number) xsize);
284 lua_pushnumber(L, (lua_Number) ysize);
286 for (i = 0; calcpr && calcpr[i]; i++) {
287 lua_pushstring(L, calcpr[i]);
288 lua_rawseti(L, -2, i+1);
289 rrd_freemem(calcpr[i]);
296 lua_rrd_flushcached(lua_State *L)
298 return rrd_common_call(L, "flushcached", rrd_flushcached);
303 lua_rrd_info (lua_State * L)
305 return lua_rrd_infocall(L, "info", rrd_info);
309 lua_rrd_graphv (lua_State * L)
311 return lua_rrd_infocall(L, "graphv", rrd_graph_v);
315 lua_rrd_updatev (lua_State * L)
317 return lua_rrd_infocall(L, "updatev", rrd_update_v);
321 /**********************************************************/
324 ** Assumes the table is on top of the stack.
327 set_info (lua_State * L)
329 lua_pushliteral (L, "_COPYRIGHT");
330 lua_pushliteral (L, "Copyright (C) 2008 Fidelis Assis");
331 lua_settable (L, -3);
332 lua_pushliteral (L, "_DESCRIPTION");
333 lua_pushliteral (L, "RRD-lua is a Lua binding for RRDTool.");
334 lua_settable (L, -3);
335 lua_pushliteral (L, "_NAME");
336 lua_pushliteral (L, "RRD-Lua");
337 lua_settable (L, -3);
338 lua_pushliteral (L, "_VERSION");
339 lua_pushliteral (L, LIB_VERSION);
340 lua_settable (L, -3);
343 /**********************************************************/
345 static const struct luaL_reg rrd[] = {
346 {"create", lua_rrd_create},
347 {"dump", lua_rrd_dump},
348 {"fetch", lua_rrd_fetch},
349 {"first", lua_rrd_first},
350 {"graph", lua_rrd_graph},
351 {"last", lua_rrd_last},
352 {"resize", lua_rrd_resize},
353 {"restore", lua_rrd_restore},
354 {"tune", lua_rrd_tune},
355 {"update", lua_rrd_update},
356 {"flushcached", lua_rrd_flushcached},
358 {"info", lua_rrd_info},
359 {"updatev", lua_rrd_updatev},
360 {"graphv", lua_rrd_graphv},
370 luaopen_rrd (lua_State * L)
373 /* luaL_module is defined in compat-5.1.c */
374 luaL_module (L, "rrd", rrd, 0);
376 luaL_register (L, "rrd", rrd);