Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add go-sysinfo cmd #103

Open
eozer opened this issue Oct 24, 2021 · 1 comment
Open

Add go-sysinfo cmd #103

eozer opened this issue Oct 24, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@eozer
Copy link

eozer commented Oct 24, 2021

Hi,

As a new user of this package, I had to spend a little more time to understand what kind of information go-sysinfo was able to provide, say compared to other packages, e.g., https://github.com/zcalusic/sysinfo.

A simple utility command line program would help new users to check quickly all the available information. For example, something like below.

package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/elastic/go-sysinfo"
	"github.com/elastic/go-sysinfo/types"
)

type SysInfo struct {
	ProcessInfo    types.ProcessInfo     `json:"process_info"`
	CPUTimes       types.CPUTimes        `json:"cpu_times"`
	MemoryInfo     types.MemoryInfo      `json:"memory_info"`
	UserInfo       types.UserInfo        `json:"user_info"`
	HostInfo       types.HostInfo        `json:"host_info"`
	HostMemoryInfo *types.HostMemoryInfo `json:"host_memory_info"`
}

func GetSysInfo() SysInfo {
	process, _ := sysinfo.Self()
	processInfo, _ := process.Info()
	cpuTime, _ := process.CPUTime()
	memoryInfo, _ := process.Memory()
	userInfo, _ := process.User()

	host, _ := sysinfo.Host()
	hostInfo := host.Info()
	hostMemoryInfo, _ := host.Memory()

	return SysInfo{
		ProcessInfo:    processInfo,
		CPUTimes:       cpuTime,
		MemoryInfo:     memoryInfo,
		UserInfo:       userInfo,
		HostInfo:       hostInfo,
		HostMemoryInfo: hostMemoryInfo,
	}
}

func main() {
	sysinf := GetSysInfo()

	data, err := json.MarshalIndent(&sysinf, "", "  ")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(data))
}

Output:

{
  "process_info": {
    "name": "sysinfo",
    "pid": 10142,
    "ppid": 25771,
    "cwd": "/workspace/go-sysinfo/cmd/sysinfo",
    "exe": "/workspace/go-sysinfo/cmd/sysinfo/sysinfo",
    "args": [
      "./sysinfo"
    ],
    "start_time": "2021-10-24T16:23:14.47Z"
  },
  "cpu_times": {
    "user": 0,
    "system": 0
  },
  "memory_info": {
    "resident_bytes": 5734400,
    "virtual_bytes": 720535552
  },
  "user_info": {
    "uid": "0",
    "euid": "0",
    "suid": "0",
    "gid": "0",
    "egid": "0",
    "sgid": "0"
  },
  "host_info": {
    "architecture": "x86_64",
    "boot_time": "2021-10-24T10:28:56Z",
    "containerized": true,
    "name": "58755aabff76",
    "ip": [
      "127.0.0.1/8",
      "172.18.0.2/16"
    ],
    "kernel_version": "5.4.72-microsoft-standard-WSL2",
    "mac": [
      "02:42:ac:12:00:02"
    ],
    "os": {
      "type": "linux",
      "family": "",
      "platform": "alpine",
      "name": "Alpine Linux",
      "version": "",
      "major": 0,
      "minor": 0,
      "patch": 0
    },
    "timezone": "UTC",
    "timezone_offset_sec": 0,
    "id": "8203ec2a7d4a85be1975970061753e5c"
  },
  "host_memory_info": {
    "total_bytes": 13293199360,
    "used_bytes": 4198158336,
    "available_bytes": 11434057728,
    "free_bytes": 9095041024,
    "virtual_total_bytes": 4294967296,
    "virtual_used_bytes": 0,
    "virtual_free_bytes": 4294967296,
    "raw": {
      "Active": 1741504512,
      "Active(anon)": 1001545728,
      "Active(file)": 739958784,
      "AnonHugePages": 262144000,
      "AnonPages": 878346240,
      "Bounce": 0,
      "Buffers": 310341632,
      "Cached": 2418610176,
      "CommitLimit": 10941566976,
      "Committed_AS": 4803932160,
      "DirectMap1G": 7516192768,
      "DirectMap2M": 6067060736,
      "DirectMap4k": 47185920,
      "Dirty": 0,
      "FileHugePages": 0,
      "FilePmdMapped": 0,
      "HugePages_Free": 0,
      "HugePages_Rsvd": 0,
      "HugePages_Surp": 0,
      "HugePages_Total": 0,
      "Hugepagesize": 2097152,
      "Hugetlb": 0,
      "Inactive": 2007437312,
      "Inactive(anon)": 297070592,
      "Inactive(file)": 1710366720,
      "KReclaimable": 216645632,
      "KernelStack": 15048704,
      "Mapped": 273330176,
      "Mlocked": 0,
      "NFS_Unstable": 0,
      "PageTables": 9781248,
      "Percpu": 6750208,
      "SReclaimable": 216645632,
      "SUnreclaim": 104050688,
      "Shmem": 386269184,
      "ShmemHugePages": 0,
      "ShmemPmdMapped": 0,
      "Slab": 320696320,
      "SwapCached": 0,
      "Unevictable": 0,
      "VmallocChunk": 0,
      "VmallocTotal": 35184372087808,
      "VmallocUsed": 35704832,
      "Writeback": 0,
      "WritebackTmp": 0
    }
  }
}
@andrewkroh andrewkroh added the enhancement New feature or request label Jun 1, 2022
@andrewkroh
Copy link
Member

Running go test -v . outputs all the data collected as JSON. But I like the idea of an example command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants