diff --git a/swarm/metrics/flags.go b/swarm/metrics/flags.go index 3e7918b168..39859a7b2d 100644 --- a/swarm/metrics/flags.go +++ b/swarm/metrics/flags.go @@ -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 +}