Repository
Munin (contrib)
Last change
2020-03-31
Family
auto
Keywords
Language
Perl

rtom_spdd

Sadly there is no documentation for this plugin

#!/usr/bin/perl -w
#
# xmlrpc based munin plugin for monitoring rtorrent's upload/download speed
# prerequisites:
#  - rtorrent 0.7.5 or newer compiled with --with-xmlrpc-c
# check http://libtorrent.rakshasa.no/wiki/RTorrentXMLRPCGuide for further information
#
# written by Gabor Hudiczius
# web: http://projects.cyla.homeip.net/rtwi/wiki/rTorrentOMeter
# email: ghudiczius@gmail.com
#
# 0.0.0 - 071218
#  initial release
#
# 0.0.1 - 071220
#  minor textbugs fixed
#
# 0.1.0d - 080519
#  full rewrite in perl
#  support for scgi_port and scgi_local
#  configurable via munin env variables
#  different ul/dl scale can be set for asymmetric connections
#  using get_(up|down)_total, and derive
#
# 0.2.0 - 080619
#  upload and download limit displayed on the graph
#
#
# Parameters:
#
#	config		required
#
#
# Configurable variables
#
#	src		"socket" when using scgi_socket, or anything else when using scgi_port
#	socket		rTorrent's rpc socket (scgi_local)	- using scgi_local	- needed, when "src" is set to "socket"
#	ip		rTorrent's ip address			- using scgi_port	- needed, when "src" is NOT set to "socket"
#	port		rTorrent's scgi port (scgi_port)	- using scgi_port	- needed, when "src" is NOT set to "socket"
#	category        Change graph category
#	diff		"yes" for using bps for upload and Bps for download, or anything else for using Bps for both
#
#
# Configuration example
#
#	[rtom_spdd]
#	user username
#	env.src socket
#	env.socket /home/user/torrent/.socket/rpc.socket
#	env.category Sometext
#
#	[rtom_spdd]
#	env.ip 127.0.0.1
#	env.port 5000
#
#
#%# family=auto


if ( $ARGV[0] and $ARGV[0] eq "config" ) {
	my $diff = $ENV{"diff"} || "";
	my $category = $ENV{"category"} || "";
	print "graph_order down up\n";
	print "graph_title rTorrent speeds\n";
	print "graph_args --base 1024\n";
	print "graph_vlabel Bytes per \${graph_period}\n";
	print "graph_category filetransfer".${category}."\n";
	print "down.label Download B/s\n";
	print "down.info Download speed in Bytes per seconds\n";
	print "down.type DERIVE\n";
	print "down.min 0\n";
	print "down.draw AREA\n";
	if ( ( defined $diff ) && ( $diff eq "yes" ) ) {
		print "up.label Upload b/s\n";
		print "up.info Upload speed in bits per seconds\n";
		print "up.cdef up,8,*\n";
	} else {
		print "up.label Upload B/s\n";
		print "up.info Upload speed in Bytes per seconds\n";
	}
	print "up.type DERIVE\n";
	print "up.min 0\n";
	print "up.draw LINE2\n";
	print "downrate.label Download limit B/s\n";
	print "downrate.info Download limit in Bytes per seconds\n";
	print "downrate.draw LINE2\n";
	if ( ( defined $diff ) && ( $diff eq "yes" ) ) {
		print "uprate.label Upload limit b/s\n";
		print "uprate.info Upload limit in bits per seconds\n";
		print "uprate.cdef uprate,8,*\n";
	} else {
		print "uprate.label Upload limit B/s\n";
		print "uprate.info Upload limit in Bytes per seconds\n";
	}
	print "uprate.draw LINE2\n";
	exit 0;
}

use IO::Socket;

my $src 	= $ENV{"src"} || "";
my $ip		= $ENV{"ip"} || "127.0.0.1";
my $port	= $ENV{"port"} || "5000";
my $socket	= $ENV{"socket"} || "";

