to link with sigar c code
This commit is contained in:
ilanuriel 2016-01-04 12:23:11 +02:00
parent 0f0dbe34ce
commit 9158290982
19 changed files with 988 additions and 0 deletions

29
go_bindings/gotoc/avg.go Normal file
View File

@ -0,0 +1,29 @@
package gotoc
import (
)
/*
#include "../../../../../Include/sigar.h"
*/
import "C"
func Avg()C.sigar_loadavg_t{
var sigar *C.sigar_t=GetSigarHandle()
var avg C.sigar_loadavg_t
C.sigar_loadavg_get(sigar, &avg)
return avg
}

52
go_bindings/gotoc/cpu.go Normal file
View File

@ -0,0 +1,52 @@
package gotoc
import (
"unsafe"
)
//#include "../../../../../Include/sigar.h"
import "C"
func Cpu() (C.sigar_cpu_list_t,[]C.sigar_cpu_t){
var sigar *C.sigar_t=GetSigarHandle()
var cpulists C.sigar_cpu_list_t
C.sigar_cpu_list_get(sigar, &cpulists)
var length int=int(cpulists.number)
usp:=GetGoSlice(length, unsafe.Pointer(cpulists.data))
var goCpu []C.sigar_cpu_t
goCpu = *(*[]C.sigar_cpu_t)(unsafe.Pointer(&usp))
C.sigar_cpu_list_destroy(sigar, &cpulists)
return cpulists, goCpu
}

View File

@ -0,0 +1,68 @@
package gotoc
import (
"fmt"
"unsafe"
)
/*
#include "../../../../../Include/sigar.h"
*/
import "C"
type CpuInfo struct{
Vendor string
Model string
Mhz int
MhzMax int
MhzMin int
CacheSize uint
TotalSockets int
TotalCores int
CorePerSocket int
}
func GetCpuInfo()(result []*CpuInfo, err error){
defer func() {
if r := recover() ; r != nil {
err = fmt.Errorf("Failed to rertieve cpu info due to: %v", err)
}
}()
var sigar *C.sigar_t=GetSigarHandle()
var cpuInfoList C.sigar_cpu_info_list_t
C.sigar_cpu_info_list_get(sigar, &cpuInfoList)
var length int=int(cpuInfoList.number)
cCpuInfo:=GetGoSlice(length, unsafe.Pointer(cpuInfoList.data))
var goCpuInfo []C.sigar_cpu_info_t
goCpuInfo= *(*[]C.sigar_cpu_info_t)(unsafe.Pointer(&cCpuInfo))
C.sigar_cpu_info_list_destroy(sigar,&cpuInfoList)
result = make([]*CpuInfo, len(goCpuInfo))
for i,sigarCpuinfo := range goCpuInfo {
result[i] = &CpuInfo{
Vendor : C.GoString(&sigarCpuinfo.vendor[0]),
Model : C.GoString(&sigarCpuinfo.model[0]),
Mhz : int(sigarCpuinfo.mhz),
MhzMax : int(sigarCpuinfo.mhz_max),
MhzMin : int(sigarCpuinfo.mhz_min),
CacheSize : uint(sigarCpuinfo.cache_size),
TotalSockets : int(sigarCpuinfo.total_sockets),
TotalCores : int(sigarCpuinfo.total_cores),
CorePerSocket : int(sigarCpuinfo.cores_per_socket),
}
}
return result,nil
}

View File

@ -0,0 +1,45 @@
package gotoc
import (
"unsafe"
)
/*
#include "../../../../../Include/sigar.h"
*/
import "C"
func FileInfo() []C.sigar_file_system_t{
var sigar *C.sigar_t=GetSigarHandle()
var fileSystemList C.sigar_file_system_list_t
//C.fileInfo(sigar,&fileSystemList)
C.sigar_file_system_list_get(sigar, &fileSystemList);
var length int=int(fileSystemList.number)
cFs:=GetGoSlice(length, unsafe.Pointer(fileSystemList.data))
var goFs []C.sigar_file_system_t
goFs = *(*[]C.sigar_file_system_t)(unsafe.Pointer(&cFs))
//fmt.Printf("%v\n", C.GoString(&goFs[1].dir_name[0]))
C.sigar_file_system_list_destroy(sigar, &fileSystemList);
return goFs
}

