Sysinfo is a Go library providing Linux OS / kernel / hardware system information.

Overview

Sysinfo

Build Status Go Report Card GoDoc License Powered by Platform

Package sysinfo is a Go library providing Linux OS / kernel / hardware system information. It's completely standalone, has no dependencies on the host system, doesn't execute external programs, doesn't even import other Go libraries. It collects only "inventory type" information, things that don't change often.

Code Example

package main

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

	"github.com/zcalusic/sysinfo"
)

func main() {
	current, err := user.Current()
	if err != nil {
		log.Fatal(err)
	}

	if current.Uid != "0" {
		log.Fatal("requires superuser privilege")
	}

	var si sysinfo.SysInfo

	si.GetSysInfo()

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

	fmt.Println(string(data))
}

Motivation

I couldn't find any self-contained library that would provide set of data/features I needed. So another sysinfo was born.

The purpose of the library is to collect only inventory info. No metrics like CPU usage or load average will be added. The rule of thumb is, if it's changing during the day, every day, it doesn't belong in the library.

The library should work well on any modern/supported Linux distribution. There are no plans to add support for older unsupported Linux distributions/kernels, to keep the code clean and robust and reduce the maintenance burden.

Requirements

Sysinfo requires:

  • Linux kernel 4.2 or later
  • access to /sys & /proc Linux virtual file systems
  • access to various files in /etc, /var, /run FS hierarchy
  • superuser privileges (to access SMBIOS/DMI table and detect RAM size and properties)

Sysinfo doesn't require ANY other external utility on the target system, which is its primary strength, IMHO.

It depends on Linux internals heavily, so there are no plans to support other operating systems.

Installation

Just use go get.

go get github.com/zcalusic/sysinfo

There's also a very simple utility demonstrating sysinfo library capabilities. Start it (as superuser) to get pretty formatted JSON output of all the info that sysinfo library provides. Due to its simplicity, the source code of the utility also doubles down as an example of how to use the library.

go get github.com/zcalusic/sysinfo/cmd/sysinfo

--

Build demo utility in Docker container: https://github.com/mattscilipoti/compile_sysinfo

Sample output

{
  "sysinfo": {
    "version": "0.9.1",
    "timestamp": "2016-09-24T13:30:28.369498856+02:00"
  },
  "node": {
    "hostname": "web12",
    "machineid": "04aa55927ebd390829367c1757b98cac",
    "timezone": "Europe/Zagreb"
  },
  "os": {
    "name": "CentOS Linux 7 (Core)",
    "vendor": "centos",
    "version": "7",
    "release": "7.2.1511",
    "architecture": "amd64"
  },
  "kernel": {
    "release": "3.10.0-327.13.1.el7.x86_64",
    "version": "#1 SMP Thu Mar 31 16:04:38 UTC 2016",
    "architecture": "x86_64"
  },
  "product": {
    "name": "RH2288H V3",
    "vendor": "Huawei",
    "version": "V100R003",
    "serial": "2103711GEL10F3430658"
  },
  "board": {
    "name": "BC11HGSA0",
    "vendor": "Huawei",
    "version": "V100R003",
    "serial": "033HXVCNG3107624"
  },
  "chassis": {
    "type": 17,
    "vendor": "Huawei"
  },
  "bios": {
    "vendor": "Insyde Corp.",
    "version": "3.16",
    "date": "03/16/2016"
  },
  "cpu": {
    "vendor": "GenuineIntel",
    "model": "Intel(R) Xeon(R) CPU E5-2630 v3 @ 2.40GHz",
    "speed": 2400,
    "cache": 20480,
    "cpus": 1,
    "cores": 8,
    "threads": 16
  },
  "memory": {
    "type": "DRAM",
    "speed": 2133,
    "size": 65536
  },
  "storage": [
    {
      "name": "sda",
      "driver": "sd",
      "vendor": "ATA",
      "model": "ST9500620NS",
      "serial": "9XF2HZ9K",
      "size": 500
    }
  ],
  "network": [
    {
      "name": "enp3s0f1",
      "driver": "ixgbe",
      "macaddress": "84:ad:5a:e3:79:71",
      "port": "fibre",
      "speed": 10000
    }
  ]
}

