github.com/elastic/gosigar@v0.14.3/sigar_interface.go (about)

     1  package gosigar
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // ErrNotImplemented is returned when a particular statistic isn't implemented on the host OS.
     8  type ErrNotImplemented struct {
     9  	OS string
    10  }
    11  
    12  func (e ErrNotImplemented) Error() string {
    13  	return "not implemented on " + e.OS
    14  }
    15  
    16  // IsNotImplemented returns true if the error is ErrNotImplemented
    17  func IsNotImplemented(err error) bool {
    18  	switch err.(type) {
    19  	case ErrNotImplemented, *ErrNotImplemented:
    20  		return true
    21  	default:
    22  		return false
    23  	}
    24  }
    25  
    26  // Sigar is an interface for gathering system host stats
    27  type Sigar interface {
    28  	CollectCpuStats(collectionInterval time.Duration) (<-chan Cpu, chan<- struct{})
    29  	GetLoadAverage() (LoadAverage, error)
    30  	GetMem() (Mem, error)
    31  	GetSwap() (Swap, error)
    32  	GetHugeTLBPages(HugeTLBPages, error)
    33  	GetFileSystemUsage(string) (FileSystemUsage, error)
    34  	GetFDUsage() (FDUsage, error)
    35  	GetRusage(who int) (Rusage, error)
    36  }
    37  
    38  // Cpu contains CPU time stats
    39  type Cpu struct {
    40  	User    uint64
    41  	Nice    uint64
    42  	Sys     uint64
    43  	Idle    uint64
    44  	Wait    uint64
    45  	Irq     uint64
    46  	SoftIrq uint64
    47  	Stolen  uint64
    48  }
    49  
    50  // Total returns total CPU time
    51  func (cpu *Cpu) Total() uint64 {
    52  	return cpu.User + cpu.Nice + cpu.Sys + cpu.Idle +
    53  		cpu.Wait + cpu.Irq + cpu.SoftIrq + cpu.Stolen
    54  }
    55  
    56  // Delta returns the difference between two Cpu stat objects
    57  func (cpu Cpu) Delta(other Cpu) Cpu {
    58  	return Cpu{
    59  		User:    cpu.User - other.User,
    60  		Nice:    cpu.Nice - other.Nice,
    61  		Sys:     cpu.Sys - other.Sys,
    62  		Idle:    cpu.Idle - other.Idle,
    63  		Wait:    cpu.Wait - other.Wait,
    64  		Irq:     cpu.Irq - other.Irq,
    65  		SoftIrq: cpu.SoftIrq - other.SoftIrq,
    66  		Stolen:  cpu.Stolen - other.Stolen,
    67  	}
    68  }
    69  
    70  // LoadAverage reports standard load averages
    71  type LoadAverage struct {
    72  	One, Five, Fifteen float64
    73  }
    74  
    75  // Uptime reports system uptime
    76  type Uptime struct {
    77  	Length float64
    78  }
    79  
    80  // Mem contains host memory stats
    81  type Mem struct {
    82  	Total      uint64
    83  	Used       uint64
    84  	Free       uint64
    85  	Cached     uint64
    86  	ActualFree uint64
    87  	ActualUsed uint64
    88  }
    89  
    90  // Swap contains stats on swap space
    91  type Swap struct {
    92  	Total uint64
    93  	Used  uint64
    94  	Free  uint64
    95  }
    96  
    97  // HugeTLBPages contains HugePages stats
    98  type HugeTLBPages struct {
    99  	Total              uint64
   100  	Free               uint64
   101  	Reserved           uint64
   102  	Surplus            uint64
   103  	DefaultSize        uint64
   104  	TotalAllocatedSize uint64
   105  }
   106  
   107  // CpuList contains a list of CPUs on the host system
   108  type CpuList struct {
   109  	List []Cpu
   110  }
   111  
   112  // FDUsage contains stats on filesystem usage
   113  type FDUsage struct {
   114  	Open   uint64
   115  	Unused uint64
   116  	Max    uint64
   117  }
   118  
   119  // FileSystem contains basic information about a given mounted filesystem
   120  type FileSystem struct {
   121  	DirName     string
   122  	DevName     string
   123  	TypeName    string
   124  	SysTypeName string
   125  	Options     string
   126  	Flags       uint32
   127  }
   128  
   129  // FileSystemList gets a list of mounted filesystems
   130  type FileSystemList struct {
   131  	List []FileSystem
   132  }
   133  
   134  // FileSystemUsage contains basic stats for the specified filesystem
   135  type FileSystemUsage struct {
   136  	Total     uint64
   137  	Used      uint64
   138  	Free      uint64
   139  	Avail     uint64
   140  	Files     uint64
   141  	FreeFiles uint64
   142  }
   143  
   144  // ProcList contains a list of processes found on the host system
   145  type ProcList struct {
   146  	List []int
   147  }
   148  
   149  // RunState is a byte-long code used to specify the current runtime state of a process
   150  type RunState byte
   151  
   152  const (
   153  	// RunStateSleep corresponds to a sleep state
   154  	RunStateSleep = 'S'
   155  	// RunStateRun corresponds to a running state
   156  	RunStateRun = 'R'
   157  	// RunStateStop corresponds to a stopped state
   158  	RunStateStop = 'T'
   159  	// RunStateZombie marks a zombie process
   160  	RunStateZombie = 'Z'
   161  	// RunStateIdle corresponds to an idle state
   162  	RunStateIdle = 'D'
   163  	// RunStateUnknown corresponds to a process in an unknown state
   164  	RunStateUnknown = '?'
   165  )
   166  
   167  // ProcState contains basic metadata and process ownership info for the specified process
   168  type ProcState struct {
   169  	Name      string
   170  	Username  string
   171  	State     RunState
   172  	Ppid      int
   173  	Pgid      int
   174  	Tty       int
   175  	Priority  int
   176  	Nice      int
   177  	Processor int
   178  }
   179  
   180  // ProcMem contains memory statistics for a specified process
   181  type ProcMem struct {
   182  	Size        uint64
   183  	Resident    uint64
   184  	Share       uint64
   185  	MinorFaults uint64
   186  	MajorFaults uint64
   187  	PageFaults  uint64
   188  }
   189  
   190  // ProcTime contains run time statistics for a specified process
   191  type ProcTime struct {
   192  	StartTime uint64
   193  	User      uint64
   194  	Sys       uint64
   195  	Total     uint64
   196  }
   197  
   198  // ProcArgs contains a list of args for a specified process
   199  type ProcArgs struct {
   200  	List []string
   201  }
   202  
   203  // ProcEnv contains a map of environment variables for specified process
   204  type ProcEnv struct {
   205  	Vars map[string]string
   206  }
   207  
   208  // ProcExe contains basic data about a specified process
   209  type ProcExe struct {
   210  	Name string
   211  	Cwd  string
   212  	Root string
   213  }
   214  
   215  // ProcFDUsage contains data on file limits and usage
   216  type ProcFDUsage struct {
   217  	Open      uint64
   218  	SoftLimit uint64
   219  	HardLimit uint64
   220  }
   221  
   222  // Rusage contains data on resource usage for a specified process
   223  type Rusage struct {
   224  	Utime    time.Duration
   225  	Stime    time.Duration
   226  	Maxrss   int64
   227  	Ixrss    int64
   228  	Idrss    int64
   229  	Isrss    int64
   230  	Minflt   int64
   231  	Majflt   int64
   232  	Nswap    int64
   233  	Inblock  int64
   234  	Oublock  int64
   235  	Msgsnd   int64
   236  	Msgrcv   int64
   237  	Nsignals int64
   238  	Nvcsw    int64
   239  	Nivcsw   int64
   240  }