| 35 | ) |
| 36 | |
| 37 | func collectDiskUsage() (err error) { |
| 38 | // run du on linux and mac |
| 39 | if runtime.GOOS != "linux" && runtime.GOOS != "darwin" { |
| 40 | return |
| 41 | } |
| 42 | |
| 43 | if conf.GConf == nil || conf.GConf.Miner == nil || conf.GConf.Miner.RootDir == "" { |
| 44 | log.Error("miner config is empty, disk usage report is disabled") |
| 45 | return |
| 46 | } |
| 47 | |
| 48 | duBin, err := exec.LookPath("du") |
| 49 | if err != nil { |
| 50 | log.WithError(err).Error("could not found du command") |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | cmd := exec.Command(duBin, "-sk", conf.GConf.Miner.RootDir) |
| 55 | duOutput, err := cmd.StdoutPipe() |
| 56 | if err != nil { |
| 57 | log.WithError(err).Error("could not get result of disk usage") |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | err = cmd.Start() |
| 62 | if err != nil { |
| 63 | log.WithError(err).Error("could not start disk usage command") |
| 64 | return |
| 65 | } |
| 66 | |
| 67 | duResult, err := ioutil.ReadAll(duOutput) |
| 68 | if err != nil { |
| 69 | log.WithError(err).Error("get disk usage result failed") |
| 70 | return |
| 71 | } |
| 72 | |
| 73 | err = cmd.Wait() |
| 74 | if err != nil { |
| 75 | log.WithError(err).Error("run disk usage command failed") |
| 76 | return |
| 77 | } |
| 78 | |
| 79 | splitResult := strings.SplitN(string(duResult), "\t", 2) |
| 80 | if len(splitResult) == 0 || len(strings.TrimSpace(splitResult[0])) == 0 { |
| 81 | log.Error("could not get disk usage result") |
| 82 | return |
| 83 | } |
| 84 | |
| 85 | usedKiloBytes, err := strconv.ParseInt(strings.TrimSpace(splitResult[0]), 10, 64) |
| 86 | if err != nil { |
| 87 | log.WithError(err).Error("could not parse usage bytes result") |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | diskUsageMetric.Add(float64(usedKiloBytes)) |
| 92 | |
| 93 | return |
| 94 | } |