nagiosplugin
nagiosplugin copied to clipboard
Should be able to add dynamically created Contexts during Probe execution
Original report by Christian Kauhaus (Bitbucket: ckauhaus, GitHub: ckauhaus).
From: "Joseph L. Casale" [email protected]
When a context is unknown until the time of check execution, how can one build a context and metric during the nagiosplugin.Check probe invocation? It would be too late during execution of the nagiosplugin.Resource as far as I can see unless I missed someway the resource can gain access to the nagiosplugin.Check instance?
Original comment by mrvinti (Bitbucket: mrvinti, GitHub: mrvinti).
One (rather ugly) workaround is to:
- create the
Checkobject without adding the contexts yet - create the
Resourceobject and pass theCheckreference to the constructor - in
probe(), add the desired context, e.g.self.check.add(my_context)before yielding theMetricobject.
Here's one example:
#!python
import nagiosplugin
class MyAwesomeResource(nagiosplugin.Resource):
def __init__(self, check=None):
super(MyAwesomeResource, self).__init__()
self.check = check
def probe(self):
data = {
'workers': 10,
'minions': 11
}
for name, value in data.items():
# add one context for each metric
self.check.add(nagiosplugin.ScalarContext(name, '16:', '@8'))
yield nagiosplugin.Metric(name, value)
@nagiosplugin.guarded
def main():
check = nagiosplugin.Check()
# pass a reference to our check object
resource = MyAwesomeResource(check)
check.add(resource)
check.main(timeout=5)
if __name__ == '__main__':
main()
(testenv) mydog:testenv mrvinti$ python test.py
MYAWESOMERESOURCE WARNING - workers is 10 (outside range 16:) | minions=11;16:;@8 workers=10;16:;@8
HTH