]>
git.datanom.net - check_http.git/blob - check_http.py
3 ###############################################################################
4 # Nagios plugin template
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
13 ###############################################################################
15 __author__
= 'Your Name Here'
18 from optparse
import OptionParser
, OptionGroup
21 ## These will override any args passed to the script normally. Comment out after testing.
23 #testargs = '--version'
27 """ Main plugin logic goes here """
29 ## Parse command-line arguments
30 args
, args2
= parse_args()
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')
44 """ Parse command-line arguments """
46 parser
= OptionParser(usage
='usage: %prog [-v|vv|vvv] [options]',
47 version
='{0}: v.{1} by {2}'.format('%prog', __version__
, __author__
))
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')
53 ## CLI arguments specific to this script
54 group
= OptionGroup(parser
,'Plugin Options')
55 group
.add_option('-x', '--extra', help='Your option here',
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='##')
64 parser
.add_option_group(group
)
66 ## Try to parse based on the testargs variable. If it doesn't exist, use args
68 args
, args2
= parser
.parse_args(testargs
.split())
70 args
, args2
= parser
.parse_args()
72 ## Set the logging level based on the -v arg
73 log
.getLogger().setLevel([log
.ERROR
, log
.WARN
, log
.INFO
, log
.DEBUG
][args
.v
])
75 log
.debug('Parsed arguments: {0}'.format(args
))
76 log
.debug('Other arguments: {0}'.format(args2
))
80 def gtfo(exitcode
, message
=''):
81 """ Exit gracefully with exitcode and (optional) message """
83 log
.debug('Exiting with status {0}. Message: {1}'.format(exitcode
, message
))
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')
This page took 0.086297 seconds and 6 git commands to generate.