2020-02-26 09:34:32 +01:00
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
2020-11-05 17:59:59 +01:00
|
|
|
"context"
|
|
|
|
|
2020-02-26 09:34:32 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var namespace = "rclone_"
|
|
|
|
|
|
|
|
// RcloneCollector is a Prometheus collector for Rclone
|
|
|
|
type RcloneCollector struct {
|
2020-11-05 17:59:59 +01:00
|
|
|
ctx context.Context
|
2020-02-26 09:34:32 +01:00
|
|
|
bytesTransferred *prometheus.Desc
|
|
|
|
transferSpeed *prometheus.Desc
|
|
|
|
numOfErrors *prometheus.Desc
|
|
|
|
numOfCheckFiles *prometheus.Desc
|
|
|
|
transferredFiles *prometheus.Desc
|
|
|
|
deletes *prometheus.Desc
|
2020-10-28 13:54:31 +01:00
|
|
|
deletedDirs *prometheus.Desc
|
2020-03-30 19:12:32 +02:00
|
|
|
renames *prometheus.Desc
|
2020-02-26 09:34:32 +01:00
|
|
|
fatalError *prometheus.Desc
|
|
|
|
retryError *prometheus.Desc
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRcloneCollector make a new RcloneCollector
|
2020-11-05 17:59:59 +01:00
|
|
|
func NewRcloneCollector(ctx context.Context) *RcloneCollector {
|
2020-02-26 09:34:32 +01:00
|
|
|
return &RcloneCollector{
|
2020-11-05 17:59:59 +01:00
|
|
|
ctx: ctx,
|
2020-02-26 09:34:32 +01:00
|
|
|
bytesTransferred: prometheus.NewDesc(namespace+"bytes_transferred_total",
|
|
|
|
"Total transferred bytes since the start of the Rclone process",
|
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
transferSpeed: prometheus.NewDesc(namespace+"speed",
|
2021-04-07 12:23:42 +02:00
|
|
|
"Average speed in bytes per second since the start of the Rclone process",
|
2020-02-26 09:34:32 +01:00
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
numOfErrors: prometheus.NewDesc(namespace+"errors_total",
|
|
|
|
"Number of errors thrown",
|
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
numOfCheckFiles: prometheus.NewDesc(namespace+"checked_files_total",
|
|
|
|
"Number of checked files",
|
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
transferredFiles: prometheus.NewDesc(namespace+"files_transferred_total",
|
|
|
|
"Number of transferred files",
|
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
deletes: prometheus.NewDesc(namespace+"files_deleted_total",
|
|
|
|
"Total number of files deleted",
|
|
|
|
nil, nil,
|
|
|
|
),
|
2020-10-28 13:54:31 +01:00
|
|
|
deletedDirs: prometheus.NewDesc(namespace+"dirs_deleted_total",
|
|
|
|
"Total number of directories deleted",
|
|
|
|
nil, nil,
|
|
|
|
),
|
2020-03-30 19:12:32 +02:00
|
|
|
renames: prometheus.NewDesc(namespace+"files_renamed_total",
|
|
|
|
"Total number of files renamed",
|
|
|
|
nil, nil,
|
|
|
|
),
|
2020-02-26 09:34:32 +01:00
|
|
|
fatalError: prometheus.NewDesc(namespace+"fatal_error",
|
|
|
|
"Whether a fatal error has occurred",
|
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
retryError: prometheus.NewDesc(namespace+"retry_error",
|
|
|
|
"Whether there has been an error that will be retried",
|
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Describe is part of the Collector interface: https://godoc.org/github.com/prometheus/client_golang/prometheus#Collector
|
|
|
|
func (c *RcloneCollector) Describe(ch chan<- *prometheus.Desc) {
|
|
|
|
ch <- c.bytesTransferred
|
|
|
|
ch <- c.transferSpeed
|
|
|
|
ch <- c.numOfErrors
|
|
|
|
ch <- c.numOfCheckFiles
|
|
|
|
ch <- c.transferredFiles
|
|
|
|
ch <- c.deletes
|
2020-10-28 13:54:31 +01:00
|
|
|
ch <- c.deletedDirs
|
2020-03-30 19:12:32 +02:00
|
|
|
ch <- c.renames
|
2020-02-26 09:34:32 +01:00
|
|
|
ch <- c.fatalError
|
|
|
|
ch <- c.retryError
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect is part of the Collector interface: https://godoc.org/github.com/prometheus/client_golang/prometheus#Collector
|
|
|
|
func (c *RcloneCollector) Collect(ch chan<- prometheus.Metric) {
|
2020-11-05 17:59:59 +01:00
|
|
|
s := groups.sum(c.ctx)
|
2020-02-26 09:34:32 +01:00
|
|
|
s.mu.RLock()
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(c.bytesTransferred, prometheus.CounterValue, float64(s.bytes))
|
2024-01-16 18:30:24 +01:00
|
|
|
ch <- prometheus.MustNewConstMetric(c.transferSpeed, prometheus.GaugeValue, s._speed())
|
2020-02-26 09:34:32 +01:00
|
|
|
ch <- prometheus.MustNewConstMetric(c.numOfErrors, prometheus.CounterValue, float64(s.errors))
|
|
|
|
ch <- prometheus.MustNewConstMetric(c.numOfCheckFiles, prometheus.CounterValue, float64(s.checks))
|
|
|
|
ch <- prometheus.MustNewConstMetric(c.transferredFiles, prometheus.CounterValue, float64(s.transfers))
|
|
|
|
ch <- prometheus.MustNewConstMetric(c.deletes, prometheus.CounterValue, float64(s.deletes))
|
2020-10-28 13:54:31 +01:00
|
|
|
ch <- prometheus.MustNewConstMetric(c.deletedDirs, prometheus.CounterValue, float64(s.deletedDirs))
|
2020-03-30 19:12:32 +02:00
|
|
|
ch <- prometheus.MustNewConstMetric(c.renames, prometheus.CounterValue, float64(s.renames))
|
2020-02-26 09:34:32 +01:00
|
|
|
ch <- prometheus.MustNewConstMetric(c.fatalError, prometheus.GaugeValue, bool2Float(s.fatalError))
|
|
|
|
ch <- prometheus.MustNewConstMetric(c.retryError, prometheus.GaugeValue, bool2Float(s.retryError))
|
|
|
|
|
|
|
|
s.mu.RUnlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// bool2Float is a small function to convert a boolean into a float64 value that can be used for Prometheus
|
|
|
|
func bool2Float(e bool) float64 {
|
|
|
|
if e {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|