Exemple #1
0
func GetMemInfo() (result *MemInfo, err error) {
	defer util.Panic2Error(&err)
	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: &Mem{
			Ram:          uint64(mem.ram),
			Total:        uint64(mem.total),
			Used:         uint64(mem.used),
			Free:         uint64(mem.free),
			ActualUsed:   uint64(mem.actual_used),
			ActualFree:   uint64(mem.actual_free),
			UsedPerecent: float64(mem.used_percent),
			FreePerecent: float64(mem.free_percent),
		},
		Swap: &Swap{
			Total:   uint64(swap.total),
			Used:    uint64(swap.used),
			Free:    uint64(swap.free),
			PageIn:  uint64(swap.page_in),
			PageOut: uint64(swap.page_out),
		},
	}, nil

}
Exemple #2
0
func GetNetIfaceUsageInfo(netIfaceName string) (netifaceInfo *NetIfaceUsageInfo, err error) {
	defer util.Panic2Error(&err)

	c_netifaceName := C.CString(netIfaceName)
	defer Free(c_netifaceName)

	var c_sigar_net_interface_stat_t C.sigar_net_interface_stat_t
	if status := int(C.sigar_net_interface_stat_get(GetSigarHandle(), c_netifaceName, &c_sigar_net_interface_stat_t)); status != SIGAR_OK {
		return nil, fmt.Errorf("Failed to rertieve network interface status for: %v with error: %v", netIfaceName, status)
	}

	return &NetIfaceUsageInfo{
		RxPackets:    uint64(c_sigar_net_interface_stat_t.rx_packets),
		RxBytes:      uint64(c_sigar_net_interface_stat_t.rx_bytes),
		RxErrors:     uint64(c_sigar_net_interface_stat_t.rx_errors),
		RxDropped:    uint64(c_sigar_net_interface_stat_t.rx_dropped),
		RxOverruns:   uint64(c_sigar_net_interface_stat_t.rx_overruns),
		RxFrame:      uint64(c_sigar_net_interface_stat_t.rx_frame),
		TxPackets:    uint64(c_sigar_net_interface_stat_t.tx_packets),
		TxBytes:      uint64(c_sigar_net_interface_stat_t.tx_bytes),
		TxErrors:     uint64(c_sigar_net_interface_stat_t.tx_errors),
		TxDropped:    uint64(c_sigar_net_interface_stat_t.tx_dropped),
		TxOverruns:   uint64(c_sigar_net_interface_stat_t.tx_overruns),
		TxCollisions: uint64(c_sigar_net_interface_stat_t.tx_collisions),
		TxCarrier:    uint64(c_sigar_net_interface_stat_t.tx_carrier),
		Speed:        uint64(c_sigar_net_interface_stat_t.speed),
	}, nil

}
Exemple #3
0
func getProcInfos(procList *C.sigar_proc_list_t, infoTypesMask ProcInfoType, sigar *C.sigar_t) (processes *ProcessInfoList, err error) {
	defer util.Panic2Error(&err)

	noOfProcesses := int(procList.number)
	pids := *(*[]C.sigar_pid_t)(CArr2SlicePtr(noOfProcesses, procList.data))

	procInfos := make([]*ProcessInfo, noOfProcesses)
	var errors, index int
	var sysMemInfoOut *MemInfo
	for _, pid := range pids {

		procInfo, sysMemInfo, err := _getProcInfo(uint64(pid), infoTypesMask, sysMemInfoOut, sigar)

		if err != nil {
			log.ErrorS(err.Error())
			errors = errors + 1
			continue
		}

		if sysMemInfoOut == nil {
			sysMemInfoOut = sysMemInfo
		}

		procInfos[index] = procInfo
		index = index + 1
	}

	if errors > 0 {
		newSize := noOfProcesses - errors
		procInfos = append(([]*ProcessInfo)(nil), procInfos[:newSize]...)
		log.ErrorS("%v errors have occurred, prunning array size from %v to %v index is: %v, newSize: %v", errors, noOfProcesses, newSize, index, len(procInfos))
	}

	return &ProcessInfoList{procInfos, sysMemInfoOut}, nil
}
Exemple #4
0
func GetTotalMem() (total uint64, err error) {
	defer util.Panic2Error(&err)
	var sigar *C.sigar_t = GetSigarHandle()
	var mem C.sigar_mem_t
	status := int(C.sigar_mem_get(sigar, &mem))
	if status != SIGAR_OK {
		return 0, fmt.Errorf("Failed to rertieve sys mem info with error: %v", status)
	}
	return uint64(mem.total), nil
}
Exemple #5
0
func GetAllProcInfos(infoTypesMask ProcInfoType) (processes *ProcessInfoList, err error) {

	var procList C.sigar_proc_list_t
	defer util.Panic2Error(&err)
	defer C.sigar_proc_list_destroy(sigar, &procList)

	sigar := GetSigarHandle()

	C.sigar_proc_list_get(sigar, &procList)

	return getProcInfos(&procList, infoTypesMask, sigar)
}
Exemple #6
0
func GetUptimeInfo() (result *UptimeInfo, err error) {
	defer util.Panic2Error(&err)
	var sigar *C.sigar_t = GetSigarHandle()

	uptime, err := _uptime(sigar)
	if err != nil {
		return nil, err
	}

	loadAvg, err := _loadAvg(sigar)
	if err != nil {
		return nil, err
	}

	return &UptimeInfo{Time: time.Now(), Uptime: uptime, LoadAvg: loadAvg}, nil
}
Exemple #7
0
func GetSysInfo() (sysinfo *SysInfo, err error) {
	defer util.Panic2Error(&err)

	var sigar *C.sigar_t = GetSigarHandle()
	var c_sysInfo C.sigar_sys_info_t
	C.sigar_sys_info_get(sigar, &c_sysInfo)

	return &SysInfo{
		Name:           C.GoString(&c_sysInfo.name[0]),
		Version:        C.GoString(&c_sysInfo.version[0]),
		Arch:           C.GoString(&c_sysInfo.arch[0]),
		Machine:        C.GoString(&c_sysInfo.machine[0]),
		Description:    C.GoString(&c_sysInfo.description[0]),
		PatchLevel:     C.GoString(&c_sysInfo.patch_level[0]),
		Vendor:         C.GoString(&c_sysInfo.vendor[0]),
		VendorName:     C.GoString(&c_sysInfo.vendor_name[0]),
		VendorVersion:  C.GoString(&c_sysInfo.vendor_version[0]),
		VendorCodeName: C.GoString(&c_sysInfo.vendor_code_name[0]),
	}, nil
}
Exemple #8
0
//------------------------------------------------------------------------------------------------------------------------------------------------
//CpuUsageInfo (uint64) CpuUsageInfo (uint64) CpuUsageInfo (uint64) CpuUsageInfo (uint64) CpuUsageInfo (uint64) CpuUsageInfo (uint64)
//------------------------------------------------------------------------------------------------------------------------------------------------
func GetCpuUsageInfo() (result *CpuUsageInfo, err error) {
	defer util.Panic2Error(&err)

	c_cpu_t, err := _getCpu(GetSigarHandle())
	if err != nil {
		return nil, err
	}

	return &CpuUsageInfo{
		User:    uint64(c_cpu_t.user),
		Sys:     uint64(c_cpu_t.sys),
		Nice:    uint64(c_cpu_t.nice),
		Idle:    uint64(c_cpu_t.idle),
		Wait:    uint64(c_cpu_t.wait),
		Irq:     uint64(c_cpu_t.irq),
		SoftIrq: uint64(c_cpu_t.soft_irq),
		Stolen:  uint64(c_cpu_t.stolen),
		Total:   uint64(c_cpu_t.total),
	}, nil
}
Exemple #9
0
func GetProcStatInfo() (procStatInfo *ProcStatInfo, err error) {
	defer util.Panic2Error(&err)
	var c_procStat C.sigar_proc_stat_t

	sigar := GetSigarHandle()
	if status := int(C.sigar_proc_stat_get(sigar, &c_procStat)); status != SIGAR_OK {
		return nil, fmt.Errorf("Failed to retrieve system proc stat info with error: %v", status)
	}

	return &ProcStatInfo{
		Total:    uint64(c_procStat.total),
		Sleeping: uint64(c_procStat.sleeping),
		Running:  uint64(c_procStat.running),
		Zombie:   uint64(c_procStat.zombie),
		Stopped:  uint64(c_procStat.stopped),
		Idle:     uint64(c_procStat.idle),
		Threads:  uint64(c_procStat.threads),
	}, nil

}
Exemple #10
0
func ExecutePtql(ptql string, infoTypesMask ProcInfoType) (result []*ProcessInfo, err error) {
	defer util.Panic2Error(&err)

	var query *C.sigar_ptql_query_t
	var queryError *C.sigar_ptql_error_t
	var proclist C.sigar_proc_list_t

	sigar := GetSigarHandle()
	ptqlC := C.CString(ptql)
	//defer C.sigar_ptql_query_destroy(query)
	defer C.sigar_proc_list_destroy(sigar, &proclist)
	defer C.sigar_ptql_re_impl_set(sigar, nil, nil)
	defer Free(ptqlC)

	if status := C.sigar_ptql_query_create(&query, ptqlC, queryError); status != SIGAR_OK {
		err = fmt.Errorf("Failed to create sigar ptql query for: %v with errorcode: %v", ptql, status)
		return nil, err
	}

	C.sigar_ptql_query_find(sigar, query, &proclist)
	C.sigar_ptql_query_destroy(query)

	/*
		noOfProcesses := int(proclist.number)
		result = make([]*ProcessInfo, noOfProcesses)
		pids :=  *(*[]C.sigar_pid_t) (CArr2SlicePtr(noOfProcesses, proclist.data))
		for i,pid := range pids {
			procInfo,err := GetProcInfo(uint64(pid), infoTypesMask)
			if err != nil {
				return nil,err
			}
			result[i] =  procInfo
		}

		return result,nil*/

	processes, err := getProcInfos(&proclist, infoTypesMask, sigar)
	return processes.Processes, err
}
Exemple #11
0
//------------------------------------------------------------------------------------------------------------------------------------------------
// CpuPctUsageInfo (Float64) CpuPctUsageInfo (Float64) CpuPctUsageInfo (Float64) CpuPctUsageInfo (Float64) CpuPctUsageInfo (Float64)
//------------------------------------------------------------------------------------------------------------------------------------------------
func GetCpuPctUsageInfo() (result *CpuPctUsageInfo, err error) {
	defer util.Panic2Error(&err)

	sigar := GetSigarHandle()
	cur_c_cpu_t, err := _getCpu(sigar)
	if err != nil {
		return nil, err
	}

	if prevSampleTs == 0 {

		_prevCpuUsage = *cur_c_cpu_t
		time.Sleep(500 * time.Millisecond)

		cur_c_cpu_t, err = _getCpu(sigar)
		if err != nil {
			return nil, err
		}
	}

	var c_pct_cpu_t C.sigar_cpu_perc_t
	C.sigar_cpu_perc_calculate(&_prevCpuUsage, cur_c_cpu_t, &c_pct_cpu_t)
	_prevCpuUsage = *cur_c_cpu_t
	prevSampleTs = util.NowEpoch()

	return &CpuPctUsageInfo{
		User:     float64(c_pct_cpu_t.user),
		Sys:      float64(c_pct_cpu_t.sys),
		Nice:     float64(c_pct_cpu_t.nice),
		Idle:     float64(c_pct_cpu_t.idle),
		Wait:     float64(c_pct_cpu_t.wait),
		Irq:      float64(c_pct_cpu_t.irq),
		SoftIrq:  float64(c_pct_cpu_t.soft_irq),
		Stolen:   float64(c_pct_cpu_t.stolen),
		Combined: float64(c_pct_cpu_t.combined),
	}, nil

}
Exemple #12
0
func GetNetInfo() (netInfo *NetInfo, err error) {
	defer util.Panic2Error(&err)

	sigar := GetSigarHandle()
	var c_netInfo C.sigar_net_info_t
	C.sigar_net_info_get(sigar, &c_netInfo)

	//[512]
	var c_fqdn [C.SIGAR_FQDN_LEN]C.char
	if status := C.sigar_fqdn_get(sigar, &c_fqdn[0], C.SIGAR_FQDN_LEN); status != SIGAR_OK {
		return nil, fmt.Errorf("Failed to retrieve fqdn due to " + err.Error())
	}

	return &NetInfo{
		DefaultGateway:          C.GoString(&c_netInfo.default_gateway[0]),
		DefaultGatewayInterface: C.GoString(&c_netInfo.default_gateway_interface[0]),
		HostName:                C.GoString(&c_netInfo.host_name[0]),
		DomainName:              C.GoString(&c_netInfo.domain_name[0]),
		PrimaryDns:              C.GoString(&c_netInfo.primary_dns[0]),
		SecondaryDns:            C.GoString(&c_netInfo.secondary_dns[0]),
		Fqdn:                    C.GoString(&c_fqdn[0]),
	}, nil

}