//go:build !windows // Package collector 的上下文切换采集器。 // 读取 /proc/stat 中的 ctxt(上下文切换)和 intr(中断)累计值。 // 注意:此处返回累计值而非速率,后端可用两次上报差值计算。 // // 署名 Logan.Li package collector import ( "fmt" "os" "ezops/agent/model" ) // ContextSwitchCollector 上下文切换采集器 type ContextSwitchCollector struct{} // NewContextSwitchCollector 创建采集器实例 func NewContextSwitchCollector() *ContextSwitchCollector { return &ContextSwitchCollector{} } // Collect 读取 /proc/stat 中的上下文切换和中断统计 func (c *ContextSwitchCollector) Collect() (*model.ContextSwitchStat, error) { data, err := os.ReadFile("/proc/stat") if err != nil { return nil, fmt.Errorf("读取 /proc/stat 失败: %w", err) } stat := &model.ContextSwitchStat{} for _, line := range SplitLines(string(data)) { if HasPrefix(line, "ctxt ") { fmt.Sscanf(line, "ctxt %f", &stat.CTXPerSec) } if HasPrefix(line, "intr ") { fmt.Sscanf(line, "intr %f", &stat.IRQPerSec) } } return stat, nil }