Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should be able to add dynamically created Contexts during Probe execution #9

Open
mpounsett opened this issue Jan 14, 2015 · 1 comment
Labels
feature New feature or request needs review Needs developer review before assigning a milestone

Comments

@mpounsett
Copy link
Owner

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
Copy link
Owner Author

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 mpounsett added major library feature New feature or request needs review Needs developer review before assigning a milestone and removed library major labels Nov 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request needs review Needs developer review before assigning a milestone
Projects
None yet
Development

No branches or pull requests

1 participant