2007-09-04 00:11:15 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
|
|
|
|
* This file is part of SIGAR.
|
|
|
|
*
|
|
|
|
* SIGAR is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms version 2 of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation. This program is distributed
|
|
|
|
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
|
|
|
|
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
|
|
* PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*/
|
|
|
|
|
2007-09-03 08:49:22 +08:00
|
|
|
#include <ruby.h>
|
|
|
|
#include "sigar.h"
|
|
|
|
#include "sigar_fileinfo.h"
|
|
|
|
#include "sigar_format.h"
|
|
|
|
|
|
|
|
#define RB_SIGAR_CROAK rb_raise(rb_eArgError, "%s", sigar_strerror(sigar, status))
|
2007-09-16 05:29:11 +08:00
|
|
|
#define NUM2PID NUM2UINT
|
2007-09-03 08:49:22 +08:00
|
|
|
|
|
|
|
static sigar_t *rb_sigar_get(VALUE obj)
|
|
|
|
{
|
|
|
|
sigar_t *sigar;
|
|
|
|
Data_Get_Struct(obj, sigar_t, sigar);
|
|
|
|
return sigar;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rb_sigar_free(void *obj)
|
|
|
|
{
|
|
|
|
free(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rb_sigar_close(void *obj)
|
|
|
|
{
|
|
|
|
sigar_close((sigar_t *)obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_sigar_new(VALUE module)
|
|
|
|
{
|
|
|
|
sigar_t *sigar;
|
|
|
|
sigar_open(&sigar);
|
|
|
|
return Data_Wrap_Struct(module, 0, rb_sigar_close, sigar);
|
|
|
|
}
|
|
|
|
|
2007-09-03 09:57:02 +08:00
|
|
|
static VALUE rb_sigar_format_size(VALUE rclass, VALUE size)
|
|
|
|
{
|
|
|
|
char buffer[56];
|
|
|
|
return rb_str_new2(sigar_format_size(NUM2LL(size), buffer));
|
|
|
|
}
|
|
|
|
|
2007-09-03 11:13:41 +08:00
|
|
|
static VALUE rb_sigar_net_interface_flags_to_s(VALUE rclass, VALUE flags)
|
|
|
|
{
|
|
|
|
char buffer[1024];
|
|
|
|
return rb_str_new2(sigar_net_interface_flags_to_string(NUM2LL(flags), buffer));
|
|
|
|
}
|
|
|
|
|
2007-09-10 01:21:45 +08:00
|
|
|
static VALUE rb_sigar_net_address_to_string(sigar_net_address_t *address)
|
2007-09-03 08:49:22 +08:00
|
|
|
{
|
|
|
|
char addr_str[SIGAR_INET6_ADDRSTRLEN];
|
2007-09-10 01:21:45 +08:00
|
|
|
sigar_net_address_to_string(NULL, address, addr_str);
|
2007-09-03 08:49:22 +08:00
|
|
|
return rb_str_new2(addr_str);
|
|
|
|
}
|
|
|
|
|
2007-09-10 01:21:45 +08:00
|
|
|
#define rb_sigar_net_address_to_s(a) rb_sigar_net_address_to_string(&a)
|
|
|
|
|
2007-09-03 08:49:22 +08:00
|
|
|
static VALUE rb_sigar_new_list(char *data, unsigned long number,
|
|
|
|
int size, VALUE rclass)
|
|
|
|
{
|
|
|
|
unsigned long i;
|
|
|
|
VALUE av = rb_ary_new2(number);
|
|
|
|
|
|
|
|
for (i=0; i<number; i++, data += size) {
|
|
|
|
void *ent = malloc(size);
|
|
|
|
|
|
|
|
memcpy(ent, data, size);
|
|
|
|
rb_ary_push(av, Data_Wrap_Struct(rclass, 0, free, ent));
|
|
|
|
}
|
|
|
|
|
|
|
|
return av;
|
|
|
|
}
|
|
|
|
|
2007-09-03 10:42:41 +08:00
|
|
|
static VALUE rb_sigar_new_strlist(char **data, unsigned long number)
|
|
|
|
{
|
|
|
|
unsigned long i;
|
|
|
|
VALUE av = rb_ary_new2(number);
|
|
|
|
|
|
|
|
for (i=0; i<number; i++) {
|
|
|
|
rb_ary_push(av, rb_str_new2(data[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return av;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_sigar_net_interface_list(VALUE obj)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
sigar_t *sigar = rb_sigar_get(obj);
|
|
|
|
sigar_net_interface_list_t iflist;
|
|
|
|
VALUE RETVAL;
|
|
|
|
|
|
|
|
status = sigar_net_interface_list_get(sigar, &iflist);
|
|
|
|
if (status != SIGAR_OK) {
|
|
|
|
RB_SIGAR_CROAK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RETVAL = rb_sigar_new_strlist(iflist.data, iflist.number);
|
|
|
|
|
|
|
|
sigar_net_interface_list_destroy(sigar, &iflist);
|
|
|
|
|
|
|
|
return RETVAL;
|
|
|
|
}
|
|
|
|
|
2007-09-03 08:49:22 +08:00
|
|
|
static VALUE rb_cSigarNetStat;
|
|
|
|
|
|
|
|
static VALUE rb_sigar_net_stat(VALUE obj)
|
|
|
|
{
|
|
|
|
return obj; /*XXX*/
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_cSigarNetConnection;
|
|
|
|
|
|
|
|
static VALUE rb_sigar_net_connection(VALUE obj)
|
|
|
|
{
|
|
|
|
return obj; /*XXX*/
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_cSigarCpuInfo;
|
|
|
|
|
2007-09-15 22:40:13 +08:00
|
|
|
static VALUE rb_sigar_cpu_info_list(VALUE obj)
|
2007-09-03 08:49:22 +08:00
|
|
|
{
|
2007-09-15 22:40:13 +08:00
|
|
|
int status;
|
|
|
|
sigar_t *sigar = rb_sigar_get(obj);
|
|
|
|
sigar_cpu_info_list_t cpu_infos;
|
|
|
|
VALUE RETVAL;
|
|
|
|
|
|
|
|
status = sigar_cpu_info_list_get(sigar, &cpu_infos);
|
|
|
|
if (status != SIGAR_OK) {
|
|
|
|
RB_SIGAR_CROAK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RETVAL = rb_sigar_new_list((char *)&cpu_infos.data[0],
|
|
|
|
cpu_infos.number,
|
|
|
|
sizeof(*cpu_infos.data),
|
|
|
|
rb_cSigarCpuInfo);
|
|
|
|
|
|
|
|
sigar_cpu_info_list_destroy(sigar, &cpu_infos);
|
|
|
|
|
|
|
|
return RETVAL;
|
2007-09-03 08:49:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_cSigarFileSystem;
|
|
|
|
|
|
|
|
static VALUE rb_sigar_file_system_list(VALUE obj)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
sigar_t *sigar = rb_sigar_get(obj);
|
|
|
|
sigar_file_system_list_t fslist;
|
|
|
|
VALUE RETVAL;
|
|
|
|
|
|
|
|
status = sigar_file_system_list_get(sigar, &fslist);
|
|
|
|
if (status != SIGAR_OK) {
|
|
|
|
RB_SIGAR_CROAK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RETVAL = rb_sigar_new_list((char *)&fslist.data[0],
|
|
|
|
fslist.number,
|
|
|
|
sizeof(*fslist.data),
|
|
|
|
rb_cSigarFileSystem);
|
|
|
|
|
|
|
|
sigar_file_system_list_destroy(sigar, &fslist);
|
|
|
|
|
|
|
|
return RETVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_cSigarWho;
|
|
|
|
|
2007-09-15 23:53:50 +08:00
|
|
|
static VALUE rb_sigar_who_list(VALUE obj)
|
2007-09-03 08:49:22 +08:00
|
|
|
{
|
2007-09-15 23:53:50 +08:00
|
|
|
int status;
|
|
|
|
sigar_t *sigar = rb_sigar_get(obj);
|
|
|
|
sigar_who_list_t list;
|
|
|
|
VALUE RETVAL;
|
|
|
|
|
|
|
|
status = sigar_who_list_get(sigar, &list);
|
|
|
|
if (status != SIGAR_OK) {
|
|
|
|
RB_SIGAR_CROAK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RETVAL = rb_sigar_new_list((char *)&list.data[0],
|
|
|
|
list.number,
|
|
|
|
sizeof(*list.data),
|
|
|
|
rb_cSigarWho);
|
|
|
|
|
|
|
|
sigar_who_list_destroy(sigar, &list);
|
|
|
|
|
|
|
|
return RETVAL;
|
2007-09-03 08:49:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_cSigarNetRoute;
|
|
|
|
|
|
|
|
static VALUE rb_sigar_net_route(VALUE obj)
|
|
|
|
{
|
|
|
|
return obj; /*XXX*/
|
|
|
|
}
|
|
|
|
|
2007-09-16 05:03:11 +08:00
|
|
|
static VALUE rb_sigar_proc_args(VALUE obj, VALUE pid)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
sigar_t *sigar = rb_sigar_get(obj);
|
|
|
|
sigar_proc_args_t args;
|
|
|
|
VALUE RETVAL;
|
|
|
|
|
2007-09-16 05:29:11 +08:00
|
|
|
status = sigar_proc_args_get(sigar, NUM2PID(pid), &args);
|
|
|
|
|
2007-09-16 05:03:11 +08:00
|
|
|
if (status != SIGAR_OK) {
|
|
|
|
RB_SIGAR_CROAK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RETVAL = rb_sigar_new_strlist(args.data, args.number);
|
|
|
|
|
|
|
|
sigar_proc_args_destroy(sigar, &args);
|
|
|
|
|
|
|
|
return RETVAL;
|
|
|
|
}
|
|
|
|
|
2007-09-16 05:29:11 +08:00
|
|
|
static int rb_sigar_env_getall(void *data,
|
|
|
|
const char *key, int klen,
|
|
|
|
char *val, int vlen)
|
|
|
|
{
|
|
|
|
rb_hash_aset(*((VALUE*)data),
|
|
|
|
rb_str_new(key, klen),
|
|
|
|
rb_str_new(val, vlen));
|
|
|
|
return SIGAR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_sigar_proc_env(VALUE obj, VALUE pid)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
sigar_t *sigar = rb_sigar_get(obj);
|
|
|
|
sigar_proc_env_t procenv;
|
|
|
|
VALUE RETVAL = rb_hash_new();
|
|
|
|
|
|
|
|
procenv.type = SIGAR_PROC_ENV_ALL;
|
|
|
|
procenv.env_getter = rb_sigar_env_getall;
|
|
|
|
procenv.data = &RETVAL;
|
|
|
|
|
|
|
|
status = sigar_proc_env_get(sigar, NUM2PID(pid), &procenv);
|
|
|
|
if (status != SIGAR_OK) {
|
|
|
|
RB_SIGAR_CROAK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RETVAL;
|
|
|
|
}
|
|
|
|
|
2007-09-03 08:49:22 +08:00
|
|
|
#include "./rbsigar_generated.rx"
|
|
|
|
|
2007-09-04 00:41:11 +08:00
|
|
|
#define RB_SIGAR_CONST_INT(name) \
|
|
|
|
rb_define_const(rclass, #name, INT2FIX(SIGAR_##name))
|
|
|
|
|
2007-09-04 01:08:35 +08:00
|
|
|
#define RB_SIGAR_CONST_STR(name) \
|
|
|
|
rb_define_const(rclass, #name, rb_obj_freeze(rb_str_new2(SIGAR_##name)))
|
|
|
|
|
2007-09-04 00:41:11 +08:00
|
|
|
static void Init_rbsigar_constants(VALUE rclass)
|
|
|
|
{
|
|
|
|
RB_SIGAR_CONST_INT(IFF_UP);
|
|
|
|
RB_SIGAR_CONST_INT(IFF_BROADCAST);
|
|
|
|
RB_SIGAR_CONST_INT(IFF_DEBUG);
|
|
|
|
RB_SIGAR_CONST_INT(IFF_LOOPBACK);
|
|
|
|
RB_SIGAR_CONST_INT(IFF_POINTOPOINT);
|
|
|
|
RB_SIGAR_CONST_INT(IFF_NOTRAILERS);
|
|
|
|
RB_SIGAR_CONST_INT(IFF_RUNNING);
|
|
|
|
RB_SIGAR_CONST_INT(IFF_NOARP);
|
|
|
|
RB_SIGAR_CONST_INT(IFF_PROMISC);
|
|
|
|
RB_SIGAR_CONST_INT(IFF_ALLMULTI);
|
|
|
|
RB_SIGAR_CONST_INT(IFF_MULTICAST);
|
2007-09-04 01:08:35 +08:00
|
|
|
|
|
|
|
RB_SIGAR_CONST_STR(NULL_HWADDR);
|
2007-09-04 00:41:11 +08:00
|
|
|
}
|
|
|
|
|
2007-09-03 08:49:22 +08:00
|
|
|
void Init_rbsigar(void)
|
|
|
|
{
|
2007-09-03 09:14:49 +08:00
|
|
|
VALUE rclass = rb_define_class("Sigar", rb_cObject);
|
2007-09-03 08:49:22 +08:00
|
|
|
|
2007-09-15 22:40:13 +08:00
|
|
|
rb_define_method(rclass, "cpu_info_list", rb_sigar_cpu_info_list, 0);
|
2007-09-03 08:49:22 +08:00
|
|
|
rb_define_method(rclass, "file_system_list", rb_sigar_file_system_list, 0);
|
2007-09-03 10:42:41 +08:00
|
|
|
rb_define_method(rclass, "net_interface_list", rb_sigar_net_interface_list, 0);
|
2007-09-15 23:53:50 +08:00
|
|
|
rb_define_method(rclass, "who_list", rb_sigar_who_list, 0);
|
2007-09-16 05:03:11 +08:00
|
|
|
rb_define_method(rclass, "proc_args", rb_sigar_proc_args, 1);
|
2007-09-16 05:29:11 +08:00
|
|
|
rb_define_method(rclass, "proc_env", rb_sigar_proc_env, 1);
|
2007-09-03 08:49:22 +08:00
|
|
|
|
|
|
|
rb_define_singleton_method(rclass, "new", rb_sigar_new, 0);
|
2007-09-03 09:57:02 +08:00
|
|
|
rb_define_singleton_method(rclass, "format_size", rb_sigar_format_size, 1);
|
2007-09-03 11:13:41 +08:00
|
|
|
rb_define_singleton_method(rclass, "net_interface_flags_to_s",
|
|
|
|
rb_sigar_net_interface_flags_to_s, 1);
|
2007-09-03 08:49:22 +08:00
|
|
|
|
2007-09-04 00:41:11 +08:00
|
|
|
Init_rbsigar_constants(rclass);
|
|
|
|
|
2007-09-03 08:49:22 +08:00
|
|
|
/* generated */
|
|
|
|
rb_sigar_define_module_methods(rclass);
|
|
|
|
}
|