u_resource.go 3.1 KB

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