Collect All Service Properties Information in Site


You can find your service properties information from ArcGIS server manager or admin but how about if you want to see all of your services’ properties? you can run a script and see from there. I found this script was really useful when i wanted to check if any publisher published their service without accepting default value, for example: change minimum instances or maximum instances or maximum idle time or different cluster. Or with a little modification, you can query some other information.

Hopefully you find this script is useful for you.

Cheers…

 

#-------------------------------------------------------------------------------
# Name: List_Svc_properties.py
# Purpose: List All Started Services and their properties
#
# This script could be scheduled to run at a regular interval.
#
# Author: Muryadi Oey
#
# Created: 03/11/2016
# Copyright: (c) muryadioey 2016
# Licence: <your licence>
#-------------------------------------------------------------------------------

# For Http calls
import httplib, urllib,json,urllib2

# For system tools
import sys, datetime

# For reading passwords without echoing
import getpass

#Get the current date
now = datetime.datetime.now()

# Defines the entry point into the script
def main(argv=None):

# ##########################################################################################################################
# Start of Variables
# ##########################################################################################################################

# Server name
##serverName = raw_input("Enter server name: ")
serverName = 'SERVER01'

# Admin/publisher user name and password
## username = raw_input("Enter user name: ")
## password = getpass.getpass("Enter password: ")
username = 'ARCGISADMIN'
password = 'passwordadmin'

# Log file location
filePath = r'\\server01\d$\temp\Svc_Properties.txt'
doc = open(filePath, 'w')
header = "Server;Service;Status;MinIns;MaxIns;MaxIdle;MaxStart;MaxUsage;MaxWait;IsolationLevel;Cluster"
doc.write(header +"\n")

# ###########################################################################################################################
# End of Variables
# ###########################################################################################################################
# Print some info
print
print "This script is to collect all services' information."
serverPort = 6080
print

# Create a list to hold folder names
folderList = []

# Create a list to hold stopped/started services
targetList = []

# Get a token
token = getToken(username, password, serverName, serverPort,doc)
if token is None:
print "Could not generate a token with the username and password provided."
doc.write("\nCould not generate a token with the username and password provided." +"\n")
doc.close()
return

folderList = getCatalog(token,serverName,serverPort,doc)

for folder in folderList:
count = targetList.__len__()
i =0
folderName = folder
print "Processing folder : " + folderName
#doc.write("\n"+ "Processing folder : " + folderName + "\n")
while(i < count):
targetList.remove(targetList.__getitem__(0))
i = i +1

# Construct URL to read folder
if folder == "ROOT":
folder = ""
else:
folder += "/"

folderURL = "/arcgis/admin/services/" + folder

# This request only needs the token and the response formatting parameter
params = urllib.urlencode({'token': token, 'f': 'json'})

headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}

# Connect to URL and post parameters
httpConn = httplib.HTTPConnection(serverName, serverPort)
httpConn.request("POST", folderURL, params, headers)

# Read response
response = httpConn.getresponse()
if (response.status != 200):
httpConn.close()
print "Could not read folder information."
doc.write("\n" + "Could not read folder information." +"\n")
return
else:
data = response.read()

# Check that data returned is not an error object
if not assertJsonSuccess(data,doc):
print "Error when reading folder information. " + str(data)
#doc.write("\n" + "Error when reading folder information. " + str(data)+"\n")
else:
print "Processed folder information successfully. Now processing services..."
#doc.write("Processed folder information successfully. Now processing services..." +"\n")
print

# Deserialize response into Python object
dataObj = json.loads(data)
httpConn.close()

counter = 0

# Loop through each service in the folder and stop or start it
for item in dataObj['services']:

fullSvcName = item['serviceName'] + "." + item['type']

# Construct URL to stop or start service, then make the request
statusURL = "/arcgis/admin/services/" + folder + fullSvcName + "/status"
httpConn.request("POST", statusURL, params, headers)
# Read status response
statusResponse = httpConn.getresponse()
if (statusResponse.status != 200):
httpConn.close()
print "Error while checking status for " + fullSvcName
doc.write("\n" + "Error while checking status for " + fullSvcName +"\n")
return
else:
statusData = statusResponse.read()

