sysinfo

package module
v0.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2016 License: MIT Imports: 16 Imported by: 60

README

Sysinfo

Build Status Go Report Card GoDoc License Powered by Platform

Package sysinfo is a pure 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"

        "github.com/zcalusic/sysinfo"
)

func main() {
        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 is mostly complete feature-wise, the challenges going forward will be to fully support more Linux distributions. So far the library has been tested to offer full functionality on the following distros:

  • CentOS 6, 7
  • Debian 7, 8, unstable
  • Ubuntu 12.04, 14.04, 16.04

OTOH, newer distributions should actually work out of the box (older ones are problematic) thanks to the newer kernels with more features and standardization efforts of the systemd team (think /etc/os-release and stuff like that).

Requirements

Sysinfo requires:

  • Linux kernel 2.6.23 or later (actually, this is what Go's run-time requires)
  • access to /sys & /proc Linux virtual file systems
  • access to various files in /etc, /var, /run FS hierarchy
  • access to DMI system data via /dev/mem virtual device (read: superuser privileges)

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

Sysinfo is developed on Linux amd64 using Go 1.7, and only occasionally tested on Linux i386, but it should work equally well on both architectures. As it heavily depends on Linux internals, there are no plans to support other operating systems. But, I would like it to support all Linux architectures, eventually.

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 the 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

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
    }
  ]
}

Todo

  • Node info
  • Hypervisor info
  • Container info
  • OS info
  • Kernel info
  • Product info
  • BIOS/Board/Chassis info
  • CPU info
  • Memory info
  • Storage info
  • Network info

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.

Documentation

Overview

Package sysinfo is a pure Go library providing Linux OS / kernel / hardware system information.

Example
package main

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

	"github.com/zcalusic/sysinfo"
)

func main() {
	var si sysinfo.SysInfo

	si.GetSysInfo()

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

	fmt.Println(string(data))
}
Output:

Index

Examples

Constants

View Source
const Version = "0.9.1"

Version of the sysinfo library.

Variables

View Source
var ErrNotExist = errors.New("SMBIOS entry point not found")

ErrNotExist indicates that SMBIOS entry point could not be found.

Functions

This section is empty.

Types

type BIOS

type BIOS struct {
	Vendor  string `json:"vendor,omitempty"`
	Version string `json:"version,omitempty"`
	Date    string `json:"date,omitempty"`
}

BIOS information.

type Board

type Board struct {
	Name     string `json:"name,omitempty"`
	Vendor   string `json:"vendor,omitempty"`
	Version  string `json:"version,omitempty"`
	Serial   string `json:"serial,omitempty"`
	AssetTag string `json:"assettag,omitempty"`
}

Board information.

type CPU

type CPU struct {
	Vendor  string `json:"vendor,omitempty"`
	Model   string `json:"model,omitempty"`
	Speed   uint   `json:"speed,omitempty"`   // CPU clock rate in MHz
	Cache   uint   `json:"cache,omitempty"`   // CPU cache size in KB
	Cpus    uint   `json:"cpus,omitempty"`    // number of physical CPUs
	Cores   uint   `json:"cores,omitempty"`   // number of physical CPU cores
	Threads uint   `json:"threads,omitempty"` // number of logical (HT) CPU cores
}

CPU information.

type Chassis

type Chassis struct {
	Type     uint   `json:"type,omitempty"`
	Vendor   string `json:"vendor,omitempty"`
	Version  string `json:"version,omitempty"`
	Serial   string `json:"serial,omitempty"`
	AssetTag string `json:"assettag,omitempty"`
}

Chassis information.

type Kernel

type Kernel struct {
	Release      string `json:"release,omitempty"`
	Version      string `json:"version,omitempty"`
	Architecture string `json:"architecture,omitempty"`
}

Kernel information.

type Memory

type Memory struct {
	Type  string `json:"type,omitempty"`
	Speed uint   `json:"speed,omitempty"` // RAM data rate in MT/s
	Size  uint   `json:"size,omitempty"`  // RAM size in MB
}

Memory information.

type Meta

type Meta struct {
	Version   string    `json:"version"`
	Timestamp time.Time `json:"timestamp"`
}

Meta information.

type NetworkDevice

type NetworkDevice struct {
	Name       string `json:"name,omitempty"`
	Driver     string `json:"driver,omitempty"`
	MACAddress string `json:"macaddress,omitempty"`
	Port       string `json:"port,omitempty"`
	Speed      uint   `json:"speed,omitempty"` // device max supported speed in Mbps
}

NetworkDevice information.

type Node

type Node struct {
	Hostname   string `json:"hostname,omitempty"`
	MachineID  string `json:"machineid,omitempty"`
	Hypervisor string `json:"hypervisor,omitempty"`
	Timezone   string `json:"timezone,omitempty"`
}

Node information.

type OS

type OS struct {
	Name         string `json:"name,omitempty"`
	Vendor       string `json:"vendor,omitempty"`
	Version      string `json:"version,omitempty"`
	Release      string `json:"release,omitempty"`
	Architecture string `json:"architecture,omitempty"`
}

OS information.

type Product

type Product struct {
	Name    string `json:"name,omitempty"`
	Vendor  string `json:"vendor,omitempty"`
	Version string `json:"version,omitempty"`
	Serial  string `json:"serial,omitempty"`
}

Product information.

type StorageDevice

type StorageDevice struct {
	Name   string `json:"name,omitempty"`
	Driver string `json:"driver,omitempty"`
	Vendor string `json:"vendor,omitempty"`
	Model  string `json:"model,omitempty"`
	Serial string `json:"serial,omitempty"`
	Size   uint   `json:"size,omitempty"` // device size in GB
}

StorageDevice information.

type SysInfo

type SysInfo struct {
	Meta    Meta            `json:"sysinfo"`
	Node    Node            `json:"node"`
	OS      OS              `json:"os"`
	Kernel  Kernel          `json:"kernel"`
	Product Product         `json:"product"`
	Board   Board           `json:"board"`
	Chassis Chassis         `json:"chassis"`
	BIOS    BIOS            `json:"bios"`
	CPU     CPU             `json:"cpu"`
	Memory  Memory          `json:"memory"`
	Storage []StorageDevice `json:"storage"`
	Network []NetworkDevice `json:"network"`
}

SysInfo struct encapsulates all other information structs.

func (*SysInfo) GetSysInfo

func (si *SysInfo) GetSysInfo()

GetSysInfo gathers all available system information.

Directories

Path Synopsis
cmd
sysinfo
sysinfo is a very simple utility demonstrating sysinfo library capabilites.
sysinfo is a very simple utility demonstrating sysinfo library capabilites.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL