swarm/metrics: collect metrics on datadir disk usage (#19576)
This commit is contained in:
parent
350a87dd3c
commit
9b0d1b9ab2
@ -17,6 +17,8 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/cmd/utils"
|
||||
@ -89,11 +91,15 @@ func Setup(ctx *cli.Context) {
|
||||
password = ctx.GlobalString(MetricsInfluxDBPasswordFlag.Name)
|
||||
enableExport = ctx.GlobalBool(MetricsEnableInfluxDBExportFlag.Name)
|
||||
enableAccountingExport = ctx.GlobalBool(MetricsEnableInfluxDBAccountingExportFlag.Name)
|
||||
datadir = ctx.GlobalString("datadir")
|
||||
)
|
||||
|
||||
// Start system runtime metrics collection
|
||||
go gethmetrics.CollectProcessMetrics(4 * time.Second)
|
||||
|
||||
// Start collecting disk metrics
|
||||
go datadirDiskUsage(datadir, 4*time.Second)
|
||||
|
||||
gethmetrics.RegisterRuntimeMemStats(metrics.DefaultRegistry)
|
||||
go gethmetrics.CaptureRuntimeMemStats(metrics.DefaultRegistry, 4*time.Second)
|
||||
|
||||
@ -110,3 +116,28 @@ func Setup(ctx *cli.Context) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func datadirDiskUsage(path string, d time.Duration) {
|
||||
for range time.Tick(d) {
|
||||
bytes, err := dirSize(path)
|
||||
if err != nil {
|
||||
log.Warn("cannot get disk space", "err", err)
|
||||
}
|
||||
|
||||
metrics.GetOrRegisterGauge("datadir.usage", nil).Update(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
func dirSize(path string) (int64, error) {
|
||||
var size int64
|
||||
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() {
|
||||
size += info.Size()
|
||||
}
|
||||
return err
|
||||
})
|
||||
return size, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user