# Check that data returned is not an error object
if not assertJsonSuccess(statusData,doc):
print "Error returned when retrieving status information for " + fullSvcName + "."
doc.write("\n" + "Error returned when retrieving status information for " + fullSvcName + "." +"\n")
print str(statusData)
doc.write("\n" + str(statusData) +"\n")

else:

statusDataObj = json.loads(statusData)
status = statusDataObj['realTimeState']

minInstURL = "/arcgis/admin/services/" + folder + fullSvcName
httpConn.request("POST", minInstURL, params, headers)

# Collect all services information, you can also query any information available in Service properties
# Read status response
minInstResponse = httpConn.getresponse()
minInstData = minInstResponse.read()
minInstDataObj = json.loads(minInstData)
minIns = minInstDataObj['minInstancesPerNode']
maxIns = minInstDataObj['maxInstancesPerNode']
maxIdle = minInstDataObj['maxIdleTime']
maxStr = minInstDataObj['maxStartupTime']
maxUsg = minInstDataObj['maxUsageTime']
maxWait = minInstDataObj['maxWaitTime']
isoLvl = minInstDataObj['isolationLevel']
cluster = minInstDataObj['clusterName']
print (serverName + ';'+ str( fullSvcName) + ';'+ str(status) + ';'+ str(minIns) +';' + str(maxIns) +';' +str(maxIdle) +';' +str(maxStr) +';' +str(maxUsg) +';' +str(maxWait) +';' +str(isoLvl) +';' +str(cluster) )
doc.write(serverName + ';'+ str( fullSvcName) + ';'+ str(status) +';'+ str(minIns) +';' + str(maxIns) +';' +str(maxIdle) +';' +str(maxStr) +';' +str(maxUsg) +';' +str(maxWait) +';' +str(isoLvl) +';' +str(cluster) +'\n' )
httpConn.close()

print
doc.close()
return

def getCatalog(token,server,port,doc):
baseUrl = "http://{}:{}/arcgis/admin/services".format(server, port)
folderNameList = []
catalog = json.load(urllib2.urlopen(baseUrl + "/" + "?f=json&token=" + token))
print ' List of Folders :'
#doc.write(" List of Folders :" +"\n")
print '\tROOT'
#doc.write("\tROOT" +"\n")
folderNameList.append("ROOT")
if "error" in catalog: return
folders = catalog['folders']
for folderName in folders:
folderNameList.append(folderName)
print '\t'+folderName
#doc.write("\t"+folderName +"\n")
if "error" in catalog: return
else: return folderNameList
# A function to generate a token given username, password and the adminURL.
def getToken(username, password, serverName, serverPort,doc):
# Token URL is typically http://server[:port]/arcgis/admin/generateToken
tokenURL = "/arcgis/admin/generateToken"

params = urllib.urlencode({'username': username, 'password': password, 'client': 'requestip', 'f': 'json'})

headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}

# Connect to URL and post parameters
httpConn = httplib.HTTPConnection(serverName, serverPort)
httpConn.request("POST", tokenURL, params, headers)

# Read response
response = httpConn.getresponse()
if (response.status != 200):
httpConn.close()
print "Error while fetching tokens from admin URL. Please check the URL and try again."
doc.write("\n" + "Error while fetching tokens from admin URL. Please check the URL and try again." +"\n")
return
else:
data = response.read()
httpConn.close()

# Check that data returned is not an error object
if not assertJsonSuccess(data,doc):
return

# Extract the token from it
token = json.loads(data)
return token['token']
# A function that checks that the input JSON object
# is not an error object.
def assertJsonSuccess(data,doc):
obj = json.loads(data)
if 'status' in obj and obj['status'] == "error":
print "Error: JSON object returns an error. " + str(obj)
#doc.write("\n" + "Error: JSON object returns an error. " + str(obj) +"\n")
return False
else:
return True
# Script start
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))

Advertisements

One thought on “Collect All Service Properties Information in Site

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s