save cache in .weboob/munin/, handle errors and print old cache if any
This commit is contained in:
parent
9d7e1cd260
commit
a475455ddc
1 changed files with 44 additions and 22 deletions
|
|
@ -20,13 +20,15 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import tempfile
|
from weboob.core import Weboob, CallErrors
|
||||||
from weboob.core import Weboob
|
|
||||||
from weboob.capabilities.bank import ICapBank
|
from weboob.capabilities.bank import ICapBank
|
||||||
|
|
||||||
class BoobankMuninPlugin(object):
|
class BoobankMuninPlugin(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.weboob = Weboob()
|
if 'weboob_path' in os.environ:
|
||||||
|
self.weboob = Weboob(os.environ['weboob_path'])
|
||||||
|
else:
|
||||||
|
self.weboob = Weboob()
|
||||||
self.monitored_accounts = None
|
self.monitored_accounts = None
|
||||||
if 'boobank_monitored' in os.environ:
|
if 'boobank_monitored' in os.environ:
|
||||||
self.monitored_accounts = os.environ['boobank_monitored'].split(' ')
|
self.monitored_accounts = os.environ['boobank_monitored'].split(' ')
|
||||||
|
|
@ -45,8 +47,8 @@ class BoobankMuninPlugin(object):
|
||||||
print '[boobank]'
|
print '[boobank]'
|
||||||
print 'user romain'
|
print 'user romain'
|
||||||
print 'group romain'
|
print 'group romain'
|
||||||
print '# Useful for weboob to find its config files.'
|
print '# The weboob directory path.'
|
||||||
print 'env.HOME /home/romain/'
|
print 'env.weboob_path /home/romain/.weboob/'
|
||||||
print '# Monitored accounts. If this parameter is missing, all accounts'
|
print '# Monitored accounts. If this parameter is missing, all accounts'
|
||||||
print '# will be displayed.'
|
print '# will be displayed.'
|
||||||
print 'env.boobank_monitored 0125XXXXXXXXXXXX@bnporc 0125XXXXXXXXXXXX@bnporc'
|
print 'env.boobank_monitored 0125XXXXXXXXXXXX@bnporc 0125XXXXXXXXXXXX@bnporc'
|
||||||
|
|
@ -63,20 +65,23 @@ class BoobankMuninPlugin(object):
|
||||||
def clear_cache(self):
|
def clear_cache(self):
|
||||||
for name in ('boobank-munin', 'boobank-munin-config'):
|
for name in ('boobank-munin', 'boobank-munin-config'):
|
||||||
try:
|
try:
|
||||||
os.unlink(self.temppath(name))
|
os.unlink(self.cachepath(name))
|
||||||
except IOError:
|
except IOError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def temppath(self, name):
|
def cachepath(self, name):
|
||||||
tmpdir = os.path.join(tempfile.gettempdir(), "weboob")
|
tmpdir = os.path.join(self.weboob.workdir, "munin")
|
||||||
if not os.path.isdir(tmpdir):
|
if not os.path.isdir(tmpdir):
|
||||||
os.makedirs(tmpdir)
|
os.makedirs(tmpdir)
|
||||||
|
|
||||||
return os.path.join(tmpdir, name)
|
return os.path.join(tmpdir, name)
|
||||||
|
|
||||||
def check_cache(self, name):
|
def check_cache(self, name):
|
||||||
|
return self.print_cache(name, check=True)
|
||||||
|
|
||||||
|
def print_cache(self, name, check=False):
|
||||||
try:
|
try:
|
||||||
f = open(self.temppath(name), 'r')
|
f = open(self.cachepath(name), 'r')
|
||||||
except IOError:
|
except IOError:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
@ -85,7 +90,7 @@ class BoobankMuninPlugin(object):
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if (last + self.cache_expire) < time.time():
|
if check and (last + self.cache_expire) < time.time():
|
||||||
return False
|
return False
|
||||||
|
|
||||||
for line in f.xreadlines():
|
for line in f.xreadlines():
|
||||||
|
|
@ -94,7 +99,8 @@ class BoobankMuninPlugin(object):
|
||||||
|
|
||||||
def new_cache(self, name):
|
def new_cache(self, name):
|
||||||
os.umask(0077)
|
os.umask(0077)
|
||||||
filename = self.temppath(name)
|
new_name = '%s.new' % name
|
||||||
|
filename = self.cachepath(new_name)
|
||||||
try:
|
try:
|
||||||
f = open(filename, 'w')
|
f = open(filename, 'w')
|
||||||
except IOError, e:
|
except IOError, e:
|
||||||
|
|
@ -104,6 +110,12 @@ class BoobankMuninPlugin(object):
|
||||||
self.cache = f
|
self.cache = f
|
||||||
self.cache.write('%d\n' % time.time())
|
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):
|
def write_output(self, line):
|
||||||
sys.stdout.write('%s\n' % line)
|
sys.stdout.write('%s\n' % line)
|
||||||
if self.cache:
|
if self.cache:
|
||||||
|
|
@ -114,15 +126,20 @@ class BoobankMuninPlugin(object):
|
||||||
return
|
return
|
||||||
|
|
||||||
self.new_cache('boobank-munin-config')
|
self.new_cache('boobank-munin-config')
|
||||||
|
self.weboob.load_backends(ICapBank)
|
||||||
self.write_output('graph_title Bank accounts')
|
self.write_output('graph_title Bank accounts')
|
||||||
self.write_output('graph_vlabel balance')
|
self.write_output('graph_vlabel balance')
|
||||||
self.write_output('graph_category weboob')
|
self.write_output('graph_category weboob')
|
||||||
self.write_output('graph_args -l 0')
|
self.write_output('graph_args -l 0')
|
||||||
self.weboob.load_backends(ICapBank)
|
try:
|
||||||
for backend, account in self.weboob.do('iter_accounts'):
|
for backend, account in self.weboob.do('iter_accounts'):
|
||||||
if self.monitored(account):
|
if self.monitored(account):
|
||||||
id = self.account2id(account)
|
id = self.account2id(account)
|
||||||
self.write_output('%s.label %s' % (id, account.label.encode('iso-8859-15')))
|
self.write_output('%s.label %s' % (id, account.label.encode('iso-8859-15')))
|
||||||
|
except CallErrors:
|
||||||
|
self.print_cache('boobank-munin-config')
|
||||||
|
else:
|
||||||
|
self.flush_cache()
|
||||||
|
|
||||||
def monitored(self, account):
|
def monitored(self, account):
|
||||||
return not self.monitored_accounts or ('%s@%s' % (account.id, account.backend)) in self.monitored_accounts
|
return not self.monitored_accounts or ('%s@%s' % (account.id, account.backend)) in self.monitored_accounts
|
||||||
|
|
@ -136,12 +153,17 @@ class BoobankMuninPlugin(object):
|
||||||
|
|
||||||
self.new_cache('boobank-munin')
|
self.new_cache('boobank-munin')
|
||||||
self.weboob.load_backends(ICapBank)
|
self.weboob.load_backends(ICapBank)
|
||||||
for backend, account in self.weboob.do('iter_accounts'):
|
try:
|
||||||
if self.monitored(account):
|
for backend, account in self.weboob.do('iter_accounts'):
|
||||||
balance = account.balance
|
if self.monitored(account):
|
||||||
if account.coming and self.add_coming:
|
balance = account.balance
|
||||||
balance += account.coming
|
if account.coming and self.add_coming:
|
||||||
self.write_output('%s.value %d' % (self.account2id(account), balance))
|
balance += account.coming
|
||||||
|
self.write_output('%s.value %d' % (self.account2id(account), balance))
|
||||||
|
except CallErrors:
|
||||||
|
self.print_cache('boobank-munin')
|
||||||
|
else:
|
||||||
|
self.flush_cache()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
cmd = (len(sys.argv) > 1 and sys.argv[1]) or "execute"
|
cmd = (len(sys.argv) > 1 and sys.argv[1]) or "execute"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue