procstat

package
v0.0.0-...-5655933 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2023 License: MIT Imports: 14 Imported by: 0

README

procstat

进程监控插件,两个核心作用,监控进程是否存活、监控进程使用了多少资源(CPU、内存、文件句柄等)

存活监控

如果进程监听了端口,就直接用 net_response 来做存活性监控即可,无需使用 procstat 来做,因为:端口在监听,说明进程一定活着

进程筛选

机器上进程很多,我们要做进程监控,就要想办法告诉 Categraf 要监控哪些进程,通过 search 打头的那几个配置,可以做进程过滤筛选:

# # executable name (ie, pgrep <search_exec_substring>)
search_exec_substring = "nginx"

# # pattern as argument for pgrep (ie, pgrep -f <search_cmdline_substring>)
# search_cmdline_substring = "n9e server"

# # windows service name
# search_win_service = ""

上面三个 search 相关的配置,每个采集目标选用其中一个。有一个额外的配置,search_user 配合search_exec_substring 或者 search_cmdline_substring 使用,表示匹配指定username的特定进程。如果不需要指定username ,保持配置注释即可。

# # search process with specific user, option with exec_substring or cmdline_substring
# search_user = ""

mode

mode 配置有两个值供选择,一个是 solaris,一个是 irix,默认是 irix,用这个配置来决定使用哪种 cpu 使用率的计算方法:

func (ins *Instance) gatherCPU(slist *types.SampleList, procs map[PID]Process, tags map[string]string, solarisMode bool) {
	var value float64
	for pid := range procs {
		v, err := procs[pid].Percent(time.Duration(0))
		if err == nil {
			if solarisMode {
				value += v / float64(runtime.NumCPU())
				slist.PushFront(types.NewSample("cpu_usage", v/float64(runtime.NumCPU()), map[string]string{"pid": fmt.Sprint(pid)}, tags))
			} else {
				value += v
				slist.PushFront(types.NewSample("cpu_usage", v, map[string]string{"pid": fmt.Sprint(pid)}, tags))
			}
		}
	}

	if ins.GatherTotal {
		slist.PushFront(types.NewSample("cpu_usage_total", value, tags))
	}
}

gather_total

比如进程名字是 mysql 的进程,同时可能运行了多个,我们想知道这个机器上的所有 mysql 的进程占用的总的 cpu、mem、fd 等,就设置 gather_total = true,当然,对于 uptime 和 limit 的采集,gather_total 的时候是取的多个进程的最小值

gather_per_pid

还是拿 mysql 举例,一个机器上可能同时运行了多个,我们可能想知道每个 mysql 进程的资源占用情况,此时就要启用 gather_per_pid 的配置,设置为 true,此时会采集每个进程的资源占用情况,并附上 pid 作为标签来区分

gather_more_metrics

默认 procstat 插件只是采集进程数量,如果想采集进程占用的资源,就要启用 gather_more_metrics 中的项,启用哪个就额外采集哪个

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Filter

type Filter func(p *process.Process) bool

func UserFilter

func UserFilter(username string) Filter

type Instance

type Instance struct {
	config.InstanceConfig

	SearchExecSubstring    string   `toml:"search_exec_substring"`
	SearchCmdlineSubstring string   `toml:"search_cmdline_substring"`
	SearchWinService       string   `toml:"search_win_service"`
	SearchUser             string   `toml:"search_user"`
	Mode                   string   `toml:"mode"`
	GatherTotal            bool     `toml:"gather_total"`
	GatherPerPid           bool     `toml:"gather_per_pid"`
	GatherMoreMetrics      []string `toml:"gather_more_metrics"`
	// contains filtered or unexported fields
}

func (*Instance) Gather

func (ins *Instance) Gather(slist *types.SampleList)

func (*Instance) Init

func (ins *Instance) Init() error

type NativeFinder

type NativeFinder struct {
}

NativeFinder uses gopsutil to find processes

func (*NativeFinder) FastProcessList

func (pg *NativeFinder) FastProcessList() ([]*process.Process, error)

func (*NativeFinder) FullPattern

func (pg *NativeFinder) FullPattern(pattern string, filters ...Filter) ([]PID, error)

FullPattern matches on the command line when the process was executed

func (*NativeFinder) Pattern

func (pg *NativeFinder) Pattern(pattern string, filters ...Filter) ([]PID, error)

Pattern matches on the process name

func (*NativeFinder) PidFile

func (pg *NativeFinder) PidFile(path string) ([]PID, error)

PidFile returns the pid from the pid file given.

func (*NativeFinder) UID

func (pg *NativeFinder) UID(user string) ([]PID, error)

Uid will return all pids for the given user

type PID

type PID int32

type PIDFinder

type PIDFinder interface {
	PidFile(path string) ([]PID, error)
	Pattern(pattern string, filters ...Filter) ([]PID, error)
	UID(user string) ([]PID, error)
	FullPattern(path string, filters ...Filter) ([]PID, error)
}

func NewNativeFinder

func NewNativeFinder() (PIDFinder, error)

type Proc

type Proc struct {
	*process.Process
	// contains filtered or unexported fields
}

func (*Proc) PID

func (p *Proc) PID() PID

func (*Proc) Percent

func (p *Proc) Percent(_ time.Duration) (float64, error)

func (*Proc) Tags

func (p *Proc) Tags() map[string]string

func (*Proc) Username

func (p *Proc) Username() (string, error)

type Process

type Process interface {
	PID() PID
	Tags() map[string]string

	PageFaults() (*process.PageFaultsStat, error)
	IOCounters() (*process.IOCountersStat, error)
	MemoryInfo() (*process.MemoryInfoStat, error)
	Name() (string, error)
	Cmdline() (string, error)
	NumCtxSwitches() (*process.NumCtxSwitchesStat, error)
	NumFDs() (int32, error)
	NumThreads() (int32, error)
	Percent(interval time.Duration) (float64, error)
	MemoryPercent() (float32, error)
	Times() (*cpu.TimesStat, error)
	RlimitUsage(bool) ([]process.RlimitStat, error)
	Username() (string, error)
	CreateTime() (int64, error)
	Ppid() (int32, error)
}

func NewProc

func NewProc(pid PID) (Process, error)

type Procstat

type Procstat struct {
	config.PluginConfig
	Instances []*Instance `toml:"instances"`
}

func (*Procstat) Clone

func (p *Procstat) Clone() inputs.Input

func (*Procstat) GetInstances

func (s *Procstat) GetInstances() []inputs.Instance

func (*Procstat) Name

func (p *Procstat) Name() string

Jump to

Keyboard shortcuts

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