From 8777efd66c60b6122a5e87fc3aa2cdb1622d1139 Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Wed, 26 Oct 2016 00:34:47 +0200 Subject: [PATCH 1/4] [tr_ratios] switch from bash to sh; quoting --- plugins/network/transmission_ratios/tr_ratios | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/plugins/network/transmission_ratios/tr_ratios b/plugins/network/transmission_ratios/tr_ratios index 343b1b46..316b6088 100755 --- a/plugins/network/transmission_ratios/tr_ratios +++ b/plugins/network/transmission_ratios/tr_ratios @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh user='' pass='' @@ -8,9 +8,8 @@ if [ "$1" = "config" ]; then echo "graph_vlabel Seed ratio %" echo "graph_category torrent" echo "graph_info This plugin shows your transmission ratios per torrent" - transmission-remote -n $user:$pass -l | gawk -f /usr/share/munin/plugins/tr_ratios_labels | iconv -f utf-8 -t ascii//translit + transmission-remote -n "$user:$pass" -l | gawk -f /usr/share/munin/plugins/tr_ratios_labels | iconv -f utf-8 -t ascii//translit exit 0 fi -transmission-remote -n $user:$pass -l | gawk -f /usr/share/munin/plugins/tr_ratios_data - +transmission-remote -n "$user:$pass" -l | gawk -f /usr/share/munin/plugins/tr_ratios_data From 1a25481e2c52769215669fb874eb0e57f17a3f90 Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Wed, 26 Oct 2016 00:38:07 +0200 Subject: [PATCH 2/4] [tr_ratios] include awk scripts; awk instead of gawk since we cannot rely on the location of the two included awk scripts, we just include them literally --- plugins/network/transmission_ratios/tr_ratios | 20 +++++++++++++++++-- .../transmission_ratios/tr_ratios_data | 10 ---------- .../transmission_ratios/tr_ratios_labels | 8 -------- 3 files changed, 18 insertions(+), 20 deletions(-) delete mode 100644 plugins/network/transmission_ratios/tr_ratios_data delete mode 100644 plugins/network/transmission_ratios/tr_ratios_labels diff --git a/plugins/network/transmission_ratios/tr_ratios b/plugins/network/transmission_ratios/tr_ratios index 316b6088..ce058a68 100755 --- a/plugins/network/transmission_ratios/tr_ratios +++ b/plugins/network/transmission_ratios/tr_ratios @@ -8,8 +8,24 @@ if [ "$1" = "config" ]; then echo "graph_vlabel Seed ratio %" echo "graph_category torrent" echo "graph_info This plugin shows your transmission ratios per torrent" - transmission-remote -n "$user:$pass" -l | gawk -f /usr/share/munin/plugins/tr_ratios_labels | iconv -f utf-8 -t ascii//translit + transmission-remote -n "$user:$pass" -l | awk ' + BEGIN { FIELDWIDTHS = "7 4 13 10 7 9 7 13 40" } + NR > 1 { + split($1,torrentid," ") + if (torrentid[1] != "Sum:") { + print "ID" torrentid[1] ".label " $9 + } + }' | iconv -f utf-8 -t ascii//translit exit 0 fi -transmission-remote -n "$user:$pass" -l | gawk -f /usr/share/munin/plugins/tr_ratios_data +transmission-remote -n "$user:$pass" -l | awk ' + BEGIN { FIELDWIDTHS = "7 4 13 10 7 9 7 13 40" } + NR > 1 { + split($1,torrentid," ") + if (torrentid[1] != "Sum:") { + split($7,ratio," ") + ratio[1] = ratio[1] * 100 + print "ID" torrentid[1] ".value " ratio[1] + } + }' diff --git a/plugins/network/transmission_ratios/tr_ratios_data b/plugins/network/transmission_ratios/tr_ratios_data deleted file mode 100644 index fd9dd551..00000000 --- a/plugins/network/transmission_ratios/tr_ratios_data +++ /dev/null @@ -1,10 +0,0 @@ -BEGIN { FIELDWIDTHS = "7 4 13 10 7 9 7 13 40" } -NR > 1 { - split($1,torrentid," ") - if(torrentid[1] != "Sum:") { - split($7,ratio," ") - ratio[1] = ratio[1] * 100 - print "ID" torrentid[1] ".value " ratio[1] - } -} - diff --git a/plugins/network/transmission_ratios/tr_ratios_labels b/plugins/network/transmission_ratios/tr_ratios_labels deleted file mode 100644 index 273ab3d3..00000000 --- a/plugins/network/transmission_ratios/tr_ratios_labels +++ /dev/null @@ -1,8 +0,0 @@ -BEGIN { FIELDWIDTHS = "7 4 13 10 7 9 7 13 40" } -NR > 1 { - split($1,torrentid," ") - if(torrentid[1] != "Sum:") { - print "ID" torrentid[1] ".label " $9 - } -} - From 839d825afed3dfab08a3ab2692b491290e9651ec Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Wed, 26 Oct 2016 02:04:11 +0200 Subject: [PATCH 3/4] [tr_ratios] add documentation --- plugins/network/transmission_ratios/tr_ratios | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/plugins/network/transmission_ratios/tr_ratios b/plugins/network/transmission_ratios/tr_ratios index ce058a68..1e59ff3f 100755 --- a/plugins/network/transmission_ratios/tr_ratios +++ b/plugins/network/transmission_ratios/tr_ratios @@ -1,7 +1,43 @@ #!/bin/sh +# -*- sh -*- user='' pass='' +: <<=cut + +=head1 NAME + +tr_ratios - monitor transfer ratios of the "transmission" bittorent program + +=head1 APPLICABLE SYSTEMS + +Any system with "transmission" installed and a transmission daemon running. + +=head1 CONFIGURATION + +Maybe you need to configure access credentials and connection settings: + + [tr_ratios] + env.host localhost + env.port 9091 + env.username alice + env.password secret + +=head1 MAGIC MARKERS + + #%# family=auto + #%# capabilities=autoconf + +=head1 AUTHOR + +unspecified + +=head1 LICENSE + +unspecified + +=cut + if [ "$1" = "config" ]; then echo "graph_title Transmission seed ratios" From c3660c2ac029b900e7ad0d0cf44cef9fb65b325b Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Wed, 26 Oct 2016 02:23:06 +0200 Subject: [PATCH 4/4] [tr_ratios] restructure code * allow configuration of username, passwort, host and port via env * reduce code duplication * remove "*" from stopped transmissions --- plugins/network/transmission_ratios/tr_ratios | 64 +++++++++++++------ 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/plugins/network/transmission_ratios/tr_ratios b/plugins/network/transmission_ratios/tr_ratios index 1e59ff3f..b2be2160 100755 --- a/plugins/network/transmission_ratios/tr_ratios +++ b/plugins/network/transmission_ratios/tr_ratios @@ -1,8 +1,6 @@ #!/bin/sh # -*- sh -*- -user='' -pass='' : <<=cut =head1 NAME @@ -39,29 +37,55 @@ unspecified =cut +CONNECTION_ARG="${host:-localhost}:${port:-9091}" +USERNAME="${username:-}" +PASSWORD="${password:-}" + + +# return a space separated list of transmissions with the following columns: +# * fieldname +# * ratio (in percent) +# * name of the transmissions +request_transmission_stats() { + if [ -n "$USERNAME$PASSWORD" ]; then + transmission-remote "$CONNECTION_ARG" --auth "$USERNAME:$PASSWORD" --list + else + transmission-remote "$CONNECTION_ARG" --list + fi | awk ' + BEGIN { FIELDWIDTHS = "7 4 13 10 7 9 7 13 40" } + NR > 1 { + split($1,torrentid," ") + # remove "*" from the ID of stopped transmissions + sub(/\*/, "", torrentid[1]) + if (torrentid[1] != "Sum:") { + split($7,ratio," ") + ratio[1] = ratio[1] * 100 + print "ID" torrentid[1], ratio[1], $9 + } + }' +} + + +if [ "$1" = "autoconf" ]; then + if [ -n "$(request_transmission_stats 2>/dev/null)" ]; then + echo "yes" + else + if which transmission-remote >/dev/null; then + echo "no (failed to connect to daemon)" + else + echo "no (missing 'transmission-remote' program)" + fi + fi + exit 0 +fi + if [ "$1" = "config" ]; then echo "graph_title Transmission seed ratios" echo "graph_vlabel Seed ratio %" echo "graph_category torrent" echo "graph_info This plugin shows your transmission ratios per torrent" - transmission-remote -n "$user:$pass" -l | awk ' - BEGIN { FIELDWIDTHS = "7 4 13 10 7 9 7 13 40" } - NR > 1 { - split($1,torrentid," ") - if (torrentid[1] != "Sum:") { - print "ID" torrentid[1] ".label " $9 - } - }' | iconv -f utf-8 -t ascii//translit + request_transmission_stats | awk '{print $1 ".label " $3 }' | iconv -f utf-8 -t ascii//translit exit 0 fi -transmission-remote -n "$user:$pass" -l | awk ' - BEGIN { FIELDWIDTHS = "7 4 13 10 7 9 7 13 40" } - NR > 1 { - split($1,torrentid," ") - if (torrentid[1] != "Sum:") { - split($7,ratio," ") - ratio[1] = ratio[1] * 100 - print "ID" torrentid[1] ".value " ratio[1] - } - }' +request_transmission_stats | awk '{print $1 ".value " $2 }'