-
Notifications
You must be signed in to change notification settings - Fork 1
/
metric_sys.go
53 lines (42 loc) · 1.2 KB
/
metric_sys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package minivmm
import (
"github.com/mackerelio/go-osstat/cpu"
"github.com/mackerelio/go-osstat/memory"
"golang.org/x/sys/unix"
)
// SysMetric is the metrics of system resrouces.
type SysMetric struct {
CPUCores int `json:"minivmm_sys_cpu_cores"`
MemoryBytes uint64 `json:"minivmm_sys_memory_bytes"`
MemoryBytesUsed uint64 `json:"minivmm_sys_memory_bytes_used"`
DiskBytes uint64 `json:"minivmm_sys_disk_bytes"`
DiskBytesUsed uint64 `json:"minivmm_sys_disk_bytes_used"`
}
// GetSysMetric returns the system resource metrics.
func GetSysMetric() (*SysMetric, error) {
var m SysMetric
cpu, err := cpu.Get()
if err != nil {
return nil, err
}
m.CPUCores = cpu.CPUCount
memory, err := memory.Get()
if err != nil {
return nil, err
}
m.MemoryBytes = memory.Total
m.MemoryBytesUsed = memory.Used
m.DiskBytes = getDiskSizeTotal(C.Dir)
m.DiskBytesUsed = getDiskSizeUsed(C.Dir)
return &m, nil
}
func getDiskSizeTotal(path string) uint64 {
var stat unix.Statfs_t
unix.Statfs(path, &stat)
return stat.Blocks * uint64(stat.Bsize)
}
func getDiskSizeUsed(path string) uint64 {
var stat unix.Statfs_t
unix.Statfs(path, &stat)
return (stat.Blocks - stat.Bavail) * uint64(stat.Bsize)
}