Browse Source
add stat of memory, disk, load, process to volume info of heartbeats for masters more details to volumes
pull/843/head
add stat of memory, disk, load, process to volume info of heartbeats for masters more details to volumes
pull/843/head
13 changed files with 2755 additions and 867 deletions
-
986weed/pb/filer_pb/filer.pb.go
-
16weed/pb/master.proto
-
829weed/pb/master_pb/master.pb.go
-
1130weed/pb/volume_server_pb/volume_server.pb.go
-
4weed/server/master_grpc_server.go
-
25weed/storage/store.go
-
108weed/storage/sysstat.go
-
50weed/storage/sysstat_test.go
-
29weed/storage/volume_info.go
-
3weed/storage/volume_super_block.go
-
2weed/topology/store_replicate.go
-
6weed/topology/topology.go
-
2weed/topology/topology_event_handling.go
986
weed/pb/filer_pb/filer.pb.go
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
829
weed/pb/master_pb/master.pb.go
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
1130
weed/pb/volume_server_pb/volume_server.pb.go
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
@ -0,0 +1,108 @@ |
|||
package storage |
|||
|
|||
import ( |
|||
"fmt" |
|||
"github.com/shirou/gopsutil/disk" |
|||
"github.com/shirou/gopsutil/load" |
|||
"github.com/shirou/gopsutil/mem" |
|||
"github.com/shirou/gopsutil/process" |
|||
"os" |
|||
"path/filepath" |
|||
"syscall" |
|||
) |
|||
|
|||
// MemoryStat stats the current total and free memory of the host
|
|||
func MemoryStat() (total, free uint64, err error) { |
|||
stat, err := mem.VirtualMemory() |
|||
if err != nil { |
|||
return 0, 0, err |
|||
} |
|||
|
|||
return stat.Total, stat.Free, nil |
|||
} |
|||
|
|||
// DiskStat stats the current total and free of specified dir
|
|||
func DiskStat(dir string) (total, free uint64, device, mountPoint string, err error) { |
|||
absPath, _ := filepath.Abs(dir) |
|||
stat, err := disk.Usage(absPath) |
|||
if err != nil { |
|||
return 0, 0, "", "", err |
|||
} |
|||
|
|||
point, err := MountPoint(absPath) |
|||
if err != nil { |
|||
return 0, 0, "", "", err |
|||
} |
|||
|
|||
partitions, _ := disk.Partitions(false) |
|||
for _, p := range partitions { |
|||
if p.Mountpoint == point { |
|||
device = p.Device |
|||
break |
|||
} |
|||
fmt.Println(p.Device, p.Fstype, p.Mountpoint) |
|||
} |
|||
|
|||
return stat.Total, stat.Free, device, point, nil |
|||
} |
|||
|
|||
// LoadStat return average load1, load5 and load15 of the host
|
|||
func LoadStat() (load1, load5, load15 float64, err error) { |
|||
stat, e := load.Avg() |
|||
if e != nil { |
|||
return 0, 0, 0, e |
|||
} |
|||
|
|||
return stat.Load1, stat.Load5, stat.Load15, nil |
|||
} |
|||
|
|||
// ProcessStat return cpu usage and RSS of the current process
|
|||
func ProcessStat() (name string, cpuUsage float64, rss uint64, err error) { |
|||
p, e := process.NewProcess(int32(os.Getpid())) |
|||
if e != nil { |
|||
return "", 0, 0, e |
|||
} |
|||
|
|||
stat, e := p.MemoryInfo() |
|||
if e != nil { |
|||
return "", 0, 0, e |
|||
} |
|||
|
|||
cpuUsage, e = p.CPUPercent() |
|||
if e != nil { |
|||
return "", 0, 0, e |
|||
} |
|||
|
|||
name, e = p.Name() |
|||
if e != nil { |
|||
return "", 0, 0, e |
|||
} |
|||
|
|||
return name, cpuUsage, stat.RSS, nil |
|||
} |
|||
|
|||
func MountPoint(absPath string) (string, error) { |
|||
pi, err := os.Stat(absPath) |
|||
if err != nil { |
|||
return "", err |
|||
} |
|||
|
|||
odev := pi.Sys().(*syscall.Stat_t).Dev |
|||
|
|||
for absPath != "/" { |
|||
_path := filepath.Dir(absPath) |
|||
|
|||
in, err := os.Stat(_path) |
|||
if err != nil { |
|||
return "", err |
|||
} |
|||
|
|||
if odev != in.Sys().(*syscall.Stat_t).Dev { |
|||
break |
|||
} |
|||
|
|||
absPath = _path |
|||
} |
|||
|
|||
return absPath, nil |
|||
} |
|||
@ -0,0 +1,50 @@ |
|||
package storage |
|||
|
|||
import ( |
|||
"fmt" |
|||
"github.com/stretchr/testify/assert" |
|||
"testing" |
|||
) |
|||
|
|||
func TestMemoryStat(t *testing.T) { |
|||
total, free, err := MemoryStat() |
|||
assert.Nil(t, err) |
|||
|
|||
if total <= 0 { |
|||
println("total", total, "free", free) |
|||
t.Fail() |
|||
} |
|||
} |
|||
|
|||
func TestDiskStat(t *testing.T) { |
|||
total, free, device, mountPoint, err := DiskStat("..") |
|||
assert.Nil(t, err) |
|||
|
|||
if total <= 0 { |
|||
println("total", total, "free", free) |
|||
t.Fail() |
|||
} |
|||
|
|||
fmt.Println("device", device, "mountPoint", mountPoint) |
|||
|
|||
} |
|||
|
|||
func TestAvgLoad(t *testing.T) { |
|||
load1, load5, load15, err := LoadStat() |
|||
assert.Nil(t, err) |
|||
|
|||
if load1 <= 0 { |
|||
println("load1", load1, "load5", load5, "load15", load15) |
|||
t.Fail() |
|||
} |
|||
} |
|||
|
|||
func TestProcessStat(t *testing.T) { |
|||
name, cpuUsage, rss, err := ProcessStat() |
|||
assert.Nil(t, err) |
|||
|
|||
if rss <= 0 { |
|||
println("name", name, "cpuUsage", cpuUsage, "RSS", rss) |
|||
t.Fail() |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue