1234567891011121314151617181920212223242526272829303132333435 |
- package util
- import (
- "cicv-data-closedloop/kinglong/common/log"
- "os/exec"
- "strconv"
- "strings"
- )
- func GetDiskUsagePercent() float64 {
-
- cmd := exec.Command("df", "--total")
- output, err := cmd.Output()
- if err != nil {
- log.GlobalLogger.Info("执行命令失败:", err)
- return 0.0
- }
-
- lines := strings.Split(string(output), "\n")
- for _, line := range lines[1:] {
- fields := strings.Fields(line)
- if len(fields) >= 6 && fields[0] == "total" {
-
- total, _ := strconv.ParseFloat(strings.TrimSuffix(fields[1], "G"), 64)
- used, _ := strconv.ParseFloat(strings.TrimSuffix(fields[2], "G"), 64)
- usedPercent := (used / total) * 100
-
- return usedPercent
- }
- }
- return 0.0
- }
|