Projet

Général

Profil

Révision c3660c2a

IDc3660c2ac029b900e7ad0d0cf44cef9fb65b325b
Parent 839d825a
Enfant 22f9fa51

Ajouté par Lars Kruse il y a environ 9 ans

[tr_ratios] restructure code

  • allow configuration of username, passwort, host and port via env
  • reduce code duplication
  • remove "*" from stopped transmissions

Voir les différences:

plugins/network/transmission_ratios/tr_ratios
1 1
#!/bin/sh
2 2
# -*- sh -*-
3 3

  
4
user=''
5
pass=''
6 4
: <<=cut
7 5

  
8 6
=head1 NAME
......
39 37
=cut
40 38

  
41 39

  
42
if [ "$1" = "config" ]; then
43
        echo "graph_title Transmission seed ratios"
44
	echo "graph_vlabel Seed ratio %"
45
	echo "graph_category torrent"
46
	echo "graph_info This plugin shows your transmission ratios per torrent"
47
	transmission-remote -n "$user:$pass" -l | awk '
40
CONNECTION_ARG="${host:-localhost}:${port:-9091}"
41
USERNAME="${username:-}"
42
PASSWORD="${password:-}"
43

  
44

  
45
# return a space separated list of transmissions with the following columns:
46
#  * fieldname
47
#  * ratio (in percent)
48
#  * name of the transmissions
49
request_transmission_stats() {
50
	if [ -n "$USERNAME$PASSWORD" ]; then
51
		transmission-remote "$CONNECTION_ARG" --auth "$USERNAME:$PASSWORD" --list
52
	else
53
		transmission-remote "$CONNECTION_ARG" --list
54
	fi | awk '
48 55
		BEGIN { FIELDWIDTHS = "7 4 13 10 7 9 7 13 40" }
49 56
		NR > 1 {
50 57
			split($1,torrentid," ")
58
			# remove "*" from the ID of stopped transmissions
59
			sub(/\*/, "", torrentid[1])
51 60
			if (torrentid[1] != "Sum:") {
52
				print "ID" torrentid[1] ".label " $9
61
				split($7,ratio," ")
62
				ratio[1] = ratio[1] * 100
63
				print "ID" torrentid[1], ratio[1], $9
53 64
			}
54
		}' | iconv -f utf-8 -t ascii//translit
65
		}'
66
}
67

  
68

  
69
if [ "$1" = "autoconf" ]; then
70
	if [ -n "$(request_transmission_stats 2>/dev/null)" ]; then
71
		echo "yes"
72
	else
73
		if which transmission-remote >/dev/null; then
74
			echo "no (failed to connect to daemon)"
75
		else
76
			echo "no (missing 'transmission-remote' program)"
77
		fi
78
	fi
79
        exit 0
80
fi
81

  
82
if [ "$1" = "config" ]; then
83
        echo "graph_title Transmission seed ratios"
84
	echo "graph_vlabel Seed ratio %"
85
	echo "graph_category torrent"
86
	echo "graph_info This plugin shows your transmission ratios per torrent"
87
	request_transmission_stats | awk '{print $1 ".label " $3 }' | iconv -f utf-8 -t ascii//translit
55 88
        exit 0
56 89
fi
57 90

  
58
transmission-remote -n "$user:$pass" -l | awk '
59
	BEGIN { FIELDWIDTHS = "7 4 13 10 7 9 7 13 40" }
60
	NR > 1 {
61
		split($1,torrentid," ")
62
		if (torrentid[1] != "Sum:") {
63
			split($7,ratio," ")
64
			ratio[1] = ratio[1] * 100
65
			print "ID" torrentid[1] ".value " ratio[1]
66
		}
67
	}'
91
request_transmission_stats | awk '{print $1 ".value " $2 }'

Formats disponibles : Unified diff