]>
Commit | Line | Data |
---|---|---|
1 | #! /usr/bin/env python | |
2 | ||
3 | ############################################################################### | |
4 | # Nagios plugin template | |
5 | # | |
6 | # Notes | |
7 | # - The RHEL boxes I work on are currently limited to Python 2.6.6, hence the | |
8 | # use of (deprecated) optparse. If I can ever get them all updated to | |
9 | # Python 2.7 (or better yet, 3.3), I'll switch to argparse | |
10 | # - This template runs in 2.6-3.3. Any changes made will need to be appropriate | |
11 | # to the Python distro you want to use | |
12 | # | |
13 | ############################################################################### | |
14 | ||
15 | __author__ = 'Your Name Here' | |
16 | __version__= 0.1 | |
17 | ||
18 | from optparse import OptionParser, OptionGroup | |
19 | import logging as log | |
20 | ||
21 | ## These will override any args passed to the script normally. Comment out after testing. | |
22 | #testargs = '--help' | |
23 | #testargs = '--version' | |
24 | #testargs = '-vvv' | |
25 | ||
26 | def main(): | |
27 | """ Main plugin logic goes here """ | |
28 | ||
29 | ## Parse command-line arguments | |
30 | args, args2 = parse_args() | |
31 | ||
32 | ## Uncomment to test logging levels against verbosity settings | |
33 | # log.debug('debug message') | |
34 | # log.info('info message') | |
35 | # log.warning('warning message') | |
36 | # log.error('error message') | |
37 | # log.critical('critical message') | |
38 | # log.fatal('fatal message') | |
39 | ||
40 | gtfo(0) | |
41 | ||
42 | ||
43 | def parse_args(): | |
44 | """ Parse command-line arguments """ | |
45 | ||
46 | parser = OptionParser(usage='usage: %prog [-v|vv|vvv] [options]', | |
47 | version='{0}: v.{1} by {2}'.format('%prog', __version__, __author__)) | |
48 | ||
49 | ## Verbosity (want this first, so it's right after --help and --version) | |
50 | parser.add_option('-v', help='Set verbosity level', | |
51 | action='count', default=0, dest='v') | |
52 | ||
53 | ## CLI arguments specific to this script | |
54 | group = OptionGroup(parser,'Plugin Options') | |
55 | group.add_option('-x', '--extra', help='Your option here', | |
56 | default=None) | |
57 | ||
58 | ## Common CLI arguments | |
59 | #parser.add_option('-c', '--critical', help='Set the critical threshold. Default: %(default)s', | |
60 | # default=97, type=float, dest='crit', metavar='##') | |
61 | #parser.add_option('-w', '--warning', help='Set the warning threshold. Default: %(default)s', | |
62 | # default=95, type=float, dest='warn', metavar='##') | |
63 | ||
64 | parser.add_option_group(group) | |
65 | ||
66 | ## Try to parse based on the testargs variable. If it doesn't exist, use args | |
67 | try: | |
68 | args, args2 = parser.parse_args(testargs.split()) | |
69 | except NameError: | |
70 | args, args2 = parser.parse_args() | |
71 | ||
72 | ## Set the logging level based on the -v arg | |
73 | log.getLogger().setLevel([log.ERROR, log.WARN, log.INFO, log.DEBUG][args.v]) | |
74 | ||
75 | log.debug('Parsed arguments: {0}'.format(args)) | |
76 | log.debug('Other arguments: {0}'.format(args2)) | |
77 | ||
78 | return args, args2 | |
79 | ||
80 | def gtfo(exitcode, message=''): | |
81 | """ Exit gracefully with exitcode and (optional) message """ | |
82 | ||
83 | log.debug('Exiting with status {0}. Message: {1}'.format(exitcode, message)) | |
84 | ||
85 | if message: | |
86 | print(message) | |
87 | exit(exitcode) | |
88 | ||
89 | if __name__ == '__main__': | |
90 | ## Initialize logging before hitting main, in case we need extra debuggability | |
91 | log.basicConfig(level=log.DEBUG, format='%(asctime)s - %(funcName)s - %(levelname)s - %(message)s') | |
92 | main() |