Allow to get attributs on more than one levels

Add an example for that
And fix minors bugs
This commit is contained in:
Florent 2013-03-01 18:01:31 +01:00 committed by Romain Bignon
commit 78bea66884
2 changed files with 37 additions and 8 deletions

View file

@ -151,6 +151,23 @@ class GenericMuninPlugin(object):
elif len(self.do) == 3:
return self.weboob.do(self.do[0], self.do[1], backends=self.do[2])
def get_value(self, result):
attribs = self.attribvalue.split('/')
for attrib in attribs:
result = getattr(result, attrib)
return result
def monitored(self, result):
return not self.tomonitore or ('%s@%s' % (getattr(result, self.attribid), result.backend)) in self.tomonitore
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
@ -167,7 +184,7 @@ class GenericMuninPlugin(object):
d = {}
for backend, result in self.build_do():
if self.monitored(result):
d['%s@%s' % (getattr(value, self.attribid), result.backend)] = result
d['%s@%s' % (self.result2id(result), result.backend)] = result
for id in self.tomonitore:
try:
@ -193,12 +210,6 @@ class GenericMuninPlugin(object):
else:
self.flush_cache()
def monitored(self, result):
return not self.tomonitore or ('%s@%s' % (getattr(value, self.attribid), result.backend)) in self.tomonitore
def result2id(self, result):
return '%s_%s' % (result.backend, getattr(result, self.attribid))
def print_errors(self, errors):
for backend, err, backtrace in errors:
print >>sys.stderr, (u'%s(%s): %s' % (type(err).__name__, backend.name, err)).encode(sys.stdout.encoding or locale.getpreferredencoding(), 'replace')
@ -214,7 +225,7 @@ class GenericMuninPlugin(object):
try:
for backend, result in self.build_do():
if self.monitored(result):
value = getattr(result, self.attribvalue)
value = self.get_value(result)
self.write_output('%s.value %d' % (self.result2id(result), value))
except CallErrors, errors:
self.print_errors(errors)

View file

@ -54,4 +54,22 @@ echo "========= ICapBill config"
./bill config
rm bill
# Monitor temperature in Rennes
export cache_expire=7200
export HOME="/home/flo"
export capa="ICapWeather"
export do="get_current,619163,yahoo"
export import="from weboob.capabilities.weather import ICapWeather"
export attribvalue="temp/value"
export attribid="temp/id"
export title="Température à Rennes"
export vlabel="Température"
echo "========= ICapWeather fetch"
cp ./generic-munin ./rennes
./rennes
echo "========= ICapWeather config"
./rennes config
rm rennes