conky : integrating rTorrent downloads monitoring

Conky is a lightweight system monitoring tool. It has many built-in probes (processor load, memory usage, temperature sensors, etc), but it is still pretty easy to extend it if you don’t find the feature you need.

In this post I’ll describe my Conky setup and explain how to extend it to monitor your rTorrent downloads.

rTorrent is a great BitTorrent client which offers an XML-RPC interface to its core functions, making it easy to get the downloads status through scripting. You can read more about that in this previous post.

Starting by the end

Here is a screenshot of the final Conky layout we’re going to setup throughout this post.

There are actually 2 instances of Conky running simultaneously : one for the calendar widget on the left, and one for the system monitor containing the rTorrent status on the right.

conky-full-thumb

The calendar widget

I can’t take credit for this calendar widget, which I’ve found in the forums. It took juste a little bit of tweaking to get to this Conky configuration file :

own_window yes
own_window_type desktop
own_window_transparent yes
own_window_hints undecorated,below,skip_taskbar,skip_pager,sticky

update_interval 5

minimum_size 250
alignment ml
double_buffer yes

use_xft yes
xftfont DejaVu Sans Mono:size=20
#xftfont Bitstream Vera Sans:size=8

TEXT
$alignc${time %H:%M}
${color red}$hr$color
${time %A} ${time %d} ${time %B}
${color red}$hr$color
${execpi 300 cal | sed -e 's/'`date | awk '{print $3}'`'/\$\{color e84448}'`date | awk '{print $3}'`'\$\{color}/'}

execpi is the directive responsible for creating the calendar. It basically executes a shell one-liner which output is then used as a Conky configuration snippet. It runs at 300 seconds (5 minutes) intervals not to waste computing resources.

This is the end result :

conky-calendar

The system monitor

The system monitor requires a basic Conky configuration file and a perl script.

Conky configuration file for the system monitor

The system monitor Conky configuration file is a simple one, with standard Conky elements. The only “original part” is the one collecting rTorrent information : like for the calendar widget, an execpi directive is used to run the “rtorrent-status.pl” every minute (60 seconds).

own_window yes
own_window_type desktop
own_window_transparent yes
own_window_hints undecorated,below,skip_taskbar,skip_pager,sticky

update_interval 5
text_buffer_size 512

minimum_size 250
alignment mr
double_buffer yes

use_xft yes
#xftfont Fixed:size=12
xftfont Bitstream Vera Sans:size=8

TEXT
${color red}CPU$color       ${hwmon 0 fan 2} RPM
Core0  ${cpubar cpu1 4,200} @ ${freq 1} MHz ${hwmon 1 temp 1}C
Core1  ${cpubar cpu2 4,200} @ ${freq 2} MHz ${hwmon 2 temp 1}C
Core2  ${cpubar cpu3 4,200} @ ${freq 3} MHz ${hwmon 3 temp 1}C
Core3  ${cpubar cpu4 4,200} @ ${freq 4} MHz ${hwmon 4 temp 1}C

${color red}MEMORY$color
Memory ${goto 60} ${membar 4,200} ${memperc}%
Swap   ${goto 60} ${swapbar 4,200} ${swapperc}%

${color red}HDD$color
sda    ${diskio sda} ${alignc 50}${hddtemp /dev/sda}

${color red}NET$color
Down   ${downspeedf eth0} KiB/s
Up     ${upspeedf eth0} KiB/s

${color red}BitTorrent$color
${execpi 60 ~/bin/rtorrent-status.pl}

Perl Script to collect rTorrent download information

The following script pulls the information from the remote rTorrent client and outputs it as Conky configuration elements which are used by Conky through the execpi directive.

This script requires the Frontier::Client perl module, which I automatically got when I installed the xmlrpc-c package (I use gentoo and I had to set the “tools” USE flag for this package. If you use a different Linux distro, you might have to sort out this point).

It also require that rTorrent be installed/configured with XML-RPC enabled, and that the XML-RPC interface be exported through a HTTP server. It seems like a lot of work, but it is not if you follow those instructions.

#! /usr/bin/perl

use strict;
use warnings;

use Frontier::Client;
use Data::Dumper;

# Configuration

use constant server		=> 'http://spaghetti/RPC2';
use constant bt_stats	=> '/tmp/rtorrent_data';

# Do not edit below this comment

