utils.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package util
  2. import (
  3. commonConfig "cicv-data-closedloop/kinglong/common/cfg"
  4. "cicv-data-closedloop/kinglong/common/log"
  5. "os/exec"
  6. "strconv"
  7. "strings"
  8. )
  9. // GetDiskUsagePercent 获取磁盘使用率
  10. func GetDiskUsagePercent() float64 {
  11. // 执行 df 命令获取磁盘使用情况
  12. cmd := exec.Command("df", "--total")
  13. output, err := cmd.Output()
  14. if err != nil {
  15. log.GlobalLogger.Info("执行命令失败:", err)
  16. return 0.0
  17. }
  18. // 解析 df 命令输出,计算磁盘占比
  19. lines := strings.Split(string(output), "\n")
  20. for _, line := range lines[1:] {
  21. fields := strings.Fields(line)
  22. if len(fields) >= 6 && fields[0] == "total" {
  23. //filesystem := fields[0]
  24. total, _ := strconv.ParseFloat(strings.TrimSuffix(fields[1], "G"), 64)
  25. used, _ := strconv.ParseFloat(strings.TrimSuffix(fields[2], "G"), 64)
  26. usedPercent := (used / total) * 100
  27. //fmt.Printf("文件系统 %s 已使用 %.2f%%\n", filesystem, usedPercent)
  28. return usedPercent
  29. }
  30. }
  31. return 0.0
  32. }
  33. func GetCopyDir(faultTime string) string {
  34. return commonConfig.CloudConfig.BagCopyDir + faultTime + "/"
  35. }