Skip to content

Instantly share code, notes, and snippets.

@markito
Created July 2, 2012 19:54
Show Gist options
  • Save markito/3035314 to your computer and use it in GitHub Desktop.
Save markito/3035314 to your computer and use it in GitHub Desktop.
WLS client for REST Management APIs
#!/usr/bin/python
'''
Created on Jun 26, 2012
@author: markito
'''
import getopt, sys, getpass
import urllib2
__version__ = 0.1
def main():
# WLS REST client
wls = WLSREST()
try:
opts, args = getopt.getopt(sys.argv[1:], "h:o:u:pvs:d:a:c", ["host=", "output=","user=", "password","version", "server=", "datasource=", "application=", "cluster="])
except getopt.GetoptError, err:
print str(err)
wls.usage()
sys.exit(2)
###
# iterate and parse each parameter and input value
###
for opt, value in opts:
if (opt in ('-v', '--version')):
print 'Version %(v)s' % {'v': __version__ }
elif (opt in ('-h', '--host')):
host = value
wls.baseURL = "%s/management/tenant-monitoring/" % (host)
elif (opt in ('-p', '--password')):
wls.password = getpass.getpass("Password: ")
elif (opt in ('-u', '--user')):
wls.username = value
elif (opt in ('-s', '--server')):
wls.checkServer(value)
elif (opt in ('-d', '--datasource')):
wls.checkDataSource(value)
elif (opt in ('-a', '--application')):
wls.checkApplication(value)
elif (opt in ('-c', '--cluster')):
wls.checkCluster(value)
else:
wls.usage()
#####
# Simple print the results but could parse it from here or from
# each wls monitoring method (checkServer, checkCluster...)
#####
print wls.GET()
'''
WLSRest class that encapsulate the monitoring methods, authentication and GET call
'''
class WLSREST():
def __init__(self):
self.baseURL = ''
self.password = ''
self.username = ''
def setURL(self, url):
self.baseURL = url
return self
def usage(self):
print("Please check the command options and syntax")
print("Syntax: ./WLSRESTPy.py -h [http://adminServerHost:port] [-sacd] [parameter] -u [username] -p")
print("For example: ./WLSRESTPy.py -h http://localhost:7001 -s AdminServer -u weblogic -p")
def checkServer(self, server):
return self.setURL(self.baseURL + "servers/%s" % server)
return self
def checkDataSource(self, dataSource):
return self.setURL(self.baseURL + "datasources/%s" % dataSource)
def checkApplication(self, application):
return self.setURL(self.baseURL + "applications/%s" % application)
def checkCluster(self, cluster):
return self.setURL(self.baseURL + "clusters/%s" % cluster)
def GET(self):
# HTTP Authentication
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, self.baseURL, self.username, self.password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
try:
data = ""
req = urllib2.Request(self.baseURL, None, {'Accept': 'application/json'})
data = urllib2.urlopen(req).read()
return data
except urllib2.HTTPError, e:
print "HTTP error: %d" % e.code
self.usage()
except urllib2.URLError, e:
print "Network error: %s" % e.reason.args[1]
self.usage()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment