From 23b449ad6161786a5888d645e126bf10f5532390 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Nikolov Date: Thu, 12 Sep 2013 13:29:02 +0300 Subject: [PATCH] docopt'ify the vmpoller-client as well --- src/vmpoller-client | 75 +++++++++++++++++++++------------------------ src/vmpoller-proxy | 4 ++- src/vmpoller-worker | 4 ++- 3 files changed, 41 insertions(+), 42 deletions(-) diff --git a/src/vmpoller-client b/src/vmpoller-client index c831984..8b27f6d 100755 --- a/src/vmpoller-client +++ b/src/vmpoller-client @@ -25,57 +25,52 @@ # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -vmpoller-client.py is an application used for polling objects' information from a VMware vCenter server. +vmpoller-client is an application used for polling objects' information from a VMware vCenter server. It is intended to be integrated into a Zabbix template for polling of ESX hosts and Datastores properties. """ -import sys -import getopt from vmpoller.core import VMPollerClient +from docopt import docopt def main(): - if len(sys.argv) != 12: - print 'usage: %s [-D|-H] -n -p -u -c -V ' % sys.argv[0] - raise SystemExit - - try: - opts, args = getopt.getopt(sys.argv[1:], "DHn:p:u:c:V:") - except getopt.GetoptError, e: - print 'usage: %s [-D|-H] -n -p -u -c -V ' % sys.argv[0] - raise SystemExit + usage=""" +Usage: + vmpoller-client [-r ] [-t ] (-D|-H) -c discover -V -e + vmpoller-client [-r ] [-t ] -H -n -p -c poll -V -e + vmpoller-client [-r ] [-t ] -D -n -p -u -c poll -V -e + vmpoller-client --help + vmpoller-client --version - for opt, arg in opts: - if opt == '-f': - myConfig = arg - elif opt == '-p': - myProperty = arg - elif opt == '-n': - name = arg - elif opt == '-u': - ds_url = arg - elif opt == '-D': - pollInfo = 'datastores' - elif opt == '-H': - pollInfo = 'hosts' - elif opt == '-c' and arg == 'poll': - cmd = 'poll' - elif opt == '-c' and arg == 'discover': - cmd = 'discover' - elif opt == '-V': - vcenter = arg - - client = VMPollerClient("/etc/vmpoller/vmpoller-client.conf") +Options: + -h, --help Display this usage info + -v, --version Display version and exit + -D, --datastores Retrieve a datastore object property + -H, --hosts Retrieve a host object property + -V , --vcenter The vCenter server to send the request to + -c , --command The command to perform, either "poll" or "discover" + -n , --name Name of the object, e.g. datastore/host name + -p , --property Name of the property as defined by the vSphere Web SDK + -u , --url Datastore URL, only applicable to datastores object type + -r , --retries Number of time to retry if a request times out [default: 3] + -t , --timeout Timeout after that period of milliseconds [default: 3000] + -e , --endpoint Endpoint of ZeroMQ Proxy/Broker the client connects to - msg = { "type": pollInfo, - "vcenter": vcenter, - "name": name, - "ds_url": ds_url, - "cmd": cmd, - "property": myProperty, - } +""" + args = docopt(usage, version="1.0.0") + + client = VMPollerClient(endpoint=args["--endpoint"], retries=args["--retries"], timeout=args["--timeout"]) + + msg = { "type": "hosts" if args["--hosts"] else "datastores", + "vcenter": args["--vcenter"], + "name": args["--name"], + "ds_url": args["--url"], + "cmd": args["--command"], + "property": args["--property"], + } + result = client.run(msg) print result diff --git a/src/vmpoller-proxy b/src/vmpoller-proxy index 508e0d7..429bfa6 100755 --- a/src/vmpoller-proxy +++ b/src/vmpoller-proxy @@ -78,9 +78,11 @@ def status(endpoint): def main(): usage=""" -Usage: vmpoller-proxy [-hvd] [-p ] [-f ] start +Usage: vmpoller-proxy [-d] [-p ] [-f ] start vmpoller-proxy -e stop vmpoller-proxy -e status + vmpoller-proxy --help + vmpoller-proxy --version Arguments: start Start the VMPoller Proxy diff --git a/src/vmpoller-worker b/src/vmpoller-worker index 735dcf2..275226a 100755 --- a/src/vmpoller-worker +++ b/src/vmpoller-worker @@ -80,9 +80,11 @@ def status(endpoint): def main(): usage=""" -Usage: vmpoller-worker [-hvd] [-p ] [-f ] start +Usage: vmpoller-worker [-d] [-p ] [-f ] start vmpoller-worker -e stop vmpoller-worker -e status + vmpoller-worker --help + vmpoller-worker --version Arguments: start Start the VMPoller Worker