2014-02-05 11:29:14 +01:00
|
|
|
#!/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
|
2017-04-17 22:43:38 +02:00
|
|
|
# check http://libtorrent.rakshasa.no/wiki/RTorrentXMLRPCGuide for further information
|
2014-02-05 11:29:14 +01:00
|
|
|
#
|
|
|
|
# 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"
|
2014-02-25 10:35:06 +01:00
|
|
|
# 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"
|
2014-02-05 11:29:14 +01:00
|
|
|
# category Change graph category
|
|
|
|
#
|
|
|
|
# Configuration example
|
|
|
|
#
|
2014-02-25 10:35:06 +01:00
|
|
|
# [rtom_allsessions_*]
|
2014-02-05 11:29:14 +01:00
|
|
|
# user username
|
|
|
|
# env.src socket
|
|
|
|
# env.socket /home/user/torrent/.socket/rpc.socket,/home/user/torrent/.socket/rpc.socket
|
|
|
|
# env.category Category
|
|
|
|
#
|
2014-02-25 10:35:06 +01:00
|
|
|
# [rtom_allsessions_*]
|
|
|
|
# user username
|
|
|
|
# env.port 5000,5001,5002,5003
|
|
|
|
# env.category Category
|
|
|
|
#
|
2014-02-05 11:29:14 +01:00
|
|
|
#%# family=auto
|
|
|
|
|
|
|
|
|
|
|
|
if ( $ARGV[0] and $ARGV[0] eq "autoconf" ) {
|
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
2017-02-24 18:29:14 +01:00
|
|
|
print "graph_category filetransfer".${category}."\n";
|
2014-02-05 11:29:14 +01:00
|
|
|
print "mem.label Memory usage\n";
|
2014-09-07 14:20:12 +02:00
|
|
|
print "mem.info Memory usage of rTorrent\n";
|
2014-02-05 11:29:14 +01:00
|
|
|
print "mem.type GAUGE\n";
|
|
|
|
print "mem.draw LINE2\n";
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
use IO::Socket;
|
|
|
|
my $src = $ENV{"src"} || "";
|
2014-02-25 10:35:06 +01:00
|
|
|
my @sockets = split /,/, $ENV{"socket"} || "";
|
|
|
|
my $ip = $ENV{"ip"} || "127.0.0.1";
|
|
|
|
my @ports = split /,/, $ENV{"port"} || "";
|
2014-02-05 11:29:14 +01:00
|
|
|
|
2014-02-25 10:35:06 +01:00
|
|
|
my $mem = 0;
|
2014-02-05 11:29:14 +01:00
|
|
|
my $pattern = qr/<value><(int|i4|i8|ex\.i8)>(\d+)<\/(int|i4|i8|ex\.i8)><\/value>/;
|
|
|
|
my $line = "<?xml version=\"1.0\" encoding=\"utf-8\"?><methodCall><methodName>get_memory_usage</methodName></methodCall>";
|
|
|
|
my $llen = length $line;
|
|
|
|
my $header = "CONTENT_LENGTH\000${llen}\000SCGI\001\000";
|
|
|
|
my $hlen = length $header;
|
|
|
|
|
2014-02-25 10:35:06 +01:00
|
|
|
if ( ( defined $src ) && ( $src eq "socket" ) ) {
|
|
|
|
for $socket (@sockets)
|
|
|
|
{
|
2014-02-05 11:29:14 +01:00
|
|
|
socket( SOCK, PF_UNIX, SOCK_STREAM, 0 ) or die;
|
|
|
|
connect( SOCK, sockaddr_un( $socket ) ) or die $!;
|
2014-02-25 10:35:06 +01:00
|
|
|
my $line = "${hlen}:${header},${line}";
|
|
|
|
print SOCK $line;
|
|
|
|
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 ) ) );
|
|
|
|
my $line = "${hlen}:${header},${line}";
|
|
|
|
print SOCK $line;
|
|
|
|
flush SOCK;
|
|
|
|
while ( $line = <SOCK> ) {
|
|
|
|
if ( $line =~ /$pattern/ ) {
|
|
|
|
$mem = $mem + $2;
|
|
|
|
}
|
2014-02-05 11:29:14 +01:00
|
|
|
}
|
2014-02-25 10:35:06 +01:00
|
|
|
close (SOCK);
|
2014-02-05 11:29:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print "mem.value ${mem}\n";
|
|
|
|
|
|
|
|
exit;
|