View File

@ -0,0 +1,58 @@
package gotoc
import (
"fmt"
"reflect"
"unsafe"
)
/*
#include "../../../../../Include/sigar.h"
*/
import "C"
var sigar *C.sigar_t=nil
func GetSigarHandle()*C.sigar_t{
if(sigar!=nil){
return sigar
}
sigar=new (C.sigar_t)
ret:=C.sigar_open(&sigar)
if(ret!=C.SIGAR_OK){
return nil
}
return sigar
}
func CloseSigarHandle(sigar *C.sigar_t) {
if(sigar!=nil){
C.sigar_close(sigar)
return
}
fmt.Println("Trying to close a nil handel, ignoring")
}
func GetGoSlice(number int, pointer unsafe.Pointer ) reflect.SliceHeader{
var length int=int(number)
cObj := reflect.SliceHeader{
Data: uintptr(pointer),
Len: length,
Cap: length,
}
return cObj
}

11
go_bindings/gotoc/main.go Normal file
View File

@ -0,0 +1,11 @@
package gotoc
import (
)
func main(){
NetInfo()
}

48
go_bindings/gotoc/mem.go Normal file
View File

@ -0,0 +1,48 @@
package gotoc
import (
)
/*
#include "../../../../../Include/sigar.h"
*/
import "C"
type Info struct {
Free int64
}
type MemInfo struct {
Mem *Info
Swap *Info
}
func GetMemInfo() (*MemInfo, error){
var sigar *C.sigar_t=GetSigarHandle()
var mem C.sigar_mem_t
var swap C.sigar_swap_t
C.sigar_mem_get( sigar, &mem)
C.sigar_swap_get(sigar, &swap);
return &MemInfo{
Mem : &Info{ Free : int64(mem.free)},
Swap : &Info{ Free : int64(swap.free)},
},nil
}

View File

@ -0,0 +1,107 @@
package gotoc
import (
"unsafe"
)
/*
#include "../../../../../Include/sigar.h"
#include<stdlib.h>
#include <stdio.h>
typedef unsigned char byte;
void translateNetConfig(sigar_t *sigar){
sigar_net_interface_config_t config;
sigar_net_interface_config_get(sigar, "eth10", &config);
sigar_uint32_t a=config.address.addr.in;
char buf[180];
unsigned char* ucp = (unsigned char *)&a;
sprintf(buf, "%d.%d.%d.%d", ucp[0] & 0xff, ucp[1] & 0xff, ucp[2] & 0xff, ucp[3] & 0xff);
printf("%s\n",buf);
}
*/
import "C"
func GetNetConfig()(C.sigar_net_interface_config_t,[]C.sigar_net_interface_config_t,[]C.sigar_net_interface_stat_t){
var sigar *C.sigar_t=GetSigarHandle()
var netlistconf C.sigar_net_interface_list_t
var netconfPrimary C.sigar_net_interface_config_t
C.sigar_net_interface_config_primary_get(sigar, &netconfPrimary)
C.sigar_net_interface_list_get(sigar, &netlistconf)
var length int=int(netlistconf.number)
goInterfacesNames := ((*[1 << 30]*C.char)(unsafe.Pointer(netlistconf.data)))[:length]
//cinteraces:=GetGoSlice(length, unsafe.Pointer(netlistconf.data))
//goInterfacesNames := *(*[]C.char)(unsafe.Pointer(&cinteraces))
var netconf =make([]C.sigar_net_interface_config_t,length)
var netstat =make([]C.sigar_net_interface_stat_t,length)
for i:=0;i<length;i++{
C.sigar_net_interface_config_get(sigar, goInterfacesNames[i],
&netconf[i])
C.sigar_net_interface_stat_get(sigar, goInterfacesNames[i],
&netstat[i])
}
C.sigar_net_interface_list_destroy(sigar, &netlistconf)
return netconfPrimary, netconf, netstat
}
func GetNetInterfaceConfig(){
//_,netconf,_:=GetNetConfig()
var sigar *C.sigar_t=GetSigarHandle()
C.translateNetConfig(sigar)
}
type NetworkService struct {
name string
RawIdentifier string
Id string
Broadcast string
Address string
NetMask string
Flags string
Mtu string
Mac string
}

