2 * collectd - src/utils_lua.c
3 * Copyright (C) 2010 Florian Forster
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
20 * Florian Forster <octo at collectd.org>
23 #include "utils_lua.h"
26 static int ltoc_values (lua_State *l, /* {{{ */
33 if (!lua_istable (l, -1))
36 /* Push initial key */
38 for (i = 0; i < ((size_t) ds->ds_num); i++)
40 /* Pops old key and pushed new key and value. */
41 status = lua_next (l, -2);
42 if (status == 0) /* no more elements */
45 ret_values[i] = luaC_tovalue (l, /* idx = */ -1, ds->ds[i].type);
48 lua_pop (l, /* nelems = */ 1);
51 lua_pop (l, /* nelems = */ 1);
54 } /* }}} int ltoc_values */
56 static int ltoc_table_values (lua_State *l, int idx, /* {{{ */
57 const data_set_t *ds, value_list_t *vl)
61 /* We're only called from "luaC_tovaluelist", which ensures that "idx" is an
62 * absolute index (i.e. a positive number) */
65 lua_pushstring (l, "values");
66 lua_gettable (l, idx);
68 if (!lua_istable (l, -1))
70 NOTICE ("lua plugin: ltoc_table_values: The \"values\" member is not a table.");
71 lua_pop (l, /* nelem = */ 1);
75 vl->values_len = ds->ds_num;
76 vl->values = calloc ((size_t) vl->values_len, sizeof (*vl->values));
77 if (vl->values == NULL)
79 ERROR ("lua plugin: calloc failed.");
81 lua_pop (l, /* nelem = */ 1);
85 status = ltoc_values (l, ds, vl->values);
87 lua_pop (l, /* nelem = */ 1);
96 } /* }}} int ltoc_table_values */
101 cdtime_t luaC_tocdtime (lua_State *l, int idx) /* {{{ */
105 if (!lua_isnumber (l, /* stack pos = */ idx))
108 d = (double) lua_tonumber (l, idx);
110 return (DOUBLE_TO_CDTIME_T (d));
111 } /* }}} int ltoc_table_cdtime */
113 int luaC_tostringbuffer (lua_State *l, int idx, /* {{{ */
114 char *buffer, size_t buffer_size)
118 str = lua_tostring (l, idx);
122 sstrncpy (buffer, str, buffer_size);
124 } /* }}} int luaC_tostringbuffer */
126 value_t luaC_tovalue (lua_State *l, int idx, int ds_type) /* {{{ */
130 memset (&v, 0, sizeof (v));
132 if (!lua_isnumber (l, idx))
135 if (ds_type == DS_TYPE_GAUGE)
136 v.gauge = (gauge_t) lua_tonumber (l, /* stack pos = */ -1);
137 else if (ds_type == DS_TYPE_DERIVE)
138 v.derive = (derive_t) lua_tointeger (l, /* stack pos = */ -1);
139 else if (ds_type == DS_TYPE_COUNTER)
140 v.counter = (counter_t) lua_tointeger (l, /* stack pos = */ -1);
141 else if (ds_type == DS_TYPE_ABSOLUTE)
142 v.absolute = (absolute_t) lua_tointeger (l, /* stack pos = */ -1);
145 } /* }}} value_t luaC_tovalue */
147 value_list_t *luaC_tovaluelist (lua_State *l, int idx) /* {{{ */
149 const data_set_t *ds;
153 int stack_top_before = lua_gettop (l);
156 /* Convert relative indexes to absolute indexes, so it doesn't change when we
157 * push / pop stuff. */
159 idx += lua_gettop (l) + 1;
161 /* Check that idx is in the valid range */
162 if ((idx < 1) || (idx > lua_gettop (l)))
165 vl = malloc (sizeof (*vl));
168 memset (vl, 0, sizeof (*vl));
172 /* Push initial key */
174 while (lua_next (l, idx) != 0)
176 const char *key = lua_tostring (l, /* stack pos = */ -2);
180 DEBUG ("luaC_tovaluelist: Ignoring non-string key.");
182 else if (strcasecmp ("host", key) == 0)
183 luaC_tostringbuffer (l, /* idx = */ -1,
184 vl->host, sizeof (vl->host));
185 else if (strcasecmp ("plugin", key) == 0)
186 luaC_tostringbuffer (l, /* idx = */ -1,
187 vl->plugin, sizeof (vl->plugin));
188 else if (strcasecmp ("plugin_instance", key) == 0)
189 luaC_tostringbuffer (l, /* idx = */ -1,
190 vl->plugin_instance, sizeof (vl->plugin_instance));
191 else if (strcasecmp ("type", key) == 0)
192 luaC_tostringbuffer (l, /* idx = */ -1,
193 vl->type, sizeof (vl->type));
194 else if (strcasecmp ("type_instance", key) == 0)
195 luaC_tostringbuffer (l, /* idx = */ -1,
196 vl->type_instance, sizeof (vl->type_instance));
197 else if (strcasecmp ("time", key) == 0)
198 vl->time = luaC_tocdtime (l, -1);
199 else if (strcasecmp ("interval", key) == 0)
200 vl->interval = luaC_tocdtime (l, -1);
201 else if (strcasecmp ("values", key) == 0)
203 /* This key is not handled here, because we have to assure "type" is read
208 DEBUG ("luaC_tovaluelist: Ignoring unknown key \"%s\".", key);
217 ds = plugin_get_ds (vl->type);
220 INFO ("lua plugin: Unable to lookup type \"%s\".", vl->type);
225 status = ltoc_table_values (l, idx, ds, vl);
228 WARNING ("lua plugin: ltoc_table_values failed.");
234 assert (stack_top_before == lua_gettop (l));
237 } /* }}} value_list_t *luaC_tovaluelist */
239 /* vim: set sw=2 sts=2 et fdm=marker : */