weboob-devel/scripts/boobank-munin

165 lines
5.8 KiB
Python
Executable file

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ft=python et softtabstop=4 cinoptions=4 shiftwidth=4 ts=4 ai
# Copyright(C) 2010 Romain Bignon
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import os
import sys
import time
import tempfile
from weboob.core import Weboob
from weboob.capabilities.bank import ICapBank
class BoobankMuninPlugin(object):
def __init__(self):
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.cache = None
def display_help(self):
print 'boobank-munin is a plugin for munin'
print ''
print 'Copyright(C) 2010 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 '# Useful for weboob to find its config files.'
print 'env.HOME /home/romain/'
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 3600'
print '# If enabled, coming operations are added to the value of accounts\''
print '# balance.'
print 'env.boobank_add_coming 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.temppath(name))
except IOError:
pass
def temppath(self, name):
tmpdir = os.path.join(tempfile.gettempdir(), "weboob")
if not os.path.isdir(tmpdir):
os.makedirs(tmpdir)
return os.path.join(tmpdir, name)
def check_cache(self, name):
try:
f = open(self.temppath(name), 'r')
except IOError:
return False
last = int(f.readline().strip())
if (last + self.cache_expire) < time.time():
return False
for line in f.xreadlines():
sys.stdout.write(line)
return True
def new_cache(self, name):
os.umask(0077)
filename = self.temppath(name)
try:
f = open(filename, 'w')
except IOError, e:
print >>sys.stderr, 'Unable to create the cache file %s: %s' % (filename, e)
return
self.cache = f
self.cache.write('%d\n' % time.time())
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.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')
self.weboob.load_backends(ICapBank)
for backend, account in self.weboob.do('iter_accounts'):
if self.monitored(account):
id = self.account2id(account)
self.write_output('%s.label %s' % (id, account.label.encode('iso-8859-15')))
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 execute(self):
if self.check_cache('boobank-munin'):
return
self.new_cache('boobank-munin')
self.weboob.load_backends(ICapBank)
for backend, 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))
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__':
BoobankMuninPlugin().run()