u_resource.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package util
  2. import (
  3. "github.com/shirou/gopsutil/cpu"
  4. "github.com/shirou/gopsutil/disk"
  5. "github.com/shirou/gopsutil/mem"
  6. "github.com/shirou/gopsutil/process"
  7. "os/exec"
  8. "sort"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. type ProcessInfo struct {
  14. Pid int32 `json:"pid"`
  15. Name string `json:"name"`
  16. CpuUsage float64 `json:"cpuUsage"`
  17. MemUsage float32 `json:"memUsage"`
  18. }
  19. func GetTop10CpuAndMem() ([]ProcessInfo, []ProcessInfo) {
  20. // 获取所有进程的CPU占用率
  21. processes, err := process.Processes()
  22. if err != nil {
  23. //fmt.Println("Error:", err)
  24. return nil, nil
  25. }
  26. // 创建一个用于存储进程CPU占用率的映射
  27. cpuPercent := make(map[int32]float64)
  28. memPercent := make(map[int32]float32)
  29. // 获取每个进程的CPU占用率
  30. for _, p := range processes {
  31. pid := p.Pid
  32. cpuPercent[pid], err = p.CPUPercent()
  33. memPercent[pid], err = p.MemoryPercent()
  34. }
  35. // 根据CPU占用率对进程进行排序
  36. sortedPidsCpu := make([]int32, 0, len(cpuPercent))
  37. sortedPidsMem := make([]int32, 0, len(cpuPercent))
  38. for pid := range cpuPercent {
  39. sortedPidsCpu = append(sortedPidsCpu, pid)
  40. sortedPidsMem = append(sortedPidsMem, pid)
  41. }
  42. sort.Slice(sortedPidsCpu, func(i, j int) bool {
  43. return cpuPercent[sortedPidsCpu[i]] > cpuPercent[sortedPidsCpu[j]]
  44. })
  45. sort.Slice(sortedPidsMem, func(i, j int) bool {
  46. return memPercent[sortedPidsCpu[i]] > memPercent[sortedPidsCpu[j]]
  47. })
  48. // 输出前10个CPU占用率最高的进程名称
  49. var top10Cpu []ProcessInfo
  50. var top10Mem []ProcessInfo
  51. for i, pid := range sortedPidsCpu {
  52. if i >= 10 {
  53. break
  54. }
  55. p, err := process.NewProcess(pid)
  56. if err != nil {
  57. continue
  58. }
  59. name, _ := p.Name()
  60. top10Cpu = append(top10Cpu, ProcessInfo{
  61. Pid: pid,
  62. Name: name,
  63. CpuUsage: cpuPercent[pid],
  64. MemUsage: memPercent[pid],
  65. })
  66. }
  67. for i, pid := range sortedPidsMem {
  68. if i >= 10 {
  69. break
  70. }
  71. p, err := process.NewProcess(pid)
  72. if err != nil {
  73. continue
  74. }
  75. name, _ := p.Name()
  76. top10Mem = append(top10Mem, ProcessInfo{
  77. Pid: pid,
  78. Name: name,
  79. CpuUsage: cpuPercent[pid],
  80. MemUsage: memPercent[pid],
  81. })
  82. }
  83. return top10Cpu, top10Mem
  84. }
  85. // cpu总占用率
  86. func GetCpuPercent() float64 {
  87. percent, _ := cpu.Percent(time.Second, false)
  88. return percent[0]
  89. }
  90. // 内存总占用率
  91. func GetMemoryPercent() float64 {
  92. memory, _ := mem.VirtualMemory()
  93. return memory.UsedPercent
  94. }
  95. // 磁盘总占用率
  96. func GetDiskPercent() float64 {
  97. parts, _ := disk.Partitions(true)
  98. diskInfo, _ := disk.Usage(parts[0].Mountpoint)
  99. return diskInfo.UsedPercent
  100. }
  101. // GetDiskUsed 解析 df 命令的输出
  102. // df -B1 /dev/vdb
  103. // Filesystem 1B-blocks Used Available Use% Mounted on
  104. // /dev/vdb 527371075584 16390344704 484120408064 4% /mnt/disk001
  105. func GetDiskUsed(filesystem string) (uint64, error) {
  106. cmd := exec.Command("df", "-B1", filesystem)
  107. output, err := cmd.CombinedOutput()
  108. if err != nil {
  109. return 0, err
  110. }
  111. lines := strings.Split(string(output), "\n")
  112. fields := strings.Fields(lines[1])
  113. parseUint, err := strconv.ParseUint(fields[2], 10, 64)
  114. if err != nil {
  115. return 0, err
  116. }
  117. return parseUint, nil
  118. }
  119. // GetDiskUsagePercent 获取磁盘使用率
  120. func GetDiskUsagePercent() (float64, error) {
  121. // 执行 df 命令获取磁盘使用情况
  122. cmd := exec.Command("df", "--total")
  123. output, err := cmd.Output()
  124. if err != nil {
  125. return 0.0, err
  126. }
  127. // 解析 df 命令输出,计算磁盘占比
  128. lines := strings.Split(string(output), "\n")
  129. for _, line := range lines[1:] {
  130. fields := strings.Fields(line)
  131. if len(fields) >= 6 && fields[0] == "total" {
  132. //filesystem := fields[0]
  133. total, _ := strconv.ParseFloat(strings.TrimSuffix(fields[1], "G"), 64)
  134. used, _ := strconv.ParseFloat(strings.TrimSuffix(fields[2], "G"), 64)
  135. usedPercent := (used / total) * 100
  136. //fmt.Printf("文件系统 %s 已使用 %.2f%%\n", filesystem, usedPercent)
  137. return usedPercent, err
  138. }
  139. }
  140. return 0.0, nil
  141. }