2023-08-14 15:43:45 +03:00
|
|
|
package osinfo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
|
2023-08-23 22:49:17 +03:00
|
|
|
"git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/utils"
|
2023-08-24 17:44:01 +03:00
|
|
|
"github.com/shirou/gopsutil/mem"
|
2023-08-30 19:24:23 +03:00
|
|
|
"github.com/zcalusic/sysinfo"
|
2023-08-14 15:43:45 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const KB = uint64(1024)
|
|
|
|
|
2023-08-30 19:24:23 +03:00
|
|
|
type Hardware struct {
|
|
|
|
CPU CPU `json:"cpu"`
|
|
|
|
Memory Memory `json:"memory"`
|
|
|
|
Disk Disk `json:"disk"`
|
|
|
|
// Node Node `json:"node"`
|
|
|
|
// Kernel Kernel `json:"kernel"`
|
|
|
|
OS OS `json:"OS"`
|
2023-08-14 15:43:45 +03:00
|
|
|
}
|
|
|
|
|
2023-08-30 19:24:23 +03:00
|
|
|
func (h *Hardware) GetHardwareInfo() {
|
|
|
|
h.getCPUInfo()
|
|
|
|
h.getMemoryInfo()
|
|
|
|
h.getDiskInfo()
|
|
|
|
h.getOSInfo()
|
2023-08-14 15:43:45 +03:00
|
|
|
}
|
|
|
|
|
2023-08-30 19:24:23 +03:00
|
|
|
// return linux Kernel, Node and System Information
|
2023-08-24 17:44:01 +03:00
|
|
|
//
|
2023-08-30 19:24:23 +03:00
|
|
|
// REVIEW are there any command that also compatible with windows?
|
|
|
|
func GetLinuxInfo() map[string]map[string]interface{} {
|
|
|
|
var uname syscall.Utsname
|
|
|
|
if err := syscall.Uname(&uname); err != nil {
|
|
|
|
panic(err)
|
2023-08-14 15:43:45 +03:00
|
|
|
}
|
2023-08-30 19:24:23 +03:00
|
|
|
|
|
|
|
var si sysinfo.SysInfo
|
|
|
|
si.GetSysInfo()
|
|
|
|
return map[string]map[string]interface{}{
|
|
|
|
"Kernel": {
|
|
|
|
"Sysname": utils.Byte2String(uname.Sysname[:]),
|
|
|
|
"Release": si.Kernel.Release,
|
|
|
|
"Version": si.Kernel.Version,
|
|
|
|
"Machine": si.Kernel.Architecture,
|
|
|
|
},
|
|
|
|
"Node": {
|
|
|
|
"Domainname": utils.Byte2String(uname.Domainname[:]),
|
|
|
|
"Hostname": si.Node.Hostname,
|
|
|
|
"MachineID": si.Node.MachineID,
|
|
|
|
"Timezone": si.Node.Timezone,
|
|
|
|
},
|
|
|
|
"OS": { //REVIEW review info in pardus
|
|
|
|
"Name": si.OS.Name,
|
|
|
|
"Vendor": si.OS.Vendor,
|
|
|
|
"Arch": si.OS.Architecture,
|
|
|
|
"Version": si.OS.Version,
|
|
|
|
"Release": si.OS.Release,
|
|
|
|
},
|
2023-08-14 15:43:45 +03:00
|
|
|
}
|
|
|
|
}
|
2023-08-24 17:44:01 +03:00
|
|
|
|
|
|
|
// return memory usage as GiB
|
|
|
|
//
|
|
|
|
// TODO also implement swap usage
|
|
|
|
func GetMemoryUsage() map[string]float64 {
|
|
|
|
v, _ := mem.VirtualMemory()
|
|
|
|
return map[string]float64{
|
|
|
|
"total": utils.Byte2GiB(v.Total),
|
|
|
|
"free": utils.Byte2GiB(v.Free),
|
|
|
|
"used": utils.Byte2GiB(v.Used),
|
|
|
|
"avaliable": utils.Byte2GiB(v.Available),
|
|
|
|
"cached": utils.Byte2GiB(v.Cached),
|
|
|
|
}
|
|
|
|
}
|