Documentation ¶
Index ¶
Constants ¶
View Source
const ( NewOperType = "add" UpdateOperType = "update" )
CMDB自定义的上报类型
View Source
const VERSION = "v1.0"
Variables ¶
View Source
var GetCPUStatus = func(ctx context.Context) (*CPU, error) { infos, err := cpu.InfoWithContext(ctx) if err != nil { return nil, err } total := len(infos) model := "" if total == 0 { model = "unknown" } else { model = infos[0].ModelName } if runtime.GOOS == "windows" { n := 0 for _, info := range infos { n += int(info.Cores) } total = n } return &CPU{Total: total, Model: model}, nil }
GetCPUStatus :
View Source
var GetData = func(ctx context.Context, cfg *configs.StaticTaskConfig) (*Report, error) { cpu, err := GetCPUStatus(ctx) if err != nil { logger.Errorf("failed to get cpu status: %v", err) } mem, err := GetMemoryStatus(ctx) if err != nil { logger.Errorf("failed to get mem status: %v", err) } disk, err := GetDiskStatus(ctx) if err != nil { logger.Errorf("failed to get disk status: %v", err) } net, err := GetNetStatus(ctx, cfg) if err != nil { logger.Errorf("failed to get net status: %v", err) } system, err := GetSystemStatus(ctx) if err != nil { logger.Errorf("failed to get system status: %v", err) } logger.Debug("collect report data success") return NewReport(cpu, mem, disk, net, system), nil }
GetData 采集全部静态数据
View Source
var GetDiskStatus = func(ctx context.Context) (*Disk, error) { infos, err := disk.PartitionsWithContext(ctx, true) if err != nil { return nil, err } diskSet := GetDisks() var total uint64 = 0 repeatMap := make(map[string]bool) for _, info := range infos { if !diskSet.Exist(info.Device) { if !strings.Contains(info.Device, "/dev/mapper") { continue } infoSymlink, err := filepath.EvalSymlinks(info.Device) if err != nil { logger.Warnf("failed to get file symlink info for: %s", err) continue } infoSymlink = strings.Replace(infoSymlink, "..", "/dev", 1) if !diskSet.Exist(infoSymlink) { continue } } if _, ok := repeatMap[info.Device]; ok { logger.Debugf("get repeat device:%s with mountpoint:%s,skip", info.Device, info.Mountpoint) continue } repeatMap[info.Device] = true usage, err := disk.UsageWithContext(ctx, info.Mountpoint) if err != nil { logger.Warnf("failed to get usage info for: %s", err) continue } total = total + usage.Total } return &Disk{ Total: total, }, nil }
GetDiskStatus linux相比windows,多了去除虚拟挂载的逻辑
View Source
var GetDisks = func() common.Set { set := common.NewSet() path := "/proc/partitions" data, err := os.ReadFile(path) if err != nil { if os.IsNotExist(err) { partitions, err := disk.Partitions(true) if err != nil { return set } for _, partition := range partitions { set.Insert(partition.Device) } return set } logger.Errorf("read file:%s,failed,error:%s", path, err) return set } lines := bytes.Split(data, []byte("\n")) for idx, line := range lines { if len(line) == 0 || idx == 0 { continue } fields := bytes.Fields(line) if len(fields) == 4 { set.Insert("/dev/" + string(fields[3])) } } return set }
GetDisks :
View Source
var GetMemoryStatus = func(ctx context.Context) (*Memory, error) { info, err := mem.VirtualMemoryWithContext(ctx) if err != nil { return nil, err } return &Memory{Total: info.Total}, nil }
GetMemoryStatus :
View Source
var GetNetStatus = func(ctx context.Context, cfg *configs.StaticTaskConfig) (*Net, error) { interfaces, err := net.Interfaces() if err != nil { return nil, err } virtualInterfaces, err := GetVirtualInterfaceSet() if err != nil { return nil, err } whiteList := make(map[string]struct{}) if cfg != nil && len(cfg.VirtualIfaceWhitelist) > 0 { for _, iface := range cfg.VirtualIfaceWhitelist { whiteList[iface] = struct{}{} } } items := make([]Interface, 0, len(interfaces)) for _, inter := range interfaces { if virtualInterfaces.Exist(inter.Name) { if _, ok := whiteList[inter.Name]; !ok { continue } } addrs, err := inter.Addrs() if err != nil { logger.Warnf("failed to get net addr info for: %s", err) continue } addrList := make([]string, 0, len(addrs)) for _, addr := range addrs { addrList = append(addrList, addr.String()) } item := Interface{ Addrs: addrList, Mac: inter.HardwareAddr.String(), Name: inter.Name, } items = append(items, item) } return &Net{Interface: items}, nil }
GetNetStatus :
View Source
var GetRandomDuration = func() time.Duration { randomSecond := rand.Intn(3600) return time.Duration(randomSecond) * time.Second }
GetRandomDuration 获取一个0~3600秒之间的数字
View Source
var GetSystemStatus = func(ctx context.Context) (*System, error) { info, err := host.InfoWithContext(ctx) if err != nil { return nil, err } if osSystemType == "" { osSystemType = tasks.GetSystemType() } bkAgentID := GetBKAgentID() return &System{ HostName: info.Hostname, SysType: osSystemType, OS: info.OS, Platform: info.Platform, PlatVer: info.PlatformVersion, BKAgentID: bkAgentID, Arch: info.KernelArch, }, nil }
GetSystemStatus :
Functions ¶
func GetVirtualInterfaceSet ¶
GetVirtualInterfaceSet 获取虚拟网卡列表
Types ¶
type Event ¶
type Event struct {
// contains filtered or unexported fields
}
Event :
type Status ¶
type Status struct { FirstReport bool // contains filtered or unexported fields }
Status 控制任务状态,负责对任务启动的执行逻辑进行控制
func (*Status) UpdateCheckTime ¶
UpdateCheckTime :
func (*Status) UpdateReportTime ¶
UpdateReportTime :
Click to show internal directories.
Click to hide internal directories.