added lua bindings

added lua bindings for the majority of sigar functions
  look at sigar-test.lua on how to use it
This commit is contained in:
Jan Kneschke 2009-08-09 15:50:36 +02:00
parent b3bfe99aea
commit 5d222575ef
15 changed files with 1807 additions and 0 deletions

1
bindings/Makefile.am Normal file
View File

@ -0,0 +1 @@
SUBDIRS=lua

31
bindings/lua/Makefile.am Normal file
View File

@ -0,0 +1,31 @@
INCLUDES=@INCLUDES@
lib_LTLIBRARIES = sigar.la
sigar_la_SOURCES =\
sigar.c \
sigar-cpu.c \
sigar-disk.c \
sigar-fs.c \
sigar-mem.c \
sigar-netif.c \
sigar-proc.c \
sigar-swap.c \
sigar-sysinfo.c \
sigar-version.c \
sigar-who.c
sigar_la_CPPFLAGS = ${LUA_CFLAGS}
sigar_la_LDFLAGS = $(AM_LDFLAGS) -module -avoid-version
sigar_la_LIBADD = $(top_builddir)/src/libsigar.la
noinst_HEADERS = lua-sigar.h
TESTS_ENVIRONMENT = \
LUA_PATH="${srcdir}/?.lua" \
LUA_CPATH="${builddir}/.libs/?.so" \
DYLD_LIBRARY_PATH="${top_builddir}/src/.libs/" \
lua
TESTS = \
sigar-test.lua

70
bindings/lua/lua-sigar.h Normal file
View File

