#!/usr/bin/perl -w
#
# Plugin to monior the Wua.la Linux client
# for shared, social storage from http://wua.la.
#
# Author: Dominik Schulz <lkml@ds.gauner.org>
# URL: http://developer.gauner.org/munin/
#
# Configuration:
#
# Set env.wualadir <yourpathtowuala>, e.g. /home/fred/wuala
# and user fred. Please note, that if you don't run this
# plugin as the correct user it won't work, since the
# wualacmd command will only work when running as the correct user.
#
# The Plugin tries to detect if wuala is running and will
# report unknown (U) as values if it is not running.
#
# This is a dual-personality plugin. If the file- or
# softlink-name of this plugin ends in _storage it will report
# the storage values and if it ends in _uptime it will report the
# uptime.
#
# Example Configuration:
#
# [wuala*]
# user fred
# env.wualadir /home/fred/wuala
#
# Parameters:
#
# 	config	(required)
#
my $wualadir = $ENV{wualadir};


if ($ARGV[0] and $ARGV[0] eq "config")
{
	if($0 =~ /.*_uptime/) {
		print "graph_args -l0 --vertical-label %\n";
		print "graph_title Wuala Uptime\n";
		print "graph_category backup\n";
		print "graph_info This graph shows the Wua.la uptime\n";
		print "uptime.label Uptime\n";
		print "uptime.draw LINE2\n";
		print "uptime.info Uptime of the Wua.la Client on this computer.\n";
	} elsif($0 =~ /.*_storage/) {
		print "graph_args --base 1024 -l 0 --vertical-label GB\n";
		print "graph_title Wuala Storage\n";
		print "graph_category backup\n";
		print "graph_info This graph shows several storage related statistics of Wua.la.\n";
		print "limit.label Local Storage Limit\n";
		print "limit.draw LINE2\n";
		print "limit.info Local shared storage.\n";
		print "earned.label Local Earned Storage\n";
		print "earned.draw LINE2\n";
		print "earned.info Sum of local earned storage.\n";
		print "local.label Remote earned storage.\n";
		print "local.draw LINE1\n";
		print "local.info Storage earned on other nodes.\n";
		print "quota.label Sum of available storage.\n";
		print "quota.draw LINE2\n";
		print "quota.info The sum of the available storage.\n";
		print "used.label Used storage.\n";
		print "used.draw LINE2\n";
		print "used.info The amount of free storage.\n";
	}
} else {
	if($0 =~ /.*_uptime/) {
		get_uptime();
	} elsif($0 =~ /.*_storage/) {
		get_storage();
	}
}
# Report the wua.la storage
# values.
sub get_storage
{
	my $limit = U;
	my $earned = U;
	my $localStored = U;
	my $quota = U;
	my $used = U;
	if(is_running()) {
	chdir($wualadir);
	open(WUALA, "$wualadir/wualacmd tradeStats |");
	while(my $line = <WUALA>) {
	  #print "D: ".$line;
	  if ($line =~ /Current limit/) {
	    $line =~ s/Current limit: ([0-9]*.?[0-9]*) GB/$1/i;
	    $limit = trim($line);
	    #print "Current limit: ".$limit."\n";
	  } elsif ($line =~ /Earned storage:/) {
	    $line =~ s/Earned storage: ([0-9]*.?[0-9]*) GB/$1/i;
	    $earned = trim($line);
	    #print "Earned: ".$earned."\n";
	  } elsif ($line =~ /Locally stored data/) {
	    $line =~ s/Locally stored data .*: ([0-9]*.?[0-9]*) GB/$1/i;
	    $localStored = trim($line);
	    #print "Local data: ".$localStored."\n";
	  }
	}
	close(WUALA);
	open(WUALA, "$wualadir/wualacmd showQuota |");
	while(my $line = <WUALA>) {
	  #print "D: ".$line;
	  if($line =~ /Quota:/) {
	    $line =~ s/Quota: ([0-9]*.[0-9]*) GB/$1/i;
	    $quota = trim($line);
	    #print "Quota: ".$quota."\n";
	  } elsif ($line =~ /List use/) {
	    $line =~ s/List use: ([0-9]*.?[0-9]*) GB/$1/i;
	    $used = trim($line);
	    #print "Used: $used\n";
	  }
	}
	close(WUALA);
	}
	print "limit.value $limit\n";
	print "earned.value $earned\n";
	print "local.value $localStored\n";
	print "quota.value $quota\n";
	print "used.value $used\n";
}
# Return the wua.la uptime in percent of day.
sub get_uptime
{
	my $uptime = U;
	if(is_running()) {
	chdir($wualadir);
	open(WUALA, "$wualadir/wualacmd tradeStats |");
	while(my $line = <WUALA>) {
	  #print "D: ".$line;
	  if($line =~ /Average online time/) {
	    $line =~ s/Average online time: ([0-9]*.[0-9]*)%/$1/i;
	    $uptime = trim($line);
	  }
	}
	close(WUALA);
	}
	print "uptime.value $uptime\n";
}
# Determine if wua.la is running.
sub is_running
{
	$output = qx(ps aux | grep loader2.jar);
	if($output =~ /java/) {
		return 1;
	}
}
# Remove whitespaces (inclduing tabs and newlines) from the
# given string.
sub trim
{
	my $string = shift;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	return $string;
}