# detect rtorrent version
use version;
my $rtorrent_version;
sub rtorrent_version_lower_than {
	if (not length $rtorrent_version){
		if ( ( defined $src ) && ( $src eq "socket" ) ) {
			socket( SOCK, PF_UNIX, SOCK_STREAM, 0 ) or die;
			connect( SOCK, sockaddr_un( $socket ) ) or die $!;
		} else {
			socket( SOCK, PF_INET, SOCK_STREAM, getprotobyname( "tcp" ) );
			connect( SOCK, sockaddr_in( $port, inet_aton( $ip ) ) );
		}
		my $line_version= "<?xml version=\"1.0\" encoding=\"utf-8\"?><methodCall><methodName>system.client_version</methodName></methodCall>";
		my $llen	= length $line_version;
		my $header	= "CONTENT_LENGTH\000${llen}\000SCGI\001\000";
		my $hlen	= length $header;
		$line_version= "${hlen}:${header},${line_version}";
		print SOCK $line_version;
		flush SOCK;
		my $pattern	= qr/<value><string>([0-9.]+)<\/string><\/value>/;
		while ( $line = <SOCK> ) {
			if ( $line =~ /$pattern/ ) {
				$rtorrent_version = $1;
			}
		}
		close (SOCK);
	}
	return version->parse($rtorrent_version) < version->parse($_[0]);
}
my $pattern	= qr/<value><(int|i4|i8|ex\.i8)>([-]{0,1}\d+)<\/(int|i4|i8|ex\.i8)><\/value>/;

my $function_totalup	= rtorrent_version_lower_than('0.9.0') ? 'get_up_total' : 'throttle.global_up.total';
my $function_totaldown	= rtorrent_version_lower_than('0.9.0') ? 'get_down_total' : 'throttle.global_down.total';
my $function_rateup	= rtorrent_version_lower_than('0.9.0') ? 'get_upload_rate' : 'throttle.global_up.max_rate';
my $function_ratedown	= rtorrent_version_lower_than('0.9.0') ? 'get_download_rate' : 'throttle.global_down.max_rate';
my $line		= "<?xml version=\"1.0\" encoding=\"utf-8\"?><methodCall><methodName>system.multicall</methodName><params><param><value><array><data><value><struct><member><name>methodName</name><value><string>$function_totalup</string></value></member><member><name>params</name><value><array><data/></array></value></member></struct></value><value><struct><member><name>methodName</name><value><string>$function_totaldown</string></value></member><member><name>params</name><value><array><data/></array></value></member></struct></value><value><struct><member><name>methodName</name><value><string>$function_rateup</string></value></member><member><name>params</name><value><array><data/></array></value></member></struct></value><value><struct><member><name>methodName</name><value><string>$function_ratedown</string></value></member><member><name>params</name><value><array><data/></array></value></member></struct></value></data></array></value></param></params></methodCall>";

my $llen	= length $line;
my $header	= "CONTENT_LENGTH\000${llen}\000SCGI\001\000";
my $hlen	= length $header;
$line		= "${hlen}:${header},${line}";

if ( ( defined $src ) && ( $src eq "socket" ) ) {
	socket( SOCK, PF_UNIX, SOCK_STREAM, 0 );
	connect( SOCK, sockaddr_un( $socket ) );
} else {
	socket( SOCK, PF_INET, SOCK_STREAM, getprotobyname( "tcp" ) );
	connect( SOCK, sockaddr_in( $port, inet_aton( $ip ) ) );
}

print SOCK $line;
flush SOCK;

my $up = -1;
my $down = -1;
my $uprate = -1;
my $downrate = -1;
while ( ( $up == -1 ) && ( $line = <SOCK> ) ) {
	if ( $line =~ /$pattern/ ) {
		$up = $2;
	}
}
while ( ( $down == -1 ) && ( $line = <SOCK> ) ) {
	if ( $line =~ /$pattern/ ) {
		$down = $2;
	}
}
while ( ( $uprate == -1 ) && ( $line = <SOCK> ) ) {
	if ( $line =~ /$pattern/ ) {
		$uprate = $2;
	}
}
while ( ( $downrate == -1 ) && ( $line = <SOCK> ) ) {
	if ( $line =~ /$pattern/ ) {
		$downrate = $2;
	}
}
close (SOCK);

print "up.value ${up}\ndown.value ${down}\nuprate.value ${uprate}\ndownrate.value ${downrate}\n";

exit;