func GetHardwareProfile() (*pb.HardwareProfile, error) { v, err := mem.VirtualMemory() if err != nil { return &pb.HardwareProfile{}, err } d, err := disk.Partitions(true) if err != nil { return &pb.HardwareProfile{}, err } hw := &pb.HardwareProfile{ Disks: make([]*pb.Disk, 0), Cpus: int64(runtime.NumCPU()), Memtotal: v.Total, Memfree: v.Free, } for k := range d { usage, err := disk.Usage(d[k].Mountpoint) if err != nil { continue } entry := &pb.Disk{ Path: d[k].Mountpoint, Device: d[k].Device, Size_: usage.Total, Used: usage.Used, } hw.Disks = append(hw.Disks, entry) } return hw, nil }
func gaugesUpdate(results runner.BenchmarkResults) { if s, err := load.Avg(); err == nil { gauges["cpu_load1"].Set(s.Load1) gauges["cpu_load5"].Set(s.Load5) gauges["cpu_load15"].Set(s.Load15) } if s, err := mem.VirtualMemory(); err == nil { gauges["mem_total"].Set(float64(s.Total)) gauges["mem_used"].Set(float64(s.Used)) } if s, err := disk.Usage("/"); err == nil { gauges["disk_total"].Set(float64(s.Total)) gauges["disk_used"].Set(float64(s.Used)) } if results != nil { gauges["io_time"].Set(results["IO"].Time) gauges["io_wall_time"].Set(results["IO"].WallTime) gauges["io_memory"].Set(float64(results["IO"].Memory)) gauges["cpu_time"].Set(results["CPU"].Time) gauges["cpu_wall_time"].Set(results["CPU"].WallTime) gauges["cpu_memory"].Set(float64(results["CPU"].Memory)) gauges["memory_time"].Set(results["Memory"].Time) gauges["memory_wall_time"].Set(results["Memory"].WallTime) gauges["memory_memory"].Set(float64(results["Memory"].Memory)) } }
// Collect collects stats related to resource usage of a host func (h *HostStatsCollector) Collect() (*HostStats, error) { hs := &HostStats{Timestamp: time.Now().UTC().UnixNano()} if memStats, err := mem.VirtualMemory(); err == nil { ms := &MemoryStats{ Total: memStats.Total, Available: memStats.Available, Used: memStats.Used, Free: memStats.Free, } hs.Memory = ms } if cpuStats, err := cpu.Times(true); err == nil { cs := make([]*CPUStats, len(cpuStats)) for idx, cpuStat := range cpuStats { cs[idx] = &CPUStats{ CPU: cpuStat.CPU, User: cpuStat.User, System: cpuStat.System, Idle: cpuStat.Idle, } percentCalculator, ok := h.statsCalculator[cpuStat.CPU] if !ok { percentCalculator = NewHostCpuStatsCalculator() h.statsCalculator[cpuStat.CPU] = percentCalculator } idle, user, system, total := percentCalculator.Calculate(cpuStat) cs[idx].Idle = idle cs[idx].System = system cs[idx].User = user cs[idx].Total = total } hs.CPU = cs } if partitions, err := disk.Partitions(false); err == nil { var diskStats []*DiskStats for _, partition := range partitions { if usage, err := disk.Usage(partition.Mountpoint); err == nil { ds := DiskStats{ Device: partition.Device, Mountpoint: partition.Mountpoint, Size: usage.Total, Used: usage.Used, Available: usage.Free, UsedPercent: usage.UsedPercent, InodesUsedPercent: usage.InodesUsedPercent, } diskStats = append(diskStats, &ds) } } hs.DiskStats = diskStats } if uptime, err := host.Uptime(); err == nil { hs.Uptime = uptime } return hs, nil }
func printDiskUsage2() { path := "/" diskUsage, err := disk.Usage(path) if err != nil { log.Fatal(err) } fmt.Println(diskUsage) }
func GetInfo() map[string]interface{} { path := "/" if runtime.GOOS == "windows" { file, _ := exec.LookPath(os.Args[0]) path = filepath.VolumeName(file) } diskinfo := disk.UsageStat{} if v, err := disk.Usage(path); err == nil { var i interface{} i = v switch inst := i.(type) { case disk.UsageStat: diskinfo = inst break case *disk.UsageStat: if inst != nil { diskinfo = *inst } break } } diskinfo.Total /= 1024 * 1024 diskinfo.Used /= 1024 * 1024 vm := mem.VirtualMemoryStat{} sm := mem.SwapMemoryStat{} if v, err := mem.VirtualMemory(); err == nil { vm = *v } vm.Total /= 1024 * 1024 vm.Used /= 1024 * 1024 if v, err := mem.SwapMemory(); err == nil { sm = *v } sm.Total /= 1024 * 1024 sm.Used /= 1024 * 1024 var cpupercent float64 ps, err := cpu.Percent(time.Millisecond, true) if err == nil && len(ps) > 0 { for _, v := range ps { cpupercent += v } cpupercent /= float64(len(ps)) } return map[string]interface{}{ "DiskInfo": diskinfo, "SwapMemory": sm, "VirtualMemory": vm, "CpuPercent": cpupercent * 100, } }
func GetDiskStats() string { d, err := disk.Usage("/") format := "fstype=%s total=%d free=%d used=%d used_percent=%f inodes_total=%d inodes_used=%d inodes_free=%d inodes_used_percent=%f" if err == nil { return fmt.Sprintf(format, d.Fstype, d.Total, d.Free, d.Used, d.UsedPercent, d.InodesTotal, d.InodesUsed, d.InodesFree, d.InodesUsedPercent) } else { return fmt.Sprintf(format, 0, 0, 0, 0, 0, 0, 0, 0, 0) } }
// needDiskSpace returns true if we need to free diskspace func (gc *GarbageCollector) needDiskSpace() bool { // If we have no metrics or minimum diskspace we remove everything if gc.minimumDiskSpace == 0 || gc.storageFolder == "" { return true } stat, err := disk.Usage(gc.storageFolder) if err != nil { // TODO: Write a warning to the log return true } return int64(stat.Free) < gc.minimumDiskSpace }
func (s *systemPS) DiskUsage( mountPointFilter []string, fstypeExclude []string, ) ([]*disk.UsageStat, error) { parts, err := disk.Partitions(true) if err != nil { return nil, err } // Make a "set" out of the filter slice mountPointFilterSet := make(map[string]bool) for _, filter := range mountPointFilter { mountPointFilterSet[filter] = true } fstypeExcludeSet := make(map[string]bool) for _, filter := range fstypeExclude { fstypeExcludeSet[filter] = true } var usage []*disk.UsageStat for _, p := range parts { if len(mountPointFilter) > 0 { // If the mount point is not a member of the filter set, // don't gather info on it. _, ok := mountPointFilterSet[p.Mountpoint] if !ok { continue } } mountpoint := os.Getenv("HOST_MOUNT_PREFIX") + p.Mountpoint if _, err := os.Stat(mountpoint); err == nil { du, err := disk.Usage(mountpoint) du.Path = p.Mountpoint if err != nil { return nil, err } // If the mount point is a member of the exclude set, // don't gather info on it. _, ok := fstypeExcludeSet[p.Fstype] if ok { continue } du.Fstype = p.Fstype usage = append(usage, du) } } return usage, nil }
func gaugesUpdate() { if s, err := load.Avg(); err == nil { gauges["cpu_load1"].Set(s.Load1) gauges["cpu_load5"].Set(s.Load5) gauges["cpu_load15"].Set(s.Load15) } if s, err := mem.VirtualMemory(); err == nil { gauges["mem_total"].Set(float64(s.Total)) gauges["mem_used"].Set(float64(s.Used)) } if s, err := disk.Usage("/"); err == nil { gauges["disk_total"].Set(float64(s.Total)) gauges["disk_used"].Set(float64(s.Used)) } }
func BuildDeviceUsage(_fsSpec, _fsFile, _fsVfstype string) (*DeviceUsage, error) { var deviceUsage DeviceUsage stat, err := disk.Usage(_fsSpec) if err != nil { return nil, err } deviceUsage = DeviceUsage{_fsSpec, _fsFile, _fsVfstype, stat.Total, stat.Used, stat.Free, stat.UsedPercent, 100 - stat.UsedPercent, stat.InodesTotal, stat.InodesUsed, stat.InodesFree, stat.InodesUsedPercent, 100 - stat.InodesUsedPercent, } return &deviceUsage, nil }
func diskPartitionMetrics() []*types.TimeSeriesData { pts, err := disk.Partitions(false) if err != nil { return nil } metrics := []*types.TimeSeriesData{} ts := time.Now().Unix() for _, p := range pts { cnt, err := disk.Usage(p.Mountpoint) if err != nil { continue } metrics = append(metrics, &types.TimeSeriesData{ Metric: "disk.free", Value: float64(cnt.Free), Cycle: Cycle, Timestamp: ts, DataType: "GAUGE", Tags: map[string]string{"path": cnt.Path, "fstype": cnt.Fstype}, }, &types.TimeSeriesData{ Metric: "disk.total", Value: float64(cnt.Total), Cycle: Cycle, Timestamp: ts, DataType: "GAUGE", Tags: map[string]string{"path": cnt.Path, "fstype": cnt.Fstype}, }, &types.TimeSeriesData{ Metric: "disk.used", Value: float64(cnt.Used), Cycle: Cycle, Timestamp: ts, DataType: "GAUGE", Tags: map[string]string{"path": cnt.Path, "fstype": cnt.Fstype}, }, &types.TimeSeriesData{ Metric: "disk.usedpercent", Value: cnt.UsedPercent, Cycle: Cycle, Timestamp: ts, DataType: "GAUGE", Tags: map[string]string{"path": cnt.Path, "fstype": cnt.Fstype}, }, &types.TimeSeriesData{ Metric: "disk.inodes.free", Value: float64(cnt.InodesFree), Cycle: Cycle, Timestamp: ts, DataType: "GAUGE", Tags: map[string]string{"path": cnt.Path, "fstype": cnt.Fstype}, }, &types.TimeSeriesData{ Metric: "disk.inodes.total", Value: float64(cnt.InodesTotal), Cycle: Cycle, Timestamp: ts, DataType: "GAUGE", Tags: map[string]string{"path": cnt.Path, "fstype": cnt.Fstype}, }, &types.TimeSeriesData{ Metric: "disk.inodes.used", Value: float64(cnt.InodesUsed), Cycle: Cycle, Timestamp: ts, DataType: "GAUGE", Tags: map[string]string{"path": cnt.Path, "fstype": cnt.Fstype}, }, &types.TimeSeriesData{ Metric: "disk.inodes.usedpercent", Value: float64(cnt.InodesUsedPercent), Cycle: Cycle, Timestamp: ts, DataType: "GAUGE", Tags: map[string]string{"path": cnt.Path, "fstype": cnt.Fstype}, }, ) } return metrics }
// Collect collects stats related to resource usage of a host func (h *HostStatsCollector) Collect() (*HostStats, error) { hs := &HostStats{Timestamp: time.Now().UTC().UnixNano()} memStats, err := mem.VirtualMemory() if err != nil { return nil, err } hs.Memory = &MemoryStats{ Total: memStats.Total, Available: memStats.Available, Used: memStats.Used, Free: memStats.Free, } ticksConsumed := 0.0 cpuStats, err := cpu.Times(true) if err != nil { return nil, err } cs := make([]*CPUStats, len(cpuStats)) for idx, cpuStat := range cpuStats { percentCalculator, ok := h.statsCalculator[cpuStat.CPU] if !ok { percentCalculator = NewHostCpuStatsCalculator() h.statsCalculator[cpuStat.CPU] = percentCalculator } idle, user, system, total := percentCalculator.Calculate(cpuStat) cs[idx] = &CPUStats{ CPU: cpuStat.CPU, User: user, System: system, Idle: idle, Total: total, } ticksConsumed += (total / 100) * (shelpers.TotalTicksAvailable() / float64(len(cpuStats))) } hs.CPU = cs hs.CPUTicksConsumed = ticksConsumed partitions, err := disk.Partitions(false) if err != nil { return nil, err } var diskStats []*DiskStats for _, partition := range partitions { usage, err := disk.Usage(partition.Mountpoint) if err != nil { return nil, err } ds := DiskStats{ Device: partition.Device, Mountpoint: partition.Mountpoint, Size: usage.Total, Used: usage.Used, Available: usage.Free, UsedPercent: usage.UsedPercent, InodesUsedPercent: usage.InodesUsedPercent, } if math.IsNaN(ds.UsedPercent) { ds.UsedPercent = 0.0 } if math.IsNaN(ds.InodesUsedPercent) { ds.InodesUsedPercent = 0.0 } diskStats = append(diskStats, &ds) } hs.DiskStats = diskStats uptime, err := host.Uptime() if err != nil { return nil, err } hs.Uptime = uptime return hs, nil }