Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / rtorrent / rtom_peers @ 51927e79

Historique | Voir | Annoter | Télécharger (3,65 ko)

1
#!/usr/bin/perl -w
2
#
3
# xmlrpc based munin plugin for monitoring rtorrent's peer count
4
# prerequisites:
5
#  - rtorrent 0.7.5 or newer compiled with --with-xmlrpc-c
6
# check http://libtorrent.rakshasa.no/wiki/RTorrentXMLRPCGuide for further information
7
#
8
# written by Gabor Hudiczius
9
# web: http://projects.cyla.homeip.net/rtwi/wiki/rTorrentOMeter
10
# email: ghudiczius@gmail.com
11
#
12
# 0.2.0 - 080619
13
#  support for scgi_port and scgi_local
14
#  configurable via munin env variables
15
#  initial release
16
#
17
#
18
# Parameters:
19
#
20
#	config		required
21
#
22
#
23
# Configurable variables
24
#
25
#	src		"socket" when using scgi_socket, or anything else when using scgi_port
26
#	socket		rTorrent's rpc socket (scgi_local)	- using scgi_local	- needed, when "src" is set to "socket"
27
#	ip		rTorrent's ip address			- using scgi_port	- needed, when "src" is NOT set to "socket"
28
#	port		rTorrent's scgi port (scgi_port)	- using scgi_port	- needed, when "src" is NOT set to "socket"
29
#	category        Change graph category
30
#
31
# Configuration example
32
#
33
#	[rtom_peers]
34
#	user username
35
#	env.src socket
36
#	env.socket /home/usernametorrent/.socket/rpc.socket
37
#	env.category Category
38
#
39
#	[rtom_peers]
40
#	env.ip 127.0.0.1
41
#	env.port 5000
42
#
43
#
44
#%# family=auto
45

    
46

    
47
if ( $ARGV[0] and $ARGV[0] eq "autoconf" ) {
48
	exit 1;
49
}
50

    
51
if ( $ARGV[0] and $ARGV[0] eq "config" ) {
52
	my $category = $ENV{"category"} || "";
53
	print "graph_title rTorrent peer statistics\n";
54
	print "graph_args --base 1000 --lower-limit 0\n";
55
	print "graph_vlabel peers\n";
56
	print "graph_category filetransfer".${category}."\n";
57
	print "outgoing.label outgoing\n";
58
	print "outgoing.draw AREA\n";
59
	print "outgoing.info number of outgoing connections\n";
60
	print "incoming.label incoming\n";
61
	print "incoming.draw STACK\n";
62
	print "incoming.info number of incoming connections\n";
63
	print "plain.label plain text\n";
64
	print "plain.draw LINE2\n";
65
	print "plain.info number of plain text connections\n";
66
	print "encrypted.label encrypted\n";
67
	print "encrypted.draw LINE2\n";
68
	print "encrypted.info number of encrypted connections\n";
69
	print "total.label total\n";
70
	print "total.draw LINE2\n";
71
	print "total.info total number of connections\n";
72
 	exit 0;
73
}
74

    
75
use IO::Socket;
76

    
77
my $src 	= $ENV{"src"} || "";
78
my $ip		= $ENV{"ip"} || "127.0.0.1";
79
my $port	= $ENV{"port"} || "5000";
80
my $socket	= $ENV{"socket"} || "";
81

    
82
my $pattern	= qr/<value><(int|i4|i8|ex\.i8)>(\d+)<\/(int|i4|i8|ex\.i8)><\/value>/;
83
my $tpattern	= qr/[0-9A-F]{20}/;
84

    
85
my $line	= "<?xml version=\"1.0\" encoding=\"utf-8\"?><methodCall><methodName>d.multicall</methodName><params><param><value><string>main</string></value></param><param><value><string>d.get_hash=</string></value></param><param><value><string>p.multicall=,p.is_encrypted=,p.is_incoming=</string></value></param></params></methodCall>";
86
my $llen	= length $line;
87
my $header	= "CONTENT_LENGTH\000${llen}\000SCGI\001\000";
88
my $hlen	= length $header;
89
$line		= "${hlen}:${header},${line}";
90

    
91
if ( ( defined $src ) && ( $src eq "socket" ) ) {
92
	socket( SOCK, PF_UNIX, SOCK_STREAM, 0 );
93
	connect( SOCK, sockaddr_un( $socket ) );
94
} else {
95
	socket( SOCK, PF_INET, SOCK_STREAM, getprotobyname( "tcp" ) );
96
	connect( SOCK, sockaddr_in( $port, inet_aton( $ip ) ) );
97
}
98

    
99
print SOCK $line;
100
flush SOCK;
101

    
102
my $tor = 0;
103
my $tot = 0;
104
my $enc = 0;
105
my $inc = 0;
106
my $pline = "";
107
my $ppline = "";
108
while ( $line = <SOCK> ) {
109
	if ( $line =~ /$tpattern/ ) {
110
		$tor += 1;
111
	} elsif ( $line =~ /$pattern/ ) {
112
		$tot += 1;
113
		$enc += $2;
114
		$line = <SOCK>;
115
		$line =~ /$pattern/;
116
		$inc += $2;
117
	}
118
	$ppline = $pline;
119
	$pline = $line;
120
}
121
close (SOCK);
122

    
123
my $out = $tot - $inc;
124
my $pla = $tot - $enc;
125

    
126
print "torrents.value ${tor}\ntotal.value ${tot}\nencrypted.value ${enc}\nplain.value ${pla}\nincoming.value ${inc}\noutgoing.value ${out}\n";
127

    
128
exit;