Skip to content

Commit

Permalink
Add local implementations of cpu/mem/disk status
Browse files Browse the repository at this point in the history
  • Loading branch information
afbjorklund committed Nov 1, 2020
1 parent 4c78a65 commit 03d950f
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ import (
"path/filepath"
"strconv"
"strings"
"time"

"github.com/blang/semver"
units "github.com/docker/go-units"
"github.com/pkg/errors"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/mem"
)

const (
Expand Down Expand Up @@ -63,6 +67,40 @@ func ConvertUnsignedBytesToMB(byteSize uint64) int64 {
return int64(byteSize / units.MiB)
}

// LocalCPU returns the cpu usage
// returns: busy, idle (%)
func LocalCPU() (int, int, error) {
p, err := cpu.Percent(time.Second, false)
if err != nil {
return 0, 0, err
}
return int(p[0]), int(100.0 - p[0]), nil
}

func mb(bytes uint64) uint64 {
return bytes / 1024 / 1024
}

// LocalMem returns the memory free
// returns: total, available (in mb)
func LocalMem() (uint64, uint64, error) {
v, err := mem.VirtualMemory()
if err != nil {
return 0, 0, err
}
return mb(v.Total), mb(v.Free), nil
}

// LocalDisk returns the disk free
// returns: total, available (in mb)
func LocalDisk(mountpoint string) (uint64, uint64, error) {
d, err := disk.Usage(mountpoint)
if err != nil {
return 0, 0, err
}
return mb(d.Total), mb(d.Free), nil
}

// ParseVMStat parses the output of the `vmstat` command
// returns: busy, idle (%)
func ParseVMStat(out string) (int, int, error) {
Expand Down

0 comments on commit 03d950f

Please sign in to comment.