2 * collectd - src/utils_lua.c
3 * Copyright (C) 2010 Florian Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Florian Forster <octo at collectd.org>
28 #include "utils_lua.h"
30 static int ltoc_values(lua_State *L, /* {{{ */
31 const data_set_t *ds, value_t *ret_values) {
32 if (!lua_istable(L, -1)) {
33 WARNING("ltoc_values: not a table");
37 /* Push initial key */
38 lua_pushnil(L); /* +1 = 1 */
40 while (lua_next(L, -2) != 0) /* -1+2 = 2 || -1 = 0 */
42 if (i >= ds->ds_num) {
43 lua_pop(L, 2); /* -2 = 0 */
48 ret_values[i] = luaC_tovalue(L, -1, ds->ds[i].type);
51 lua_pop(L, 1); /* -1 = 1 */
53 } /* while (lua_next) */
55 if (i != ds->ds_num) {
56 WARNING("ltoc_values: invalid size for datasource \"%s\": expected %" PRIsz
58 ds->type, ds->ds_num, i);
63 } /* }}} int ltoc_values */
65 static int ltoc_table_values(lua_State *L, int idx, /* {{{ */
66 const data_set_t *ds, value_list_t *vl) {
67 /* We're only called from "luaC_tovaluelist", which ensures that "idx" is an
68 * absolute index (i.e. a positive number) */
71 lua_getfield(L, idx, "values");
72 if (!lua_istable(L, -1)) {
73 WARNING("utils_lua: ltoc_table_values: The \"values\" member is a %s "
74 "value, not a table.",
75 lua_typename(L, lua_type(L, -1)));
80 vl->values_len = ds->ds_num;
81 vl->values = calloc(vl->values_len, sizeof(*vl->values));
82 if (vl->values == NULL) {
83 ERROR("utils_lua: calloc failed.");
89 int status = ltoc_values(L, ds, vl->values);
99 } /* }}} int ltoc_table_values */
101 static int luaC_pushvalues(lua_State *L, const data_set_t *ds,
102 const value_list_t *vl) /* {{{ */
104 assert(vl->values_len == ds->ds_num);
107 for (size_t i = 0; i < vl->values_len; i++) {
108 lua_pushinteger(L, (lua_Integer)i + 1);
109 luaC_pushvalue(L, vl->values[i], ds->ds[i].type);
114 } /* }}} int luaC_pushvalues */
116 static int luaC_pushdstypes(lua_State *L, const data_set_t *ds) /* {{{ */
119 for (size_t i = 0; i < ds->ds_num; i++) {
120 lua_pushinteger(L, (lua_Integer)i);
121 lua_pushstring(L, DS_TYPE_TO_STRING(ds->ds[i].type));
126 } /* }}} int luaC_pushdstypes */
128 static int luaC_pushdsnames(lua_State *L, const data_set_t *ds) /* {{{ */
131 for (size_t i = 0; i < ds->ds_num; i++) {
132 lua_pushinteger(L, (lua_Integer)i);
133 lua_pushstring(L, ds->ds[i].name);
138 } /* }}} int luaC_pushdsnames */
143 cdtime_t luaC_tocdtime(lua_State *L, int idx) /* {{{ */
145 if (!lua_isnumber(L, /* stack pos = */ idx))
148 double d = lua_tonumber(L, idx);
150 return DOUBLE_TO_CDTIME_T(d);
151 } /* }}} int ltoc_table_cdtime */
153 int luaC_tostringbuffer(lua_State *L, int idx, /* {{{ */
154 char *buffer, size_t buffer_size) {
155 const char *str = lua_tostring(L, idx);
159 sstrncpy(buffer, str, buffer_size);
161 } /* }}} int luaC_tostringbuffer */
163 value_t luaC_tovalue(lua_State *L, int idx, int ds_type) /* {{{ */
167 if (!lua_isnumber(L, idx))
170 if (ds_type == DS_TYPE_GAUGE)
171 v.gauge = (gauge_t)lua_tonumber(L, /* stack pos = */ -1);
172 else if (ds_type == DS_TYPE_DERIVE)
173 v.derive = (derive_t)lua_tointeger(L, /* stack pos = */ -1);
174 else if (ds_type == DS_TYPE_COUNTER)
175 v.counter = (counter_t)lua_tointeger(L, /* stack pos = */ -1);
176 else if (ds_type == DS_TYPE_ABSOLUTE)
177 v.absolute = (absolute_t)lua_tointeger(L, /* stack pos = */ -1);
180 } /* }}} value_t luaC_tovalue */
182 value_list_t *luaC_tovaluelist(lua_State *L, int idx) /* {{{ */
185 int stack_top_before = lua_gettop(L);
188 /* Convert relative indexes to absolute indexes, so it doesn't change when we
189 * push / pop stuff. */
191 idx += lua_gettop(L) + 1;
193 /* Check that idx is in the valid range */
194 if ((idx < 1) || (idx > lua_gettop(L))) {
195 DEBUG("luaC_tovaluelist: idx(%d), top(%d)", idx, stack_top_before);
199 value_list_t *vl = calloc(1, sizeof(*vl));
201 DEBUG("luaC_tovaluelist: calloc failed");
205 /* Push initial key */
207 while (lua_next(L, idx) != 0) {
208 const char *key = lua_tostring(L, -2);
211 DEBUG("luaC_tovaluelist: Ignoring non-string key.");
212 } else if (strcasecmp("host", key) == 0)
213 luaC_tostringbuffer(L, -1, vl->host, sizeof(vl->host));
214 else if (strcasecmp("plugin", key) == 0)
215 luaC_tostringbuffer(L, -1, vl->plugin, sizeof(vl->plugin));
216 else if (strcasecmp("plugin_instance", key) == 0)
217 luaC_tostringbuffer(L, -1, vl->plugin_instance,
218 sizeof(vl->plugin_instance));
219 else if (strcasecmp("type", key) == 0)
220 luaC_tostringbuffer(L, -1, vl->type, sizeof(vl->type));
221 else if (strcasecmp("type_instance", key) == 0)
222 luaC_tostringbuffer(L, -1, vl->type_instance, sizeof(vl->type_instance));
223 else if (strcasecmp("time", key) == 0)
224 vl->time = luaC_tocdtime(L, -1);
225 else if (strcasecmp("interval", key) == 0)
226 vl->interval = luaC_tocdtime(L, -1);
227 else if (strcasecmp("values", key) == 0) {
228 /* This key is not handled here, because we have to assure "type" is read
231 DEBUG("luaC_tovaluelist: Ignoring unknown key \"%s\".", key);
238 const data_set_t *ds = plugin_get_ds(vl->type);
240 INFO("utils_lua: Unable to lookup type \"%s\".", vl->type);
245 int status = ltoc_table_values(L, idx, ds, vl);
247 WARNING("utils_lua: ltoc_table_values failed.");
253 assert(stack_top_before == lua_gettop(L));
256 } /* }}} value_list_t *luaC_tovaluelist */
258 int luaC_pushcdtime(lua_State *L, cdtime_t t) /* {{{ */
260 double d = CDTIME_T_TO_DOUBLE(t);
262 lua_pushnumber(L, (lua_Number)d);
264 } /* }}} int luaC_pushcdtime */
266 int luaC_pushvalue(lua_State *L, value_t v, int ds_type) /* {{{ */
268 if (ds_type == DS_TYPE_GAUGE)
269 lua_pushnumber(L, (lua_Number)v.gauge);
270 else if (ds_type == DS_TYPE_DERIVE)
271 lua_pushinteger(L, (lua_Integer)v.derive);
272 else if (ds_type == DS_TYPE_COUNTER)
273 lua_pushinteger(L, (lua_Integer)v.counter);
274 else if (ds_type == DS_TYPE_ABSOLUTE)
275 lua_pushinteger(L, (lua_Integer)v.absolute);
279 } /* }}} int luaC_pushvalue */
281 int luaC_pushvaluelist(lua_State *L, const data_set_t *ds,
282 const value_list_t *vl) /* {{{ */
286 lua_pushstring(L, vl->host);
287 lua_setfield(L, -2, "host");
289 lua_pushstring(L, vl->plugin);
290 lua_setfield(L, -2, "plugin");
291 lua_pushstring(L, vl->plugin_instance);
292 lua_setfield(L, -2, "plugin_instance");
294 lua_pushstring(L, vl->type);
295 lua_setfield(L, -2, "type");
296 lua_pushstring(L, vl->type_instance);
297 lua_setfield(L, -2, "type_instance");
299 luaC_pushvalues(L, ds, vl);
300 lua_setfield(L, -2, "values");
302 luaC_pushdstypes(L, ds);
303 lua_setfield(L, -2, "dstypes");
305 luaC_pushdsnames(L, ds);
306 lua_setfield(L, -2, "dsnames");
308 luaC_pushcdtime(L, vl->time);
309 lua_setfield(L, -2, "time");
311 luaC_pushcdtime(L, vl->interval);
312 lua_setfield(L, -2, "interval");
315 } /* }}} int luaC_pushvaluelist */