@ -0,0 +1,70 @@
/**
* Copyright (c) 2009, Sun Microsystems Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of Sun Microsystems Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __LUA_SIGAR_H__
#define __LUA_SIGAR_H__
#include "sigar.h"
#define LUA_EXPORT_INT(x, y) \
if (x->y == SIGAR_FIELD_NOTIMPL) { \
lua_pushnil(L); \
} else { \
lua_pushnumber(L, x->y); \
} \
lua_setfield(L, -2, #y);
#define LUA_EXPORT_DOUBLE(x, y) \
lua_pushnumber(L, x->y); \
lua_setfield(L, -2, #y);
#define LUA_EXPORT_STR(x, y) \
lua_pushstring(L, x->y); \
lua_setfield(L, -2, #y);
#define LUA_EXPORT_ADDRESS(x, y) \
lua_sigar_push_address(L, &(x->y)); \
lua_setfield(L, -2, #y);
int lua_sigar_cpus_get(lua_State *L);
int lua_sigar_mem_get(lua_State *L);
int lua_sigar_swap_get(lua_State *L);
int lua_sigar_procs_get(lua_State *L);
int lua_sigar_proc_get(lua_State *L);
int lua_sigar_pid_get(lua_State *L);
int lua_sigar_fses_get(lua_State *L);
int lua_sigar_disk_get(lua_State *L);
int lua_sigar_disks_get(lua_State *L);
int lua_sigar_netifs_get(lua_State *L);
int lua_sigar_who_get(lua_State *L);
int lua_sigar_version_get(lua_State *L);
int lua_sigar_sysinfo_get(lua_State *L);
int lua_sigar_push_address(lua_State *L, sigar_net_address_t *addr);
#endif

183
bindings/lua/sigar-cpu.c Normal file
View File

@ -0,0 +1,183 @@
/**
* Copyright (c) 2009, Sun Microsystems Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of Sun Microsystems Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <string.h>
#include "sigar.h"
#include "lua-sigar.h"
typedef struct {
sigar_cpu_list_t data;
sigar_cpu_info_list_t info;
int ref_count;
sigar_t *sigar;
} lua_sigar_cpus_t;
typedef struct {
lua_sigar_cpus_t *cpus;
int ndx;
} lua_sigar_cpu_t;
static int lua_sigar_cpus_free(lua_State *L, lua_sigar_cpus_t *cpus);
static int lua_sigar_cpu_gc(lua_State *L) {
lua_sigar_cpu_t *cpu = (lua_sigar_cpu_t *)luaL_checkudata(L, 1, "sigar_cpu");
lua_sigar_cpus_free(L, cpu->cpus);
return 0;
}
static int lua_sigar_cpu_get_data(lua_State *L) {
lua_sigar_cpu_t *cpu = (lua_sigar_cpu_t *)luaL_checkudata(L, 1, "sigar_cpu");
lua_newtable(L);
#define DATA \
(&(cpu->cpus->data.data[cpu->ndx]))
LUA_EXPORT_INT(DATA, user);
LUA_EXPORT_INT(DATA, sys);
LUA_EXPORT_INT(DATA, idle);
LUA_EXPORT_INT(DATA, nice);
LUA_EXPORT_INT(DATA, wait);
LUA_EXPORT_INT(DATA, irq);
LUA_EXPORT_INT(DATA, soft_irq);
LUA_EXPORT_INT(DATA, stolen);
LUA_EXPORT_INT(DATA, total);
#undef DATA
return 1;
}
static int lua_sigar_cpu_get_info(lua_State *L) {
lua_sigar_cpu_t *cpu = (lua_sigar_cpu_t *)luaL_checkudata(L, 1, "sigar_cpu");
lua_newtable(L);
#define DATA \
(&(cpu->cpus->info.data[cpu->ndx]))
LUA_EXPORT_STR(DATA, vendor);
LUA_EXPORT_STR(DATA, model);
LUA_EXPORT_INT(DATA, mhz);
LUA_EXPORT_INT(DATA, total_sockets);
LUA_EXPORT_INT(DATA, total_cores);
LUA_EXPORT_INT(DATA, cores_per_socket);
LUA_EXPORT_INT(DATA, cache_size);
#undef DATA
return 1;
}
static int lua_sigar_cpus_get_cpu(lua_State *L) {
lua_sigar_cpus_t *cpus = (lua_sigar_cpus_t *)luaL_checkudata(L, 1, "sigar_cpus");
lua_sigar_cpu_t *cpu;
int ndx = luaL_checkint(L, 2);
if (ndx - 1 < 0 ||
ndx - 1 >= cpus->data.number) {
luaL_error(L, ".cpu[%d] out of range: 1..%d", ndx, cpus->data.number);
}
cpu = lua_newuserdata(L, sizeof(lua_sigar_cpu_t));
cpu->cpus = cpus;
cpu->ndx = ndx - 1;
cpus->ref_count++;
if (0 != luaL_newmetatable(L, "sigar_cpu")) {
lua_newtable(L);
lua_pushcfunction(L, lua_sigar_cpu_get_data);
lua_setfield(L, -2, "data");
lua_pushcfunction(L, lua_sigar_cpu_get_info);
lua_setfield(L, -2, "info");
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua_sigar_cpu_gc);
lua_setfield(L, -2, "__gc");
}
lua_setmetatable(L, -2);
return 1;
}
static int lua_sigar_cpus_free(lua_State *L, lua_sigar_cpus_t *cpus) {
if (--cpus->ref_count == 0) {
sigar_cpu_list_destroy(cpus->sigar, &(cpus->data));
sigar_cpu_info_list_destroy(cpus->sigar, &(cpus->info));
}
return 0;
}
static int lua_sigar_cpus_gc(lua_State *L) {
lua_sigar_cpus_t *cpus = (lua_sigar_cpus_t *)luaL_checkudata(L, 1, "sigar_cpus");
lua_sigar_cpus_free(L, cpus);
return 0;
}
static int lua_sigar_cpus_len(lua_State *L) {
lua_sigar_cpus_t *cpus = (lua_sigar_cpus_t *)luaL_checkudata(L, 1, "sigar_cpus");
lua_pushinteger(L, cpus->data.number);
return 1;
}
int lua_sigar_cpus_get(lua_State *L) {
sigar_t *s = *(sigar_t **)luaL_checkudata(L, 1, "sigar");
lua_sigar_cpus_t *cpus;
cpus = lua_newuserdata(L, sizeof(lua_sigar_cpus_t));
cpus->sigar = s;
sigar_cpu_list_get(s, &(cpus->data));
sigar_cpu_info_list_get(s, &(cpus->info));
cpus->ref_count = 1;
if (0 != luaL_newmetatable(L, "sigar_cpus")) {
lua_pushcfunction(L, lua_sigar_cpus_len);
lua_setfield(L, -2, "__len");
lua_pushcfunction(L, lua_sigar_cpus_get_cpu);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua_sigar_cpus_gc);
lua_setfield(L, -2, "__gc");
}
lua_setmetatable(L, -2);
return 1;
}

192
bindings/lua/sigar-disk.c Normal file
View File

@ -0,0 +1,192 @@
/**
* Copyright (c) 2009, Sun Microsystems Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of Sun Microsystems Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <string.h>
#include <stdlib.h>
#include "sigar.h"
#include "lua-sigar.h"
/* disks */
typedef struct {
sigar_file_system_list_t disks;
sigar_t *sigar;
int ref_count;
} lua_sigar_disks_t;
typedef struct {
char *dev_name;
sigar_t *sigar;
} lua_sigar_disk_t;
static int lua_sigar_disks_free(lua_State *L, lua_sigar_disks_t *disks) {
if (--disks->ref_count == 0) {
sigar_file_system_list_destroy(disks->sigar, &(disks->disks));
}
return 0;
}
static int lua_sigar_disks_gc(lua_State *L) {
lua_sigar_disks_t *disks = (lua_sigar_disks_t *)luaL_checkudata(L, 1, "sigar_disks");
lua_sigar_disks_free(L, disks);
return 0;
}
static int lua_sigar_disks_len(lua_State *L) {
lua_sigar_disks_t *disks = (lua_sigar_disks_t *)luaL_checkudata(L, 1, "sigar_disks");
lua_pushinteger(L, disks->disks.number);
return 1;
}
static int lua_sigar_disk_gc(lua_State *L) {
lua_sigar_disk_t *disk = (lua_sigar_disk_t *)luaL_checkudata(L, 1, "sigar_disk");
free(disk->dev_name);
return 0;
}
static int lua_sigar_disk_get_name(lua_State *L) {
lua_sigar_disk_t *disk = (lua_sigar_disk_t *)luaL_checkudata(L, 1, "sigar_disk");
lua_pushstring(L, disk->dev_name);
return 1;
}
static int lua_sigar_disk_get_usage(lua_State *L) {
lua_sigar_disk_t *disk = (lua_sigar_disk_t *)luaL_checkudata(L, 1, "sigar_disk");
sigar_disk_usage_t usage;
int err;
if (SIGAR_OK != (err = sigar_disk_usage_get(disk->sigar, disk->dev_name, &usage))) {
lua_pushnil(L);
lua_pushstring(L, strerror(err));
return 2;
}
lua_newtable(L);
#define DATA \
(&(usage))
LUA_EXPORT_INT(DATA, reads);
LUA_EXPORT_INT(DATA, writes);
LUA_EXPORT_INT(DATA, read_bytes);
LUA_EXPORT_INT(DATA, write_bytes);
LUA_EXPORT_INT(DATA, rtime);
LUA_EXPORT_INT(DATA, wtime);
LUA_EXPORT_INT(DATA, qtime);
LUA_EXPORT_INT(DATA, time);
LUA_EXPORT_INT(DATA, snaptime);
LUA_EXPORT_DOUBLE(DATA, service_time);
LUA_EXPORT_DOUBLE(DATA, queue);
#undef DATA
return 1;
}
static int lua_sigar_disk_set_metatable(lua_State *L, int ndx) {
if (0 != luaL_newmetatable(L, "sigar_disk")) {
lua_newtable(L);
lua_pushcfunction(L, lua_sigar_disk_get_name);
lua_setfield(L, -2, "name");
lua_pushcfunction(L, lua_sigar_disk_get_usage);
lua_setfield(L, -2, "usage");
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua_sigar_disk_gc);
lua_setfield(L, -2, "__gc");
}
lua_setmetatable(L, ndx - 1);
return 0;
}
static int lua_sigar_disks_get_disk(lua_State *L) {
lua_sigar_disks_t *disks = (lua_sigar_disks_t *)luaL_checkudata(L, 1, "sigar_disks");
lua_sigar_disk_t *disk;
int ndx = luaL_checkint(L, 2);
if (ndx - 1 < 0 ||
ndx - 1 >= disks->disks.number) {
luaL_error(L, ".disks[%d] out of range: 1..%d", ndx, disks->disks.number);
}
disk = lua_newuserdata(L, sizeof(*disk));
disk->dev_name = strdup(disks->disks.data[ndx - 1].dir_name);
disk->sigar = disks->sigar;
lua_sigar_disk_set_metatable(L, -1);
return 1;
}
int lua_sigar_disk_get(lua_State *L) {
sigar_t *s = *(sigar_t **)luaL_checkudata(L, 1, "sigar");
lua_sigar_disk_t *disk;
const char *key = luaL_checkstring(L, 2);
disk = lua_newuserdata(L, sizeof(*disk));
disk->dev_name = strdup(key);
disk->sigar = s;
lua_sigar_disk_set_metatable(L, -1);
return 1;
}
int lua_sigar_disks_get(lua_State *L) {
sigar_t *s = *(sigar_t **)luaL_checkudata(L, 1, "sigar");
lua_sigar_disks_t *disks;
disks = lua_newuserdata(L, sizeof(lua_sigar_disks_t));
disks->sigar = s;
sigar_file_system_list_get(s, &(disks->disks));
disks->ref_count = 1;
if (0 != luaL_newmetatable(L, "sigar_disks")) {
lua_pushcfunction(L, lua_sigar_disks_len);
lua_setfield(L, -2, "__len");
lua_pushcfunction(L, lua_sigar_disks_get_disk);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua_sigar_disks_gc);
lua_setfield(L, -2, "__gc");
}
lua_setmetatable(L, -2);
return 1;
}

