Projet

Général

Profil

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

root / plugins / rtorrent / rtom_allsessions_peers @ 81db94e2

Historique | Voir | Annoter | Télécharger (4,45 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 informations
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
#	category        Change graph category
28
#
29
# Configuration example
30
#
31
#       [rtom_allsessions_*]
32
#       user username
33
#       env.src socket
34
#       env.socket /home/user/torrent/.socket/rpc.socket,/home/user/torrent/.socket/rpc.socket
35
#       env.category Category
36
#
37
#       [rtom_allsessions_*]
38
#       user username
39
#       env.port 5000,5001,5002,5003
40
#       env.category Category
41
#
42
#%# family=auto
43

    
44

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

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

    
73
use IO::Socket;
74

    
75
my $src         = $ENV{"src"} || "";
76
my @sockets     = split /,/, $ENV{"socket"} || "";
77
my $ip          = $ENV{"ip"} || "127.0.0.1";
78
my @ports       = split /,/, $ENV{"port"} || "";
79

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

    
83
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>";
84
my $llen	= length $line;
85
my $header	= "CONTENT_LENGTH\000${llen}\000SCGI\001\000";
86
my $hlen	= length $header;
87

    
88
my $tor = 0;
89
my $tot = 0;
90
my $enc = 0;
91
my $inc = 0;
92
my $pline = "";
93
my $ppline = "";
94
my $out = 0;
95
my $pla = 0;
96

    
97

    
98
if ( ( defined $src ) && ( $src eq "socket" ) ) {
99
        for $socket (@sockets)
100
        {
101
                socket( SOCK, PF_UNIX, SOCK_STREAM, 0 ) or die;
102
                connect( SOCK, sockaddr_un( $socket ) ) or die $!;
103
                my $line = "${hlen}:${header},${line}";
104
                print SOCK $line;
105
                flush SOCK;
106
                while ( $line = <SOCK> ) {
107
					if ( $line =~ /$tpattern/ ) {
108
						$tor += 1;
109
					} elsif ( $line =~ /$pattern/ ) {
110
						$tot += 1;
111
						$enc += $2;
112
						$line = <SOCK>;
113
						$line =~ /$pattern/;
114
						$inc += $2;
115
					}
116
					$ppline = $pline;
117
					$pline = $line;
118
				}
119
				close (SOCK);
120
				$out = $out + $tot - $inc;
121
				$pla = $pla + $tot - $enc;
122
        }        
123
} else {
124
        for $port (@ports)
125
        { 
126
                socket( SOCK, PF_INET, SOCK_STREAM, getprotobyname( "tcp" ) );
127
                connect( SOCK, sockaddr_in( $port, inet_aton( $ip ) ) );
128
                my $line = "${hlen}:${header},${line}";
129
                print SOCK $line;
130
                flush SOCK;
131
                while ( $line = <SOCK> ) {
132
					if ( $line =~ /$tpattern/ ) {
133
						$tor += 1;
134
					} elsif ( $line =~ /$pattern/ ) {
135
						$tot += 1;
136
						$enc += $2;
137
						$line = <SOCK>;
138
						$line =~ /$pattern/;
139
						$inc += $2;
140
					}
141
					$ppline = $pline;
142
					$pline = $line;
143
				}
144
				close (SOCK);
145
				$out = $out + $tot - $inc;
146
				$pla = $pla + $tot - $enc;
147
        }
148
}
149

    
150

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

    
153
exit;