pax_global_header 0000666 0000000 0000000 00000000064 13507732167 0014525 g ustar 00root root 0000000 0000000 52 comment=6b63fe4304e3517ec36c20a0a7a420d992edaeb2
woob-6b63fe4304e3517ec36c20a0a7a420d992edaeb2-contrib-munin/ 0000775 0000000 0000000 00000000000 13507732167 0022313 5 ustar 00root root 0000000 0000000 woob-6b63fe4304e3517ec36c20a0a7a420d992edaeb2-contrib-munin/contrib/ 0000775 0000000 0000000 00000000000 13507732167 0023753 5 ustar 00root root 0000000 0000000 woob-6b63fe4304e3517ec36c20a0a7a420d992edaeb2-contrib-munin/contrib/munin/ 0000775 0000000 0000000 00000000000 13507732167 0025101 5 ustar 00root root 0000000 0000000 woob-6b63fe4304e3517ec36c20a0a7a420d992edaeb2-contrib-munin/contrib/munin/boobank-munin 0000775 0000000 0000000 00000020260 13507732167 0027566 0 ustar 00root root 0000000 0000000 #!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ft=python et softtabstop=4 cinoptions=4 shiftwidth=4 ts=4 ai
# Copyright(C) 2010-2011 Romain Bignon
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with weboob. If not, see .
from __future__ import print_function
import os
import sys
import locale
import time
import logging
from weboob.core import Weboob, CallErrors
from weboob.capabilities.bank import CapBank
from weboob.exceptions import BrowserIncorrectPassword
class BoobankMuninPlugin(object):
def __init__(self):
if 'weboob_path' in os.environ:
self.weboob = Weboob(os.environ['weboob_path'])
else:
self.weboob = Weboob()
self.monitored_accounts = None
if 'boobank_monitored' in os.environ:
self.monitored_accounts = os.environ['boobank_monitored'].split(' ')
self.cache_expire = long(os.environ.get('boobank_cache_expire', 3600))
self.add_coming = int(os.environ.get('boobank_add_coming', 1))
self.cumulate = int(os.environ.get('boobank_cumulate', 1))
self.cache = None
def display_help(self):
print('boobank-munin is a plugin for munin')
print('')
print('Copyright(C) 2010-2011 Romain Bignon')
print('')
print('To use it, create a symlink /etc/munin/plugins/boobank to this script')
print('and add this section in /etc/munin/plugin-conf.d/munin-node:')
print('')
print('[boobank]')
print('user romain')
print('group romain')
print('env.HOME /home/romain')
print('# The weboob directory path.')
print('env.weboob_path /home/romain/.config/weboob/')
print('# Monitored accounts. If this parameter is missing, all accounts')
print('# will be displayed.')
print('env.boobank_monitored 0125XXXXXXXXXXXX@bnporc 0125XXXXXXXXXXXX@bnporc')
print('# To prevent mass connections to bank websites, results are cached.')
print('# You can set here the expiration delay (in seconds).')
print('env.boobank_cache_expire 7200')
print('# If enabled, coming operations are added to the value of accounts\'')
print('# balance.')
print('env.boobank_add_coming 1')
print('# Cumulate accounts values')
print('env.boobank_cumulate 1')
print('')
print('When you change configuration, you can use this command to reset cache:')
print('$ boobank-munin --reset')
def clear_cache(self):
for name in ('boobank-munin', 'boobank-munin-config'):
try:
os.unlink(self.cachepath(name))
except IOError:
pass
def cachepath(self, name):
tmpdir = os.path.join(self.weboob.workdir, "munin")
if not os.path.isdir(tmpdir):
os.makedirs(tmpdir)
return os.path.join(tmpdir, name)
def check_cache(self, name):
return self.print_cache(name, check=True)
def print_cache(self, name, check=False):
try:
f = open(self.cachepath(name), 'r')
except IOError:
return False
try:
last = int(f.readline().strip())
except ValueError:
return False
if check and (last + self.cache_expire) < time.time():
return False
for line in f:
sys.stdout.write(line)
return True
def new_cache(self, name):
os.umask(0o077)
new_name = '%s.new' % name
filename = self.cachepath(new_name)
try:
f = open(filename, 'w')
except IOError as e:
print('Unable to create the cache file %s: %s' % (filename, e), file=sys.stderr)
return
self.cache = f
self.cache.write('%d\n' % time.time())
def flush_cache(self):
old_name = self.cache.name
new_name = self.cache.name[:-4]
self.cache.close()
os.rename(old_name, new_name)
def write_output(self, line):
sys.stdout.write('%s\n' % line)
if self.cache:
self.cache.write('%s\n' % line)
def config(self):
if self.check_cache('boobank-munin-config'):
return
self.new_cache('boobank-munin-config')
self.weboob.load_backends(CapBank)
self.write_output('graph_title Bank accounts')
self.write_output('graph_vlabel balance')
self.write_output('graph_category weboob')
self.write_output('graph_args -l 0')
try:
accounts = []
if self.monitored_accounts is not None:
d = {}
for account in self.weboob.do('iter_accounts'):
if self.monitored(account):
d['%s@%s' % (account.id, account.backend)] = account
for id in self.monitored_accounts:
try:
accounts.append(d[id])
except KeyError:
pass
else:
accounts = reversed([a for a in self.weboob.do('iter_accounts')])
first = True
for account in accounts:
id = self.account2id(account)
type = 'STACK'
if first:
type = 'AREA'
first = False
self.write_output('%s.label %s' % (id, account.label.encode('iso-8859-15')))
if self.cumulate:
self.write_output('%s.draw %s' % (id, type))
except CallErrors as errors:
self.print_errors(errors)
self.print_cache('boobank-munin-config')
else:
self.flush_cache()
def monitored(self, account):
return not self.monitored_accounts or ('%s@%s' % (account.id, account.backend)) in self.monitored_accounts
def account2id(self, account):
return '%s_%s' % (account.backend, account.id)
def print_errors(self, errors):
for backend, err, backtrace in errors:
print((u'%s(%s): %s' % (type(err).__name__, backend.name, err)).encode(sys.stdout.encoding or locale.getpreferredencoding(), 'replace'), file=sys.stderr)
if isinstance(err, BrowserIncorrectPassword):
self.weboob.backends_config.edit_backend(backend.name, backend.NAME, {'_enabled': 'false'})
def execute(self):
if self.check_cache('boobank-munin'):
return
self.new_cache('boobank-munin')
self.weboob.load_backends(CapBank)
try:
for account in self.weboob.do('iter_accounts'):
if self.monitored(account):
balance = account.balance
if account.coming and self.add_coming:
balance += account.coming
self.write_output('%s.value %d' % (self.account2id(account), balance))
except CallErrors as errors:
self.print_errors(errors)
self.print_cache('boobank-munin')
else:
self.flush_cache()
def run(self):
cmd = (len(sys.argv) > 1 and sys.argv[1]) or "execute"
if cmd == 'execute':
self.execute()
elif cmd == 'config':
self.config()
elif cmd == 'autoconf':
print('no')
sys.exit(1)
elif cmd == 'suggest':
sys.exit(1)
elif cmd == 'help' or cmd == '-h' or cmd == '--help':
self.display_help()
elif cmd == 'reload' or cmd == '--reload' or \
cmd == 'reset' or cmd == '--reset':
self.clear_cache()
if self.cache:
self.cache.close()
sys.exit(0)
if __name__ == '__main__':
logging.basicConfig()
BoobankMuninPlugin().run()
woob-6b63fe4304e3517ec36c20a0a7a420d992edaeb2-contrib-munin/contrib/munin/freemobile-munin 0000775 0000000 0000000 00000013515 13507732167 0030271 0 ustar 00root root 0000000 0000000 #!/usr/bin/env perl
# -*- perl -*-
=head1 NAME
freemobile - A plugin to monitor a freemobile subscription
=head1 INSTALLATION
Create a link to this script in /etc/munin/plugins/ :
$ ln -s /path/to/freemobile-munin /etc/munin/plugins/freemobile
=head1 CONFIGURATION
You need to configure the plugin like that:
[freemobile]
user florent
env.HOME /home/florent
env.phonenumber 06ABCDEFGH
env.freemonitored voice sms mms data specialvoice
env.cache_expire 3600
timeout 30
C I: user with freemobile backend configured
C I: path to user home
C (optional): add your phone number if you have more than one
subscription for this account.
C (optional): default only 'voice sms'
The full list of monitored options is :
* voice sms mms data specialvoice voicetoint voiceint smsint mmsint dataint
C (optional): cache interval in second, or time
between two connection to the website. The cache interval is 3 hours by default.
C (optional): Munin internal option. The plugin can be slow,
30s is recommended.
=head1 LICENSE
LGPLv3
=cut
use strict;
use warnings;
use Carp;
use English qw(-no_match_vars);
#use encoding 'iso-8859-15'; # Munin doesn't like utf-8 :-(
use Encode;
my @monitored = split / /, $ENV{'freemonitored'} || 'voice sms';
my $cachedir = $ENV{'HOME'} . '/.config/weboob/munin/';
my $cachefile = "$cachedir/freemobile-munin";
my $refreshtime = $ENV{'cache_expire'} || 10_800;
my $phone = $ENV{'phonenumber'};
my $account = '';
if (length($phone) > 0) {
$account = $phone . '@freemobile';
}
my $weboob = 'boobill -f table -b freemobile details ' . $account;
my $cache_fh;
my %label = (
'voice' => 'Voix en France (min)',
'voicetoint' => 'Voix vers l\'international (min)',
'specialvoice' => 'Numéros spéciaux (min)',
'sms' => 'SMS en France',
'mms' => 'MMS en France',
'data' => 'Data en France',
'voiceint' => 'Voix à l\'international (min)',
'smsint' => 'SMS à l\'international',
'mmsint' => 'MMS à l\'international',
'dataint' => 'Data à l\'international',
);
my %linenum = (
'voice' => 3,
'voicetoint' => 3,
'specialvoice' => 4,
'sms' => 5,
'mms' => 6,
'data' => 7,
'voiceint' => 8,
'smsint' => 9,
'mmsint' => 10,
'dataint' => 11,
);
my %regexp = (
'voice' => 'National : (\d+)h(\d+)min(\d+)s',
'voicetoint' => 'International : (\d+)h(\d+)min(\d+)s',
'specialvoice' => '\| (\d+)h(\d+) min (\d+)s',
'sms' => 'Conso SMS \s+ \| (\d+) \/ (.*)',
'mms' => 'Vous avez consommé (\d+) MMS',
'data' => 'Vous avez consommé ([\d\-\.]+) (Mo|Go)',
'voiceint' => 'Appels émis (\d+)h(\d+)min(\d+)s',
'smsint' => 'Conso SMS (international) \| (\d+)',
'mmsint' => 'Vous avez consommé (\d+) MMS',
'dataint' => 'Vous avez consommé ([\d\-\.]+) (Mo|Go)',
);
my %post = (
'voice' => 'postvoice',
'voicetoint' => 'postvoice',
'specialvoice' => 'postvoice',
'sms' => 'simplepost',
'mms' => 'simplepost',
'data' => 'datapost',
'voiceint' => 'postvoice',
'smsint' => 'simplepost',
'mmsint' => 'simplepost',
'dataint' => 'datapost',
);
sub doubleprint {
my $var = shift;
print {$cache_fh} $var;
print $var;
return 0;
}
sub postvoice {
my @args = @_;
my $minutes = $args[0] * 60 + $args[1] + $args[2] / 60;
doubleprint "$minutes \n";
return 0;
}
sub simplepost {
my @args = @_;
doubleprint "$args[0] \n";
return 0;
}
sub datapost {
my @args = @_;
my $multi = 1;
my $unit = $args[1];
if ($unit eq "Go") {
$multi = 1024;
}
$multi = $args[0] * $multi;
doubleprint "$multi \n";
return 0;
}
sub config {
binmode STDOUT, ':encoding(iso-8859-1)';
print <<'EOF';
graph_title Conso Free
graph_vlabel Suivi conso du forfait Free Mobile
graph_category weboob
graph_args -l 0
EOF
foreach (@monitored) {
print "$_.label $label{$_}\n";
}
return 0;
}
sub fetch {
my @cache_data;
# Check if cache exist and not older than the refresh threshold.
if ( open $cache_fh, '<', $cachefile ) {
@cache_data = <$cache_fh>;
close $cache_fh or croak "unable to close: $ERRNO";
# File not empty?
if ( @cache_data > 0 ) {
# Data is still fresh. Display cached values and exit.
if ( time - $cache_data[0] < $refreshtime ) {
print join q{}, @cache_data[ 1 .. $#cache_data ];
exit 0;
}
}
}
# execute weboob
open my $data, q(-|), $weboob or croak "Couldn't execute program: $ERRNO";
my @lines = <$data>;
close $data or carp "unable to close: $ERRNO";
# If error output, print the cache (if exist) and exit
if ( @lines == 0 ) {
if ( @cache_data > 0 ) {
print join q{}, @cache_data[ 1 .. $#cache_data ];
exit 0;
}
exit 1;
}
# Open cache for writing
open $cache_fh, '>', $cachefile
or croak "Failed to open file $cachefile";
print {$cache_fh} time . "\n";
foreach my $monit (@monitored) {
doubleprint "$monit.value ";
if ( my @results = $lines[ $linenum{$monit} ] =~ /$regexp{$monit}/ ) {
my $postfunction = $post{$monit};
&{ \&$postfunction }(@results);
}
else {
doubleprint "0 \n";
}
}
close $cache_fh or croak "unable to close: $ERRNO";
return 0;
}
# Create the munin cache dir if missing
if ( !-d $cachedir ) {
mkdir $cachedir;
}
if ( $ARGV[0] and $ARGV[0] eq 'config' ) {
config;
}
else {
fetch;
}
__END__
woob-6b63fe4304e3517ec36c20a0a7a420d992edaeb2-contrib-munin/contrib/munin/nettokom-munin 0000775 0000000 0000000 00000011732 13507732167 0030017 0 ustar 00root root 0000000 0000000 #!/usr/bin/env perl
# -*- perl -*-
=head1 NAME
nettokom - A plugin to monitor a nettokom subscription
=head1 INSTALLATION
Create a link to this script in /etc/munin/plugins/ :
$ ln -s /path/to/nettokom-munin /etc/munin/plugins/nettokom
=head1 CONFIGURATION
You need to configure the plugin like that:
[nettokom]
user florent
env.HOME /home/florent
env.monitored 0177-XXXXXXXX
env.exclude 02
env.cache_expire 10800
timeout 30
C I: user with nettokom backend configured
C I: path to user home
C I: phone number to generated the weboob ID
C (optional): default nothing
We can exclude some boobill output. The filter is on the label.
Some examples:
* festnetz community sms smsinsausland O2
C (optional): cache interval in second, or time
between two connection to the website. The cache interval is 3 hours by default.
Be aware that the website is only daily updated
C (optional): Munin internal option. The plugin can be slow,
30s is recommended.
=head1 LICENSE
LGPLv3
=cut
use strict;
use warnings;
use Carp;
use English qw(-no_match_vars);
use encoding 'iso-8859-1'; # Munin doesn't like utf-8 :-(
use Encode;
my $id = $ENV{'monitored'} || '';
if ($id eq '') {
print STDERR "Error: env.monitored not exist \n";
exit 2;
}
$id = $id . '@nettokom';
my @exclude = split / /, $ENV{'exclude'} || '';
my $cachedir = $ENV{'HOME'} . '/.config/weboob/munin/';
my $cachefile = "$cachedir/nettokom-munin";
my $cacheconfigfile = "$cachedir/nettokom-munin-config";
my $refreshtime = $ENV{'cache_expire'} || 10800;
my $weboob = "/usr/bin/env boobill details $id -f table";
my @knowlabels = ("AbgehendeSMSimAusland", "AnkommendeGespr_cheimAusland",
"Community", "D1", "D2", "Deutschland", "Festnetz",
"FreeCall", "Mailbox", "O2", "SMS", "SMSinsAusland");
my $cache_fh;
sub config {
execute($cacheconfigfile, 1);
}
sub fetch {
execute($cachefile);
}
sub doubleprint {
my $var = shift;
print {$cache_fh} $var;
print $var;
return 0;
}
sub printconfig {
my @lines = @_;
doubleprint <<'EOF';
graph_title Conso Nettokom
graph_vlabel Nettokom Verbindungs
graph_category weboob
graph_args -l 0
EOF
my $first = 1;
for my $label (@knowlabels) {
my $shortlabel = $label;
if (!(grep {$_ == $shortlabel} @exclude)) {
doubleprint "$shortlabel.label $label\n";
if ($first > 0) {
doubleprint "$shortlabel.draw AREA\n";
$first = 0;
}
else {
doubleprint "$shortlabel.draw STACK\n";
}
}
}
for my $line (@lines) {
if ($line =~ /nettokom \| (.*) \| (\d)(.*) \| (\d).(\d)/) {
my $label = $1;
my $shortlabel = $label;
$shortlabel =~ s/\s+//g;
if (!(grep {$_ == $shortlabel} @exclude)) {
if (!(grep {$_ == $shortlabel} @knowlabels)) {
doubleprint "$shortlabel.label $label\n";
doubleprint "$shortlabel.draw STACK\n";
}
}
}
}
}
sub printvalue {
my @lines = @_;
for my $line (@lines) {
if ($line =~ /nettokom \| (.*) \| (\d)(.*) \| (\d).(\d)/) {
my $label = $1;
my $value = $2;
my $shortlabel = $label;
$shortlabel =~ s/\s+//g;
if (!(grep {$_ == $shortlabel} @exclude)) {
doubleprint "$shortlabel.value $value\n";
}
}
}
}
sub execute {
my @cache_data;
my $cachefile = $_[0];
my $doconfig = $_[1];
# Check if cache exist and not older than the refresh threshold.
if ( open $cache_fh, '<', $cachefile ) {
@cache_data = <$cache_fh>;
close $cache_fh or croak "unable to close: $ERRNO";
# File not empty?
if ( @cache_data > 0 ) {
# Data is still fresh. Display cached values and exit.
if ( time - $cache_data[0] < $refreshtime ) {
print join q{}, @cache_data[ 1 .. $#cache_data ];
exit 0;
}
}
}
# execute weboob
open my $data, q(-|), $weboob or croak "Couldn't execute program: $ERRNO";
my @lines = <$data>;
close $data or carp "unable to close: $ERRNO";
# If error output, print the cache (if exist) and exit
if ( @lines == 0 ) {
if ( @cache_data > 0 ) {
print join q{}, @cache_data[ 1 .. $#cache_data ];
exit 0;
}
exit 1;
}
# Open cache for writing
open $cache_fh, '>', $cachefile
or croak "Failed to open file $cachefile";
print {$cache_fh} time . "\n";
if ($doconfig) {
printconfig(@lines);
}
else {
printvalue(@lines);
}
}
# Create the munin cache dir if missing
if ( !-d $cachedir ) {
mkdir $cachedir;
}
if ( $ARGV[0] and $ARGV[0] eq 'config' ) {
config;
}
else {
fetch;
}
__END__
woob-6b63fe4304e3517ec36c20a0a7a420d992edaeb2-contrib-munin/contrib/munin/weboob-generic 0000775 0000000 0000000 00000035640 13507732167 0027726 0 ustar 00root root 0000000 0000000 #!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ft=python et softtabstop=4 cinoptions=4 shiftwidth=4 ts=4 ai
# Copyright(C) 2013 Romain Bignon, Florent Fourcot
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see .
### Installation ###
# 1) Create a symlink from /etc/munin/plugins/yourchoice to the script
# 2) Configure the plugin in /etc/munin/plugin-conf.d/ See below for the options
# 3) Restart/reload munin-node
# 4) Note that cached values are stored in folder ~/.config/weboob/munin/
### Configuration ###
## Mandatory options ##
# env.capa: The Weboob capability to load
# Example: env.capa CapBank
#
# env.do: The Weboob command to call. It can take more than one argument.
# With two argument, the second is used as parameter for the command.
# The third is used to restrict backends.
# Example: env.do get_balance
#
# env.import: The import line to import the capabilities
# Example: from weboob.capabilities.bank import CapBank
#
# env.attribvalue: The attribut name of objects returned by the do command.
# For example, the "balance" member of Account objects
# If the attribut is itself one object, a hierarchical call can be done
# with the "/" operators.
# Example: env.attribvalue balance
# Example: env.attribvalue temp/value
## Optionals -- more configuration ##
# env.id_monitored: Restrict the results to a list of ids (space is used as separator)
# Example: env.id_monitored account1@backend1 account2@backend2
#
# env.exclude: Exclude some results (space is used as separator)
# Example: env.exclude 550810@sachsen
#
# env.cache_expire: To avoid site flooding, results are cached in folder
# /.config/weboob/munin/. The default lifetime of a cache value is 3600s
# Example: env.cache_expire 7200
#
# env.cumulate: Display data in Area mode (default) or in line mode.
# Example: env.cumulate 0
#
# env.get_object_list: optional pre-call to get a list of objects, to applied
# the do function on it.
# Exemple: env.get_object_list="iter_subscriptions"
#
# env.attribid: Munin needs an id for each value. The default is to use the id of results,
# but another attribute can be used. "/" can be used as separator for
# hierarchical calls
# Example: env.attribid id
#
# env.title: A title for the graph (default: nothing)
# Example: env.title a wonderful graph
#
# env.vlabel: A vertical label for the graph
# Example: env.vlabel Balance
#
# env.label: Each data in munin as a label. Per default, the script takes the
# "label" attribute of objects. However, it does not always exist,
# and a better choice can be possible
# Example: env.label id
#
# env.category: set the graph category (default: weboob)
# Example: env.category bank
# For some running examples, see at the end of the script
from __future__ import print_function
import os
import sys
import locale
import time
import logging
from weboob.capabilities.base import NotAvailable
from weboob.core import Weboob, CallErrors
from weboob.exceptions import BrowserIncorrectPassword
class GenericMuninPlugin(object):
def __init__(self):
if 'weboob_path' in os.environ:
self.weboob = Weboob(os.environ['weboob_path'])
else:
self.weboob = Weboob()
self.cache_expire = long(os.environ.get('cache_expire', 3600))
self.cumulate = int(os.environ.get('cumulate', 1))
self.cache = None
self.name = sys.argv[0]
if "/" in self.name:
self.name = self.name.split('/')[-1]
# Capability to load
self.capa = os.environ['capa']
# Command to pass to Weboob
self.do = os.environ['do'].split(',')
# Not easy to load modules automatically...
self.mimport = os.environ["import"]
exec(self.mimport)
# We can monitore only some objects
self.object_list = None
if 'get_object_list' in os.environ:
self.object_list = os.environ["get_object_list"]
self.tomonitore = None
if 'id_monitored' in os.environ:
self.tomonitore = os.environ['id_monitored'].decode('utf-8').split(' ')
self.exclude = None
if 'exclude' in os.environ:
self.exclude = os.environ['exclude'].split(' ')
# Attribut of object to use as ID (default: id)
self.attribid = "id"
if 'attribid' in os.environ:
self.attribid = os.environ['attribid']
self.attribvalue = os.environ['attribvalue']
self.title = ''
if 'title' in os.environ:
self.title = os.environ['title'].decode('utf-8')
self.attriblabel = "label"
if 'label' in os.environ:
self.attriblabel = os.environ['label']
self.vlabel = self.attribvalue
if 'vlabel' in os.environ:
self.vlabel = os.environ['vlabel'].decode('utf-8')
self.category = "weboob"
if 'category' in os.environ:
self.category = os.environ['category'].decode('utf-8')
def display_help(self):
print('generic-munin is a plugin for munin')
print('')
print('Copyright(C) 2013 Romain Bignon, Florent Fourcot')
print('')
print('To use it, create a symlink /etc/munin/plugins/nameyouwant to this script')
print('and add this section in /etc/munin/plugin-conf.d/munin-node:')
print('')
print('[nameyouwant]')
print('user romain')
print('group romain')
print('env.HOME /home/romain')
print('# The weboob directory path.')
print('env.weboob_path /home/romain/.config/weboob/')
print('# Monitored objects. If this parameter is missing, all objects')
print('# will be displayed.')
print('env.id_monitored myid@backend1 otherid@backend2')
print('# To prevent mass connections to websites, results are cached.')
print('# You can set here the expiration delay (in seconds).')
print('env.cache_expire 7200')
print('# Cumulate values')
print('env.cumulate 1')
print('')
def cachepath(self, name):
tmpdir = os.path.join(self.weboob.workdir, "munin")
if not os.path.isdir(tmpdir):
os.makedirs(tmpdir)
return os.path.join(tmpdir, name)
def check_cache(self, name):
return self.print_cache(name, check=True)
def print_cache(self, name, check=False):
try:
f = open(self.cachepath(name), 'r')
except IOError:
return False
try:
last = int(f.readline().strip())
except ValueError:
return False
if check and (last + self.cache_expire) < time.time():
return False
for line in f:
sys.stdout.write(line)
return True
def new_cache(self, name):
os.umask(0o077)
new_name = '%s.new' % name
filename = self.cachepath(new_name)
try:
f = open(filename, 'w')
except IOError as e:
print('Unable to create the cache file %s: %s' % (filename, e), file=sys.stderr)
return
self.cache = f
self.cache.write('%d\n' % time.time())
def flush_cache(self):
old_name = self.cache.name
new_name = self.cache.name[:-4]
self.cache.close()
os.rename(old_name, new_name)
def write_output(self, line):
sys.stdout.write('%s\n' % line)
if self.cache:
self.cache.write('%s\n' % line)
def build_do(self):
if self.object_list:
results = []
for result in self.weboob.do(self.object_list):
results.append(result)
for result in results:
try:
for i in self.weboob.do(self.do[0], result.id, backends=result.backend):
yield i
# Do not crash if one module does not implement the feature
except CallErrors:
pass
elif len(self.do) == 1:
for i in self.weboob.do(self.do[0]):
yield i
elif len(self.do) == 2:
for i in self.weboob.do(self.do[0], self.do[1]):
yield i
elif len(self.do) == 3:
for i in self.weboob.do(self.do[0], self.do[1], backends=self.do[2]):
yield i
def get_value(self, result):
attribs = self.attribvalue.split('/')
for attrib in attribs:
result = getattr(result, attrib)
if type(result) is list:
result = result[0]
return result
def monitored(self, result):
id = self.result2weboobid(result)
if self.exclude and id in self.exclude:
return False
return not self.tomonitore or id in self.tomonitore
def result2weboobid(self, result):
attribs = self.attribid.split('/')
id = '%s@%s' % (getattr(result, attribs[0]), result.backend)
return id
def result2id(self, result):
attribs = self.attribid.split('/')
id = result
for attrib in attribs:
id = getattr(id, attrib)
return '%s_%s' % (result.backend, id)
def config(self):
if self.check_cache('%s-config' % self.name):
return
self.new_cache('%s-config' % self.name)
self.weboob.load_backends(self.capa)
self.write_output('graph_title %s' % self.title.encode('iso-8859-15'))
self.write_output('graph_vlabel %s' % self.vlabel.encode('iso-8859-15'))
self.write_output('graph_category %s' % self.category)
self.write_output('graph_args --rigid')
if self.cumulate:
self.write_output('graph_total Total')
try:
objects = []
if self.tomonitore or self.exclude:
d = {}
for result in self.build_do():
if self.monitored(result):
d[self.result2weboobid(result)] = result
if self.tomonitore:
for id in self.tomonitore:
try:
objects.append(d[id])
except KeyError:
pass
else:
for id in d:
objects.append(d[id])
else:
objects = reversed([a for a in self.build_do()])
first = True
for result in objects:
id = self.result2id(result)
type = 'STACK'
if first:
type = 'AREA'
first = False
self.write_output('%s.label %s' % (id.encode('iso-8859-15'), getattr(result, self.attriblabel).encode('iso-8859-15')))
if self.cumulate:
self.write_output('%s.draw %s' % (id, type))
except CallErrors as errors:
self.print_errors(errors)
self.print_cache('%s-config' % self.name)
else:
self.flush_cache()
def print_errors(self, errors):
for backend, err, backtrace in errors:
print((u'%s(%s): %s' % (type(err).__name__, backend.name, err)).encode(sys.stdout.encoding or locale.getpreferredencoding(), 'replace'), file=sys.stderr)
if isinstance(err, BrowserIncorrectPassword):
self.weboob.backends_config.edit_backend(backend.name, backend.NAME, {'_enabled': 'false'})
def execute(self):
if self.check_cache(self.name):
return
self.new_cache(self.name)
self.weboob.load_backends(self.capa)
try:
for result in self.build_do():
if self.monitored(result):
value = self.get_value(result)
if value is not NotAvailable:
self.write_output('%s.value %f' % (self.result2id(result).encode('iso-8859-15'), value))
except CallErrors as errors:
self.print_errors(errors)
self.print_cache(self.name)
else:
self.flush_cache()
def run(self):
cmd = (len(sys.argv) > 1 and sys.argv[1]) or "execute"
if cmd == 'execute':
self.execute()
elif cmd == 'config':
self.config()
elif cmd == 'autoconf':
print('no')
sys.exit(1)
elif cmd == 'suggest':
sys.exit(1)
elif cmd == 'help' or cmd == '-h' or cmd == '--help':
self.display_help()
if self.cache:
self.cache.close()
sys.exit(0)
if __name__ == '__main__':
logging.basicConfig()
GenericMuninPlugin().run()
### Examples ###
## Like boobank-munin does
## Only for the example, you should use boobank-munin instead
#[bank]
#user florent
#group florent
#env.cache_expire 7200
#env.HOME /home/flo
#env.capa CapBank
#env.do iter_accounts
#env.import from weboob.capabilities.bank import CapBank
#env.attribvalue balance
#env.title Solde des comptes
#
#
## Balance of your leclercmobile subscription
#[leclercmobile]
#user florent
#group florent
#env.cache_expire 16800
#env.HOME /home/flo
#env.capa CapBill
#env.do get_balance,06XXXXXXXX,leclercmobile
#env.import from weboob.capabilities.bill import CapBill
#env.attribvalue price
#env.title Forfait leclercmobile
#env.vlabel Solde
#
#Result: http://fourcot.fr/weboob/leclercmobile-day.png
#
## Monitor water level in Dresden
#[leveldresden]
#user florent
#group florent
#env.cache_expire 7200
#env.HOME /home/flo
#env.capa CapGauge
#env.do get_last_measure,501060-level
#env.import from weboob.capabilities.gauge import CapGauge
#env.attribvalue level
#env.title Niveau de l'elbe
#env.label id
#
#
## The level of the elbe in all Sachsen's cities
#[levelelbesachsen]
#user florent
#env.cache_expire 800
#env.HOME /home/flo
#env.cumulate 0
#env.capa CapGauge
#env.do iter_gauges,Elbe,sachsen
#env.import from weboob.capabilities.gauge import CapGauge
#env.attribvalue sensors/lastvalue/level
#env.title Niveau de l'elbe en Saxe
#env.label name
#env.vlabel Hauteur du fleuve (cm)
#env.exclude 550810@sachsen
#
#Result: http://fourcot.fr/weboob/elbesachsen-day.png
#
## Temperature in Rennes
#[temprennes]
#user florent
#env.HOME /home/flo
#env.cumulate 0
#env.capa CapWeather
#env.do get_current,619163,yahoo
#env.import from weboob.capabilities.weather import CapWeather
#env.attribvalue temp/value
#env.attribid temp/id
#env.title Température à Rennes
#env.vlabel Température
#env.label id
#
#Result: http://fourcot.fr/weboob/temprennes-day.png