nagiosplugin icon indicating copy to clipboard operation
nagiosplugin copied to clipboard

Should be able to add dynamically created Contexts during Probe execution

Open mpounsett opened this issue 11 years ago • 1 comments

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?

mpounsett avatar Jan 14 '15 12:01 mpounsett

Original comment by mrvinti (Bitbucket: mrvinti, GitHub: mrvinti).


One (rather ugly) workaround is to:

  1. create the Check object without adding the contexts yet
  2. create the Resource object and pass the Check reference to the constructor
  3. in probe(), add the desired context, e.g. self.check.add(my_context) before yielding the Metric object.

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

mpounsett avatar Nov 10 '16 14:11 mpounsett