Repository
Munin (contrib)
Last change
2020-04-05
Family
auto
Keywords
Language
Perl

rtom_allsessions_mem

Sadly there is no documentation for this plugin

#!/usr/bin/perl -w
#
# xmlrpc based munin plugin for monitoring rtorrent's memory usage
# 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_allsessions_*]
#       user username
#       env.src socket
#       env.socket /home/user/torrent/.socket/rpc.socket,/home/user/torrent/.socket/rpc.socket
#       env.category Category
#
#       [rtom_allsessions_*]
#       user username
#       env.port 5000,5001,5002,5003
#       env.category Category
#
#%# family=auto


if ( $ARGV[0] and $ARGV[0] eq "config" ) {
  my $category = $ENV{"category"} || "";
  print "graph_title rTorrent memory usage\n";
  print "graph_args --base 1024 --lower-limit 0\n";
  print "graph_vlabel Bytes\n";
  print "graph_category filetransfer".${category}."\n";
  print "mem.label Memory usage\n";
  print "mem.info Memory usage of rTorrent\n";
  print "mem.type GAUGE\n";
  print "mem.draw LINE2\n";
  exit 0;
}

use IO::Socket;
my $src         = $ENV{"src"} || "";
my @sockets     = split /,/, $ENV{"socket"} || "";
my $ip          = $ENV{"ip"} || "127.0.0.1";
my @ports       = split /,/, $ENV{"port"} || "";

# detect rtorrent version
use version;
my %rtorrent_version;
sub get_rtorrent_version {
	my $version;
	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/ ) {
			$version = $1;
		}
	}
	close (SOCK);
	$rtorrent_version{$_[0]} = $version;
}
sub rtorrent_version_lower_than {
	if (keys %rtorrent_version == 0 && not defined $_[0]){
		if ( ( defined $src ) && ( $src eq "socket" ) ) {
		  for $socket (@sockets)
		  {
		    socket( SOCK, PF_UNIX, SOCK_STREAM, 0 ) or die;
		    connect( SOCK, sockaddr_un( $socket ) ) or die $!;
		    get_rtorrent_version $socket;
		    close (SOCK);
		  }
		} else {
		  for $port (@ports)
		  {
		    socket( SOCK, PF_INET, SOCK_STREAM, getprotobyname( "tcp" ) );
		    connect( SOCK, sockaddr_in( $port, inet_aton( $ip ) ) );
		    get_rtorrent_version $port;
		    close (SOCK);
		  }
		}
	}
	if(defined $_[1]){
		return version->parse($rtorrent_version{$_[0]}) < version->parse($_[1]);
	}
}
# init rtorrent_version
rtorrent_version_lower_than();

my $pattern     = qr/<value><(int|i4|i8|ex\.i8)>(\d+)<\/(int|i4|i8|ex\.i8)><\/value>/;
my $mem         = 0;
sub construct_line {
	my $function	= rtorrent_version_lower_than($_[0], '0.9.0') ? 'get_memory_usage' : 'pieces.memory.current';
	my $line	= "<?xml version=\"1.0\" encoding=\"utf-8\"?><methodCall><methodName>$function</methodName></methodCall>";
	my $llen        = length $line;
	my $header      = "CONTENT_LENGTH\000${llen}\000SCGI\001\000";
	my $hlen        = length $header;
	$line = "${hlen}:${header},${line}";
	return $line;
}

if ( ( defined $src ) && ( $src eq "socket" ) ) {
  for $socket (@sockets)
  {
    socket( SOCK, PF_UNIX, SOCK_STREAM, 0 ) or die;
    connect( SOCK, sockaddr_un( $socket ) ) or die $!;
    print SOCK construct_line($socket);
    flush SOCK;
    while ( $line = <SOCK> ) {
      if ( $line =~ /$pattern/ ) {
        $mem = $mem + $2;
      }
    }
    close (SOCK);
  }
} else {
  for $port (@ports)
  {
    socket( SOCK, PF_INET, SOCK_STREAM, getprotobyname( "tcp" ) );
    connect( SOCK, sockaddr_in( $port, inet_aton( $ip ) ) );
    print SOCK construct_line($port);
    flush SOCK;
    while ( $line = <SOCK> ) {
      if ( $line =~ /$pattern/ ) {
        $mem = $mem + $2;
      }
    }
    close (SOCK);
  }
}

print "mem.value ${mem}\n";

exit;