Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
webctrl/wc_monitor.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
158 lines (139 sloc)
6.45 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import suds | |
import sys | |
import xml | |
import argparse | |
import getpass | |
import time | |
import datetime | |
from Environment import readSettings | |
def main(args): | |
#################################################################################################################### | |
### Check all arguments | |
#################################################################################################################### | |
errorMessage = "No path to SETTINGS file specified. Provide one via '-f my_settingsFile' or {..., 'settingsFile':'my_settingsFile', ...}" | |
try: | |
if args['settingsFile'] is None or not args['settingsFile']: | |
print(errorMessage) | |
sys.exit(1) | |
else: | |
settingsFile = args['settingsFile'] | |
except KeyError: | |
print(errorMessage) | |
sys.exit(1) | |
settings = readSettings(settingsFile) | |
errorMessage = "No username specified. Provide one via '-u my_username' or {..., 'username':'my_username', ...} or WC_USERNAME=my_username" | |
try: | |
if args['username'] is None or not args['username']: | |
if not settings['WC_USERNAME']: | |
print(errorMessage) | |
sys.exit(1) | |
else: | |
username = settings['WC_USERNAME'] | |
else: | |
username = str(args['username']) | |
except KeyError: | |
print(errorMessage) | |
sys.exit(1) | |
# The password cannot be given via environment variable | |
errorMessage = "No password specified. Provide one via {..., 'password':'my_password', ...}" | |
try: | |
if args['password'] is None or not args['password']: | |
print(errorMessage) | |
sys.exit(1) | |
else: | |
password = args['password'] | |
except KeyError: | |
print(errorMessage) | |
sys.exit(1) | |
nodeDefault = '/trees/geographic' | |
errorMessage = "No node specified. Provide one via '-n my_node' or {..., 'node':'my_node', ...} or WC_NODE=my_node. " \ | |
"Using a default now: '" + nodeDefault + "'" | |
try: | |
if args['node'] is None or not args['node']: | |
if not settings['WC_NODE']: | |
print(errorMessage) | |
node = nodeDefault | |
else: | |
node = settings['WC_NODE'] | |
else: | |
node = str(args['node']) | |
except KeyError: | |
print(errorMessage) | |
node = nodeDefault | |
urlDefault = 'https://webctrl.rz-berlin.mpg.de' | |
errorMessage = "No URL specified. Provide one via '-url my_url' or {..., 'url':'my_url', ...} or WC_URL=my_url. " \ | |
"Using a default now: '" + urlDefault + "'" | |
try: | |
if args['url'] is None or not args['url']: | |
if not settings['WC_URL']: | |
print(errorMessage) | |
url = urlDefault | |
else: | |
url = settings['WC_URL'] | |
else: | |
url = str(args['url']) | |
except KeyError: | |
print(errorMessage) | |
url = urlDefault | |
wsdlFile = url + '/_common/webservices/Eval?wsdl' | |
#################################################################################################################### | |
### Connect to the webCTRL server | |
#################################################################################################################### | |
try: | |
client = suds.client.Client(wsdlFile, username=username, password=password) | |
except AttributeError: | |
print 'Error: Incorrect username and/or password' | |
except xml.sax._exceptions.SAXParseException: | |
print 'Error: Incorrect/Misspelled WSDL file. It should be: http(s)://URL?wsdl' | |
sys.exit(1) | |
except: | |
print("Unexpected error:", sys.exc_info()[0]) | |
print('Perhaps your URL to the WSDL file is not correct.') | |
sys.exit(1) | |
#################################################################################################################### | |
### Go into an infinite loop and retrieve a value every second but only print it to the screen if the current value | |
### is different from the last one; the loop is exited once CTRL+C is pressed | |
#################################################################################################################### | |
currentValue = 0 | |
try: | |
while True: | |
oldValue = currentValue | |
try: | |
currentValue = client.service.getValue(node) | |
except suds.WebFault as fault: | |
print fault | |
sys.exit(1) | |
if currentValue is None: | |
print "You haven't specified a particular controller. The command could look like this:" | |
print '$ wc_monitor "#controller/m001"' | |
sys.exit(1) | |
elif currentValue != oldValue: | |
print datetime.datetime.now().time().strftime("%H:%M:%S") + " " + currentValue | |
time.sleep(1) | |
else: | |
time.sleep(1) | |
except KeyboardInterrupt: | |
print('Exiting due to keyboard interupt...') | |
# The argument parser is only called if this script is called as a script/executable (via command line) but not when | |
# imported by another script | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print "You haven't specified any arguments. Use -h to get more details on how to use this command." | |
sys.exit(1) | |
parser = argparse.ArgumentParser() | |
parser._action_groups.pop() | |
required = parser.add_argument_group('required arguments') | |
optional = parser.add_argument_group('optional arguments (can also be specified via SETTINGS file)') | |
required.add_argument('--password', '-p', type=str, default=None, help='Password for the login to the WebCTRL server') | |
required.add_argument('--settingsFile', '-f', type=str, default=None, help='File name of settings containing WC_* variables') | |
required.add_argument('--username', '-u', type=str, default=None, help='Username for the login to the WebCTRL server') | |
optional.add_argument('--url', '-url', type=str, default=None, help="URL of the webctrl server like 'http(s)://webctrl.de'") | |
optional.add_argument('--node', '-n', type=str, default=None, help='Path to the point or node that you want to monitor') | |
args = parser.parse_args() | |
# Get the password if it hasn't been passed as argument | |
if args.password is None or not args.password: | |
args.password = getpass.getpass('No password specified via -p. Please enter your WebCTRL login password: ') | |
# Convert the argparse.Namespace to a dictionary via vars(args) | |
main(vars(args)) | |
sys.exit(0) |