Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement system, proc, cpu and net (partially) functions

  • Loading branch information...
commit dc43b40c56f7001db2d35121c397d0f685399872 1 parent 752c9da
@wdavidw authored
View
11 index.js
@@ -0,0 +1,11 @@
+
+var sigar = new require('./build/Release/sigar');
+
+module.exports = function(){
+ return new sigar.NodeSigar;
+}
+for(var k in sigar){
+ if(k !== 'NodeSigar'){
+ module.exports[k] = sigar[k];
+ }
+}
View
11 package.json
@@ -0,0 +1,11 @@
+{
+ "name": "sigar",
+ "version": "0.0.1",
+ "description": "Sigar binding",
+ "author": "David Worms <david@adaltas.com>",
+ "contributors": [{
+ "name": "David Worms",
+ "email": "david@adaltas.com"
+ }],
+ "engines": { "node": ">= 0.6" }
+}
View
880 src/node_sigar.cc
@@ -0,0 +1,880 @@
+#include <v8.h>
+#include <node.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <errno.h>
+#include "sigar.h"
+#include "sigar_fileinfo.h"
+#include "sigar_log.h"
+#include "sigar_format.h"
+#include "sigar_ptql.h"
+
+using namespace node;
+using namespace v8;
+
+class NodeSigar: public ObjectWrap
+{
+private:
+ sigar_t *sigar;
+public:
+ static void Initialize(Handle<Object> target)
+ {
+ HandleScope scope;
+ Local<FunctionTemplate> t = FunctionTemplate::New(New);
+ Local<FunctionTemplate> s_ct = Local<FunctionTemplate>::New(t);
+ s_ct->InstanceTemplate()->SetInternalFieldCount(1);
+ s_ct->SetClassName(String::NewSymbol("NodeSigar"));
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "mem", GetMem);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "swap", GetSwap);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "cpu", SigarCpuGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "cpuList", SigarCpuListGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "cpuInfo", SigarCpuInfoListGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "uptime", SigarUptimeGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "loadavg", SigarLoadavgGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "resourceLimit", SigarResourceLimitGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "procList", SigarProcListGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "procStat", SigarProcStatGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "procMem", SigarProcMemGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "procCred", SigarProcCredGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "procTime", SigarProcTimeGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "procCpu", SigarProcCpuGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "procState", SigarProcStateGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "procArgs", SigarProcArgsGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "procEnv", SigarProcEnvGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "procFd", SigarProcFdGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "procExe", SigarProcExeGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "procModules", SigarProcModulesGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "threadCpu", SigarThreadCpuGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "fileSystemList", SigarFileSystemListGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "fileSystemUsage", SigarFileSystemUsageGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "fileSystemPing", SigarFileSystemPingGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "netInfo", SigarNetInfoGet);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "netRouteList", SigarNetRouteListGet);
+ target->Set(String::NewSymbol("NodeSigar"), s_ct->GetFunction());
+ target->Set(String::NewSymbol("FSTYPE_UNKNOWN"), Number::New(SIGAR_FSTYPE_UNKNOWN));
+ target->Set(String::NewSymbol("FSTYPE_NONE"), Number::New(SIGAR_FSTYPE_NONE));
+ target->Set(String::NewSymbol("FSTYPE_LOCAL_DISK"), Number::New(SIGAR_FSTYPE_LOCAL_DISK));
+ target->Set(String::NewSymbol("FSTYPE_NETWORK"), Number::New(SIGAR_FSTYPE_NETWORK));
+ target->Set(String::NewSymbol("FSTYPE_RAM_DISK"), Number::New(SIGAR_FSTYPE_RAM_DISK));
+ target->Set(String::NewSymbol("FSTYPE_CDROM"), Number::New(SIGAR_FSTYPE_CDROM));
+ target->Set(String::NewSymbol("FSTYPE_SWAP"), Number::New(SIGAR_FSTYPE_SWAP));
+ target->Set(String::NewSymbol("FSTYPE_MAX"), Number::New(SIGAR_FSTYPE_MAX));
+ }
+ ~NodeSigar()
+ {
+ sigar_close(this->sigar);
+ }
+ static Handle<Value> New(const Arguments& args)
+ {
+ HandleScope scope;
+ NodeSigar* g = new NodeSigar();
+ g->Wrap(args.This());
+ sigar_open(&(g->sigar));
+
+ return args.This();
+ }
+// static Handle<Object> MapSigarNetAddress(sigar_net_address_t& address)
+// {
+// Handle<Object> h_address = Object::New();
+//
+// sigar_net_address_to_string(sigar_t *sigar, *address, char *addr_str)
+//
+// return h_address;
+
+// Handle<Object> h_address = Object::New();
+// h_address->Set(String::NewSymbol("family"), Number::New(address.family));
+// h_address->Set(String::NewSymbol("addr_in"), Number::New(address.addr.in));
+// Handle<Array> h_address_in6 = Array::New();
+// h_address_in6->Set(0, Number::New(address.addr.in6[0]));
+// h_address_in6->Set(1, Number::New(address.addr.in6[1]));
+// h_address_in6->Set(2, Number::New(address.addr.in6[2]));
+// h_address_in6->Set(3, Number::New(address.addr.in6[3]));
+// h_address->Set(String::NewSymbol("addr_in6"), h_address_in6);
+// h_address->Set(String::NewSymbol("mac"), String::New(address.addr.mac));
+// return h_address;
+// }
+ static Handle<Value> GetMem(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_mem_t mem;
+ int status = sigar_mem_get(g->sigar, &mem);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_mem_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ }
+ Handle<Object> h_mem = Object::New();
+ h_mem->Set(String::NewSymbol("ram"), Number::New(mem.ram));
+ h_mem->Set(String::NewSymbol("total"), Number::New(mem.total));
+ h_mem->Set(String::NewSymbol("used"), Number::New(mem.used));
+ h_mem->Set(String::NewSymbol("free"), Number::New(mem.free));
+ h_mem->Set(String::NewSymbol("actual_used"), Number::New(mem.actual_used));
+ h_mem->Set(String::NewSymbol("actual_free"), Number::New(mem.actual_free));
+ h_mem->Set(String::NewSymbol("used_percent"), Number::New(mem.used_percent));
+ h_mem->Set(String::NewSymbol("free_percent"), Number::New(mem.free_percent));
+ return scope.Close(h_mem);
+ }
+ static Handle<Value> GetSwap(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_swap_t swap;
+ int status = sigar_swap_get(g->sigar, &swap);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_swap_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ }
+ Handle<Object> h_swap = Object::New();
+ h_swap->Set(String::NewSymbol("total"), Number::New(swap.total));
+ h_swap->Set(String::NewSymbol("used"), Number::New(swap.used));
+ h_swap->Set(String::NewSymbol("free"), Number::New(swap.free));
+ h_swap->Set(String::NewSymbol("page_in"), Number::New(swap.page_in));
+ h_swap->Set(String::NewSymbol("page_out"), Number::New(swap.page_out));
+ return scope.Close(h_swap);
+ }
+ static Handle<Value> SigarCpuGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_cpu_t cpu;
+ int status = sigar_cpu_get(g->sigar, &cpu);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_cpu_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ }
+ Handle<Object> h_cpu = Object::New();
+ h_cpu->Set(String::NewSymbol("user"), Number::New(cpu.user));
+ h_cpu->Set(String::NewSymbol("sys"), Number::New(cpu.sys));
+ h_cpu->Set(String::NewSymbol("nice"), Number::New(cpu.nice));
+ h_cpu->Set(String::NewSymbol("idle"), Number::New(cpu.idle));
+ h_cpu->Set(String::NewSymbol("wait"), Number::New(cpu.wait));
+ h_cpu->Set(String::NewSymbol("irq"), Number::New(cpu.irq));
+ h_cpu->Set(String::NewSymbol("soft_irq"), Number::New(cpu.soft_irq));
+ h_cpu->Set(String::NewSymbol("stolen"), Number::New(cpu.stolen));
+ h_cpu->Set(String::NewSymbol("total"), Number::New(cpu.total));
+ return scope.Close(h_cpu);
+ }
+ static Handle<Value> SigarCpuListGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_cpu_list_t cpulist;
+ unsigned i;
+ int status = sigar_cpu_list_get(g->sigar, &cpulist);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_cpu_list_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ }
+ Handle<Array> h_cpus = Array::New(cpulist.number);
+ for (i=0; i<cpulist.number; i++) {
+ sigar_cpu_t cpu = cpulist.data[i];
+ Handle<Object> h_cpu = Object::New();
+ h_cpu->Set(String::NewSymbol("user"), Number::New(cpu.user));
+ h_cpu->Set(String::NewSymbol("sys"), Number::New(cpu.sys));
+ h_cpu->Set(String::NewSymbol("nice"), Number::New(cpu.nice));
+ h_cpu->Set(String::NewSymbol("idle"), Number::New(cpu.idle));
+ h_cpu->Set(String::NewSymbol("wait"), Number::New(cpu.wait));
+ h_cpu->Set(String::NewSymbol("irq"), Number::New(cpu.irq));
+ h_cpu->Set(String::NewSymbol("soft_irq"), Number::New(cpu.soft_irq));
+ h_cpu->Set(String::NewSymbol("stolen"), Number::New(cpu.stolen));
+ h_cpu->Set(String::NewSymbol("total"), Number::New(cpu.total));
+ h_cpus->Set(i, h_cpu);
+ }
+ sigar_cpu_list_destroy(g->sigar, &cpulist);
+ return scope.Close(h_cpus);
+ }
+ static Handle<Value> SigarCpuInfoListGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_cpu_info_list_t cpuinfo;
+ unsigned i;
+ int status = sigar_cpu_info_list_get(g->sigar, &cpuinfo);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_cpu_list_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ }
+ Handle<Array> h_cpus = Array::New(cpuinfo.number);
+ for (i=0; i<cpuinfo.number; i++) {
+ sigar_cpu_info_t cpu = cpuinfo.data[i];
+ Handle<Object> h_cpu = Object::New();
+ h_cpu->Set(String::NewSymbol("vendor"), String::New(cpu.vendor));
+ h_cpu->Set(String::NewSymbol("model"), String::New(cpu.model));
+ h_cpu->Set(String::NewSymbol("mhz"), Number::New(cpu.mhz));
+ h_cpu->Set(String::NewSymbol("mhz_max"), Number::New(cpu.mhz_max));
+ h_cpu->Set(String::NewSymbol("mhz_min"), Number::New(cpu.mhz_min));
+ h_cpu->Set(String::NewSymbol("cache_size"), Number::New(cpu.cache_size));
+ h_cpu->Set(String::NewSymbol("total_sockets"), Number::New(cpu.total_sockets));
+ h_cpu->Set(String::NewSymbol("total_cores"), Number::New(cpu.total_cores));
+ h_cpu->Set(String::NewSymbol("cores_per_socket"), Number::New(cpu.cores_per_socket));
+ h_cpus->Set(i, h_cpu);
+ }
+ sigar_cpu_info_list_destroy(g->sigar, &cpuinfo);
+ return scope.Close(h_cpus);
+ }
+ static Handle<Value> SigarUptimeGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_uptime_t uptime;
+ int status = sigar_uptime_get(g->sigar, &uptime);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_uptime_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ }
+ return scope.Close(Number::New(uptime.uptime));
+ }
+ static Handle<Value> SigarLoadavgGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_loadavg_t loadavg;
+ int status = sigar_loadavg_get(g->sigar, &loadavg);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_uptime_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ }
+ Handle<Array> h_loadavgs = Array::New(3);
+ h_loadavgs->Set(0, Number::New(loadavg.loadavg[0]));
+ h_loadavgs->Set(1, Number::New(loadavg.loadavg[1]));
+ h_loadavgs->Set(2, Number::New(loadavg.loadavg[2]));
+ return scope.Close(h_loadavgs);
+ }
+ static Handle<Value> SigarResourceLimitGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_resource_limit_t resourcelimit;
+ int status = sigar_resource_limit_get(g->sigar, &resourcelimit);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_cpu_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ }
+ Handle<Object> h_resourcelimit= Object::New();
+ h_resourcelimit->Set(String::NewSymbol("cpu_cur"), Number::New(resourcelimit.cpu_cur));
+ h_resourcelimit->Set(String::NewSymbol("cpu_max"), Number::New(resourcelimit.cpu_max));
+ h_resourcelimit->Set(String::NewSymbol("file_size_cur"), Number::New(resourcelimit.file_size_cur));
+ h_resourcelimit->Set(String::NewSymbol("file_size_max"), Number::New(resourcelimit.file_size_max));
+ h_resourcelimit->Set(String::NewSymbol("pipe_size_cur"), Number::New(resourcelimit.pipe_size_cur));
+ h_resourcelimit->Set(String::NewSymbol("pipe_size_max"), Number::New(resourcelimit.pipe_size_max));
+ h_resourcelimit->Set(String::NewSymbol("data_cur"), Number::New(resourcelimit.data_cur));
+ h_resourcelimit->Set(String::NewSymbol("data_max"), Number::New(resourcelimit.data_max));
+ h_resourcelimit->Set(String::NewSymbol("stack_cur"), Number::New(resourcelimit.stack_cur));
+ h_resourcelimit->Set(String::NewSymbol("stack_max"), Number::New(resourcelimit.stack_max));
+ h_resourcelimit->Set(String::NewSymbol("core_cur"), Number::New(resourcelimit.core_cur));
+ h_resourcelimit->Set(String::NewSymbol("core_max"), Number::New(resourcelimit.core_max));
+ h_resourcelimit->Set(String::NewSymbol("memory_cur"), Number::New(resourcelimit.memory_cur));
+ h_resourcelimit->Set(String::NewSymbol("memory_max"), Number::New(resourcelimit.memory_max));
+ h_resourcelimit->Set(String::NewSymbol("processes_cur"), Number::New(resourcelimit.processes_cur));
+ h_resourcelimit->Set(String::NewSymbol("processes_max"), Number::New(resourcelimit.processes_max));
+ h_resourcelimit->Set(String::NewSymbol("open_files_cur"), Number::New(resourcelimit.open_files_cur));
+ h_resourcelimit->Set(String::NewSymbol("open_files_max"), Number::New(resourcelimit.open_files_max));
+ h_resourcelimit->Set(String::NewSymbol("virtual_memory_cur"), Number::New(resourcelimit.virtual_memory_cur));
+ h_resourcelimit->Set(String::NewSymbol("virtual_memory_max"), Number::New(resourcelimit.virtual_memory_max));
+ return scope.Close(h_resourcelimit);
+ }
+ static Handle<Value> SigarProcListGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_proc_list_t proclist;
+ unsigned i;
+ int status = sigar_proc_list_get(g->sigar, &proclist);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_proc_list_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ }
+ Handle<Array> h_proclist = Array::New(proclist.number);
+ for (i=0; i<proclist.number; i++) {
+ sigar_pid_t pid = proclist.data[i];
+ h_proclist->Set(i, Number::New(pid));
+ }
+ sigar_proc_list_destroy(g->sigar, &proclist);
+ return scope.Close(h_proclist);
+ }
+ static Handle<Value> SigarProcStatGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_proc_stat_t procstat;
+ int status = sigar_proc_stat_get(g->sigar, &procstat);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_proc_stat_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ }
+ Handle<Object> h_procstat = Object::New();
+ h_procstat->Set(String::NewSymbol("total"), Number::New(procstat.total));
+ h_procstat->Set(String::NewSymbol("sleeping"), Number::New(procstat.sleeping));
+ h_procstat->Set(String::NewSymbol("running"), Number::New(procstat.running));
+ h_procstat->Set(String::NewSymbol("zombie"), Number::New(procstat.zombie));
+ h_procstat->Set(String::NewSymbol("stopped"), Number::New(procstat.stopped));
+ h_procstat->Set(String::NewSymbol("idle"), Number::New(procstat.idle));
+ h_procstat->Set(String::NewSymbol("threads"), Number::New(procstat.threads));
+ return scope.Close(h_procstat);
+ }
+ static Handle<Value> SigarProcMemGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 1){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_proc_mem_t procmem;
+ int status = sigar_proc_mem_get(g->sigar, args[0]->IntegerValue(), &procmem);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_proc_stat_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ }
+ Handle<Object> h_procmem = Object::New();
+ h_procmem->Set(String::NewSymbol("size"), Number::New(procmem.size));
+ h_procmem->Set(String::NewSymbol("resident"), Number::New(procmem.resident));
+ h_procmem->Set(String::NewSymbol("share"), Number::New(procmem.share));
+ h_procmem->Set(String::NewSymbol("minor_faults"), Number::New(procmem.minor_faults));
+ h_procmem->Set(String::NewSymbol("major_faults"), Number::New(procmem.major_faults));
+ h_procmem->Set(String::NewSymbol("page_faults"), Number::New(procmem.page_faults));
+ return scope.Close(h_procmem);
+ }
+ static Handle<Value> SigarProcCredGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 1){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_proc_cred_t proccred;
+ int status = sigar_proc_cred_get(g->sigar, args[0]->IntegerValue(), &proccred);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_proc_stat_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ }
+ Handle<Object> h_proccred = Object::New();
+ h_proccred->Set(String::NewSymbol("uid"), Number::New(proccred.uid));
+ h_proccred->Set(String::NewSymbol("gid"), Number::New(proccred.gid));
+ h_proccred->Set(String::NewSymbol("euid"), Number::New(proccred.euid));
+ h_proccred->Set(String::NewSymbol("egid"), Number::New(proccred.egid));
+ return scope.Close(h_proccred);
+ }
+ static Handle<Value> SigarProcTimeGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 1){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_proc_time_t proctime;
+ int status = sigar_proc_time_get(g->sigar, args[0]->IntegerValue(), &proctime);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_proc_stat_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ }
+ Handle<Object> h_proctime = Object::New();
+ h_proctime->Set(String::NewSymbol("start_time"), Number::New(proctime.start_time));
+ h_proctime->Set(String::NewSymbol("user"), Number::New(proctime.user));
+ h_proctime->Set(String::NewSymbol("sys"), Number::New(proctime.sys));
+ h_proctime->Set(String::NewSymbol("total"), Number::New(proctime.total));
+ return scope.Close(h_proctime);
+ }
+ static Handle<Value> SigarProcCpuGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 1){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_proc_cpu_t proccpu;
+ int status = sigar_proc_cpu_get(g->sigar, args[0]->IntegerValue(), &proccpu);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_proc_stat_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ }
+ Handle<Object> h_proccpu = Object::New();
+ h_proccpu->Set(String::NewSymbol("start_time"), Number::New(proccpu.start_time));
+ h_proccpu->Set(String::NewSymbol("user"), Number::New(proccpu.user));
+ h_proccpu->Set(String::NewSymbol("sys"), Number::New(proccpu.sys));
+ h_proccpu->Set(String::NewSymbol("total"), Number::New(proccpu.total));
+ h_proccpu->Set(String::NewSymbol("last_time"), Number::New(proccpu.last_time));
+ h_proccpu->Set(String::NewSymbol("percent"), Number::New(proccpu.percent));
+ return scope.Close(h_proccpu);
+ }
+ static Handle<Value> SigarProcStateGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 1){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_proc_state_t procstate;
+ int status = sigar_proc_state_get(g->sigar, args[0]->IntegerValue(), &procstate);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_proc_stat_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ };
+ // TODO: state is probably not handled correctly,
+ // eg, moving the next 2 lines after h_procstate creation will add extra characters
+ char state[0];
+ state[0] = procstate.state;
+ Handle<Object> h_procstate = Object::New();
+ h_procstate->Set(String::NewSymbol("name"), String::New(procstate.name));
+ h_procstate->Set(String::NewSymbol("state"), String::New(state));
+ h_procstate->Set(String::NewSymbol("ppid"), Number::New(procstate.ppid));
+ h_procstate->Set(String::NewSymbol("tty"), Number::New(procstate.tty));
+ h_procstate->Set(String::NewSymbol("priority"), Number::New(procstate.priority));
+ h_procstate->Set(String::NewSymbol("nice"), Number::New(procstate.nice));
+ h_procstate->Set(String::NewSymbol("processor"), Number::New(procstate.processor));
+ h_procstate->Set(String::NewSymbol("threads"), Number::New(procstate.threads));
+ return scope.Close(h_procstate);
+ }
+ static Handle<Value> SigarProcArgsGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 1){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_proc_args_t procargs;
+ unsigned i;
+ int status = sigar_proc_args_get(g->sigar, args[0]->IntegerValue(), &procargs);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_proc_args_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ }
+ Handle<Array> h_procargs = Array::New(procargs.number);
+ for (i=0; i<procargs.number; i++) {
+ char* arg = procargs.data[i];
+ h_procargs->Set(i, String::New(arg));
+ }
+ sigar_proc_args_destroy(g->sigar, &procargs);
+ return scope.Close(h_procargs);
+ }
+ static int SigarProcEnvGet_getter(void *data,
+ const char *key, int klen,
+ char *val, int vlen)
+ {
+ Handle<Object> d = *((Handle<Object>*)data);
+ d->Set(String::New(key, klen), String::New(val, vlen));
+ return SIGAR_OK;
+ }
+ static Handle<Value> SigarProcEnvGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 1){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_proc_env_t procenv;
+ procenv.type = procenv.SIGAR_PROC_ENV_ALL;
+ procenv.env_getter = NodeSigar::SigarProcEnvGet_getter;
+ Handle<Object> data = Object::New();
+ procenv.data = &data;
+ int status = sigar_proc_env_get(g->sigar, args[0]->IntegerValue(), &procenv);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_proc_env_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ }
+ return scope.Close(data);
+ }
+ static Handle<Value> SigarProcFdGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 1){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_proc_fd_t procfd;
+ int status = sigar_proc_fd_get(g->sigar, args[0]->IntegerValue(), &procfd);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_proc_fd_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ }
+ // Note, return an object because only total but a comment
+ // suggest they might add other properties
+ Handle<Object> h_procfd = Object::New();
+ h_procfd->Set(String::NewSymbol("total"), Number::New(procfd.total));
+ return scope.Close(h_procfd);
+ }
+ static Handle<Value> SigarProcExeGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 1){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_proc_exe_t procexe;
+ int status = sigar_proc_exe_get(g->sigar, args[0]->IntegerValue(), &procexe);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_proc_exe_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ };
+ Handle<Object> h_procexe = Object::New();
+ h_procexe->Set(String::NewSymbol("name"), String::New(procexe.name));
+ h_procexe->Set(String::NewSymbol("cwd"), String::New(procexe.cwd));
+ h_procexe->Set(String::NewSymbol("root"), String::New(procexe.root));
+ return scope.Close(h_procexe);
+ }
+ static int SigarProcModulesGet_getter(void *data,
+ char *key, int klen)
+ {
+ Handle<Array> d = *((Handle<Array>*)data);
+ // Not sure if index arg is meant to work
+ d->Set(d->Length(), String::New(key, klen));
+ return SIGAR_OK;
+ }
+ static Handle<Value> SigarProcModulesGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 1){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_proc_modules_t procmodule;
+ procmodule.module_getter = NodeSigar::SigarProcModulesGet_getter;
+ Handle<Array> data = Array::New();
+ procmodule.data = &data;
+ int status = sigar_proc_modules_get(g->sigar, args[0]->IntegerValue(), &procmodule);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_proc_module_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ }
+ return scope.Close(data);
+ }
+ static Handle<Value> SigarThreadCpuGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 1){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_thread_cpu_t threadcpu;
+ int status = sigar_thread_cpu_get(g->sigar, args[0]->IntegerValue(), &threadcpu);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_thread_cpu_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ };
+ Handle<Object> h_threadcpu = Object::New();
+ h_threadcpu->Set(String::NewSymbol("user"), Number::New(threadcpu.user));
+ h_threadcpu->Set(String::NewSymbol("sys"), Number::New(threadcpu.sys));
+ h_threadcpu->Set(String::NewSymbol("total"), Number::New(threadcpu.total));
+ return scope.Close(h_threadcpu);
+ }
+ static Handle<Value> SigarFileSystemListGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_file_system_list_t filesystemlist;
+ unsigned i;
+ int status = sigar_file_system_list_get(g->sigar, &filesystemlist);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_file_system_list_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ }
+ Handle<Array> h_filesystemlist = Array::New(filesystemlist.number);
+ for (i=0; i<filesystemlist.number; i++) {
+ sigar_file_system_t filesystem = filesystemlist.data[i];
+ Handle<Object> h_filesystem = Object::New();
+ h_filesystem->Set(String::NewSymbol("dir_name"), String::New(filesystem.dir_name));
+ h_filesystem->Set(String::NewSymbol("dev_name"), String::New(filesystem.dev_name));
+ h_filesystem->Set(String::NewSymbol("type_name"), String::New(filesystem.type_name));
+ h_filesystem->Set(String::NewSymbol("sys_type_name"), String::New(filesystem.sys_type_name));
+ h_filesystem->Set(String::NewSymbol("options"), String::New(filesystem.options));
+ h_filesystem->Set(String::NewSymbol("type"), Number::New(filesystem.type));
+ h_filesystem->Set(String::NewSymbol("flags"), Number::New(filesystem.flags));
+ h_filesystemlist->Set(i, h_filesystem);
+ }
+ sigar_file_system_list_destroy(g->sigar, &filesystemlist);
+ return scope.Close(h_filesystemlist);
+ }
+ static Handle<Value> SigarFileSystemUsageGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 1){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_file_system_usage_t filesystemusage;
+ String::Utf8Value dirname(args[0]->ToString());
+ int status = sigar_file_system_usage_get(g->sigar, *dirname, &filesystemusage);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_file_system_usage_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ };
+ Handle<Object> h_filesystemusage = Object::New();
+ Handle<Object> h_diskusage= Object::New();
+ sigar_disk_usage_t diskusage = filesystemusage.disk;
+ h_diskusage->Set(String::NewSymbol("reads"), Number::New(diskusage.reads));
+ h_diskusage->Set(String::NewSymbol("writes"), Number::New(diskusage.writes));
+ h_diskusage->Set(String::NewSymbol("write_bytes"), Number::New(diskusage.write_bytes));
+ h_diskusage->Set(String::NewSymbol("read_bytes"), Number::New(diskusage.read_bytes));
+ h_diskusage->Set(String::NewSymbol("rtime"), Number::New(diskusage.rtime));
+ h_diskusage->Set(String::NewSymbol("wtime"), Number::New(diskusage.wtime));
+ h_diskusage->Set(String::NewSymbol("qtime"), Number::New(diskusage.qtime));
+ h_diskusage->Set(String::NewSymbol("time"), Number::New(diskusage.time));
+ h_diskusage->Set(String::NewSymbol("snaptime"), Number::New(diskusage.snaptime));
+ h_diskusage->Set(String::NewSymbol("service_time"), Number::New(diskusage.service_time));
+ h_diskusage->Set(String::NewSymbol("queue"), Number::New(diskusage.queue));
+ h_filesystemusage->Set(String::NewSymbol("disk"), h_diskusage);
+ h_filesystemusage->Set(String::NewSymbol("use_percent"), Number::New(filesystemusage.use_percent));
+ h_filesystemusage->Set(String::NewSymbol("total"), Number::New(filesystemusage.total));
+ h_filesystemusage->Set(String::NewSymbol("free"), Number::New(filesystemusage.free));
+ h_filesystemusage->Set(String::NewSymbol("used"), Number::New(filesystemusage.used));
+ h_filesystemusage->Set(String::NewSymbol("avail"), Number::New(filesystemusage.avail));
+ h_filesystemusage->Set(String::NewSymbol("files"), Number::New(filesystemusage.files));
+ h_filesystemusage->Set(String::NewSymbol("free_files"), Number::New(filesystemusage.free_files));
+ return scope.Close(h_filesystemusage);
+ }
+ static Handle<Value> SigarFileSystemPingGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_file_system_t filesystem;
+ int status = sigar_file_system_ping(g->sigar, &filesystem);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_file_system_ping error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ };
+ Handle<Object> h_filesystem = Object::New();
+ //printf("TEST %s | %u \n", filesystem.dir_name, filesystem.dev_name);
+ h_filesystem->Set(String::NewSymbol("dir_name"), String::New(filesystem.dir_name));
+ h_filesystem->Set(String::NewSymbol("dev_name"), String::New(filesystem.dev_name));
+ h_filesystem->Set(String::NewSymbol("type_name"), String::New(filesystem.type_name));
+ h_filesystem->Set(String::NewSymbol("sys_type_name"), String::New(filesystem.sys_type_name));
+ h_filesystem->Set(String::NewSymbol("options"), String::New(filesystem.options));
+ h_filesystem->Set(String::NewSymbol("type"), Number::New(filesystem.type));
+ h_filesystem->Set(String::NewSymbol("flags"), Number::New(filesystem.flags));
+ return scope.Close(h_filesystem);
+ }
+ static Handle<Value> SigarNetInfoGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_net_info_t netinfo;
+ int status = sigar_net_info_get(g->sigar, &netinfo);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_file_system_ping error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ };
+ Handle<Object> h_netinfo = Object::New();
+ h_netinfo->Set(String::NewSymbol("default_gateway"), String::New(netinfo.default_gateway));
+ h_netinfo->Set(String::NewSymbol("default_gateway_interface"), String::New(netinfo.default_gateway_interface));
+ h_netinfo->Set(String::NewSymbol("host_name"), String::New(netinfo.host_name));
+ h_netinfo->Set(String::NewSymbol("domain_name"), String::New(netinfo.domain_name));
+ h_netinfo->Set(String::NewSymbol("primary_dns"), String::New(netinfo.primary_dns));
+ h_netinfo->Set(String::NewSymbol("secondary_dns"), String::New(netinfo.secondary_dns));
+ return scope.Close(h_netinfo);
+ }
+ static Handle<String> nodesigar_net_address_to_string(sigar_net_address_t *address)
+ {
+ char addr_str[SIGAR_INET6_ADDRSTRLEN];
+ sigar_net_address_to_string(NULL, address, addr_str);
+ return String::New(addr_str);
+ }
+ static Handle<Value> SigarNetRouteListGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_net_route_list_t netroutelist;
+ unsigned i;
+ int status = sigar_net_route_list_get(g->sigar, &netroutelist);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_net_route_list_get error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ }
+ Handle<Array> h_netroutelist = Array::New(netroutelist.number);
+ for (i=0; i<netroutelist.number; i++) {
+ sigar_net_route_t netroute = netroutelist.data[i];
+ Handle<Object> h_netroute = Object::New();
+
+ //char* h_destination;
+// char h_destination[SIGAR_INET6_ADDRSTRLEN];
+// sigar_net_address_t destination = netroute.destination;
+// sigar_net_address_to_string(NULL, &destination, h_destination);
+// h_netroute->Set(String::NewSymbol("destination"), String::New(h_destination));
+
+// sigar_net_address_t destination = netroute.destination;
+// Handle<String> toto = nodesigar_net_address_to_string(&destination);
+// h_netroute->Set(String::NewSymbol("destination"), toto);
+
+ sigar_net_address_t destination = netroute.destination;
+ Handle<Object> h_destination = Object::New();
+ h_destination->Set(String::NewSymbol("family"), Number::New(destination.family));
+ h_destination->Set(String::NewSymbol("in"), Number::New(destination.addr.in));
+ Handle<Array> h_destination_in6 = Array::New();
+ h_destination_in6->Set(0, Number::New(destination.addr.in6[0]));
+ h_destination_in6->Set(1, Number::New(destination.addr.in6[1]));
+ h_destination_in6->Set(2, Number::New(destination.addr.in6[2]));
+ h_destination_in6->Set(3, Number::New(destination.addr.in6[3]));
+ h_destination->Set(String::NewSymbol("in6"), h_destination_in6);
+ h_netroute->Set(String::NewSymbol("destination"), h_destination);
+
+// char* gateway;
+// sigar_net_address_to_string(g->sigar, &netroute.gateway, gateway);
+// h_netroute->Set(String::NewSymbol("gateway"), String::New(gateway));
+
+ sigar_net_address_t gateway = netroute.gateway;
+ Handle<Object> h_gateway = Object::New();
+ h_gateway->Set(String::NewSymbol("family"), Number::New(gateway.family));
+ h_gateway->Set(String::NewSymbol("in"), Number::New(gateway.addr.in));
+ Handle<Array> h_gateway_in6 = Array::New();
+ h_gateway_in6->Set(0, Number::New(gateway.addr.in6[0]));
+ h_gateway_in6->Set(1, Number::New(gateway.addr.in6[1]));
+ h_gateway_in6->Set(2, Number::New(gateway.addr.in6[2]));
+ h_gateway_in6->Set(3, Number::New(gateway.addr.in6[3]));
+ h_gateway->Set(String::NewSymbol("in6"), h_gateway_in6);
+ h_netroute->Set(String::NewSymbol("gateway"), h_gateway);
+
+// char* mask;
+// sigar_net_address_to_string(g->sigar, &netroute.mask, mask);
+// h_netroute->Set(String::NewSymbol("mask"), String::New(mask));
+
+ sigar_net_address_t mask = netroute.mask;
+ Handle<Object> h_mask = Object::New();
+ h_mask->Set(String::NewSymbol("family"), Number::New(mask.family));
+ h_mask->Set(String::NewSymbol("in"), Number::New(mask.addr.in));
+ Handle<Array> h_mask_in6= Array::New();
+ h_mask_in6->Set(0, Number::New(mask.addr.in6[0]));
+ h_mask_in6->Set(1, Number::New(mask.addr.in6[1]));
+ h_mask_in6->Set(2, Number::New(mask.addr.in6[2]));
+ h_mask_in6->Set(3, Number::New(mask.addr.in6[3]));
+ h_mask->Set(String::NewSymbol("in6"), h_mask_in6);
+ h_netroute->Set(String::NewSymbol("mask"), h_mask);
+
+ h_netroute->Set(String::NewSymbol("flags"), Number::New(netroute.flags));
+ h_netroute->Set(String::NewSymbol("refcnt"), Number::New(netroute.refcnt));
+ h_netroute->Set(String::NewSymbol("use"), Number::New(netroute.use));
+ h_netroute->Set(String::NewSymbol("metric"), Number::New(netroute.metric));
+ h_netroute->Set(String::NewSymbol("mtu"), Number::New(netroute.mtu));
+ h_netroute->Set(String::NewSymbol("window"), Number::New(netroute.window));
+ h_netroute->Set(String::NewSymbol("irtt"), Number::New(netroute.irtt));
+ h_netroute->Set(String::NewSymbol("ifname"), String::New(netroute.ifname));
+ h_netroutelist->Set(i, h_netroute);
+ }
+ sigar_net_route_list_destroy(g->sigar, &netroutelist);
+ return scope.Close(h_netroutelist);
+ }
+ static Handle<Value> SigarNetInterfaceConfigGet(const Arguments& args)
+ {
+ HandleScope scope;
+ if(args.Length() != 0){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ NodeSigar* g = ObjectWrap::Unwrap<NodeSigar>(args.This());
+ sigar_net_interface_config_t netinterface;
+ String::Utf8Value name(args[0]->ToString());
+ int status = sigar_net_interface_config_get(g->sigar, *name, &netinterface);
+ if (status != SIGAR_OK) {
+ char msg[200];
+ sprintf(msg, "sigar_file_system_ping error: %d (%s)", status, sigar_strerror(g->sigar, status));
+ return ThrowException(Exception::TypeError(String::New(msg)));
+ };
+ Handle<Object> h_netinterface = Object::New();
+ h_netinterface->Set(String::NewSymbol("name"), String::New(netinterface.name));
+ h_netinterface->Set(String::NewSymbol("type"), String::New(netinterface.type));
+ h_netinterface->Set(String::NewSymbol("description"), String::New(netinterface.description));
+
+ char* hwaddr;
+ sigar_net_address_to_string(g->sigar, &netinterface.hwaddr, hwaddr);
+ h_netinterface->Set(String::NewSymbol("hwaddr"), String::New(hwaddr));
+ char* address;
+ sigar_net_address_to_string(g->sigar, &netinterface.address, address);
+ h_netinterface->Set(String::NewSymbol("address"), String::New(address));
+ char* destination;
+ sigar_net_address_to_string(g->sigar, &netinterface.destination, destination);
+ h_netinterface->Set(String::NewSymbol("destination"), String::New(destination));
+ char* broadcast;
+ sigar_net_address_to_string(g->sigar, &netinterface.broadcast, broadcast);
+ h_netinterface->Set(String::NewSymbol("broadcast"), String::New(broadcast));
+ char* address6;
+ sigar_net_address_to_string(g->sigar, &netinterface.address6, address6);
+ h_netinterface->Set(String::NewSymbol("address6"), String::New(address6));
+
+// h_netinterface->Set(String::NewSymbol("hwaddr"), NodeSigar::MapSigarNetAddress(netinterface.hwaddr));
+// h_netinterface->Set(String::NewSymbol("address"), NodeSigar::MapSigarNetAddress(netinterface.address));
+// h_netinterface->Set(String::NewSymbol("destination"), NodeSigar::MapSigarNetAddress(netinterface.destination));
+// h_netinterface->Set(String::NewSymbol("broadcast"), NodeSigar::MapSigarNetAddress(netinterface.broadcast));
+// h_netinterface->Set(String::NewSymbol("netmask"), NodeSigar::MapSigarNetAddress(netinterface.netmask));
+// h_netinterface->Set(String::NewSymbol("address6"), NodeSigar::MapSigarNetAddress(netinterface.address6));
+ h_netinterface->Set(String::NewSymbol("prefix6_length"), Number::New(netinterface.prefix6_length));
+ h_netinterface->Set(String::NewSymbol("scope6"), Number::New(netinterface.scope6));
+ h_netinterface->Set(String::NewSymbol("flags"), Number::New(netinterface.flags));
+ h_netinterface->Set(String::NewSymbol("mtu"), Number::New(netinterface.mtu));
+ h_netinterface->Set(String::NewSymbol("metric"), Number::New(netinterface.metric));
+ h_netinterface->Set(String::NewSymbol("tx_queue_len"), Number::New(netinterface.tx_queue_len));
+ return scope.Close(h_netinterface);
+ }
+};
+
+extern "C" void
+init(v8::Handle<v8::Object> target)
+{
+ v8::HandleScope scope;
+ NodeSigar::Initialize(target);
+}
View
31 test/CpuTest.coffee
@@ -0,0 +1,31 @@
+
+assert = require 'assert'
+sigar = require '..'
+
+module.exports =
+ 'Test cpu': (next) ->
+ s = sigar()
+ cpu = s.cpu()
+ # console.log 'cpu', cpu
+ assert.eql [
+ 'user', 'sys', 'nice', 'idle', 'wait', 'irq', 'soft_irq', 'stolen', 'total'
+ ], Object.keys(cpu)
+ next()
+ 'Test cpuList': (next) ->
+ s = sigar()
+ cpus = s.cpuList()
+ # console.log 'cpus', cpus
+ assert.ok cpus.length > 0
+ assert.eql [
+ 'user', 'sys', 'nice', 'idle', 'wait', 'irq', 'soft_irq', 'stolen', 'total'
+ ], Object.keys(cpus[0])
+ next()
+ 'Test cpuInfo': (next) ->
+ s = sigar()
+ cpus = s.cpuInfo()
+ # console.log 'cpus', cpus
+ assert.ok cpus.length > 0
+ assert.eql [
+ 'vendor', 'model', 'mhz', 'mhz_max', 'mhz_min', 'cache_size', 'total_sockets', 'total_cores', 'cores_per_socket'
+ ], Object.keys(cpus[0])
+ next()
View
57 test/FileSystemTest.coffee
@@ -0,0 +1,57 @@
+
+assert = require 'assert'
+sigar = require '..'
+
+module.exports =
+ 'Test fileSystem constants': (next) ->
+ # console.log 'FSTYPE_UNKNOWN', sigar.FSTYPE_UNKNOWN
+ # console.log 'FSTYPE_NONE', sigar.FSTYPE_NONE
+ # console.log 'FSTYPE_LOCAL_DISK', sigar.FSTYPE_LOCAL_DISK
+ # console.log 'FSTYPE_NETWORK', sigar.FSTYPE_NETWORK
+ # console.log 'FSTYPE_RAM_DISK', sigar.FSTYPE_RAM_DISK
+ # console.log 'FSTYPE_CDROM', sigar.FSTYPE_CDROM
+ # console.log 'FSTYPE_SWAP', sigar.FSTYPE_SWAP
+ # console.log 'FSTYPE_MAX', sigar.FSTYPE_MAX
+ assert.eql sigar.FSTYPE_UNKNOWN, 0
+ assert.eql sigar.FSTYPE_NONE, 1
+ assert.eql sigar.FSTYPE_LOCAL_DISK, 2
+ assert.eql sigar.FSTYPE_NETWORK, 3
+ assert.eql sigar.FSTYPE_RAM_DISK, 4
+ assert.eql sigar.FSTYPE_CDROM, 5
+ assert.eql sigar.FSTYPE_SWAP, 6
+ assert.eql sigar.FSTYPE_MAX, 7
+ next()
+ 'Test fileSystemList': (next) ->
+ # Note, doesn't seem to be implemented by thread on osx, return rusage of the current thread
+ s = sigar()
+ fileSystemList = s.fileSystemList()
+ # console.log fileSystemList
+ for fs in fileSystemList
+ if fs.dir_name is '/'
+ foundRoot = true
+ assert.eql [
+ 'dir_name', 'dev_name', 'type_name', 'sys_type_name', 'options', 'type', 'flags'
+ ], Object.keys(fs)
+ assert.ok foundRoot
+ next()
+ 'Test fileSystemUsage': (next) ->
+ # Note, doesn't seem to be implemented by thread on osx, return rusage of the current thread
+ s = sigar()
+ fileSystemUsage = s.fileSystemUsage '/'
+ # console.log 'fileSystemUsage', fileSystemUsage
+ assert.eql [
+ 'disk', 'use_percent', 'total', 'free', 'used', 'avail', 'files', 'free_files'
+ ], Object.keys(fileSystemUsage)
+ assert.eql [
+ 'reads', 'writes', 'write_bytes', 'read_bytes', 'rtime', 'wtime', 'qtime', 'time', 'snaptime', 'service_time', 'queue'
+ ], Object.keys(fileSystemUsage.disk)
+ next()
+ 'Test fileSystemPing': (next) ->
+ # Note, text decoding is wrong
+ s = sigar()
+ fileSystemPing = s.fileSystemPing()
+ # console.log fileSystemPing
+ assert.eql [
+ 'dir_name', 'dev_name', 'type_name', 'sys_type_name', 'options', 'type', 'flags'
+ ], Object.keys(fileSystemPing)
+ next()
View
35 test/NetTest.coffee
@@ -0,0 +1,35 @@
+
+assert = require 'assert'
+sigar = require '..'
+
+module.exports =
+ 'Test netInfo': (next) ->
+ s = sigar()
+ netInfo = s.netInfo()
+ # console.log netInfo
+ assert.eql [
+ 'default_gateway', 'default_gateway_interface', 'host_name', 'domain_name', 'primary_dns', 'secondary_dns'
+ ], Object.keys(netInfo)
+ next()
+ 'Test netRouteList': (next) ->
+ s = sigar()
+ netRouteList = s.netRouteList()
+ assert.ok netRouteList.length > 0
+ for netRoute in netRouteList
+ # console.log netRoute
+ assert.eql [
+ 'destination', 'gateway', 'mask', 'flags', 'refcnt', 'use', 'metric', 'mtu', 'window', 'irtt', 'ifname'
+ ], Object.keys(netRoute)
+ # assert.eql [
+ # 'family', 'addr_in', 'addr_in6', 'addr_mac'
+ # ], Object.keys(netRoute.destination)
+ # assert.eql [
+ # 'family', 'addr_in', 'addr_in6', 'addr_mac'
+ # ], Object.keys(netRoute.gateway)
+ # assert.eql [
+ # 'family', 'addr_in', 'addr_in6', 'addr_mac'
+ # ], Object.keys(netRoute.mask)
+ next()
+
+
+
View
136 test/ProcTest.coffee
@@ -0,0 +1,136 @@
+
+assert = require 'assert'
+sigar = require '..'
+
+module.exports =
+ 'Test procList': (next) ->
+ s = sigar()
+ procList = s.procList()
+ # console.log 'procList', procList
+ assert.ok procList.indexOf process.pid isnt -1
+ next()
+ 'Test procStat': (next) ->
+ s = sigar()
+ procStat = s.procStat()
+ # console.log 'procStat', procStat
+ assert.eql [
+ 'total', 'sleeping', 'running', 'zombie', 'stopped', 'idle', 'threads'
+ ], Object.keys(procStat)
+ next()
+ 'Test procMem': (next) ->
+ s = sigar()
+ procMem = s.procMem process.pid
+ # console.log 'procMem', procMem
+ assert.eql [
+ 'size', 'resident', 'share', 'minor_faults', 'major_faults', 'page_faults'
+ ], Object.keys(procMem)
+ next()
+ 'Test procCred': (next) ->
+ s = sigar()
+ procCred = s.procCred process.pid
+ # console.log 'procCred', procCred
+ assert.eql [
+ 'uid', 'gid', 'euid', 'egid'
+ ], Object.keys(procCred)
+ # procList = s.procList()
+ # for pid in procList
+ # console.log pid, s.procCred pid
+ next()
+ 'Test procTime': (next) ->
+ s = sigar()
+ procTime = s.procTime process.pid
+ # console.log 'procTime', procTime
+ assert.eql [
+ 'start_time', 'user', 'sys', 'total'
+ ], Object.keys(procTime)
+ # procList = s.procList()
+ # for pid in procList
+ # console.log pid, s.procTime pid
+ next()
+ 'Test procCpu': (next) ->
+ s = sigar()
+ procCpu = s.procCpu process.pid
+ # console.log 'procCpu', procCpu
+ assert.eql [
+ 'start_time', 'user', 'sys', 'total', 'last_time', 'percent'
+ ], Object.keys(procCpu)
+ # procList = s.procList()
+ # for pid in procList
+ # console.log pid, s.procCpu pid
+ next()
+ 'Test procState': (next) ->
+ s = sigar()
+ procState = s.procState process.pid
+ # console.log 'procState', procState
+ assert.eql [
+ 'name', 'state', 'ppid', 'tty', 'priority', 'nice', 'processor', 'threads'
+ ], Object.keys(procState)
+ # procList = s.procList()
+ # for pid in procList
+ # state = s.procState pid
+ # console.log state.name, String.fromCharCode(state.state)
+ # console.log state.name, state.state
+ next()
+ 'Test procArgs': (next) ->
+ s = sigar()
+ procArgs = s.procArgs process.pid
+ # console.log 'procArgs', procArgs
+ assert.ok procArgs.length > 0
+ assert.eql procArgs[0], 'node'
+ # procList = s.procList()
+ # for pid in procList
+ # console.log pid, s.procArgs pid
+ next()
+ 'Test procEnv': (next) ->
+ s = sigar()
+ procEnv = s.procEnv process.pid
+ # console.log 'procEnv', procEnv
+ assert.eql procEnv['HOME'], process.env['HOME']
+ next()
+ 'Test procFd': (next) ->
+ s = sigar()
+ try
+ procFd = s.procFd process.pid
+ console.log 'procFd', procFd
+ assert.eql [
+ 'total'
+ ], Object.keys(procFd)
+ catch e # OSX
+ assert.eql e.message, 'sigar_proc_fd_get error: 20001 (This function has not been implemented on this platform)'
+ next()
+ 'Test procExe': (next) ->
+ s = sigar()
+ procExe = s.procExe process.pid
+ # console.log 'procExe', procExe
+ assert.eql [
+ 'name', 'cwd', 'root'
+ ], Object.keys(procExe)
+ # procList = s.procList()
+ # for pid in procList
+ # console.log pid, s.procExe pid
+ next()
+ 'Test procModules': (next) ->
+ s = sigar()
+ procModules = s.procModules process.pid
+ # console.log 'procModules', procModules
+ assert.ok Array.isArray procModules
+ assert.ok procModules.length > 0
+ # procList = s.procList()
+ # for pid in procList
+ # try
+ # console.log pid, s.procModules pid
+ # catch e
+ # console.log pid, e.message
+ next()
+ 'Test threadCpu': (next) ->
+ # Note, doesn't seem to be implemented by thread on osx, return rusage of the current thread
+ s = sigar()
+ # threadCpu = s.threadCpu process.pid
+ # console.log 'threadCpu', threadCpu
+ # procList = s.procList()
+ # for pid in procList
+ # try
+ # console.log pid, s.threadCpu pid+23
+ # catch e
+ # console.log pid, e.message
+ next()
View
63 test/SystemTest.coffee
@@ -0,0 +1,63 @@
+
+assert = require 'assert'
+sigar = require '..'
+
+module.exports =
+ 'Test mem': (next) ->
+ s = sigar()
+ mem = s.mem()
+ #console.log 'mem', mem
+ assert.eql [
+ 'ram', 'total', 'used', 'free', 'actual_used', 'actual_free', 'used_percent', 'free_percent'
+ ], Object.keys(mem)
+ next()
+ 'Test swap': (next) ->
+ s = sigar()
+ swap = s.swap()
+ #console.log 'swap', swap
+ assert.eql [
+ 'total', 'used', 'free','page_in','page_out'
+ ], Object.keys(swap)
+ next()
+ 'Test uptime': (next) ->
+ s = sigar()
+ uptime = s.uptime()
+ # console.log 'uptime', uptime
+ assert.ok uptime > 0
+ next()
+ 'Test loadavg': (next) ->
+ s = sigar()
+ loadavg = s.loadavg()
+ # console.log 'loadavg', loadavg
+ assert.eql loadavg.length, 3
+ assert.ok loadavg[0] > 0
+ next()
+ 'Test resourceLimit': (next) ->
+ s = sigar()
+ resourceLimit = s.resourceLimit()
+ # console.log 'resourceLimit', resourceLimit
+ assert.eql [
+ 'cpu_cur', 'cpu_max',
+ 'file_size_cur', 'file_size_max',
+ 'pipe_size_cur', 'pipe_size_max',
+ 'data_cur', 'data_max',
+ 'stack_cur', 'stack_max',
+ 'core_cur', 'core_max',
+ 'memory_cur', 'memory_max',
+ 'processes_cur', 'processes_max',
+ 'open_files_cur', 'open_files_max',
+ 'virtual_memory_cur', 'virtual_memory_max'
+ ], Object.keys(resourceLimit)
+ next()
+
+
+
+
+
+
+
+
+
+
+
+
View
62 wscript
@@ -0,0 +1,62 @@
+#!/usr/bin/env python
+
+import sys, os
+from os.path import abspath
+
+def set_options(conf):
+ conf.tool_options("compiler_cxx")
+
+def configure(conf):
+ conf.check_tool("compiler_cxx")
+ conf.check_tool('node_addon')
+
+ # hasSigarHeader = conf.check(header_name="sigar.h", errmsg="Missing include files for sigar", mandatory=False)
+ # hasSigarLib = conf.check_cxx(lib="sigar", uselib_store='SIGAR', errmsg="Missing sigar", mandatory=False)
+
+
+ conf.env.append_value("LIBPATH_SIGAR", "/usr/local/lib")
+ conf.env.append_value("CPPPATH_SIGAR", "/usr/local/include/")
+ conf.env.append_value("LIB_SIGAR", "sigar")
+
+ # if not hasSigarHeader and not hasSigarLib:
+ # srcpath = abspath("deps/sigar")
+ # temppath = abspath("build/sigar")
+ # buildpath = abspath("build/Release")
+ # cmd = "rm -rf \"%s\" && cp -rp \"%s\" \"%s\" && cd \"%s\" && cmake . && make && make install"
+ # if os.system(cmd % (temppath, srcpath, temppath, temppath)) != 0:
+ # conf.fatal("Configuring sigar failed.")
+ # conf.check(header_name="sigar.h", errmsg="Missing include files for sigar", mandatory=True)
+ # conf.check_cxx(lib="sigar", uselib_store='SIGAR', errmsg="Missing sigar", mandatory=True)
+
+ # if not hasSigarHeader and not hasSigarLib:
+ # srcpath = abspath("deps/sigar")
+ # temppath = abspath("build/sigar")
+ # buildpath = abspath("build/Release")
+ # cmd = "cp -rp %s %s && cd \"%s\" && cmake -DCMAKE_INSTALL_PREFIX=%s && make && make install"
+ # if os.system(cmd % (srcpath, temppath, temppath, buildpath)) != 0:
+ # conf.fatal("Configuring sigar failed.")
+ # conf.env.append_value("LIBPATH_SIGAR", abspath("build/Release/lib/"))
+ # conf.env.append_value("CPPPATH_SIGAR", abspath("build/Release/include/"))
+ # conf.env.append_value("STATICLIB_SIGAR",["sigar"])
+
+def build(conf):
+ t = conf.new_task_gen('cxx', 'shlib', 'node_addon')
+ t.source = [
+ 'src/node_sigar.cc',
+ # conf.glob('deps/sigar/include/*.h'),
+ # conf.glob('deps/sigar/src/**/*.[ch]'),
+ # conf.glob('deps/sigar/src/**/*.in')
+ ]
+ t.target = 'sigar'
+ t.uselib = 'SIGAR'
+ t.linkflags = ['-lsigar']
+ t.cxxflags = ["-g", "-fPIC", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE", "-Wall"]
+ #t.lib = ['sigar']
+
+ # Dir.glob("bindings/ruby/**/*") +
+ # Dir.glob("include/*.h") +
+ # Dir.glob("src/**/*.[ch]") +
+ # Dir.glob("src/**/*.in")
+ # t.includes = ["lib/", "/usr/local/include/libgtop-2.0/", "/usr/local/include/glib-2.0/",]
+ #t.use = ['SIGAR']
+ #t.include = ['lib/']
Please sign in to comment.
Something went wrong with that request. Please try again.