my $server = Frontier::Client->new(url => server);
my $result = $server->call('d.multicall', "main", 
	"d.get_base_filename=",
	"d.get_bytes_done=",
	"d.get_size_bytes=",
	"to_kb=\$d.get_down_rate=");

open DATA, ">" . bt_stats;

my $idx = 1;
my @res;
foreach my $d (@$result) {
	my $cur = $d->[1];
	my $max = $d->[2];
	my $dl_rate = $d->[3];
	print DATA $cur/$max, "\n";
	push @res,
		sprintf("%.40s \${alignr}%7.1f KiB/s\$alignr\n" . 
			" " x 5 . "%3.2f%%" . "\${goto 70}\${execbar sed -ne '$idx p' " .
			bt_stats . '}', $d->[0], $dl_rate, 100 * $cur / $max);
	$idx++;
}
close DATA;

print join("\n\n", @res), "\n";

Here is the end result for this :

conky-main

Bringing it all together

Finally I wrote this shell script to start both Conky instances at once. You can add this script in the “autorun” or equivalent settings for your desktop environment.

#! /bin/sh

conky -dc ~/.conkyrc
conky -dc ~/.conkyrc_cal

That’s all folks !

That was it. If you didn’t know how flexible and easy to extend Conky truly is, I hope this post will have helped you to get started writing your own Conky widgets and monitoring/extension scripts.

If you have any question or suggestion, please share them in the comments.

7 thoughts on “conky : integrating rTorrent downloads monitoring”

  1. Hi, I found your entry very enlightening!

    Not being familiar with xmlrpc, conky, rtorrent and perl made this challenging for me to implement (I despise reading perl code – haha).

    I ran into trouble with your script as it stands because my rtorrent runs on a different, 64 bit machine. As such, it has a more recent xmlrpc-c implementation that reports xml types of , which the ubuntu installed version of xmlrpc-c doesn’t have. I wrestled with compiling and installing a more recent version of xmlrpc-c for ubuntu but wasn’t having much success, but then found out I can just add to Frontier’s scalers in perl so this fixed that problem:
    $Frontier::RPC2::scalars{‘i8’} = 1;

    Additionally, I improved your script by removing the need for Data::Dump. You can just echo the value back to execbar. I also added a total up and down rate summary to the top of the bittorrent section and limited the retrieved list of torrents to those that are active. I also simplified/fixed the conversion of bytes to kB and removed some unnecessary creation of variables.

    Here is my modified section of your perl script:
    my $uprate = $server->call(‘get_up_rate’);
    my $downrate = $server->call(‘get_down_rate’);

    my $torrents = $server->call(‘d.multicall’, “active”,
    “d.get_base_filename=”,
    “d.get_bytes_done=”,
    “d.get_size_bytes=”,
    “d.get_down_rate=”);

    my @res;
    foreach my $d (@$torrents) {
    my $dl_rate = $d->[3] / 1024;
    my $percent_done = 100 * ($d->[1] / $d->[2]);
    push @res,
    sprintf(“%.44s \${alignr}%6.1f kB/s\$alignr\n” .
    ” ” x 5 . “%3.2f%%” . “\${goto 70}\${execbar echo %3.0f }”,
    $d->[0], $dl_rate, $percent_done, $percent_done);
    }

    printf(“%6.1f kB/s up / %6.1f kB/s down\n”, $uprate / 1024, $downrate / 1024);
    print join(“\n\n”, @res), “\n”;

    So thanks for pointing me in the right direction! I think that I have a small problem where it doesn’t seem to pick up that a new torrent has been activated – not sure about that yet.

    1. Right, I totally forgot about that ! I think Frontier::Client could use an update !

      You need to edit Frontier::RPC2 and add the i8 data type like this :

      %scalars = (
          'base64' => 1,
          'boolean' => 1,
          'dateTime.iso8601' => 1,
          'double' => 1,
          'int' => 1,
          'i4' => 1,
          'string' => 1,
          'i8' => 1,
      );
      

      Stephane

  2. # Calander hack

    ${execpi 3600 cal –color=always| sed ‘1s/\([^0-9]*\)\(.*\)/${color 8ba9c2}${font Argos:size=20}\1${font Argos:size=15}\2${font}/; 2s/$/${color}/; s/\x1b\[7m\([0-9]\+\).*m/${color e84448}\1${color cc9966}/’}

Comments are closed.