inspect

package
v0.0.0-...-886287a Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2022 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package inspect inspects '/proc/*'.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ProcHeader lists all Proc CSV columns.
	ProcHeader = append([]string{"UNIX-NANOSECOND", "UNIX-SECOND"}, columnsPSEntry...)

	// ProcHeaderIndex maps each Proc column name to its index in row.
	ProcHeaderIndex = make(map[string]int)
)

Functions

func BinarySearchInt64

func BinarySearchInt64(nums []int64, v int64) int

BinarySearchInt64 binary-searches the int64 slice and returns the index of the matching element. So input slice must be sorted. It returns -1 if not found.

func ConvertDS

func ConvertDS(dss ...DSEntry) (header []string, rows [][]string)

ConvertDS converts to rows.

func ConvertNS

func ConvertNS(nss ...NSEntry) (header []string, rows [][]string)

ConvertNS converts to rows.

func ConvertPS

func ConvertPS(nss ...PSEntry) (header []string, rows [][]string)

ConvertPS converts to rows.

func ConvertSS

func ConvertSS(nss ...SSEntry) (header []string, rows [][]string)

ConvertSS converts to rows.

func StringDS

func StringDS(header []string, rows [][]string, topLimit int) string

StringDS converts in print-friendly format.

func StringNS

func StringNS(header []string, rows [][]string, topLimit int) string

StringNS converts in print-friendly format.

func StringPS

func StringPS(header []string, rows [][]string, topLimit int) string

StringPS converts in print-friendly format.

func StringSS

func StringSS(header []string, rows [][]string, topLimit int) string

StringSS converts in print-friendly format.

Types

type CSV

type CSV struct {
	FilePath         string
	PID              int64
	DiskDevice       string
	NetworkInterface string

	Header      []string
	HeaderIndex map[string]int

	MinUnixNanosecond int64
	MinUnixSecond     int64
	MaxUnixNanosecond int64
	MaxUnixSecond     int64

	// ExtraPath contains extra information.
	ExtraPath string

	// TopStream feeds realtime 'top' command data in the background, every second.
	// And whenver 'Add' gets called, returns the latest 'top' data.
	// Use this to provide more accurate CPU usage.
	TopStream *top.Stream

	// Rows are sorted by unix time in nanoseconds.
	// It's the number of nanoseconds (not seconds) elapsed
	// since January 1, 1970 UTC.
	Rows []Proc
}

CSV represents CSV data (header, rows, etc.).

func NewCSV

func NewCSV(fpath string, pid int64, diskDevice string, networkInterface string, extraPath string, tcfg *top.Config) (c *CSV, err error)

NewCSV returns a new CSV.

func ReadCSV

func ReadCSV(fpath string) (*CSV, error)

ReadCSV reads a CSV file and convert to 'CSV'. Make sure to change this whenever 'Proc' fields are updated.

func (*CSV) Add

func (c *CSV) Add() error

Add is called periodically to append a new entry to CSV; it only appends. If the data is used for time series, make sure to handle missing time stamps between. e.g. interpolate by estimating the averages between last row and new row to be inserted.

func (*CSV) Interpolate

func (c *CSV) Interpolate() (cc *CSV, err error)

Interpolate interpolates missing rows in CSV assuming CSV is to be collected for every second. 'Missing' means unix seconds in rows are not continuous. It fills in the empty rows by estimating the averages. It returns a new copy of CSV. And the new copy sets all unix nanoseconds to 0., since it's now aggregated by the unix "second".

func (*CSV) Save

func (c *CSV) Save() error

Save saves CSV to disk.

type DSEntry

type DSEntry struct {
	Device string

	ReadsCompleted     uint64
	SectorsRead        uint64
	TimeSpentOnReading string

	WritesCompleted    uint64
	SectorsWritten     uint64
	TimeSpentOnWriting string

	// extra fields for sorting
	TimeSpentOnReadingMs uint64
	TimeSpentOnWritingMs uint64
}

DSEntry represents disk statistics. Simplied from 'DiskStat'.

func GetDS

func GetDS() ([]DSEntry, error)

GetDS lists all disk statistics.

type EntryOp

type EntryOp struct {
	ProgramMatchFunc func(string) bool

	PID      int64
	TopLimit int

	// for ss
	TCP        bool
	TCP6       bool
	LocalPort  int64
	RemotePort int64

	// for ps
	TopExecPath string
	TopStream   *top.Stream

	// for Proc
	DiskDevice       string
	NetworkInterface string
	ExtraPath        string
	// contains filtered or unexported fields
}

EntryOp defines entry option(filter).

type NSEntry

type NSEntry struct {
	Interface string

	ReceiveBytes    string
	ReceivePackets  uint64
	TransmitBytes   string
	TransmitPackets uint64

	// extra fields for sorting
	ReceiveBytesNum  uint64
	TransmitBytesNum uint64
}

NSEntry represents network statistics. Simplied from 'NetDev'.

func GetNS

func GetNS() ([]NSEntry, error)

GetNS lists all '/proc/net/dev' statistics.

type OpFunc

type OpFunc func(*EntryOp)

OpFunc applies each filter.

func WithDiskDevice

func WithDiskDevice(name string) OpFunc

WithDiskDevice to filter entries by disk device.

func WithExtraPath

func WithExtraPath(path string) OpFunc

WithExtraPath to filter entries by disk device.

func WithLocalPort

func WithLocalPort(port int64) OpFunc

WithLocalPort to filter entries by local port.

func WithNetworkInterface

func WithNetworkInterface(name string) OpFunc

