use cache with the 'config' command too, and add command 'reset'
This commit is contained in:
parent
b1999641ea
commit
9f109d6228
1 changed files with 69 additions and 33 deletions
|
|
@ -32,6 +32,7 @@ class BoobankMuninPlugin(object):
|
||||||
self.monitored_accounts = os.environ['boobank_monitored'].split(' ')
|
self.monitored_accounts = os.environ['boobank_monitored'].split(' ')
|
||||||
self.cache_expire = long(os.environ.get('boobank_cache_expire', 3600))
|
self.cache_expire = long(os.environ.get('boobank_cache_expire', 3600))
|
||||||
self.add_coming = int(os.environ.get('boobank_add_coming', 1))
|
self.add_coming = int(os.environ.get('boobank_add_coming', 1))
|
||||||
|
self.cache = None
|
||||||
|
|
||||||
def display_help(self):
|
def display_help(self):
|
||||||
print 'boobank-munin is a plugin for munin'
|
print 'boobank-munin is a plugin for munin'
|
||||||
|
|
@ -55,17 +56,69 @@ class BoobankMuninPlugin(object):
|
||||||
print '# If enabled, coming operations are added to the value of accounts\''
|
print '# If enabled, coming operations are added to the value of accounts\''
|
||||||
print '# balance.'
|
print '# balance.'
|
||||||
print 'env.boobank_add_coming 1'
|
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):
|
def config(self):
|
||||||
print 'graph_title Bank accounts'
|
if self.check_cache('boobank-munin-config'):
|
||||||
print 'graph_vlabel balance'
|
return
|
||||||
print 'graph_category weboob'
|
|
||||||
print 'graph_args -l 0'
|
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)
|
self.weboob.load_backends(ICapBank)
|
||||||
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)
|
||||||
print '%s.label %s' % (id, account.label.encode('iso-8859-15'))
|
self.write_output('%s.label %s' % (id, account.label.encode('iso-8859-15')))
|
||||||
|
|
||||||
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
|
||||||
|
|
@ -74,44 +127,20 @@ class BoobankMuninPlugin(object):
|
||||||
return '%s_%s' % (account.backend, account.id)
|
return '%s_%s' % (account.backend, account.id)
|
||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
tmpdir = os.path.join(tempfile.gettempdir(), "weboob")
|
if self.check_cache('boobank-munin'):
|
||||||
if not os.path.isdir(tmpdir):
|
return
|
||||||
os.makedirs(tmpdir)
|
|
||||||
|
|
||||||
filename = os.path.join(tmpdir, 'boobank-munin')
|
|
||||||
|
|
||||||
try:
|
|
||||||
f = open(filename, 'r')
|
|
||||||
except IOError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
last = int(f.readline().strip())
|
|
||||||
if (last + self.cache_expire) > time.time():
|
|
||||||
for line in f.xreadlines():
|
|
||||||
sys.stdout.write(line)
|
|
||||||
return
|
|
||||||
|
|
||||||
os.umask(0077)
|
|
||||||
try:
|
|
||||||
f = open(filename, 'w')
|
|
||||||
f.write('%d\n' % time.time())
|
|
||||||
except IOError, e:
|
|
||||||
print >>sys.stderr, 'Unable to create the cache file %s: %s' % (filename, e)
|
|
||||||
f = None
|
|
||||||
|
|
||||||
|
self.new_cache('boobank-munin')
|
||||||
self.weboob.load_backends(ICapBank)
|
self.weboob.load_backends(ICapBank)
|
||||||
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):
|
||||||
balance = account.balance
|
balance = account.balance
|
||||||
if account.coming and self.add_coming:
|
if account.coming and self.add_coming:
|
||||||
balance += account.coming
|
balance += account.coming
|
||||||
res = '%s.value %d' % (self.account2id(account), balance)
|
self.write_output('%s.value %d' % (self.account2id(account), balance))
|
||||||
print res
|
|
||||||
if f:
|
|
||||||
f.write('%s\n' % res)
|
|
||||||
|
|
||||||
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"
|
||||||
if cmd == 'execute':
|
if cmd == 'execute':
|
||||||
self.execute()
|
self.execute()
|
||||||
elif cmd == 'config':
|
elif cmd == 'config':
|
||||||
|
|
@ -123,6 +152,13 @@ class BoobankMuninPlugin(object):
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
elif cmd == 'help' or cmd == '-h' or cmd == '--help':
|
elif cmd == 'help' or cmd == '-h' or cmd == '--help':
|
||||||
self.display_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)
|
sys.exit(0)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue