Repository
Munin (contrib)
Last change
2021-08-05
Family
auto
Keywords
Language
Perl

rtom_vol

Sadly there is no documentation for this plugin.

#!/usr/bin/perl -w
#
# xmlrpc based munin plugin for monitoring rtorrent's torrent count
# 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.2.0 - 080619
#  support for scgi_port and scgi_local
#  configurable via munin env variables
#  initial release
#
#
# 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
#
# Configuration example
#
#	[rtom_vol]
#	user username
#	env.src socket
#	env.socket /home/user/torrent/.socket/rpc.socket
#	env.category Sometext
#
#	[rtom_vol]
#	env.ip 127.0.0.1
#	env.port 5000
#
#
#%# family=auto

my @views = ( "default", "started", "stopped", "complete", "incomplete" );

if ( $ARGV[0] and $ARGV[0] eq "config" ) {
	my $category = $ENV{"category"} || "";
	print "graph_args --base 1000 -r --lower-limit 0\n";
	print "graph_title rTorrent volume\n";
	print "graph_vlabel active torrents\n";
	print "graph_category filetransfer".${category}."\n";
	print "complete.label complete\n";
	print "complete.draw AREA\n";
	print "complete.info complete torrents\n";
	print "incomplete.label incomplete\n";
	print "incomplete.draw STACK\n";
	print "incomplete.info incomplete torrents\n";
	print "stopped.label stopped\n";
	print "stopped.draw LINE2\n";
	print "stopped.info stopped torrents\n";
	print "started.label started\n";
	print "started.draw LINE2\n";
	print "started.info started torrents\n";
	print "default.label total\n";
	print "default.draw LINE2\n";
	print "default.info all torrents\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);
	}
	if(defined $_[0]){
		return version->parse($rtorrent_version) < version->parse($_[0]);
	}
}
# init rtorrent_version
rtorrent_version_lower_than();

my $pattern	= qr/<value><string>([A-Z0-9]+)<\/string><\/value>/;

my $line;
my $llenmy;
my $header;
my $hlen;

my $function_multicall;
my $function_multicall_arg;
my $function_hash;

my $num;
foreach ( @views ) {
	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 ) ) );
	}

	$function_multicall	= rtorrent_version_lower_than('0.9.0')? 'd.multicall' : 'd.multicall2';
	$function_multicall_arg	= rtorrent_version_lower_than('0.9.0') ? '' : '<param><value><string></string></value></param>';
	$function_hash	= rtorrent_version_lower_than('0.9.0')? 'd.get_hash=' : 'd.hash=';
	$line	= "<?xml version=\"1.0\" encoding=\"utf-8\"?><methodCall><methodName>$function_multicall</methodName><params>$function_multicall_arg<param><value><string>${_}</string></value></param><param><value><string>$function_hash</string></value></param></params></methodCall>";

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

	print SOCK $line;
	flush SOCK;

	$num = 0;
	while ( $line = <SOCK> ) {
		if ( $line =~ /$pattern/ ) {
			$num++;
		}
	}
	print "${_}.value ${num}\n";

	close (SOCK);
}

exit;