From 9b0d1b9ab2447f2e7b876cff9905294a1208565b Mon Sep 17 00:00:00 2001 From: Anton Evangelatov Date: Wed, 15 May 2019 12:22:32 +0200 Subject: [PATCH] swarm/metrics: collect metrics on datadir disk usage (#19576) --- swarm/metrics/flags.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/swarm/metrics/flags.go b/swarm/metrics/flags.go index 3e7918b16..39859a7b2 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 +}