Contributors

Contributors are welcome, just open a new issue / pull request.

License

The MIT License (MIT)

Copyright © 2016 Zlatko Čalušić

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Issues
  • try fallback to use sysfs of DMI block to decode memory info

    try fallback to use sysfs of DMI block to decode memory info

    try to fallback to /sys/firmware/dmi/tables/DMI when open /dev/mem return Operation not permitted

    opened by Haraguroicha 7
  • GetSysInfo segfaults

    GetSysInfo segfaults

    I got a segfault when calling si.GetSysInfo, as shown in the stack trace.

    The code responsible for this segfault is:

    https://github.com/zcalusic/sysinfo/blob/aff387a52b3af60423548f44c504eee6d2d647ec/network.go#L92

    unexpected fault address 0x0                                                                                                                                            
    fatal error: fault                                                                                                                                                      
    [signal SIGSEGV: segmentation violation code=0x80 addr=0x0 pc=0x561cda8bc692]                                                                                           
                                                                                                                                                                            
    goroutine 1 [running]:                                                                                                                                                  
    runtime.throw({0x561cda8e0b5d, 0xc0002b7838})                                                                                                                           
            runtime/panic.go:1198 +0x71 fp=0xc0002b7800 sp=0xc0002b77d0 pc=0x561cda52ceb1                                                                                   
    runtime.sigpanic()                                                                                                                                                      
            runtime/signal_unix.go:742 +0x2f6 fp=0xc0002b7850 sp=0xc0002b7800 pc=0x561cda542656                                                                             
    github.com/zcalusic/sysinfo.getSupported({0xc00002278f, 0x4})                                                                                                           
            github.com/zcalusic/[email protected]/network.go:92 +0x172 fp=0xc0002b7908 sp=0xc0002b7850 pc=0x561cda8bc692                           
    github.com/zcalusic/sysinfo.(*SysInfo).getNetworkInfo(0xc0002b7b38)                                                                                                     
            github.com/zcalusic/[email protected]/network.go:117 +0x1da fp=0xc0002b7a60 sp=0xc0002b7908 pc=0x561cda8bc97a                          
    github.com/zcalusic/sysinfo.(*SysInfo).GetSysInfo(0xc0002b7b38)                                                                                                         
            github.com/zcalusic/[email protected]/sysinfo.go:44 +0xcf fp=0xc0002b7a78 sp=0xc0002b7a60 pc=0x561cda8bf02f                            
    github.com/jm33-m0/emp3r0r/core/lib/agent.CollectSystemInfo()
    
    opened by jm33-m0 5
  • Added SMBIOS version in BIOS info

    Added SMBIOS version in BIOS info

    Added support for Redhat release in OS info. Added support for SMBIOS version in BIOS info.

    opened by chatenilesh 3
  • Couldn't compile on Mac OS

    Couldn't compile on Mac OS

    I have a problem with build sysinfo on my OSX Laptop.

    ../sysinfo/kernel.go:24: undefined: syscall.Utsname ../sysinfo/kernel.go:25: undefined: syscall.Uname

    opened by agareev 2
  • Cannot get mem info

    Cannot get mem info

      "os": {
        "name": "Fedora 28 (Twenty Eight)",
        "vendor": "fedora",
        "version": "28",
        "architecture": "amd64"
      },
      "kernel": {
        "release": "4.18.9-200.fc28.x86_64",
        "version": "#1 SMP Thu Sep 20 02:43:23 UTC 2018",
        "architecture": "x86_64"
      },
      "cpu": {
        "vendor": "AuthenticAMD",
        "model": "AMD E-450 APU with Radeon(tm) HD Graphics",
        "cache": 512,
        "cpus": 1,
        "cores": 2,
        "threads": 2
      },
      "memory": {},
      "storage": [
        {
          "name": "sda",
          "driver": "sd",
          "vendor": "ATA",
          "model": "INTEL SSDSC2BW12",
          "serial": "CVTR55210026120AGN",
          "size": 120
        }
      ],
    
    opened by mia0x75 2
  • Error on armv7 architecture

    Error on armv7 architecture

    Hello,

    This program crashes if I run the program on armv7l architecture. Here is the error stack trace:

    unexpected fault address 0x0
    fatal error: fault
    [signal SIGBUS: bus error code=0x3 addr=0x0 pc=0x12f58]
    goroutine 1 [running]:
    runtime.throw(0x473c24, 0x5)
            /usr/local/go/src/runtime/panic.go:1116 +0x5c fp=0xc3ec2c sp=0xc3ec18 pc=0x46804
    runtime.sigpanic()
            /usr/local/go/src/runtime/signal_unix.go:714 +0x378 fp=0xc3ec44 sp=0xc3ec2c pc=0x5df14
    memeqbody()
            /usr/local/go/src/internal/bytealg/equal_arm.s:89 +0x88 fp=0xc3ec48 sp=0xc3ec48 pc=0x12f58
    bytes.Equal(...)
            /usr/local/go/src/bytes/bytes.go:20
    github.com/zcalusic/sysinfo.getStructureTableAddress(0xc90378, 0x0, 0x0, 0x0, 0x0, 0x0)
            /go/networkd/src/github.com/zcalusic/sysinfo/memory.go:111 +0x268 fp=0xc3ecac sp=0xc3ec48 pc=0x16ded4
    github.com/zcalusic/sysinfo.getStructureTable(0x0, 0x0, 0x0, 0x0, 0x0)
            /go/networkd/src/github.com/zcalusic/sysinfo/memory.go:139 +0x2a4 fp=0xc3ed04 sp=0xc3ecac pc=0x16e29c
    github.com/zcalusic/sysinfo.(*SysInfo).getMemoryInfo(0xccc000)
            /go/networkd/src/github.com/zcalusic/sysinfo/memory.go:155 +0x2c fp=0xc3ee58 sp=0xc3ed04 pc=0x16e370
    github.com/zcalusic/sysinfo.(*SysInfo).GetSysInfo(0xccc000)
            /go/networkd/src/github.com/zcalusic/sysinfo/sysinfo.go:36 +0xa0 fp=0xc3ee84 sp=0xc3ee58 pc=0x171538
    main.GetSysInfo(0x0, 0xc3ef20)
    
    opened by blaspix-tech 2
  • Add bhyve detection

    Add bhyve detection

    Heya,

    thought I'd say hi, and tell you I swiped your code to make https://github.com/klauspost/cpuid/pull/14

    The main reason I didn't just use your code, is that when RancherOS is bootstrapping, not quite enough of the OS is up, and this sysinfo fatal'd out. - cpuid was simpler. I'll circle back later though, as i'm not sure adding dmi to cpuid is the right thing to do.

    opened by SvenDowideit 1
  • Fix go test

    Fix go test

    I noticed that example_test.go no longer works with newer go version, so I fixed it.

    opened by jm33-m0 1
  • Fix the issue that it may panic on arm architecture due to the mmap memory access

    Fix the issue that it may panic on arm architecture due to the mmap memory access

    Fix https://github.com/zcalusic/sysinfo/issues/26

    opened by lucklove 1
  • Feature: compile via a docker container

    Feature: compile via a docker container

    Are you interested in providing the ability to compile via a docker container? I would be happy to work with you to incorporate what I did at https://github.com/mattscilipoti/compile_sysinfo. I tried to share architectural decisions in the readme, but I'm happy to discuss anytime.

    opened by mattscilipoti 1
  • Version semantics

    Version semantics

    Hi! Thanks for this project! Can you create release tags? v0.9.5? go mod tidy github.com/zcalusic/sysinfo v0.0.0-20210905121133-6fa2f969a900 Maybe it will be better? github.com/zcalusic/sysinfo v0.9.5

    opened by Danile71 2
  • Move information retrieval to each structure

    Move information retrieval to each structure

    Right now, it is not possible to use the library to get only a subset of the information. This PR adds a GetInfo method to every member of the SysInfo struct to allow it. It also has the advantage on making the ordering requirements a bit clearer.

    opened by lebauce 0
  • Unable to use library to pull Computer/Bios/CPU/etc. data from virtual Arm server

    Unable to use library to pull Computer/Bios/CPU/etc. data from virtual Arm server

    Hello, I am trying to use the sysinfo to pull system data while running on an Arm server. The following stack shows that there is a failure in a Go routine due to a SIGBUS error:

    Experiment URL: https://optimizer.concertio.com/projects/8a6a2052-3a06-4779-9d81-cb30cf86b392/experiments#70b2cc3b-05f5-44d1-8682-fc36f860896a unexpected fault address 0x0 fatal error: fault [signal SIGBUS: bus error code=0x3 addr=0x0 pc=0x8a1b34] goroutine 1 [running]: runtime.throw(0xb41f1d, 0x5) /usr/lib/go-1.13/src/runtime/panic.go:774 +0x54 fp=0x40001208c0 sp=0x4000120890 pc=0x45c3bc runtime.sigpanic() /usr/lib/go-1.13/src/runtime/signal_unix.go:391 +0x434 fp=0x40001208f0 sp=0x40001208c0 pc=0x471b7c github.com/zcalusic/sysinfo.cloneBytes(0xffffa040e000, 0x10000, 0x10000, 0x10000, 0x1, 0x1) /root/go/pkg/mod/github.com/zcalusic/[email protected]/memory.go:46 +0x4c fp=0x4000120930 sp=0x4000120900 pc=0x8a1b34

    This error started occurring after attempting to implement the sysinfo library into the codebase. If you are interested I may be able to provide access to the Arm server that was used (no access to the code). Hoping that the access to this server would potentially allow you time to debug so that I could use this library. Please let me know your thoughts and if you need more info in order to debug, thank you!

    opened by samfishman1 1
  • Any chance of pulling simple GPU data?

    Any chance of pulling simple GPU data?

    No metrics needed... just simple static info like make/model, vram size, etc... would be nice.

    opened by DocDrydenn 0
  • RFE to allow for container usage

    RFE to allow for container usage

    My use case is probably fairly unusual and I do have a work around, but I was wondering if you'd be willing to consider adding an optional value to systemInfo struct that would place a prefix on all the paths to files? What I'm attempting to do is use this package inside a docker container and mount the host filesystem into the container in order to detect the host's info. Typically when this is done you'll mount the host's FS into a directory like /host so it doesn't collide with the container's FS. So if it was possible to set something like si.PathPrefix or whatever name you like that gets prepended to each of the currently hardcoded file paths that would be very useful. For now I'm able to simply mount the host's /etc dir directly over the container's /etc and it works as expected (I personally am most interested in the OS info, so only need etc), but it would be best, and allow expanded use cases, to keep the host and container FS's separated properly.

    opened by adkerr 2
  • Support for net interfaces on FreeBSD and macOS

    Support for net interfaces on FreeBSD and macOS

    I've found some information of low level related to net interfaces, for if somebody with knowledge of low level would want to add it.

    • https://kbyanc.blogspot.com/2010/11/python-enumerating-ip-addresses-on.html
    • https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/getifaddrs.3.html
    • https://nxmnpg.lemoda.net/3/getifaddrs
    • https://www.lemoda.net/freebsd/net-interfaces/index.html
    opened by tredoe 0
  • When getting the time zone, something unexpected happened on my host, so I changed it to this

    When getting the time zone, something unexpected happened on my host, so I changed it to this

    [[email protected] admin]# uname -a Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux [[email protected] admin]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) [[email protected] admin]# ll /etc/localtime lrwxrwxrwx. 1 root root 35 2019-10-10 13:56 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai D1BF3049-2CC9-41ca-BED6-D9A4CDEFB106 image

    opened by offer365 0
  • Network can get ip address?

    Network can get ip address?

    if Network info has ip addressm, may be better

    opened by azvs 2
  • add detailed decode for separed memories

    add detailed decode for separed memories

    There are only accept the first meet of memory type and speed, but there may have two or more different kind of memories installed into system at same time but there can not describe it.

    So I add a Memories property to store the memory detailed information (I only pick some general use fields to decode)

    opened by Haraguroicha 4
Owner
Zlatko Čalušić
Golang, DevOps & Cloud Native
Zlatko Čalušić
Identify containers at runtime and observe them. No container runtime required. Read only access to the kernel.

Linux Telemetry The Double Slit Experiment Taken from an interesting physics anomaly where the behavior of a physical system mutates simply by being o

Kris Nóva 11 Oct 5, 2021
A fully Go userland with Linux bootloaders! u-root can create a one-binary root file system (initramfs) containing a busybox-like set of tools written in Go.

u-root Description u-root embodies four different projects. Go versions of many standard Linux tools, such as ls, cp, or shutdown. See cmds/core for m

null 1.8k Jan 17, 2022
A Go library for the Linux Landlock sandboxing feature

Go Landlock library The Go Landlock library restricts the current processes' ability to use files, using Linux 5.13's Landlock feature. (Package docum

Landlock LSM 35 Jan 12, 2022
get windows system call number dynamically, it doesn't need the system call table.

GetSyscallID get windows system call number dynamically, it doesn't need the system call table. Try download go get github.com/akkuman/getsyscallid/cm

null 6 Dec 18, 2021
A program for extracting information from chrome session files.

What A tool for dumping the contents of chrome session files. Why? Most tools for reading SNSS files (chrome session files) are either broken or outda

null 27 Jan 10, 2022
A process that receives probe information and stores it in a database for reporting and analysis

probed is a process that receives probe information and stores it in a database for reporting and analysis.

Weald Technology 0 Dec 10, 2021
Otx - otx tool can scrap to find sensitive information and vulnerable endpoint urls.

otx Description This tool is base on AlienVault Open Threat Exchange (OTX)? and this tool can help you to extract all the urls endpoints which can be

ShaneKhant 1 Jan 2, 2022
utf8 - provide unicode information on runes

utf8 utf8 provides unicode code point values for input runes and the unicode rune (if printable) for a given unicode code point. With no arguments, pr

Sean Hinchee 1 Jan 8, 2022
Perforator is a tool for recording performance metrics over subregions of a program using the Linux "perf" interface.

Perforator Perforator is a tool for recording performance metrics over subregions of a program (e.g., functions) using the Linux "perf" interface.

Zachary Yedidia 26 Dec 18, 2021
Package ethtool allows control of the Linux ethtool generic netlink interface.

ethtool Package ethtool allows control of the Linux ethtool generic netlink interface.

Matt Layher 33 Jan 5, 2022
Automatically set GOMAXPROCS to match Linux container CPU quota.

automaxprocs Automatically set GOMAXPROCS to match Linux container CPU quota. Installation go get -u go.uber.org/automaxprocs Quick Start import _ "go

Uber Go 1.8k Jan 12, 2022
📋 cross-platform clipboard package that supports accessing text and image in Go (macOS/Linux/Windows/Android/iOS)

clipboard Cross platform (macOS/Linux/Windows/Android/iOS) clipboard package in Go import "golang.design/x/clipboard" Features Cross platform supports

The golang.design Initiative 150 Jan 19, 2022
Execute a binary from memory, without touching the disk. Linux only.

Memit Execute a binary from memory, without touching the disk. Linux only. Available as both a Go module and a binary. Using the Go module The Command

Liam Galvin 188 Jan 5, 2022
A utility library to do files/io/bytes processing/parsing in file-system or network.

goreader A utility library to do files/io/bytes processing/parsing in file-system or network. These features are really common to be implemented for a

VOrishirne 4 Nov 1, 2021
Ento is an Entity Component System written in Go.

Ento is an Entity Component System written in Go.

Wojciech Franczyk 8 Jan 15, 2022
ptypes is a pointer-based box typing system for golang.

ptypes bypass go's type system through unsafe pointers the paradigm is to created a "boxed" type with .From and then use whatever types we want by ass

prequist 3 Aug 26, 2021
System software of computers

SSoC System software of computers BSUIR labs Project structure Client client client/components client/components/client client/components/command clie

Max Ersh 2 Jan 5, 2022
Provides simple, semantic manipulation of the operating system's signal processing.

Provides simple, semantic manipulation of the operating system's signal processing.

Jayson Wang 0 Dec 15, 2021
Govalid is a data validation library that can validate most data types supported by golang

Govalid is a data validation library that can validate most data types supported by golang. Custom validators can be used where the supplied ones are not enough.

null 59 Dec 30, 2020