systats

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2022 License: MIT Imports: 13 Imported by: 5

README

systats

Go module to get linux system stats.

Go Go Report Card

Provides following information on systems:

  • System
    • Returns OS, Hostname, Kernel, Up time, last boot date, timezone, logged in users list
  • CPU
    • CPU model, freq, load average (overall, per core), etc
  • Memory/SWAP
  • Disks
    • File system, type, mount point, usage, inodes
  • Networks
    • Interface, IP, Rx/Tx
  • Service status
    • Returns if given service is active or not
  • Processes
    • Returns list of processes sorted by CPU/Memory usage (with PID, exec path, user, usage)

Usage

Import the module

import (
	"github.com/dhamith93/systats"
)

func main() {
    syStats := systats.New()
}

And use the methods to get the required and supported system stats.

System

Returns OS, Hostname, Kernel, Up time, last boot date, timezone, logged in users list

func main() {
	syStats := systats.New()
	system, err := systats.GetSystem()
}
CPU

CPU info and load avg info (overall, and per core)

func main() {
	syStats := systats.New()
	cpu, err := systats.GetCPU()
}
Memory
func main() {
	syStats := systats.New()
	memory, err := systats.GetMemory(systats.Megabyte)
}
SWAP
func main() {
	syStats := systats.New()
	swap, err := systats.GetSwap(systats.Megabyte)
}
Disks
func main() {
	syStats := systats.New()
	disks, err := syStats.GetDisks()
}
Networks

Interface info and usage info

func main() {
	syStats := systats.New()
	networks, err := syStats.GetNetworks()
}
Service status

Returns if service is running or not

func main() {
	syStats := systats.New()
	running := syStats.IsServiceRunning(service)
	if !running {
		fmt.Println(service + " not running")
	}
}
Running processes

Returns running processes sorted by CPU or memory usage

func main() {
	syStats := systats.New()
	procs, err := syStats.GetTopProcesses(10, "cpu")
	procs, err := syStats.GetTopProcesses(10, "memory")
}

Documentation

Index

Constants

View Source
const (
	Byte     string = "B"
	Kilobyte string = "KB"
	Megabyte string = "MB"
	Gigabyte string = "GB"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CPU

type CPU struct {
	LoadAvg   int
	CoreAvg   []int
	Model     string
	NoOfCores int
	Freq      string
	Cache     string
	Time      int64
}

CPU holds information on CPU and CPU usage

type Disk

type Disk struct {
	FileSystem string
	Type       string
	MountedOn  string
	Usage      DiskUsage
	Inodes     InodeUsage
	Time       int64
}

Disk holds information on single disk

func (*Disk) Convert

func (d *Disk) Convert(unit string)

type DiskUsage

type DiskUsage struct {
	Size      uint64
	Used      uint64
	Available uint64
	Usage     string
	Unit      string
}

DiskUsage holds information on single disk usage information

type InodeUsage

type InodeUsage struct {
	Inodes    uint64
	Available uint64
	Used      uint64
	Usage     string
}

InodeUsage holds information on single disk inode usage

type Memory

type Memory struct {
	PercentageUsed float64
	Available      uint64
	Free           uint64
	Used           uint64
	Time           int64
	Total          uint64
	Unit           string
}

Memory holds information on system memory usage

type Network

type Network struct {
	Interface string
	Ip        string
	Usage     NetworkUsage
	Time      int64
}

Network holds interface information

type NetworkUsage

type NetworkUsage struct {
	RxBytes   uint64
	TxBytes   uint64
	RxPackets uint64
	TxPackets uint64
}

NetworkUsage holds Tx/Rx usage information

type Process

type Process struct {
	Pid      int
	ExecPath string
	User     string
	CPUUsage float32
	MemUsage float32
}

Process holds information on single process

type Swap

type Swap struct {
	PercentageUsed float64
	Free           uint64
	Used           uint64
	Time           int64
	Total          uint64
	Unit           string
}

Swap holds information on system swap usage

type SyStats

type SyStats struct {
	MeminfoPath     string
	StatFilePath    string
	CPUinfoFilePath string
	VersionPath     string
	EtcPath         string
	UptimePath      string
}

SyStats holds information used to collect data

func New

func New() SyStats

func (*SyStats) CanConnectExternal

func (systats *SyStats) CanConnectExternal(url string) (bool, error)

func (*SyStats) EstablishedTCPConnCount

func (systats *SyStats) EstablishedTCPConnCount(procName string) int

func (*SyStats) GetCPU

func (systats *SyStats) GetCPU() (CPU, error)

func (*SyStats) GetDisks

func (systats *SyStats) GetDisks() ([]Disk, error)

func (*SyStats) GetMemory

func (systats *SyStats) GetMemory(unit string) (Memory, error)

func (*SyStats) GetNetworkUsage

func (systats *SyStats) GetNetworkUsage(networkInterface string) NetworkUsage

func (*SyStats) GetNetworks

func (systats *SyStats) GetNetworks() ([]Network, error)

func (*SyStats) GetSwap

func (systats *SyStats) GetSwap(unit string) (Swap, error)

func (*SyStats) GetSystem

func (systats *SyStats) GetSystem() (System, error)

func (*SyStats) GetTopProcesses

func (systats *SyStats) GetTopProcesses(count int, sort string) ([]Process, error)

func (*SyStats) IsPortOpen

func (systats *SyStats) IsPortOpen(port int) bool

func (*SyStats) IsServiceRunning

func (systats *SyStats) IsServiceRunning(service string) bool

type System

type System struct {
	HostName      string
	OS            string
	Kernel        string
	UpTime        string
	LastBootDate  time.Time
	LoggedInUsers []User
	Time          int64
	TimeZone      string
}

System holds operating system information

type User

type User struct {
	Username     string
	RemoteHost   string
	LoggedInTime time.Time
}

User holds logged in user information

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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