186
bindings/lua/sigar-fs.c Normal file
View File

@ -0,0 +1,186 @@
/**
* Copyright (c) 2009, Sun Microsystems Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of Sun Microsystems Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <string.h>
#include "sigar.h"
#include "lua-sigar.h"
/* file-systems */
typedef struct {
sigar_file_system_list_t fses;
sigar_t *sigar;
int ref_count;
} lua_sigar_fses_t;
typedef struct {
lua_sigar_fses_t *fses;
int ndx;
} lua_sigar_fs_t;
static int lua_sigar_fses_free(lua_State *L, lua_sigar_fses_t *fses) {
if (--fses->ref_count == 0) {
sigar_file_system_list_destroy(fses->sigar, &(fses->fses));
}
return 0;
}
static int lua_sigar_fses_gc(lua_State *L) {
lua_sigar_fses_t *fses = (lua_sigar_fses_t *)luaL_checkudata(L, 1, "sigar_fses");
lua_sigar_fses_free(L, fses);
return 0;
}
static int lua_sigar_fses_len(lua_State *L) {
lua_sigar_fses_t *fses = (lua_sigar_fses_t *)luaL_checkudata(L, 1, "sigar_fses");
lua_pushinteger(L, fses->fses.number);
return 1;
}
static int lua_sigar_fs_gc(lua_State *L) {
lua_sigar_fs_t *fs = (lua_sigar_fs_t *)luaL_checkudata(L, 1, "sigar_fs");
lua_sigar_fses_free(L, fs->fses);
return 0;
}
static int lua_sigar_fs_get_info(lua_State *L) {
lua_sigar_fs_t *fs = (lua_sigar_fs_t *)luaL_checkudata(L, 1, "sigar_fs");
lua_newtable(L);
#define DATA \
(&(fs->fses->fses.data[fs->ndx]))
LUA_EXPORT_STR(DATA, dir_name);
LUA_EXPORT_STR(DATA, dev_name);
LUA_EXPORT_STR(DATA, type_name);
LUA_EXPORT_STR(DATA, sys_type_name);
LUA_EXPORT_STR(DATA, options);
#if 0
/* valgrind says: Conditional jump or move depends on uninitialised value
* looks like sigar isn't initializing it */
LUA_EXPORT_INT(DATA, flags);
#endif
#undef DATA
return 1;
}
static int lua_sigar_fs_get_usage(lua_State *L) {
lua_sigar_fs_t *fs = (lua_sigar_fs_t *)luaL_checkudata(L, 1, "sigar_fs");
const char *dir_name = fs->fses->fses.data[fs->ndx].dir_name;
sigar_file_system_usage_t usage;
int err;
if (SIGAR_OK != (err = sigar_file_system_usage_get(fs->fses->sigar, dir_name, &usage))) {
lua_pushnil(L);
lua_pushstring(L, strerror(err));
return 2;
}
lua_newtable(L);
#define DATA \
(&(usage))
LUA_EXPORT_INT(DATA, total);
LUA_EXPORT_INT(DATA, free);
LUA_EXPORT_INT(DATA, used);
LUA_EXPORT_INT(DATA, avail);
LUA_EXPORT_INT(DATA, files);
LUA_EXPORT_INT(DATA, free_files);
#undef DATA
return 1;
}
static int lua_sigar_fses_get_fs(lua_State *L) {
lua_sigar_fses_t *fses = (lua_sigar_fses_t *)luaL_checkudata(L, 1, "sigar_fses");
lua_sigar_fs_t *fs;
int ndx = luaL_checkint(L, 2);
if (ndx - 1 < 0 ||
ndx - 1 >= fses->fses.number) {
luaL_error(L, ".fses[%d] out of range: 1..%d", ndx, fses->fses.number);
}
fs = lua_newuserdata(L, sizeof(*fs));
fs->ndx = ndx - 1;
fs->fses = fses;
fses->ref_count++;
if (0 != luaL_newmetatable(L, "sigar_fs")) {
lua_newtable(L);
lua_pushcfunction(L, lua_sigar_fs_get_info);
lua_setfield(L, -2, "info");
lua_pushcfunction(L, lua_sigar_fs_get_usage);
lua_setfield(L, -2, "usage");
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua_sigar_fs_gc);
lua_setfield(L, -2, "__gc");
}
lua_setmetatable(L, -2);
return 1;
}
int lua_sigar_fses_get(lua_State *L) {
sigar_t *s = *(sigar_t **)luaL_checkudata(L, 1, "sigar");
lua_sigar_fses_t *fses;
fses = lua_newuserdata(L, sizeof(lua_sigar_fses_t));
fses->sigar = s;
sigar_file_system_list_get(s, &(fses->fses));
fses->ref_count = 1;
if (0 != luaL_newmetatable(L, "sigar_fses")) {
lua_pushcfunction(L, lua_sigar_fses_len);
lua_setfield(L, -2, "__len");
lua_pushcfunction(L, lua_sigar_fses_get_fs);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua_sigar_fses_gc);
lua_setfield(L, -2, "__gc");
}
lua_setmetatable(L, -2);
return 1;
}

62
bindings/lua/sigar-mem.c Normal file
View File

@ -0,0 +1,62 @@
/**
* Copyright (c) 2009, Sun Microsystems Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of Sun Microsystems Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <string.h>
#include "sigar.h"
#include "lua-sigar.h"
int lua_sigar_mem_get(lua_State *L) {
sigar_t *s = *(sigar_t **)luaL_checkudata(L, 1, "sigar");
sigar_mem_t mem;
sigar_mem_get(s, &mem);
lua_newtable(L);
#define DATA \
(&(mem))
LUA_EXPORT_INT(DATA, ram);
LUA_EXPORT_INT(DATA, total);
LUA_EXPORT_INT(DATA, used);
LUA_EXPORT_INT(DATA, free);
LUA_EXPORT_INT(DATA, actual_used);
LUA_EXPORT_INT(DATA, actual_free);
LUA_EXPORT_DOUBLE(DATA, used_percent);
LUA_EXPORT_DOUBLE(DATA, free_percent);
#undef DATA
return 1;
}

206
bindings/lua/sigar-netif.c Normal file
View File

@ -0,0 +1,206 @@
/**
* Copyright (c) 2009, Sun Microsystems Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of Sun Microsystems Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <string.h>
#include "sigar.h"
#include "lua-sigar.h"
/* network interfaces */
typedef struct {
sigar_net_interface_list_t netifs;
sigar_t *sigar;
int ref_count;
} lua_sigar_netifs_t;
typedef struct {
lua_sigar_netifs_t *netifs;
int ndx;
} lua_sigar_netif_t;
static int lua_sigar_netifs_free(lua_State *L, lua_sigar_netifs_t *netifs) {
if (--netifs->ref_count == 0) {
sigar_net_interface_list_destroy(netifs->sigar, &(netifs->netifs));
}
return 0;
}
static int lua_sigar_netifs_gc(lua_State *L) {
lua_sigar_netifs_t *netifs = (lua_sigar_netifs_t *)luaL_checkudata(L, 1, "sigar_netifs");
lua_sigar_netifs_free(L, netifs);
return 0;
}
static int lua_sigar_netifs_len(lua_State *L) {
lua_sigar_netifs_t *netifs = (lua_sigar_netifs_t *)luaL_checkudata(L, 1, "sigar_netifs");
lua_pushinteger(L, netifs->netifs.number);
return 1;
}
static int lua_sigar_netif_gc(lua_State *L) {
lua_sigar_netif_t *netif = (lua_sigar_netif_t *)luaL_checkudata(L, 1, "sigar_netif");
lua_sigar_netifs_free(L, netif->netifs);
return 0;
}
static int lua_sigar_netif_get_info(lua_State *L) {
lua_sigar_netif_t *netif = (lua_sigar_netif_t *)luaL_checkudata(L, 1, "sigar_netif");
int err;
const char *if_name = netif->netifs->netifs.data[netif->ndx];
sigar_net_interface_config_t usage;
if (SIGAR_OK != (err = sigar_net_interface_config_get(netif->netifs->sigar, if_name, &usage))) {
lua_pushnil(L);
lua_pushstring(L, strerror(err));
return 2;
}
lua_newtable(L);
#define DATA \
(&(usage))
LUA_EXPORT_STR(DATA, name);
LUA_EXPORT_STR(DATA, type);
LUA_EXPORT_ADDRESS(DATA, hwaddr);
LUA_EXPORT_ADDRESS(DATA, address);
LUA_EXPORT_ADDRESS(DATA, destination);
LUA_EXPORT_ADDRESS(DATA, broadcast);
LUA_EXPORT_ADDRESS(DATA, netmask);
LUA_EXPORT_INT(DATA, flags);
LUA_EXPORT_INT(DATA, mtu);
LUA_EXPORT_INT(DATA, metric);
#undef DATA
return 1;
}
static int lua_sigar_netif_get_usage(lua_State *L) {
lua_sigar_netif_t *netif = (lua_sigar_netif_t *)luaL_checkudata(L, 1, "sigar_netif");
int err;
const char *if_name = netif->netifs->netifs.data[netif->ndx];
sigar_net_interface_stat_t usage;
if (SIGAR_OK != (err = sigar_net_interface_stat_get(netif->netifs->sigar, if_name, &usage))) {
lua_pushnil(L);
lua_pushstring(L, strerror(err));
return 2;
}
lua_newtable(L);
#define DATA \
(&(usage))
LUA_EXPORT_INT(DATA, rx_packets);
LUA_EXPORT_INT(DATA, rx_bytes);
LUA_EXPORT_INT(DATA, rx_errors);
LUA_EXPORT_INT(DATA, rx_overruns);
LUA_EXPORT_INT(DATA, rx_dropped);
LUA_EXPORT_INT(DATA, rx_frame);
LUA_EXPORT_INT(DATA, tx_packets);
LUA_EXPORT_INT(DATA, tx_bytes);
LUA_EXPORT_INT(DATA, tx_errors);
LUA_EXPORT_INT(DATA, tx_overruns);
LUA_EXPORT_INT(DATA, tx_dropped);
LUA_EXPORT_INT(DATA, tx_collisions);
LUA_EXPORT_INT(DATA, tx_carrier);
LUA_EXPORT_INT(DATA, speed);
#undef DATA
return 1;
}
static int lua_sigar_netifs_get_netif(lua_State *L) {
lua_sigar_netifs_t *netifs = (lua_sigar_netifs_t *)luaL_checkudata(L, 1, "sigar_netifs");
lua_sigar_netif_t *netif;
int ndx = luaL_checkint(L, 2);
if (ndx - 1 < 0 ||
ndx - 1 >= netifs->netifs.number) {
luaL_error(L, ".netifs[%d] out of range: 1..%d", ndx, netifs->netifs.number);
}
netif = lua_newuserdata(L, sizeof(*netif));
netif->ndx = ndx - 1;
netif->netifs = netifs;
netifs->ref_count++;
if (0 != luaL_newmetatable(L, "sigar_netif")) {
lua_newtable(L);
lua_pushcfunction(L, lua_sigar_netif_get_info);
lua_setfield(L, -2, "info");
lua_pushcfunction(L, lua_sigar_netif_get_usage);
lua_setfield(L, -2, "usage");
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua_sigar_netif_gc);
lua_setfield(L, -2, "__gc");
}
lua_setmetatable(L, -2);
return 1;
}
int lua_sigar_netifs_get(lua_State *L) {
sigar_t *s = *(sigar_t **)luaL_checkudata(L, 1, "sigar");
lua_sigar_netifs_t *netifs;
netifs = lua_newuserdata(L, sizeof(lua_sigar_netifs_t));
netifs->sigar = s;
sigar_net_interface_list_get(s, &(netifs->netifs));
netifs->ref_count = 1;
if (0 != luaL_newmetatable(L, "sigar_netifs")) {
lua_pushcfunction(L, lua_sigar_netifs_len);
lua_setfield(L, -2, "__len");
lua_pushcfunction(L, lua_sigar_netifs_get_netif);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua_sigar_netifs_gc);
lua_setfield(L, -2, "__gc");
}
lua_setmetatable(L, -2);
return 1;
}