View File

@ -0,0 +1,50 @@
package gotoc
import (
"unsafe"
)
/*
#include "../../../../../Include/sigar.h"
*/
import "C"
func NetConections(flags int)(C.sigar_net_connection_list_t,
[]C.sigar_net_connection_t){
var sigar *C.sigar_t=GetSigarHandle()
var netlistconn C.sigar_net_connection_list_t
C.sigar_net_connection_list_get(sigar, &netlistconn, C.int(flags))
var length int=int(netlistconn.number)
cconnection:=GetGoSlice(length, unsafe.Pointer(netlistconn.data))
var goConnections []C.sigar_net_connection_t
goConnections = *(*[]C.sigar_net_connection_t)(unsafe.Pointer(&cconnection))
C.sigar_net_connection_list_destroy(sigar, &netlistconn)
return netlistconn, goConnections
}

View File

@ -0,0 +1,28 @@
package gotoc
import (
)
/*
#include "../../../../../Include/sigar.h"
*/
import "C"
func NetInfo() C.sigar_net_info_t{
var sigar *C.sigar_t=GetSigarHandle()
var netInfo C.sigar_net_info_t
C.sigar_net_info_get(sigar, &netInfo)
return netInfo
}

View File

@ -0,0 +1,57 @@
package gotoc
import (
"unsafe"
)
/*
#include "../../../../../Include/sigar.h"
*/
import "C"
func NetRoute() (C.sigar_net_route_list_t ,
[]C.sigar_net_route_t){
sigar:=GetSigarHandle()
var netlist C.sigar_net_route_list_t
C.sigar_net_route_list_get(sigar, &netlist)
var length int=int(netlist.number)
var goNetRoute =make([]C.sigar_net_route_t, length)
croute:=GetGoSlice(length, unsafe.Pointer(netlist.data))
goNetRoute = *(*[]C.sigar_net_route_t)(unsafe.Pointer(&croute))
C.sigar_net_route_list_destroy(sigar, &netlist)
return netlist, goNetRoute
}

View File

@ -0,0 +1,45 @@
package gotoc
import (
)
/*
#include "../../../../../Include/sigar.h"
*/
import "C"
func NetUtils(flags int)(C.sigar_net_stat_t,
C.sigar_tcp_t,
C.sigar_nfs_client_v2_t,
C.sigar_nfs_server_v2_t,
C.sigar_nfs_client_v3_t,
C.sigar_nfs_server_v3_t){
var sigar *C.sigar_t=GetSigarHandle()
var netstat C.sigar_net_stat_t
var tcp C.sigar_tcp_t
var nfs2 C.sigar_nfs_client_v2_t
var nfsServer2 C.sigar_nfs_server_v2_t
var nfs3 C.sigar_nfs_client_v3_t
var nfsServer3 C.sigar_nfs_server_v3_t
C.sigar_net_stat_get( sigar,&netstat,C.int(flags))
C.sigar_tcp_get(sigar,&tcp)
C.sigar_nfs_client_v2_get(sigar, &nfs2)
C.sigar_nfs_server_v2_get (sigar, &nfsServer2)
C.sigar_nfs_client_v3_get(sigar, &nfs3)
C.sigar_nfs_server_v3_get (sigar, &nfsServer3)
return netstat, tcp, nfs2,nfsServer2, nfs3,nfsServer3
}

107
go_bindings/gotoc/ps.go Normal file
View File

