u_resource.go 3.1 KB

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