246
bindings/lua/sigar-proc.c Normal file
View File

@ -0,0 +1,246 @@
/**
* Copyright (c) 2009, Sun Microsystems Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of Sun Microsystems Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <string.h>
#include "sigar.h"
#include "lua-sigar.h"
typedef struct {
sigar_proc_list_t procs;
sigar_t *sigar;
} lua_sigar_procs_t;
static int lua_sigar_procs_gc(lua_State *L) {
lua_sigar_procs_t *procs = (lua_sigar_procs_t *)luaL_checkudata(L, 1, "sigar_procs");
sigar_proc_list_destroy(procs->sigar, &(procs->procs));
return 0;
}
static int lua_sigar_procs_len(lua_State *L) {
lua_sigar_procs_t *procs = (lua_sigar_procs_t *)luaL_checkudata(L, 1, "sigar_procs");
lua_pushinteger(L, procs->procs.number);
return 1;
}
static int lua_sigar_procs_get_pid(lua_State *L) {
lua_sigar_procs_t *procs = (lua_sigar_procs_t *)luaL_checkudata(L, 1, "sigar_procs");
int ndx = luaL_checkint(L, 2);
if (ndx - 1 < 0 ||
ndx - 1 >= procs->procs.number) {
luaL_error(L, ".procs[%d] out of range: 1..%d", ndx, procs->procs.number);
}
lua_pushinteger(L, procs->procs.data[ndx - 1]);
return 1;
}
int lua_sigar_procs_get(lua_State *L) {
sigar_t *s = *(sigar_t **)luaL_checkudata(L, 1, "sigar");
lua_sigar_procs_t *procs;
procs = lua_newuserdata(L, sizeof(lua_sigar_procs_t));
procs->sigar = s;
sigar_proc_list_get(s, &(procs->procs));
if (0 != luaL_newmetatable(L, "sigar_procs")) {
lua_pushcfunction(L, lua_sigar_procs_len);
lua_setfield(L, -2, "__len");
lua_pushcfunction(L, lua_sigar_procs_get_pid);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua_sigar_procs_gc);
lua_setfield(L, -2, "__gc");
}
lua_setmetatable(L, -2);
return 1;
}
typedef struct {
sigar_pid_t pid;
sigar_t *sigar;
} lua_sigar_proc_t;
static int lua_sigar_proc_gc(lua_State *L) {
lua_sigar_proc_t *proc = (lua_sigar_proc_t *)luaL_checkudata(L, 1, "sigar_proc");
return 0;
}
static int lua_sigar_proc_get_mem(lua_State *L) {
lua_sigar_proc_t *proc = (lua_sigar_proc_t *)luaL_checkudata(L, 1, "sigar_proc");
sigar_proc_mem_t mem;
int err;
if (SIGAR_OK != (err = sigar_proc_mem_get(proc->sigar, proc->pid, &mem))) {
lua_pushnil(L);
lua_pushstring(L, strerror(err));
return 2;
}
lua_newtable(L);
#define DATA \
(&(mem))
LUA_EXPORT_INT(DATA, size);
LUA_EXPORT_INT(DATA, resident);
LUA_EXPORT_INT(DATA, share);
LUA_EXPORT_INT(DATA, major_faults);
LUA_EXPORT_INT(DATA, minor_faults);
LUA_EXPORT_INT(DATA, page_faults);
#undef DATA
return 1;
}
static int lua_sigar_proc_get_time(lua_State *L) {
lua_sigar_proc_t *proc = (lua_sigar_proc_t *)luaL_checkudata(L, 1, "sigar_proc");
sigar_proc_time_t t;
int err;
if (SIGAR_OK != (err = sigar_proc_time_get(proc->sigar, proc->pid, &t))) {
lua_pushnil(L);
lua_pushstring(L, strerror(err));
return 2;
return 0;
}
lua_newtable(L);
#define DATA \
(&(t))
LUA_EXPORT_INT(DATA, start_time);
LUA_EXPORT_INT(DATA, user);
LUA_EXPORT_INT(DATA, sys);
LUA_EXPORT_INT(DATA, total);
#undef DATA
return 1;
}
static int lua_sigar_proc_get_state(lua_State *L) {
lua_sigar_proc_t *proc = (lua_sigar_proc_t *)luaL_checkudata(L, 1, "sigar_proc");
sigar_proc_state_t state;
int err;
if (SIGAR_OK != (err = sigar_proc_state_get(proc->sigar, proc->pid, &state))) {
lua_pushnil(L);
lua_pushstring(L, strerror(err));
return 2;
}
lua_newtable(L);
#define DATA \
(&(state))
LUA_EXPORT_STR(DATA, name);
LUA_EXPORT_INT(DATA, ppid);
LUA_EXPORT_INT(DATA, tty);
LUA_EXPORT_INT(DATA, priority);
LUA_EXPORT_INT(DATA, nice);
LUA_EXPORT_INT(DATA, processor);
LUA_EXPORT_INT(DATA, threads);
#undef DATA
return 1;
}
static int lua_sigar_proc_get_exe(lua_State *L) {
lua_sigar_proc_t *proc = (lua_sigar_proc_t *)luaL_checkudata(L, 1, "sigar_proc");
sigar_proc_exe_t t;
int err;
if (SIGAR_OK != (err = sigar_proc_exe_get(proc->sigar, proc->pid, &t))) {
lua_pushnil(L);
lua_pushstring(L, strerror(err));
return 2;
}
lua_newtable(L);
#define DATA \
(&(t))
LUA_EXPORT_STR(DATA, name);
LUA_EXPORT_STR(DATA, cwd);
LUA_EXPORT_STR(DATA, root);
#undef DATA
return 1;
}
int lua_sigar_proc_get(lua_State *L) {
sigar_t *s = *(sigar_t **)luaL_checkudata(L, 1, "sigar");
sigar_pid_t pid = luaL_checknumber(L, 2);
lua_sigar_proc_t *proc;
proc = lua_newuserdata(L, sizeof(lua_sigar_proc_t));
proc->sigar = s;
proc->pid = pid;
if (0 != luaL_newmetatable(L, "sigar_proc")) {
lua_newtable(L);
lua_pushcfunction(L, lua_sigar_proc_get_mem);
lua_setfield(L, -2, "mem");
lua_pushcfunction(L, lua_sigar_proc_get_time);
lua_setfield(L, -2, "time");
lua_pushcfunction(L, lua_sigar_proc_get_exe);
lua_setfield(L, -2, "exe");
lua_pushcfunction(L, lua_sigar_proc_get_state);
lua_setfield(L, -2, "state");
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua_sigar_proc_gc);
lua_setfield(L, -2, "__gc");
}
lua_setmetatable(L, -2);
return 1;
}
int lua_sigar_pid_get(lua_State *L) {
sigar_t *s = *(sigar_t **)luaL_checkudata(L, 1, "sigar");
lua_pushnumber(L, sigar_pid_get(s));
return 1;
}

59
bindings/lua/sigar-swap.c Normal file
View File

@ -0,0 +1,59 @@
/**
* Copyright (c) 2009, Sun Microsystems Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of Sun Microsystems Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <string.h>
#include "sigar.h"
#include "lua-sigar.h"
int lua_sigar_swap_get(lua_State *L) {
sigar_t *s = *(sigar_t **)luaL_checkudata(L, 1, "sigar");
sigar_swap_t swap;
sigar_swap_get(s, &swap);
lua_newtable(L);
#define DATA \
(&(swap))
LUA_EXPORT_INT(DATA, total);
LUA_EXPORT_INT(DATA, used);
LUA_EXPORT_INT(DATA, free);
LUA_EXPORT_INT(DATA, page_in);
LUA_EXPORT_INT(DATA, page_out);
#undef DATA
return 1;
}

View File

@ -0,0 +1,65 @@
/**
* Copyright (c) 2009, Sun Microsystems Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of Sun Microsystems Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <string.h>
#include <stdlib.h>
#include "sigar.h"
#include "lua-sigar.h"
int lua_sigar_sysinfo_get(lua_State *L) {
sigar_t *s = *(sigar_t **)luaL_checkudata(L, 1, "sigar");
sigar_sys_info_t sysinfo;
sigar_sys_info_get(s, &sysinfo);
lua_newtable(L);
#define DATA \
(&(sysinfo))
LUA_EXPORT_STR(DATA, name);
LUA_EXPORT_STR(DATA, version);
LUA_EXPORT_STR(DATA, arch);
LUA_EXPORT_STR(DATA, machine);
LUA_EXPORT_STR(DATA, description);
LUA_EXPORT_STR(DATA, patch_level);
LUA_EXPORT_STR(DATA, vendor);
LUA_EXPORT_STR(DATA, vendor_version);
LUA_EXPORT_STR(DATA, vendor_name);
LUA_EXPORT_STR(DATA, vendor_code_name);
#undef DATA
return 1;
}

153
bindings/lua/sigar-test.lua Normal file
View File

@ -0,0 +1,153 @@
--[[
-- Copyright (c) 2009, Sun Microsystems Inc.
-- All rights reserved.
--
-- Redistribution and use in source and binary forms, with or without modification,
-- are permitted provided that the following conditions are met:
--
-- * Redistributions of source code must retain the above copyright notice,
-- this list of conditions and the following disclaimer.
-- * Redistributions in binary form must reproduce the above copyright notice,
-- this list of conditions and the following disclaimer in the documentation
-- and/or other materials provided with the distribution.
-- * Neither the name of Sun Microsystems Inc. nor the names of its contributors
-- may be used to endorse or promote products derived from this software
-- without specific prior written permission.
--
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
-- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
-- BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-- OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
-- EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--]]
local sigar = assert(require("sigar"))
-- test if the GC is called nicely
local s = assert(sigar.new())
s = nil
function print_table(t)
for k, v in pairs(t) do
print((" %s: %s"):format(k, v))
end
end
local s = sigar.new()
print("-- cpus")
local cpus = s:cpus()
for i = 1, #cpus do
local cpu = cpus[i]
print(i)
print(" info")
local info = cpu:info()
print_table(info)
local data = cpu:data()
print(" data")
print_table(data)
end
print("-- mem")
print_table(s:mem())
print("-- swap")
print_table(s:swap())
print("-- procs")
local procs = s:procs()
for i = 1, #procs do
local pid = procs[i]
local proc = s:proc(pid) -- get the data ... but don't use it
if pid == s:pid() then
print(pid)
print(" mem")
local mem, msg = proc:mem()
if mem then print_table(mem) else print(" -- no mem info: " .. msg) end
print(" time")
local t, msg = proc:time()
if t then print_table(t) else print(" -- no time info: " .. msg) end
print(" state")
local t, msg = proc:state()
if t then print_table(t) else print(" -- no state info: " .. msg) end
print(" exe")
local t, msg = proc:exe()
if t then print_table(t) else print(" -- no exe info: " .. msg) end
end
end
print("-- filesystems")
local fses = s:filesystems()
for i = 1, #fses do
local fs = fses[i]
print(i)
print(" info")
local info = fs:info()
if info then print_table(info) else print(" -- no fs info") end
print(" usage")
local usage = fs:usage()
if usage then print_table(usage) else print(" -- no fs usage") end
end
print("-- disks")
local disks = s:disks()
for i = 1, #disks do
local disk = disks[i]
print(" usage")
local usage, msg = disk:usage()
if usage then print_table(usage) else print(" -- no disk usage: " .. msg) end
end
---
-- try to specify a device that is known, but not listed by the fs-list
local disk = s:disk("/dev/disk1")
if disk then
print(disk:name())
print(" usage")
local usage, msg = disk:usage()
if usage then print_table(usage) else print(" -- no disk usage: " .. msg) end
end
print("-- who")
local who = s:who()
for i = 1, #who do
local w = who[i]
print(" usage")
local usage, msg = w
if usage then print_table(usage) else print(" -- no who usage: " .. msg) end
end
print("-- netifs")
local netifs = s:netifs()
for i = 1, #netifs do
local netif = netifs[i]
print(" info")
local usage, msg = netif:info()
if usage then print_table(usage) else print(" -- no netif info: " .. msg) end
print(" usage")
local usage, msg = netif:usage()
if usage then print_table(usage) else print(" -- no netif usage: " .. msg) end
end
print("-- version")
print_table(s:version())
print("-- sysinfo")
print_table(s:sysinfo())

View File

@ -0,0 +1,65 @@
/**
* Copyright (c) 2009, Sun Microsystems Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of Sun Microsystems Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <string.h>
#include <stdlib.h>
#include "sigar.h"
#include "lua-sigar.h"
int lua_sigar_version_get(lua_State *L) {
sigar_t *s = *(sigar_t **)luaL_checkudata(L, 1, "sigar");
sigar_version_t *version;
version = sigar_version_get();
lua_newtable(L);
#define DATA \
(version)
LUA_EXPORT_STR(DATA, build_date);
LUA_EXPORT_STR(DATA, scm_revision);
LUA_EXPORT_STR(DATA, version);
LUA_EXPORT_STR(DATA, archname);
LUA_EXPORT_STR(DATA, archlib);
LUA_EXPORT_STR(DATA, binname);
LUA_EXPORT_STR(DATA, description);
LUA_EXPORT_INT(DATA, major);
LUA_EXPORT_INT(DATA, minor);
LUA_EXPORT_INT(DATA, maint);
LUA_EXPORT_INT(DATA, build);
#undef DATA
return 1;
}

103
bindings/lua/sigar-who.c Normal file
View File

@ -0,0 +1,103 @@
/**
* Copyright (c) 2009, Sun Microsystems Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of Sun Microsystems Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <string.h>
#include <stdlib.h>
#include "sigar.h"
#include "lua-sigar.h"
typedef struct {
sigar_who_list_t who;
sigar_t *sigar;
} lua_sigar_who_t;
static int lua_sigar_who_gc(lua_State *L) {
lua_sigar_who_t *who = (lua_sigar_who_t *)luaL_checkudata(L, 1, "sigar_who");
sigar_who_list_destroy(who->sigar, &(who->who));
return 0;
}
static int lua_sigar_who_len(lua_State *L) {
lua_sigar_who_t *who = (lua_sigar_who_t *)luaL_checkudata(L, 1, "sigar_who");
lua_pushinteger(L, who->who.number);
return 1;
}
static int lua_sigar_who_get_who(lua_State *L) {
lua_sigar_who_t *who = (lua_sigar_who_t *)luaL_checkudata(L, 1, "sigar_who");
int ndx = luaL_checkint(L, 2);
if (ndx - 1 < 0 ||
ndx - 1 >= who->who.number) {
luaL_error(L, ".who[%d] out of range: 1..%d", ndx, who->who.number);
}
lua_newtable(L);
#define DATA \
(&(who->who.data[ndx - 1]))
LUA_EXPORT_STR(DATA, user);
LUA_EXPORT_STR(DATA, device);
LUA_EXPORT_STR(DATA, host);
LUA_EXPORT_INT(DATA, time);
#undef DATA
return 1;
}
int lua_sigar_who_get(lua_State *L) {
sigar_t *s = *(sigar_t **)luaL_checkudata(L, 1, "sigar");
lua_sigar_who_t *who;
who = lua_newuserdata(L, sizeof(lua_sigar_who_t));
who->sigar = s;
sigar_who_list_get(s, &(who->who));
if (0 != luaL_newmetatable(L, "sigar_who")) {
lua_pushcfunction(L, lua_sigar_who_len);
lua_setfield(L, -2, "__len");
lua_pushcfunction(L, lua_sigar_who_get_who);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua_sigar_who_gc);
lua_setfield(L, -2, "__gc");
}
lua_setmetatable(L, -2);
return 1;
}

185
bindings/lua/sigar.c Normal file
View File

@ -0,0 +1,185 @@
/**
* Copyright (c) 2009, Sun Microsystems Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of Sun Microsystems Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <string.h>
#include <stdlib.h>
#include "sigar.h"
#include "lua-sigar.h"
/**
* push the converted sigar_net_address_t as string on the stack
*/
int lua_sigar_push_address(lua_State *L, sigar_net_address_t *addr) {
char s[24 + 1]; /* AF_LINK is 2 * 8 + 7 colons = 23
AF_INET6 is 4 * 4 + 3 colons = 17
AF_INET is 4 * 3 + 3 dots = 15
*/
size_t s_have = sizeof(s) - 1;
size_t s_need;
switch (addr->family) {
case SIGAR_AF_UNSPEC:
lua_pushnil(L);
return 1;
case SIGAR_AF_INET:
lua_pushfstring(L, "%d.%d.%d.%d",
(addr->addr.in >> 0) & 0xff,
(addr->addr.in >> 8) & 0xff,
(addr->addr.in >> 16) & 0xff,
(addr->addr.in >> 24) & 0xff);
return 1;
case SIGAR_AF_INET6:
s_need = snprintf(s, s_have, "%4x:%4x:%4x:%4x",
(addr->addr.in6[0]),
(addr->addr.in6[1]),
(addr->addr.in6[2]),
(addr->addr.in6[3]));
if (s_need > s_have) {
/* string is truncated, but written to s */
luaL_error(L, "can't convert INET6 address string, not enough memory: %d need, %d available",
s_need, s_have);
}
lua_pushstring(L, s);
return 1;
case SIGAR_AF_LINK:
s_need = snprintf(s, s_have, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
addr->addr.mac[0],
addr->addr.mac[1],
addr->addr.mac[2],
addr->addr.mac[3],
addr->addr.mac[4],
addr->addr.mac[5],
addr->addr.mac[6],
addr->addr.mac[7]);
if (s_need > s_have) {
/* string is truncated, but written to s */
luaL_error(L, "can't convert MAC address string, not enough memory: %d need, %d available",
s_need, s_have);
}
lua_pushstring(L, s);
return 1;
}
return 0;
}
static int lua_sigar_free(lua_State *L) {
sigar_t *s = *(sigar_t **)luaL_checkudata(L, 1, "sigar");
sigar_close(s);
return 0;
}
static int lua_sigar_new(lua_State *L) {
sigar_t **_s;
sigar_t *s;
if (SIGAR_OK != sigar_open(&s)) {
luaL_error(L, "sigar_open() failed");
return 0;
}
_s = lua_newuserdata(L, sizeof(sigar_t *));
*_s = s;
if (0 != luaL_newmetatable(L, "sigar")) {
lua_newtable(L);
lua_pushcfunction(L, lua_sigar_cpus_get);
lua_setfield(L, -2, "cpus");
lua_pushcfunction(L, lua_sigar_procs_get);
lua_setfield(L, -2, "procs");
lua_pushcfunction(L, lua_sigar_fses_get);
lua_setfield(L, -2, "filesystems");
lua_pushcfunction(L, lua_sigar_disks_get);
lua_setfield(L, -2, "disks");
lua_pushcfunction(L, lua_sigar_disk_get);
lua_setfield(L, -2, "disk");
lua_pushcfunction(L, lua_sigar_who_get);
lua_setfield(L, -2, "who");
lua_pushcfunction(L, lua_sigar_netifs_get);
lua_setfield(L, -2, "netifs");
lua_pushcfunction(L, lua_sigar_proc_get);
lua_setfield(L, -2, "proc");
lua_pushcfunction(L, lua_sigar_pid_get);
lua_setfield(L, -2, "pid");
lua_pushcfunction(L, lua_sigar_mem_get);
lua_setfield(L, -2, "mem");
lua_pushcfunction(L, lua_sigar_swap_get);
lua_setfield(L, -2, "swap");
lua_pushcfunction(L, lua_sigar_version_get);
lua_setfield(L, -2, "version");
lua_pushcfunction(L, lua_sigar_sysinfo_get);
lua_setfield(L, -2, "sysinfo");
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua_sigar_free);
lua_setfield(L, -2, "__gc");
}
lua_setmetatable(L, -2);
return 1;
}
/*
** Assumes the table is on top of the stack.
*/
static void set_info (lua_State *L) {
lua_pushliteral (L, "_COPYRIGHT");
lua_pushliteral (L, "Copyright (c) 2009 Sun Microsystems, Inc.");
lua_settable (L, -3);
lua_pushliteral (L, "_DESCRIPTION");
lua_pushliteral (L, "sigar.*");
lua_settable (L, -3);
lua_pushliteral (L, "_VERSION");
lua_pushliteral (L, "LuaSigar 0.1");
lua_settable (L, -3);
}
static const struct luaL_reg sigarlib[] = {
{"new", lua_sigar_new},
{NULL, NULL}
};
#if defined(_WIN32)
# define LUAEXT_API __declspec(dllexport)
#else
# define LUAEXT_API extern
#endif
LUAEXT_API int luaopen_sigar (lua_State *L) {
luaL_register (L, "sigar", sigarlib);
set_info (L);
return 1;
}