@ -0,0 +1,107 @@
package gotoc
import (
"unsafe"
)
/*
#include "../../../../../Include/sigar.h"
*/
import "C"
func Ps() ([]C.sigar_proc_state_t ,
[]C.sigar_proc_time_t,
[]C.sigar_proc_mem_t,
[]C.sigar_proc_disk_io_t,
[]C.sigar_proc_cumulative_disk_io_t,
[]C.sigar_proc_cred_t,
[]C.sigar_proc_cred_name_t,
[]C.sigar_proc_cpu_t,
[]C.sigar_proc_args_t,
[]C.sigar_proc_fd_t,
[]C.sigar_proc_exe_t){
sigar:=GetSigarHandle()
var procList C.sigar_proc_list_t
C.sigar_proc_list_get(sigar, &procList)
var length int=int(procList.number)
var pstate =make([]C.sigar_proc_state_t, length)
var ptime =make([]C.sigar_proc_time_t,length)
var pmem =make([]C.sigar_proc_mem_t,length)
var pdiskio =make([]C.sigar_proc_disk_io_t,length)
var pcumdiskio =make([]C.sigar_proc_cumulative_disk_io_t,length)
var pcred =make([]C.sigar_proc_cred_t,length)
var pcredname =make([]C.sigar_proc_cred_name_t,length)
var pcpu =make([]C.sigar_proc_cpu_t,length)
var pargs =make([]C.sigar_proc_args_t,length)
var pfd =make([]C.sigar_proc_fd_t,length)
var pexe =make([]C.sigar_proc_exe_t,length)
cpid:=GetGoSlice(length, unsafe.Pointer(procList.data))
var goPid []C.sigar_pid_t
goPid = *(*[]C.sigar_pid_t)(unsafe.Pointer(&cpid))
for i:=0;i<length;i++{
C.sigar_proc_state_get(sigar, goPid[i], &pstate[i])
C.sigar_proc_time_get(sigar, goPid[i], &ptime[i])
C.sigar_proc_mem_get(sigar, goPid[i], &pmem[i])
C.sigar_proc_disk_io_get(sigar, goPid[i], &pdiskio[i])
C.sigar_proc_cumulative_disk_io_get(sigar, goPid[i], &pcumdiskio[i])
C.sigar_proc_cred_get(sigar, goPid[i], &pcred[i])
C.sigar_proc_cred_name_get(sigar, goPid[i], &pcredname[i])
C.sigar_proc_cpu_get(sigar, goPid[i], &pcpu[i])
C.sigar_proc_fd_get(sigar, goPid[i], &pfd[i])
C.sigar_proc_exe_get(sigar, goPid[i], &pexe[i])
C.sigar_proc_args_get(sigar, goPid[i], &pargs[i])
C.sigar_proc_args_destroy(sigar, &pargs[i])
}
C.sigar_proc_list_destroy(sigar, &procList)
return pstate,
ptime,
pmem,
pdiskio,
pcumdiskio,
pcred,
pcredname,
pcpu,
pargs,
pfd,
pexe
}

View File

@ -0,0 +1,25 @@
package gotoc
import (
)
/*
#include "../../../../../Include/sigar.h"
*/
import "C"
func Resources()C.sigar_resource_limit_t{
var resources C.sigar_resource_limit_t
C.sigar_resource_limit_get(sigar, &resources)
return resources
}

View File

@ -0,0 +1,55 @@
package gotoc
import (
"unsafe"
)
/*
#include "../../../../../Include/sigar.h"
#cgo CFLAGS: -I../../../../../Include
#cgo LDFLAGS: C:/Users/iuriel/workspace/vmeerkat/leap-project/Include/sigar-amd64-winnt.dll
*/
import "C"
func SysInfo() (C.sigar_sys_info_t, []C.sigar_who_t) {
var sigar *C.sigar_t=GetSigarHandle()
var sysInfo C.sigar_sys_info_t
var wholist C.sigar_who_list_t
C.sigar_sys_info_get(sigar, &sysInfo)
C.sigar_who_list_get(sigar,&wholist)
var length int=int(wholist.number)
var goWho =make([]C.sigar_who_t, length)
cwho:=GetGoSlice(length, unsafe.Pointer(wholist.data))
goWho = *(*[]C.sigar_who_t)(unsafe.Pointer(&cwho))
C.sigar_who_list_destroy(sigar , &wholist)
return sysInfo, goWho
}

View File

@ -0,0 +1,33 @@
package gotoc
import (
)
/*
#include "../../../../../Include/sigar.h"
*/
import "C"
func Threads(id int64)C.sigar_thread_cpu_t{
var threadCpu C.sigar_thread_cpu_t
var sigar *C.sigar_t=GetSigarHandle()
C.sigar_thread_cpu_get(sigar , C.sigar_uint64_t(id) ,&threadCpu)
return threadCpu
}

