|
| 1 | +/* |
| 2 | + * Copyright 2019 The CovenantSQL Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "expvar" |
| 21 | + "io/ioutil" |
| 22 | + "os/exec" |
| 23 | + "runtime" |
| 24 | + "strconv" |
| 25 | + "strings" |
| 26 | + |
| 27 | + "github.com/CovenantSQL/CovenantSQL/conf" |
| 28 | + "github.com/CovenantSQL/CovenantSQL/utils/log" |
| 29 | +) |
| 30 | + |
| 31 | +var ( |
| 32 | + diskUsageMetric = expvar.NewInt("service:miner:disk:usage") |
| 33 | +) |
| 34 | + |
| 35 | +func collectDiskUsage() (err error) { |
| 36 | + // run du on linux and mac |
| 37 | + if runtime.GOOS != "linux" && runtime.GOOS != "darwin" { |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + if conf.GConf == nil || conf.GConf.Miner == nil || conf.GConf.Miner.RootDir == "" { |
| 42 | + log.Error("miner config is empty, disk usage report is disabled") |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + duBin, err := exec.LookPath("du") |
| 47 | + if err != nil { |
| 48 | + log.WithError(err).Error("could not found du command") |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + cmd := exec.Command(duBin, "-sk", conf.GConf.Miner.RootDir) |
| 53 | + duOutput, err := cmd.StdoutPipe() |
| 54 | + if err != nil { |
| 55 | + log.WithError(err).Error("could not get result of disk usage") |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + err = cmd.Start() |
| 60 | + if err != nil { |
| 61 | + log.WithError(err).Error("could not start disk usage command") |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + err = cmd.Wait() |
| 66 | + if err != nil { |
| 67 | + log.WithError(err).Error("run disk usage command failed") |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + duResult, err := ioutil.ReadAll(duOutput) |
| 72 | + if err != nil { |
| 73 | + log.WithError(err).Error("get disk usage result failed") |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + splitResult := strings.SplitN(string(duResult), "\t", 2) |
| 78 | + if len(splitResult) == 0 || len(strings.TrimSpace(splitResult[0])) == 0 { |
| 79 | + log.Error("could not get disk usage result") |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + usedKiloBytes, err := strconv.ParseInt(strings.TrimSpace(splitResult[0]), 10, 64) |
| 84 | + if err != nil { |
| 85 | + log.WithError(err).Error("could not parse usage bytes result") |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + diskUsageMetric.Set(usedKiloBytes) |
| 90 | + |
| 91 | + return |
| 92 | +} |
0 commit comments