WithNetworkInterface to filter entries by disk device.

func WithPID

func WithPID(pid int64) OpFunc

WithPID to filter entries by PIDs.

func WithProgram

func WithProgram(name string) OpFunc

WithProgram to filter entries by program name.

func WithProgramMatch

func WithProgramMatch(matchFunc func(string) bool) OpFunc

WithProgramMatch matches command name.

func WithRemotePort

func WithRemotePort(port int64) OpFunc

WithRemotePort to filter entries by remote port.

func WithTCP

func WithTCP() OpFunc

WithTCP to filter entries by TCP. Can be used with 'WithTCP6'.

func WithTCP6

func WithTCP6() OpFunc

WithTCP6 to filter entries by TCP6. Can be used with 'WithTCP'.

func WithTopExecPath

func WithTopExecPath(path string) OpFunc

WithTopExecPath configures 'top' command path.

func WithTopLimit

func WithTopLimit(limit int) OpFunc

WithTopLimit to filter entries with limit.

func WithTopStream

func WithTopStream(str *top.Stream) OpFunc

WithTopStream gets the PSEntry from the 'top' stream.

type PSEntry

type PSEntry struct {
	Program string
	State   string
	PID     int64
	PPID    int64

	CPU    string
	VMRSS  string
	VMSize string

	FD      uint64
	Threads uint64

	VoluntaryCtxtSwitches    uint64
	NonvoluntaryCtxtSwitches uint64

	// extra fields for sorting
	CPUNum    float64
	VMRSSNum  uint64
	VMSizeNum uint64
}

PSEntry is a process entry. Simplied from 'Stat' and 'Status'.

func GetPS

func GetPS(opts ...OpFunc) (pss []PSEntry, err error)

GetPS finds all PSEntry by given filter.

type Proc

type Proc struct {
	// UnixNanosecond is unix nano second when this Proc row gets created.
	UnixNanosecond int64

	// UnixSecond is the converted Unix seconds from UnixNano.
	UnixSecond int64

	PSEntry PSEntry

	LoadAvg proc.LoadAvg

	DSEntry              DSEntry
	ReadsCompletedDelta  uint64
	SectorsReadDelta     uint64
	WritesCompletedDelta uint64
	SectorsWrittenDelta  uint64

	// ReadBytesDelta is calculated from SectorsReadDelta
	// while SECTOR_SIZE is 512 (one sector is 512-byte) in Linux kernel
	// (http://lkml.iu.edu/hypermail/linux/kernel/1508.2/00431.html).
	ReadBytesDelta     uint64
	ReadMegabytesDelta uint64

	// WriteBytesDelta is calculated from SectorsWrittenDelta
	// while SECTOR_SIZE is 512 (one sector is 512-byte) in Linux kernel
	// (http://lkml.iu.edu/hypermail/linux/kernel/1508.2/00431.html).
	WriteBytesDelta     uint64
	WriteMegabytesDelta uint64

	NSEntry               NSEntry
	ReceiveBytesDelta     string
	ReceivePacketsDelta   uint64
	TransmitBytesDelta    string
	TransmitPacketsDelta  uint64
	ReceiveBytesNumDelta  uint64
	TransmitBytesNumDelta uint64

	// Extra exists to support customized data query.
	Extra []byte
}

Proc represents an entry of various system statistics.

func Combine

func Combine(procs ...Proc) Proc

Combine combines a list Proc and returns one combined Proc. Field values are estimated. UnixNanosecond is reset 0. And UnixSecond and other fields that cannot be averaged are set with the field value in the last element. This is meant to be used to combine Proc rows with duplicate unix second timestamps.

func GetProc

func GetProc(opts ...OpFunc) (Proc, error)

GetProc returns current 'Proc' data. PID is required. Disk device, network interface, extra path are optional.

func Interpolate

func Interpolate(lower, upper Proc) (procs []Proc, err error)

Interpolate returns the missing, estimated 'Proc's if any. It assumes that 'upper' Proc is later than 'lower'. And UnixSecond and other fields that cannot be averaged are set with the field value in the last element.

func (*Proc) ToRow

func (p *Proc) ToRow() (row []string)

ToRow converts 'Proc' to string slice. Make sure to change this whenever 'Proc' fields are updated.

type ProcSlice

type ProcSlice []Proc

ProcSlice is a slice of 'Proc' and implements the sort.Sort interface in unix nano/second ascending order.

func (ProcSlice) Len

func (p ProcSlice) Len() int

func (ProcSlice) Less

func (p ProcSlice) Less(i, j int) bool

func (ProcSlice) Swap

func (p ProcSlice) Swap(i, j int)

type SSEntry

type SSEntry struct {
	Protocol string

	Program string
	State   string
	PID     int64

	LocalIP   string
	LocalPort int64

	RemoteIP   string
	RemotePort int64

	User user.User
}

SSEntry is a socket entry. Simplied from 'NetTCP'.

func GetSS

func GetSS(opts ...OpFunc) (sss []SSEntry, err error)

GetSS finds all SSEntry by given filter.

type Tree

type Tree interface {
	Closest(v float64) (index int, value float64)
}

Tree defines binary search tree.

func NewBinaryTree

func NewBinaryTree(nums []float64) Tree

NewBinaryTree builds a new binary search tree. The original slice won't be sorted.

func NewBinaryTreeInt64

func NewBinaryTreeInt64(nums []int64) Tree

NewBinaryTreeInt64 builds a new binary search tree. The original slice won't be sorted.

Jump to

Keyboard shortcuts

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