View File

@ -0,0 +1,27 @@
package gotoc
import (
)
/*
#include "../../../../../Include/sigar.h"
*/
import "C"
func UpTime()C.sigar_uptime_t{
var sigar *C.sigar_t=GetSigarHandle()
var uptime C.sigar_uptime_t
C.sigar_uptime_get(sigar, &uptime)
return uptime
}

123
go_bindings/gotoc/usage.go Normal file
View File

@ -0,0 +1,123 @@
package gotoc
import (
"fmt"
"unsafe"
)
/*
#include <stdlib.h>
#include "../../../../../Include/sigar.h"
*/
import "C"
type sigar_file_system_usage_t struct {
UsedPct float64
Total int64
Free int64
Used int64
Avail int64
Files int64
FreeFiles int64
}
type FsUsage struct {
UsedPct float64
Total int64
Free int64
Used int64
Avail int64
Files int64
FreeFiles int64
}
func (this *FsUsage) String() string { return fmt.Sprintf("used: %v, total: %v, free: %v, used: %v, avail: %v, files: %v, freeFiles: %v", this.UsedPct, this.Total, this.Free, this.Used, this.Avail, this.Files, this.FreeFiles) }
type DiskUsage struct {
Reads int64
Writes int64
WriteBytes int64
ReadBytes int64
Rtime int64
Wtime int64
Qtime int64
Time int64
SnapTime int64
ServiceTime float64
Queue float64
}
type FsUsageInfo struct {
Fs *FsUsage
Disk *DiskUsage
}
func GetFsUsageInfo(dirName string, diskName string)(fsUsageInfo *FsUsageInfo,err error){
var sigar *C.sigar_t=GetSigarHandle()
//TODO: noticed that sigar_file_system_usage_t contains a sigar_disk_usage_t member, should there be two invocations?
fsUsage, err := _getFsUsage(dirName, sigar)
if err != nil {
return nil, err
}
diskUsage, err := _getDiskUsage(diskName, sigar) ;
if err != nil {
return nil,err
}
return &FsUsageInfo {fsUsage, diskUsage}, err
}
func GetFsUsage(dirName string) (*FsUsage, error) {
return _getFsUsage(dirName, GetSigarHandle())
}
func _getFsUsage(dirName string, sigar *C.sigar_t) (out *FsUsage, err error) {
var fsusage C.sigar_file_system_usage_t
dir:=C.CString(dirName)
C.free(unsafe.Pointer(dir))
C.sigar_file_system_usage_get(sigar , dir, &fsusage)
out = &FsUsage{
float64(fsusage.use_percent),
int64(fsusage.total),
int64(fsusage.free),
int64(fsusage.used),
int64(fsusage.avail),
int64(fsusage.files),
int64(fsusage.free_files),
}
fmt.Printf("Fs usage for dir %v is %v", dirName, out)
return out,nil
}
func GetDiskUsage(diskName string) (*DiskUsage, error) {
return _getDiskUsage(diskName, GetSigarHandle())
}
func _getDiskUsage(diskName string, sigar *C.sigar_t) (*DiskUsage, error) {
var diskusage C.sigar_disk_usage_t
disk:=C.CString(diskName)
defer C.free(unsafe.Pointer(disk))
C.sigar_disk_usage_get(sigar , disk, &diskusage)
return &DiskUsage {
int64(diskusage.reads),
int64(diskusage.writes),
int64(diskusage.write_bytes),
int64(diskusage.read_bytes),
int64(diskusage.rtime),
int64(diskusage.wtime),
int64(diskusage.qtime),
int64(diskusage.time),
int64(diskusage.snaptime),
float64(diskusage.service_time),
float64(diskusage.queue),
},nil
}

View File

@ -0,0 +1,20 @@
package gotoc
import (
"unsafe"
)
//#include <stdlib.h>
import "C"
func SigarStringToGoString(cstring *C.char) string{
var theString string = C.GoString(cstring)
defer C.free(unsafe.Pointer(cstring))
return theString
}