Skip to content

Commit

Permalink
[common] add disk usage monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
lzf575 committed Dec 19, 2023
1 parent 869e45b commit bac0a2f
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions server/common/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/deepflowio/deepflow/server/libs/stats"
"github.com/deepflowio/deepflow/server/libs/utils"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/net"
Expand Down Expand Up @@ -109,6 +110,8 @@ func NewMonitor() (*Monitor, error) {
}
myNodeIP, _ := os.LookupEnv(ENV_K8S_NODE_IP)
stats.RegisterCountable("monitor", m, stats.OptionStatTags{"host_ip": myNodeIP})
NewDiskMonitor([]string{"/", "/mnt"}, myNodeIP)

return m, nil
}

Expand All @@ -134,3 +137,40 @@ func (m *Monitor) GetCounter() interface{} {
func (m *Monitor) Stop() {
m.Close()
}

type DiskMonitor struct {
path string
utils.Closable
}

type DiskCounter struct {
Total uint64 `statsd:"total"`
Free uint64 `statsd:"free"`
Used uint64 `statsd:"used"`
UsedPercent float64 `statsd:"used-percent"`
}

func (m *DiskMonitor) GetCounter() interface{} {
usage, err := disk.Usage(m.path)
if err != nil {
return &Counter{}

}
return &DiskCounter{
Total: usage.Total,
Free: usage.Free,
Used: usage.Used,
UsedPercent: usage.UsedPercent,
}
}

func (m *DiskMonitor) Stop() {
m.Close()
}

func NewDiskMonitor(paths []string, hostIp string) {
for _, path := range paths {
m := &DiskMonitor{path: path}
stats.RegisterCountable("monitor_disk", m, stats.OptionStatTags{"host_ip": hostIp, "path": path})
}
}

0 comments on commit bac0a2f

Please sign in to comment.