github.com/iotexproject/iotex-core@v1.14.1-rc1/server/itx/nodestats/systemstats.go (about) 1 package nodestats 2 3 import ( 4 "runtime" 5 "strconv" 6 "strings" 7 "syscall" 8 9 "github.com/mackerelio/go-osstat/cpu" 10 "github.com/mackerelio/go-osstat/memory" 11 ) 12 13 // DiskStatus is the status of the disk 14 type DiskStatus struct { 15 All uint64 `json:"All"` 16 Used uint64 `json:"Used"` 17 Free uint64 `json:"Free"` 18 } 19 20 // diskusage of path/disk 21 func diskUsage(path string) (disk DiskStatus) { 22 fs := syscall.Statfs_t{} 23 err := syscall.Statfs(path, &fs) 24 if err != nil { 25 return 26 } 27 disk.All = fs.Blocks * uint64(fs.Bsize) 28 disk.Free = fs.Bfree * uint64(fs.Bsize) 29 disk.Used = disk.All - disk.Free 30 return 31 } 32 33 var _ StatsReporter = (*systemStats)(nil) 34 35 type systemStats struct { 36 cpuValue *cpu.Stats 37 } 38 39 func newSystemStats() *systemStats { 40 cpuValue, _ := cpu.Get() 41 return &systemStats{ 42 cpuValue: cpuValue, 43 } 44 } 45 46 // GetMemory returns the memory stats 47 func (s *systemStats) GetMemory() (*memory.Stats, error) { return memory.Get() } 48 49 // GetCPU returns the cpu stats 50 func (s *systemStats) GetCPU() (*cpu.Stats, error) { return cpu.Get() } 51 52 // GetDisk returns the disk stats 53 func (s *systemStats) GetDisk() DiskStatus { return diskUsage("/") } 54 55 // BuildReport builds the report 56 func (s *systemStats) BuildReport() string { 57 stringBuilder := strings.Builder{} 58 cpuValue, _ := s.GetCPU() 59 total := float64(cpuValue.Total - s.cpuValue.Total) 60 user := float64(cpuValue.User - s.cpuValue.User) 61 system := float64(cpuValue.System - s.cpuValue.System) 62 idle := float64(cpuValue.Idle - s.cpuValue.Idle) 63 64 s.cpuValue = cpuValue 65 stringBuilder.WriteString("CPU: ") 66 stringBuilder.WriteString(strconv.Itoa(runtime.NumCPU())) 67 stringBuilder.WriteString("CPUs ") 68 stringBuilder.WriteString("User: ") 69 stringBuilder.WriteString(strconv.FormatFloat(user/total*100, 'f', 2, 64)) 70 stringBuilder.WriteString("% ") 71 stringBuilder.WriteString("System: ") 72 stringBuilder.WriteString(strconv.FormatFloat(system/total*100, 'f', 2, 64)) 73 stringBuilder.WriteString("% ") 74 stringBuilder.WriteString("Idle: ") 75 stringBuilder.WriteString(strconv.FormatFloat(idle/total*100, 'f', 2, 64)) 76 stringBuilder.WriteString("% ") 77 78 stringBuilder.WriteString("\n") 79 memValue, _ := s.GetMemory() 80 stringBuilder.WriteString("Memory: ") 81 stringBuilder.WriteString("Total: ") 82 stringBuilder.WriteString(byteCountIEC(memValue.Total)) 83 stringBuilder.WriteString(" ") 84 stringBuilder.WriteString("Free: ") 85 stringBuilder.WriteString(byteCountIEC(memValue.Free)) 86 stringBuilder.WriteString(" ") 87 stringBuilder.WriteString("Used: ") 88 stringBuilder.WriteString(byteCountIEC(memValue.Used)) 89 stringBuilder.WriteString(" ") 90 stringBuilder.WriteString("Cached: ") 91 stringBuilder.WriteString(byteCountIEC(memValue.Cached)) 92 93 stringBuilder.WriteString("\n") 94 diskValue := s.GetDisk() 95 stringBuilder.WriteString("Disk: ") 96 stringBuilder.WriteString("Total: ") 97 stringBuilder.WriteString(byteCountIEC(diskValue.All)) 98 stringBuilder.WriteString(" ") 99 stringBuilder.WriteString("Free: ") 100 stringBuilder.WriteString(byteCountIEC(diskValue.Free)) 101 stringBuilder.WriteString(" ") 102 stringBuilder.WriteString("Used: ") 103 stringBuilder.WriteString(byteCountIEC(diskValue.Used)) 104 return stringBuilder.String() 105 }