package resources import ( "linkfog.com/public/lib/l" "linkfog.com/public/lib/unit" ) var ( preHostDiskIOCountersStat *HostDiskIOCountersStatWrap preHostCPUTimesStat *HostCPUTimesStatWrap preHostNetIOCountersStat *HostNetIOCountersStatWrap cpuUsage float64 memUsage float64 diskUsage float64 inodeUsage float64 ) func getAndPrintHostInfo() { getAndPrintHostCPUInfo() getAndPrintHostCPUStates() getAndPrintHostMemoryUsage() // getAndPrintDockerRootDiskSpaceUsage() getAndPrintHostDiskSpaceUsage() getAndPrintHostDiskIOUsage() getAndPrintHostNetIOUsage() printHostSummary() } func getAndPrintHostCPUInfo() { cnt, err := GetHostCPUCount() if err != nil { l.Errorf("get host cpu count error: %v", err) return } usage, err := GetHostCPUUsage() if err != nil { l.Errorf("get host cpu usage error: %v", err) return } cpuUsage = usage avg, err := GetHostCPULoad() if err != nil { l.Errorf("get host cpu load error: %v", err) return } l.Debugf("host cpu info, count:%d, usage:%.1f%%, load1:%.2f, load5:%.2f, load15:%.2f", cnt, usage, avg.Load1, avg.Load5, avg.Load15) } func getAndPrintHostCPUStates() { var states []HostCPUStatesPct var err error states, preHostCPUTimesStat, err = GetHostCPUStatesWithPre(preHostCPUTimesStat) if err != nil { l.Errorf("get host cpu states error: %v", err) return } for _, state := range states { l.Debugf("host cpu states, cpu:%s, us:%.1f%%, sy:%.1f%%, ni:%.1f%%, id:%.1f%%, wa:%.1f%%, "+ "hi:%.1f%%, si:%.1f%%, st:%.1f%%, gu:%.1f%%, gn:%.1f%%", state.CPU, state.User, state.System, state.Nice, state.Idle, state.Iowait, state.Irq, state.Softirq, state.Steal, state.Guest, state.GuestNice) } } func getAndPrintHostMemoryUsage() { vmStat, err := GetHostVirtualMemory() if err != nil { l.Errorf("get host virtual memory error: %v", err) return } memUsage = vmStat.UsedPercent vmExStat, err := GetHostVirtualMemoryEx() if err != nil { l.Errorf("get host virtual memoryEx error: %v", err) return } l.Debugf("host mem usage, total:%s, used:%s, avail:%s, act_file:%s, inact_file:%s, "+ "act_anon:%s, inact_anon:%s, buff:%s, cache:%s, shr:%s, slab:%s", unit.ByteSize(vmStat.Total), unit.ByteSize(vmStat.Used), unit.ByteSize(vmStat.Available), unit.ByteSize(vmExStat.ActiveFile), unit.ByteSize(vmExStat.InactiveFile), unit.ByteSize(vmExStat.ActiveAnon), unit.ByteSize(vmExStat.InactiveAnon), unit.ByteSize(vmStat.Buffers), unit.ByteSize(vmStat.Cached), unit.ByteSize(vmStat.Shared), unit.ByteSize(vmStat.Slab)) smStat, err := GetHostSwapMemory() if err != nil { l.Errorf("get host swap memory error: %v", err) return } l.Debugf("host swap mem usage, total:%s, used:%s, avail:%s", unit.ByteSize(smStat.Total), unit.ByteSize(smStat.Used), unit.ByteSize(smStat.Free)) } //func getAndPrintDockerRootDiskSpaceUsage() { // usage, err := GetDockerRootDiskSpaceUsage() // if err != nil { // l.Errorf("get docker root disk space usage error: %v", err) // return // } // diskUsage = usage.UsedPercent // inodeUsage = usage.InodesUsedPercent // // l.Debugf("host docker root disk space usage, path:%s, size:%s, used:%s, avail:%s, usage:%.2f%%, "+ // "isize:%s, iused:%s, iavail:%s, iusage:%.2f%%", usage.Path, // unit.ByteSize(usage.Total), unit.ByteSize(usage.Used), // unit.ByteSize(usage.Free), usage.UsedPercent, // unit.ByteSize(usage.InodesTotal), unit.ByteSize(usage.InodesUsed), // unit.ByteSize(usage.InodesFree), usage.InodesUsedPercent) //} func getAndPrintHostDiskSpaceUsage() { usages, err := GetHostDiskSpaceUsage() if err != nil { l.Errorf("get host disk space usage error: %v", err) return } filter := make(map[string]struct{}) for _, usage := range usages { if _, ok := filter[usage.PartitionStat.Device]; ok { continue } filter[usage.PartitionStat.Device] = struct{}{} l.Debugf("host disk space usage, dev:%s, size:%s, used:%s, avail:%s, usage:%.2f%%, "+ "isize:%d, iused:%d, iavail:%d, iusage:%.2f%%", usage.PartitionStat.Device, unit.ByteSize(usage.UsageStat.Total), unit.ByteSize(usage.UsageStat.Used), unit.ByteSize(usage.UsageStat.Free), usage.UsageStat.UsedPercent, usage.UsageStat.InodesTotal, usage.UsageStat.InodesUsed, usage.UsageStat.InodesFree, usage.UsageStat.InodesUsedPercent) } } func getAndPrintHostDiskIOUsage() { var usages []HostDiskIOUsage var err error usages, preHostDiskIOCountersStat, err = GetHostDiskIOUsageWithPre(preHostDiskIOCountersStat) if err != nil { l.Errorf("get host disk io usage error: %v", err) return } for _, usage := range usages { l.Debugf("host disk io stats, dev:%s, rps:%s, wps:%s, r:%s, w:%s", usage.Device, unit.ByteSize(usage.ReadUsage), unit.ByteSize(usage.WriteUsage), unit.ByteSize(usage.ReadBytes), unit.ByteSize(usage.WriteBytes)) } } func getAndPrintHostNetIOUsage() { var usages []HostNetIOUsage var err error usages, preHostNetIOCountersStat, err = GetHostNetIOUsageWithPre(preHostNetIOCountersStat) if err != nil { l.Errorf("get host net io usage error: %v", err) return } for _, usage := range usages { l.Infof("host net io stats, if:%s, "+ "rx/s:%s, tx/s:%s, rx_pkg/s:%d, tx_pkg/s:%d, rx_err/s:%d, tx_err/s:%d, "+ "rx_drop/s:%d, tx_drop/s:%d, rx_fifo/s:%d, tx_fifo/s:%d", usage.Name, unit.ByteSize(usage.BytesRecv), unit.ByteSize(usage.BytesSent), usage.PacketsRecv, usage.PacketsSent, usage.Errin, usage.Errout, usage.Dropin, usage.Dropout, usage.Fifoin, usage.Fifoout) } } func printHostSummary() { l.Infof("host stats, cpu_usage:%.2f%%, mem_usage:%.2f%%, disk_usage:%.2f%%, inode_usage:%.2f%%", cpuUsage, memUsage, diskUsage, inodeUsage) }