From a8658d8fb204abc7b5b7e7d47f9e4146316596b6 Mon Sep 17 00:00:00 2001 From: Tommy Wang Date: Fri, 11 Aug 2017 10:03:13 +0200 Subject: [PATCH 1/3] Add TrackerDomain, DownloadTotal, and UploadTotal metrics for all torrents --- cmd/rtorrent_exporter/main.go | 2 +- downloadscollector.go | 90 ++++++++++++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 3 deletions(-) diff --git a/cmd/rtorrent_exporter/main.go b/cmd/rtorrent_exporter/main.go index 7b1d7d9..7d9243a 100644 --- a/cmd/rtorrent_exporter/main.go +++ b/cmd/rtorrent_exporter/main.go @@ -48,7 +48,7 @@ func main() { http.Redirect(w, r, *metricsPath, http.StatusMovedPermanently) }) - log.Printf("starting rTorrent exporter on %q for server %q (authentication: %v)", + log.Printf("starting rTorrent v0.1.1 exporter on %q for server %q (authentication: %v)", *telemetryAddr, *rtorrentAddr, rt != nil) if err := http.ListenAndServe(*telemetryAddr, nil); err != nil { diff --git a/downloadscollector.go b/downloadscollector.go index af160a6..28eeac8 100644 --- a/downloadscollector.go +++ b/downloadscollector.go @@ -23,8 +23,12 @@ type DownloadsSource interface { Active() ([]string, error) BaseFilename(infoHash string) (string, error) + TrackerDomain(infoHash string) (string, error) DownloadRate(infoHash string) (int, error) UploadRate(infoHash string) (int, error) + + DownloadTotal(infoHash string) (int, error) + UploadTotal(infoHash string) (int, error) } // A DownloadsCollector is a Prometheus collector for metrics regarding rTorrent @@ -43,6 +47,9 @@ type DownloadsCollector struct { DownloadRateBytes *prometheus.Desc UploadRateBytes *prometheus.Desc + DownloadTotalBytes *prometheus.Desc + UploadTotalBytes *prometheus.Desc + ds DownloadsSource } @@ -57,7 +64,7 @@ func NewDownloadsCollector(ds DownloadsSource) *DownloadsCollector { ) var ( - labels = []string{"info_hash", "name"} + labels = []string{"info_hash", "name", "tracker"} ) return &DownloadsCollector{ @@ -139,6 +146,20 @@ func NewDownloadsCollector(ds DownloadsSource) *DownloadsCollector { nil, ), + DownloadTotalBytes: prometheus.NewDesc( + prometheus.BuildFQName(namespace, subsystem, "download_total_bytes"), + "Current download total in bytes.", + labels, + nil, + ), + + UploadTotalBytes: prometheus.NewDesc( + prometheus.BuildFQName(namespace, subsystem, "upload_total_bytes"), + "Current upload total in bytes.", + labels, + nil, + ), + ds: ds, } } @@ -154,6 +175,10 @@ func (c *DownloadsCollector) collect(ch chan<- prometheus.Metric) (*prometheus.D return desc, err } + if desc, err := c.collectAllDownloads(ch); err != nil { + return desc, err + } + return nil, nil } @@ -271,9 +296,15 @@ func (c *DownloadsCollector) collectActiveDownloads(ch chan<- prometheus.Metric) return c.DownloadRateBytes, err } + tracker, err := c.ds.TrackerDomain(a) + if err != nil { + return c.DownloadRateBytes, err + } + labels := []string{ a, name, + tracker, } down, err := c.ds.DownloadRate(a) @@ -288,13 +319,65 @@ func (c *DownloadsCollector) collectActiveDownloads(ch chan<- prometheus.Metric) ch <- prometheus.MustNewConstMetric( c.DownloadRateBytes, - prometheus.GaugeValue, + prometheus.CounterValue, float64(down), labels..., ) ch <- prometheus.MustNewConstMetric( c.UploadRateBytes, + prometheus.CounterValue, + float64(up), + labels..., + ) + } + + return nil, nil +} + +// collectAllDownloads collects information about all downloads +func (c *DownloadsCollector) collectAllDownloads(ch chan<- prometheus.Metric) (*prometheus.Desc, error) { + all, err := c.ds.All() + if err != nil { + return c.Downloads, err + } + + for _, a := range all { + name, err := c.ds.BaseFilename(a) + if err != nil { + return c.DownloadTotalBytes, err + } + + tracker, err := c.ds.TrackerDomain(a) + if err != nil { + return c.DownloadTotalBytes, err + } + + labels := []string{ + a, + name, + tracker, + } + + down, err := c.ds.DownloadTotal(a) + if err != nil { + return c.DownloadTotalBytes, err + } + + up, err := c.ds.UploadTotal(a) + if err != nil { + return c.UploadTotalBytes, err + } + + ch <- prometheus.MustNewConstMetric( + c.DownloadTotalBytes, + prometheus.GaugeValue, + float64(down), + labels..., + ) + + ch <- prometheus.MustNewConstMetric( + c.UploadTotalBytes, prometheus.GaugeValue, float64(up), labels..., @@ -320,6 +403,9 @@ func (c *DownloadsCollector) Describe(ch chan<- *prometheus.Desc) { c.DownloadRateBytes, c.UploadRateBytes, + + c.DownloadTotalBytes, + c.UploadTotalBytes, } for _, d := range ds { From dc8b415328c2d1cd1cefb5713cc0e3adfd155e35 Mon Sep 17 00:00:00 2001 From: Tommy Wang Date: Fri, 11 Aug 2017 10:32:26 +0200 Subject: [PATCH 2/3] update readme --- README.md | 4 +- sample_dash.json | 665 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 667 insertions(+), 2 deletions(-) create mode 100644 sample_dash.json diff --git a/README.md b/README.md index 8a18883..42de992 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,6 @@ Sample ------ Here is a screenshot of a sample dashboard created using [`grafana`](https://github.com/grafana/grafana) -with metrics from exported from `rtorrent_exporter`. +with metrics from exported from `rtorrent_exporter` (see sample_dash.json). -![sample](https://cloud.githubusercontent.com/assets/1926905/13891308/bad263be-ed26-11e5-9601-9d770d95c538.png) +![sample](https://user-images.githubusercontent.com/766820/29206006-923e7b20-7e45-11e7-84ae-1e51d0755f92.jpg) diff --git a/sample_dash.json b/sample_dash.json new file mode 100644 index 0000000..7ba6988 --- /dev/null +++ b/sample_dash.json @@ -0,0 +1,665 @@ +{ + "annotations": { + "list": [] + }, + "editable": true, + "gnetId": null, + "graphTooltip": 0, + "hideControls": false, + "id": 1, + "links": [], + "refresh": false, + "rows": [ + { + "collapse": false, + "height": "750", + "panels": [ + { + "columns": [ + { + "text": "Current", + "value": "current" + } + ], + "datasource": "Prometheus", + "fontSize": "100%", + "height": "", + "hideTimeOverride": true, + "id": 4, + "links": [], + "pageSize": null, + "scroll": true, + "showHeader": true, + "sort": { + "col": 1, + "desc": true + }, + "span": 6, + "styles": [ + { + "alias": "Torrent", + "colorMode": null, + "colors": [ + "rgba(245, 54, 54, 0.9)", + "rgba(237, 129, 40, 0.89)", + "rgba(50, 172, 45, 0.97)" + ], + "dateFormat": "YYYY-MM-DD HH:mm:ss", + "decimals": 2, + "pattern": "Metric", + "thresholds": [], + "type": "string", + "unit": "short" + }, + { + "alias": "Bytes", + "colorMode": null, + "colors": [ + "rgba(245, 54, 54, 0.9)", + "rgba(237, 129, 40, 0.89)", + "rgba(50, 172, 45, 0.97)" + ], + "dateFormat": "YYYY-MM-DD HH:mm:ss", + "decimals": 2, + "pattern": "Current", + "thresholds": [], + "type": "number", + "unit": "decbytes" + } + ], + "targets": [ + { + "expr": "increase(rtorrent_downloads_upload_total_bytes[$agg_interval])>0", + "format": "time_series", + "interval": "2s", + "intervalFactor": 1, + "legendFormat": "{{name}}", + "metric": "", + "refId": "A", + "step": 2 + } + ], + "timeFrom": "1s", + "title": "Torrents by Upload ($agg_interval)", + "transform": "timeseries_aggregations", + "type": "table" + }, + { + "aliasColors": {}, + "cacheTimeout": null, + "combine": { + "label": "Others", + "threshold": "0.03" + }, + "datasource": "Prometheus", + "fontSize": "80%", + "format": "decbytes", + "height": "", + "hideTimeOverride": true, + "id": 5, + "interval": null, + "legend": { + "percentage": true, + "show": true, + "sortDesc": true, + "values": true + }, + "legendType": "Right side", + "links": [], + "maxDataPoints": 3, + "nullPointMode": "connected", + "pieType": "pie", + "span": 6, + "strokeWidth": 1, + "targets": [ + { + "expr": "increase(rtorrent_downloads_upload_total_bytes[$agg_interval])>0", + "format": "time_series", + "interval": "2s", + "intervalFactor": 1, + "legendFormat": "{{name}}", + "refId": "A", + "step": 2 + } + ], + "timeFrom": "1s", + "title": "Uploads By Torrent ($agg_interval)", + "type": "grafana-piechart-panel", + "valueName": "current" + } + ], + "repeat": null, + "repeatIteration": null, + "repeatRowId": null, + "showTitle": false, + "title": "Dashboard Row", + "titleSize": "h6" + }, + { + "collapse": false, + "height": "750", + "panels": [ + { + "aliasColors": {}, + "bars": true, + "dashLength": 10, + "dashes": false, + "datasource": "Prometheus", + "fill": 1, + "hideTimeOverride": true, + "id": 7, + "legend": { + "alignAsTable": false, + "avg": false, + "current": false, + "hideEmpty": true, + "hideZero": true, + "max": false, + "min": false, + "show": false, + "sort": "current", + "sortDesc": true, + "total": false, + "values": false + }, + "lines": false, + "linewidth": 1, + "links": [], + "nullPointMode": "null", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "spaceLength": 10, + "span": 12, + "stack": true, + "steppedLine": false, + "targets": [ + { + "expr": "increase(rtorrent_downloads_upload_total_bytes[1h])>0", + "format": "time_series", + "interval": "1h", + "intervalFactor": 1, + "legendFormat": "{{name}}", + "refId": "A", + "step": 3600 + } + ], + "thresholds": [], + "timeFrom": "1d", + "timeShift": null, + "title": "Hourly Uploads", + "tooltip": { + "shared": true, + "sort": 2, + "value_type": "individual" + }, + "type": "graph", + "xaxis": { + "buckets": null, + "mode": "time", + "name": null, + "show": true, + "values": [ + "total" + ] + }, + "yaxes": [ + { + "format": "decbytes", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": false + } + ] + } + ], + "repeat": null, + "repeatIteration": null, + "repeatRowId": null, + "showTitle": false, + "title": "Dashboard Row", + "titleSize": "h6" + }, + { + "collapse": false, + "height": "750", + "panels": [ + { + "aliasColors": {}, + "bars": false, + "dashLength": 10, + "dashes": false, + "datasource": "Prometheus", + "fill": 1, + "height": "", + "id": 1, + "legend": { + "alignAsTable": true, + "avg": true, + "current": true, + "hideEmpty": true, + "hideZero": true, + "max": true, + "min": true, + "show": true, + "sort": "avg", + "sortDesc": true, + "total": false, + "values": true + }, + "lines": true, + "linewidth": 1, + "links": [], + "nullPointMode": "null", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "spaceLength": 10, + "span": 6, + "stack": false, + "steppedLine": false, + "targets": [ + { + "expr": "rtorrent_downloads_download_rate_bytes", + "format": "time_series", + "interval": "", + "intervalFactor": 2, + "legendFormat": "{{name}}", + "refId": "A", + "step": 120 + } + ], + "thresholds": [], + "timeFrom": null, + "timeShift": null, + "title": "Download Rate Bytes by Torrent", + "tooltip": { + "shared": false, + "sort": 0, + "value_type": "individual" + }, + "type": "graph", + "xaxis": { + "buckets": null, + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "Bps", + "label": "", + "logBase": 1, + "max": null, + "min": null, + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": false + } + ] + }, + { + "aliasColors": {}, + "bars": false, + "dashLength": 10, + "dashes": false, + "datasource": "Prometheus", + "decimals": null, + "fill": 1, + "height": "", + "id": 2, + "legend": { + "alignAsTable": true, + "avg": true, + "current": true, + "hideEmpty": true, + "hideZero": true, + "max": true, + "min": true, + "rightSide": false, + "show": true, + "sort": "avg", + "sortDesc": true, + "total": false, + "values": true + }, + "lines": true, + "linewidth": 1, + "links": [], + "nullPointMode": "null", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "spaceLength": 10, + "span": 6, + "stack": false, + "steppedLine": false, + "targets": [ + { + "expr": "rtorrent_downloads_upload_rate_bytes", + "format": "time_series", + "intervalFactor": 2, + "legendFormat": "{{name}}", + "refId": "A", + "step": 120 + } + ], + "thresholds": [], + "timeFrom": null, + "timeShift": null, + "title": "Upload Rates Bytes by Torrent", + "tooltip": { + "shared": false, + "sort": 0, + "value_type": "individual" + }, + "type": "graph", + "xaxis": { + "buckets": null, + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "Bps", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": false + } + ] + } + ], + "repeat": null, + "repeatIteration": null, + "repeatRowId": null, + "showTitle": false, + "title": "Dashboard Row", + "titleSize": "h6" + }, + { + "collapse": false, + "height": "500", + "panels": [ + { + "aliasColors": {}, + "cacheTimeout": null, + "combine": { + "label": "Others", + "threshold": "0.01" + }, + "datasource": "Prometheus", + "fontSize": "80%", + "format": "decbytes", + "height": "500", + "hideTimeOverride": true, + "id": 6, + "interval": null, + "legend": { + "percentage": false, + "show": true, + "values": true + }, + "legendType": "On graph", + "links": [], + "maxDataPoints": 3, + "nullPointMode": "connected", + "pieType": "pie", + "span": 6, + "strokeWidth": 1, + "targets": [ + { + "expr": "sum(increase(rtorrent_downloads_upload_total_bytes[$agg_interval])) by(tracker)", + "format": "time_series", + "interval": "2s", + "intervalFactor": 1, + "legendFormat": "{{tracker}}", + "metric": "", + "refId": "A", + "step": 2 + } + ], + "timeFrom": "1s", + "title": "Uploads by Tracker ($agg_interval)", + "type": "grafana-piechart-panel", + "valueName": "current" + }, + { + "aliasColors": {}, + "bars": false, + "dashLength": 10, + "dashes": false, + "datasource": "Prometheus", + "fill": 1, + "id": 8, + "legend": { + "alignAsTable": true, + "avg": true, + "current": false, + "max": true, + "min": false, + "rightSide": true, + "show": true, + "total": false, + "values": true + }, + "lines": true, + "linewidth": 1, + "links": [], + "nullPointMode": "null", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "spaceLength": 10, + "span": 6, + "stack": false, + "steppedLine": false, + "targets": [ + { + "expr": "sum(rtorrent_downloads_download_rate_bytes)", + "format": "time_series", + "interval": "", + "intervalFactor": 2, + "legendFormat": "download", + "refId": "A", + "step": 120 + }, + { + "expr": "sum(rtorrent_downloads_upload_rate_bytes)", + "format": "time_series", + "interval": "", + "intervalFactor": 2, + "legendFormat": "upload", + "refId": "B", + "step": 120 + } + ], + "thresholds": [], + "timeFrom": null, + "timeShift": null, + "title": "Up/Down Speed", + "tooltip": { + "shared": true, + "sort": 0, + "value_type": "individual" + }, + "type": "graph", + "xaxis": { + "buckets": null, + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "Bps", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": false + } + ] + } + ], + "repeat": null, + "repeatIteration": null, + "repeatRowId": null, + "showTitle": false, + "title": "Dashboard Row", + "titleSize": "h6" + } + ], + "schemaVersion": 14, + "style": "dark", + "tags": [], + "templating": { + "list": [ + { + "auto": false, + "auto_count": 30, + "auto_min": "10s", + "current": { + "selected": true, + "text": "1d", + "value": "1d" + }, + "hide": 0, + "label": "Torrents By Upload Interval", + "name": "agg_interval", + "options": [ + { + "selected": false, + "text": "1m", + "value": "1m" + }, + { + "selected": false, + "text": "10m", + "value": "10m" + }, + { + "selected": false, + "text": "30m", + "value": "30m" + }, + { + "selected": false, + "text": "1h", + "value": "1h" + }, + { + "selected": false, + "text": "6h", + "value": "6h" + }, + { + "selected": false, + "text": "12h", + "value": "12h" + }, + { + "selected": true, + "text": "1d", + "value": "1d" + }, + { + "selected": false, + "text": "7d", + "value": "7d" + }, + { + "selected": false, + "text": "14d", + "value": "14d" + }, + { + "selected": false, + "text": "30d", + "value": "30d" + } + ], + "query": "1m,10m,30m,1h,6h,12h,1d,7d,14d,30d", + "refresh": 2, + "type": "interval" + } + ] + }, + "time": { + "from": "now-24h", + "to": "now" + }, + "timepicker": { + "refresh_intervals": [ + "5s", + "10s", + "30s", + "1m", + "5m", + "15m", + "30m", + "1h", + "2h", + "1d" + ], + "time_options": [ + "5m", + "15m", + "1h", + "6h", + "12h", + "24h", + "2d", + "7d", + "30d" + ] + }, + "timezone": "", + "title": "rutorrent", + "version": 28 +} From 39b565862f2618a8a6120df9ddba4dd7677334f5 Mon Sep 17 00:00:00 2001 From: Tommy Wang Date: Fri, 11 Aug 2017 10:38:40 +0200 Subject: [PATCH 3/3] fix change were rates were changed to counters as opposed to totals --- downloadscollector.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/downloadscollector.go b/downloadscollector.go index 28eeac8..61c7727 100644 --- a/downloadscollector.go +++ b/downloadscollector.go @@ -319,14 +319,14 @@ func (c *DownloadsCollector) collectActiveDownloads(ch chan<- prometheus.Metric) ch <- prometheus.MustNewConstMetric( c.DownloadRateBytes, - prometheus.CounterValue, + prometheus.GaugeValue, float64(down), labels..., ) ch <- prometheus.MustNewConstMetric( c.UploadRateBytes, - prometheus.CounterValue, + prometheus.GaugeValue, float64(up), labels..., ) @@ -371,14 +371,14 @@ func (c *DownloadsCollector) collectAllDownloads(ch chan<- prometheus.Metric) (* ch <- prometheus.MustNewConstMetric( c.DownloadTotalBytes, - prometheus.GaugeValue, + prometheus.CounterValue, float64(down), labels..., ) ch <- prometheus.MustNewConstMetric( c.UploadTotalBytes, - prometheus.GaugeValue, + prometheus.CounterValue, float64(up), labels..., )