Re: Writing a plugin
Posted: Wed Jul 24, 2013 4:03 pm
Thanks for the replies and advice!
Agreed, I had realized that after I put it all together and was trying to figure out the best way to make that the same function with passed parameters.nscott wrote: Why are iswarning and iscritical different functions?
They are both doing nearly the same thing, and they are both almost 100 lines long. You've effectively doubled the code it takes to check ranges. Perhaps think about it in terms of:
Code: Select all
def is_within_range(value, threshold): # some code goes here to determine if a value falls into a threshold if is_within_range(my_checked_value, my_critical_threshold): status = 'CRITICAL' elif is_within_range(my_checked_value, my_warning_threshold): status = 'WARNING' else: status = 'OK'
Good question, and I don't have a good answer, would it be better to pass them to the function then return the values instead of setting the global variable directly? To be fair to myself, I'm only learning Python so I don't really know the ins/outs of it, my background is VB6/VBS. And most of this code was from another plugin that I liked the framework of and I can wrap my head around Python so I thought I'd use it as a base framework. I'm certainly open to a better way of doing it and will see if I can reduce/eliminate the global usage.nscott wrote: Globals
Globals are really handy in some situations. However they get dicey later. Nagios plugins generally aren't long enough to expose questionable programming practices. However, reading your code gets a bit tiresome because you're constantly calling 'global' in front of variables. Is there some other workflow you can think of for the variables? Do they need to be global? Also sometimes I'm not sure why you're returning globals:
On a side note, its naming convention to make your global capitalized, to make a clear distinction between what is global and what is local. However, since you are required to preface all use of a global with 'global', its clear whats a global, but I generally find it easier to think of in those terms, just my two cents.Code: Select all
def nagios_status(newStatus): global exit_status exit_status = max(exit_status, newStatus) return exit_status
I'll have to noodle that to make sure I understand what you're saying, I'll ask back for more clarity if I can't figure it out. This would be so I don't have a pile of if/elif/elif/elif, etc.?nscott wrote:Functions
You did a good job breaking the items up into functions. Perhaps the arg parsing could be placed into a function as well? That way your bottom would be:
Something like that.Code: Select all
if __name__ == "__main__": args, options = parse_args() value = run_check(args, options) if is_within_range(value, options.critical_thresh): # Do the critical string making elif is_within_range(value, options.warning_thresh): # Do the warning string making else: # Do the Ok string making print stdout_string sys.exit(return_code)
Nice, will definitely look into that one!nscott wrote:PyNagios
There are a few packages around that make it really easy to make Nagios plugins in Python. One is called PyNagios whose homepage is https://pypi.python.org/pypi/pynagios. Might be right up your alley.
Hope this helps.