2
0
mirror of https://github.com/munin-monitoring/contrib.git synced 2018-11-08 00:59:34 +01:00

Command line options added. Code restructured.

This commit is contained in:
Michel Albert 2012-04-05 15:15:21 +02:00
parent b8ed5d1005
commit 583d318c62

View File

@ -1,11 +1,14 @@
#!/usr/bin/python
from optparse import OptionParser
from os import listdir, access, X_OK
from os.path import join, isdir
from subprocess import call
from os import listdir, access, X_OK
from socket import gethostname
import sys
__version__ = '1.0dev1'
__version__ = '1.0dev3'
spoolfetch_dir = ''
@ -15,8 +18,10 @@ host = 'hostname'
class CmdHandler(object):
def __init__(self, out_stream):
def __init__(self, in_stream, out_stream, options):
self.out_stream = out_stream
self.in_stream = in_stream
self.options = options
def do_version(self, arg):
"""
@ -94,33 +99,71 @@ class CmdHandler(object):
do_exit = do_quit
def handle_input(in_stream, handler, out_stream):
for line in in_stream:
line = line.strip()
line = line.split(' ')
cmd = line[0]
if len(line) == 1:
arg = ''
elif len(line) == 2:
arg = line[1]
else:
raise ValueError('Invalid input: %s' % line)
def handle_input(self):
for line in self.in_stream:
line = line.strip()
line = line.split(' ')
cmd = line[0]
if len(line) == 1:
arg = ''
elif len(line) == 2:
arg = line[1]
else:
raise ValueError('Invalid input: %s' % line)
if not cmd:
continue
if not cmd:
continue
func = getattr(handler, 'do_%s' % cmd, None)
if not func:
commands = [_[3:] for _ in dir(handler) if _.startswith('do_')]
print "# Unknown command. Supported commands: %s" % commands
sys.exit(1)
func = getattr(self, 'do_%s' % cmd, None)
if not func:
commands = [_[3:] for _ in dir(self) if _.startswith('do_')]
print "# Unknown command. Supported commands: %s" % commands
sys.exit(1)
func(arg)
out_stream.write('\n')
func(arg)
self.out_stream.write('\n')
def usage(option, opt, value, parser):
parser.print_help()
sys.exit(0)
def get_options():
"""
Parses command-line arguments.
"""
parser = OptionParser(add_help_option=False)
parser.add_option('-p', '--port', dest='port',
default=None,
help='TCP Port to listen on. (If not specified, use stdin/stdout)')
parser.add_option('-d', '--plugin-dir', dest='plugin_dir',
default='.',
help=('The directory containing the munin-plugins.'
' Default: <current dir>'))
parser.add_option('-h', '--host', dest='host',
help=('The hostname which will be reported in the pluging.'
' Default: %s' % gethostname()),
default=gethostname())
parser.add_option('-s', '--spoolfech-dir', dest='spoolfech_dir',
default=None,
help='The spoolfetch folder. Default: disabled')
parser.add_option('--help', action='callback', callback=usage,
help='Shows this help')
return parser.parse_args()
def main():
handle_input(sys.stdin, CmdHandler(sys.stdout), sys.stdout)
options, args = get_options()
if not options.port:
in_stream = sys.stdin
out_stream = sys.stdout
else:
sys.stderr.write("TCP connections not yet supported\n")
sys.exit(1)
handler = CmdHandler(in_stream, out_stream, options)
handler.handle_input()
if __name__ == '__main__':
main()