u_resource.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. }
  41. sort.Slice(sortedPidsCpu, func(i, j int) bool {
  42. return cpuPercent[sortedPidsCpu[i]] > cpuPercent[sortedPidsCpu[j]]
  43. })
  44. // 输出前10个CPU占用率最高的进程名称
  45. var top10Cpu []ProcessInfo
  46. for i, pid := range sortedPidsCpu {
  47. if i >= 10 {
  48. break
  49. }
  50. p, err := process.NewProcess(pid)
  51. if err != nil {
  52. continue
  53. }
  54. name, _ := p.Name()
  55. top10Cpu = append(top10Cpu, ProcessInfo{
  56. Pid: pid,
  57. Name: name,
  58. CpuUsage: cpuPercent[pid],
  59. MemUsage: memPercent[pid],
  60. })
  61. }
  62. // --------------------- 内存
  63. var top10Mem []ProcessInfo
  64. for pid := range memPercent {
  65. sortedPidsMem = append(sortedPidsMem, pid)
  66. }
  67. sort.Slice(sortedPidsMem, func(i, j int) bool {
  68. return memPercent[sortedPidsMem[i]] > memPercent[sortedPidsMem[j]]
  69. })
  70. for i, pid := range sortedPidsMem {
  71. if i >= 10 {
  72. break
  73. }
  74. p, err := process.NewProcess(pid)
  75. if err != nil {
  76. continue
  77. }
  78. name, _ := p.Name()
  79. top10Mem = append(top10Mem, ProcessInfo{
  80. Pid: pid,
  81. Name: name,
  82. CpuUsage: cpuPercent[pid],
  83. MemUsage: memPercent[pid],
  84. })
  85. }
  86. return top10Cpu, top10Mem
  87. }
  88. // cpu总占用率
  89. func GetCpuPercent() float64 {
  90. percent, _ := cpu.Percent(time.Second, false)
  91. return percent[0]
  92. }
  93. // 内存总占用率
  94. func GetMemoryPercent() float64 {
  95. memory, _ := mem.VirtualMemory()
  96. return memory.UsedPercent
  97. }
  98. // 磁盘总占用率
  99. func GetDiskPercent() float64 {
  100. parts, _ := disk.Partitions(true)
  101. diskInfo, _ := disk.Usage(parts[0].Mountpoint)
  102. return diskInfo.UsedPercent
  103. }
  104. // GetDiskUsed 解析 df 命令的输出
  105. // df -B1 /dev/vdb
  106. // Filesystem 1B-blocks Used Available Use% Mounted on
  107. // /dev/vdb 527371075584 16390344704 484120408064 4% /mnt/disk001
  108. func GetDiskUsed(filesystem string) (uint64, error) {
  109. cmd := exec.Command("df", "-B1", filesystem)
  110. output, err := cmd.CombinedOutput()
  111. if err != nil {
  112. return 0, err
  113. }
  114. lines := strings.Split(string(output), "\n")
  115. fields := strings.Fields(lines[1])
  116. parseUint, err := strconv.ParseUint(fields[2], 10, 64)
  117. if err != nil {
  118. return 0, err
  119. }
  120. return parseUint, nil
  121. }
  122. // GetDiskUsagePercent 获取磁盘使用率
  123. func GetDiskUsagePercent() (float64, error) {
  124. // 执行 df 命令获取磁盘使用情况
  125. cmd := exec.Command("df", "--total")
  126. output, err := cmd.Output()
  127. if err != nil {
  128. return 0.0, err
  129. }
  130. // 解析 df 命令输出,计算磁盘占比
  131. lines := strings.Split(string(output), "\n")
  132. for _, line := range lines[1:] {
  133. fields := strings.Fields(line)
  134. if len(fields) >= 6 && fields[0] == "total" {
  135. //filesystem := fields[0]
  136. total, _ := strconv.ParseFloat(strings.TrimSuffix(fields[1], "G"), 64)
  137. used, _ := strconv.ParseFloat(strings.TrimSuffix(fields[2], "G"), 64)
  138. usedPercent := (used / total) * 100
  139. //fmt.Printf("文件系统 %s 已使用 %.2f%%\n", filesystem, usedPercent)
  140. return usedPercent, err
  141. }
  142. }
  143. return 0.0, nil
  144. }