Skip to content

Commit

Permalink
Merge branch 'release/0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
metalmatze committed Dec 19, 2016
2 parents dcb45d4 + a7dc5e6 commit 9a31366
Show file tree
Hide file tree
Showing 19 changed files with 1,180 additions and 97 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
TRANSMISSION_ADDR=http://localhost:9091
TRANSMISSION_PASSWORD=
TRANSMISSION_USERNAME=
WEB_ADDR=:19091
WEB_PATH=/metrics
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.env
/transmission-exporter

*.o
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ ENV Variable | Description
| TRANSMISSION_USERNAME | Transmission username, no default |
| TRANSMISSION_PASSWORD | Transmission password, no default |

### Build

make

For development we encourage you to use `make install` instead, it's faster.

### Docker

docker pull metalmatze/transmission-exporter
Expand All @@ -49,6 +43,14 @@ Example `docker-compose.yml` with Transmission also running in docker.
environment:
TRANSMISSION_ADDR: http://transmission:9091

### Development

make

For development we encourage you to use `make install` instead, it's faster.

Now simply copy the `.env.example` to `.env`, like `cp .env.example .env` and set your preferences.
Now you're good to go.

### Original authors of the Transmission package
Tobias Blom (https://github.com/tubbebubbe/transmission)
Expand Down
22 changes: 19 additions & 3 deletions cmd/transmission-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@ import (
"net/http"

arg "github.com/alexflint/go-arg"
"github.com/joho/godotenv"
transmission "github.com/metalmatze/transmission-exporter"
"github.com/prometheus/client_golang/prometheus"
)

// Config gets its content from env and passes it on to different packages
type Config struct {
WebPath string `arg:"env:WEB_PATH"`
WebAddr string `arg:"env:WEB_ADDR"`
TransmissionAddr string `arg:"env:TRANSMISSION_ADDR"`
TransmissionUsername string `arg:"env:TRANSMISSION_USERNAME"`
TransmissionPassword string `arg:"env:TRANSMISSION_PASSWORD"`
TransmissionUsername string `arg:"env:TRANSMISSION_USERNAME"`
WebAddr string `arg:"env:WEB_ADDR"`
WebPath string `arg:"env:WEB_PATH"`
}

func main() {
log.Println("starting transmission-exporter")

err := godotenv.Load()
if err != nil {
log.Println("no .env present")
}

c := Config{
WebPath: "/metrics",
WebAddr: ":19091",
Expand All @@ -40,8 +46,11 @@ func main() {
client := transmission.New(c.TransmissionAddr, user)

prometheus.MustRegister(NewTorrentCollector(client))
prometheus.MustRegister(NewSessionCollector(client))
prometheus.MustRegister(NewSessionStatsCollector(client))

http.Handle(c.WebPath, prometheus.Handler())

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Node Exporter</title></head>
Expand All @@ -54,3 +63,10 @@ func main() {

log.Fatal(http.ListenAndServe(c.WebAddr, nil))
}

func boolToString(true bool) string {
if true {
return "1"
}
return "0"
}
200 changes: 200 additions & 0 deletions cmd/transmission-exporter/session_collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package main

import (
"log"

"github.com/metalmatze/transmission-exporter"
"github.com/prometheus/client_golang/prometheus"
)

// SessionCollector exposes session metrics
type SessionCollector struct {
client *transmission.Client

AltSpeedDown *prometheus.Desc
AltSpeedUp *prometheus.Desc
CacheSize *prometheus.Desc
FreeSpace *prometheus.Desc
QueueDown *prometheus.Desc
QueueUp *prometheus.Desc
PeerLimitGlobal *prometheus.Desc
PeerLimitTorrent *prometheus.Desc
SeedRatioLimit *prometheus.Desc
SpeedLimitDown *prometheus.Desc
SpeedLimitUp *prometheus.Desc
Version *prometheus.Desc
}

// NewSessionCollector takes a transmission.Client and returns a SessionCollector
func NewSessionCollector(client *transmission.Client) *SessionCollector {
return &SessionCollector{
client: client,

AltSpeedDown: prometheus.NewDesc(
namespace+"alt_speed_down",
"Alternative max global download speed",
[]string{"enabled"},
nil,
),
AltSpeedUp: prometheus.NewDesc(
namespace+"alt_speed_up",
"Alternative max global upload speed",
[]string{"enabled"},
nil,
),
CacheSize: prometheus.NewDesc(
namespace+"cache_size_bytes",
"Maximum size of the disk cache",
nil,
nil,
),
FreeSpace: prometheus.NewDesc(
namespace+"free_space",
"Free space left on device to download to",
[]string{"download_dir", "incomplete_dir"},
nil,
),
QueueDown: prometheus.NewDesc(
namespace+"queue_down",
"Max number of torrents to download at once",
[]string{"enabled"},
nil,
),
QueueUp: prometheus.NewDesc(
namespace+"queue_up",
"Max number of torrents to upload at once",
[]string{"enabled"},
nil,
),
PeerLimitGlobal: prometheus.NewDesc(
namespace+"global_peer_limit",
"Maximum global number of peers",
nil,
nil,
),
PeerLimitTorrent: prometheus.NewDesc(
namespace+"torrent_peer_limit",
"Maximum number of peers for a single torrent",
nil,
nil,
),
SeedRatioLimit: prometheus.NewDesc(
namespace+"seed_ratio_limit",
"The default seed ratio for torrents to use",
[]string{"enabled"},
nil,
),
SpeedLimitDown: prometheus.NewDesc(
namespace+"speed_limit_down_bytes",
"Max global download speed",
[]string{"enabled"},
nil,
),
SpeedLimitUp: prometheus.NewDesc(
namespace+"speed_limit_up_bytes",
"Max global upload speed",
[]string{"enabled"},
nil,
),
Version: prometheus.NewDesc(
namespace+"version",
"Transmission version as label",
[]string{"version"},
nil,
),
}
}

// Describe implements the prometheus.Collector interface
func (sc *SessionCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- sc.AltSpeedDown
ch <- sc.AltSpeedUp
ch <- sc.CacheSize
ch <- sc.FreeSpace
ch <- sc.QueueDown
ch <- sc.QueueUp
ch <- sc.PeerLimitGlobal
ch <- sc.PeerLimitTorrent
ch <- sc.SeedRatioLimit
ch <- sc.SpeedLimitDown
ch <- sc.SpeedLimitUp
ch <- sc.Version
}

// Collect implements the prometheus.Collector interface
func (sc *SessionCollector) Collect(ch chan<- prometheus.Metric) {
session, err := sc.client.GetSession()
if err != nil {
log.Printf("failed to get session: %v", err)
}

ch <- prometheus.MustNewConstMetric(
sc.AltSpeedDown,
prometheus.GaugeValue,
float64(session.AltSpeedDown),
boolToString(session.AltSpeedEnabled),
)
ch <- prometheus.MustNewConstMetric(
sc.AltSpeedUp,
prometheus.GaugeValue,
float64(session.AltSpeedUp),
boolToString(session.AltSpeedEnabled),
)
ch <- prometheus.MustNewConstMetric(
sc.CacheSize,
prometheus.GaugeValue,
float64(session.CacheSizeMB*1024*1024),
)
ch <- prometheus.MustNewConstMetric(
sc.FreeSpace,
prometheus.GaugeValue,
float64(session.DownloadDirFreeSpace),
session.DownloadDir, session.IncompleteDir,
)
ch <- prometheus.MustNewConstMetric(
sc.QueueDown,
prometheus.GaugeValue,
float64(session.DownloadQueueSize),
boolToString(session.DownloadQueueEnabled),
)
ch <- prometheus.MustNewConstMetric(
sc.QueueUp,
prometheus.GaugeValue,
float64(session.SeedQueueSize),
boolToString(session.SeedQueueEnabled),
)
ch <- prometheus.MustNewConstMetric(
sc.PeerLimitGlobal,
prometheus.GaugeValue,
float64(session.PeerLimitGlobal),
)
ch <- prometheus.MustNewConstMetric(
sc.PeerLimitTorrent,
prometheus.GaugeValue,
float64(session.PeerLimitPerTorrent),
)
ch <- prometheus.MustNewConstMetric(
sc.SeedRatioLimit,
prometheus.GaugeValue,
float64(session.SeedRatioLimit),
boolToString(session.SeedRatioLimited),
)
ch <- prometheus.MustNewConstMetric(
sc.SpeedLimitDown,
prometheus.GaugeValue,
float64(session.SpeedLimitDown),
boolToString(session.SpeedLimitDownEnabled),
)
ch <- prometheus.MustNewConstMetric(
sc.SpeedLimitUp,
prometheus.GaugeValue,
float64(session.SpeedLimitUp),
boolToString(session.SpeedLimitUpEnabled),
)
ch <- prometheus.MustNewConstMetric(
sc.Version,
prometheus.GaugeValue,
float64(1),
session.Version,
)
}
Loading

0 comments on commit 9a31366

Please sign in to comment.