Select to view content in your preferred language

Managing Data - Can I query Feature Service Information

1292
5
Jump to solution
07-27-2017 04:53 PM
AdamChavez
Deactivated User

We are trying to migrate some feature classes and feature tables from an older geodatabase(Oracle) to a new 10.5X geodatabase(SQL).

One of the underlying question is whether or not our ArcGIS Server has a published feature service connected to the data in the older geodatabase.

Can I quickly query the ArcGIS Server that has 50+ services to get a list of all of the tables that I "shouldn't move".  I'm hoping there is a "connection string" like attribute that I can query.

Thanks!

I found a workable solution below.  On your old ArcGIS 10 server, copy the .msd folders to a new location from arcgisserver\arcgisinput\ and run python similar to below. 

It goes through the folders that are in the same directory as this python script and finds .msd files.  Then it will collect the connection string from those files.

##################################################################

import arcpy,sys, string, os, arcgisscripting,arcpy,traceback,csv,zipfile,time,logging
from arcpy import env
from time import gmtime, strftime
from xml.etree.cElementTree import iterparse
#FIRST COPY ALL YOUR MSD FILES FROM YOUR ARCGIS SERVER into a seperate location and then place the python script in that location as well.
#FOR MY CASE IT WAS HERE c:\arcgisserver\arcgisinput
data_path =              (os.path.dirname(sys.argv[0]) + "/")
FileDate =               str(strftime("%a, %d %b %Y", gmtime()))
TXTFILEOUTPUT =          data_path + "MSD_RESULTS_"+ FileDate+".txt"
f = open(TXTFILEOUTPUT,'a')
def PrintMessage(MESSAGE,DEFINEDWRITINGTOOL):
     try:
          DEFINEDWRITINGTOOL.write(str(MESSAGE) + '\n')
     except:
          pass

def sListInfo(MSD):
     MSD = zipfile.ZipFile(MSD)
     for name in MSD.namelist():
          for _event, elem in iterparse(MSD.open(name)):
               if elem.tag == "WorkspaceConnectionString" or elem.tag =="Dataset":
                    for items in elem.text.split(';'):
                         PrintMessage('          '+ str(items),f)
     MSD.close()
                              ##data_path is the folder that I am running the python script from, you can always hard code instead. ##
for root, dirs, files in os.walk(data_path):
     for file in files:
          if file.endswith(".msd"):
               MapService = os.path.join(root, file).replace("/","\\")
               PrintMessage("#######################################################################################################################################",f)
               PrintMessage(MapService,f)
               sListInfo(MapService)
               PrintMessage("#######################################################################################################################################",f)

0 Kudos
1 Solution

Accepted Solutions
SethLewis1
Frequent Contributor

With admin privileges to the ArcGIS Server instance, the following script will print the service, onServerWorkspace, onServerConnectionStrings for the underlying database server, version and database as well as a list of tables/feature class objects that are referenced in the service. It'll also output a CSV listing all services and their properties but I think the print statements are what you're looking for.

# Reads the following properties from services and writes them to a comma-delimited file:
#  ServiceName, Folder, Type, Status, Min Instances, Max Instances, KML,
#  WMS, Max Records, Cluster, Cache Directory, Jobs Directory, Output Directory

# For HTTP calls
import httplib, urllib, json

# For system tools
import sys

# For reading passwords without echoing
import getpass

import uuid
import re

regexpSERVER = re.compile("SERVER=([^;]*);")
regexpVERSION = re.compile("VERSION=([^;]*);")
regexpDATABASE = re.compile("DATABASE=([^;]*);")

def main(argv=None):
    # Ask for admin/publisher user name and password
    username = "username"
    password = "password"

    # Ask for server name & port
    serverName = "servername"
    serverPort = 6080

    # Get the location and the name of the file to be created
    resultFile = str(uuid.uuid4()) + ".csv"

    # Get a token
    token = getToken(username, password, serverName, serverPort)

    # Get the root info
    serverURL = "/arcgis/admin/services/"

    # 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", serverURL, params, headers)

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

        # Check that data returned is not an error object
        if not assertJsonSuccess(data):
            print "Error when reading server information. " + str(data)
            return
        else:
            print "Processed server information successfully. Now processing folders..."

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

        #Store the Folders in a list to loop on
        folders = dataObj["folders"]

        #Remove the System and Utilities folders
        folders.remove("System")
        folders.remove("Utilities")

        #Add an entry for the root folder
        folders.append("")

        #Create the summary file of services
        serviceResultFile = open(resultFile,'w')
        serviceResultFile.write("ServiceName,Folder,Type,Status,Min Instances,Max Instances,FeatureService,kml,wms,Max Records,Cluster,Cache Directory,Jobs Directory,Output Directory" + "\n")

        #Loop on the found folders and discover the services and write the service information
        for folder in folders:

            # Determine if the loop is working on the root folder or not
            if folder != "":
                folder += "/"

            # Build the URL for the current folder
            folderURL = "/arcgis/admin/services/" + folder
            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."
                return
            else:
                data = response.read()

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

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

                # Loop through each service in the folder
                for item in dataObj['services']:

                    if item["type"] == "GeometryServer":# and folder == "":
                        # Build the Service URL
                        if folder:
                            sUrl = "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"])
                            statusUrl = "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"])
                        else:
                            sUrl = "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"])
                            statusUrl = "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"])



                        httpConn.request("POST", sUrl, params, headers)

                        # Get the response
                        servResponse = httpConn.getresponse()
                        readData = servResponse.read()
                        jsonOBJ = json.loads(readData)

                        # Build the Service URL to test the running status


                        # Submit the request to the server
                        httpConn.request("POST", statusUrl, params, headers)
                        servStatusResponse = httpConn.getresponse()

                        # Obtain the data from the response
                        readData = servStatusResponse.read()
                        jsonOBJStatus = json.loads(readData)

                        # Build the line to write to the output file
                        ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["clusterName"]) + "," + "NA" + "," + "NA" + "," + "NA" +"\n"

                        # Write the results to the file
                        serviceResultFile.write(ln)

                        httpConn.close()

                    elif item["type"] == "SearchServer":# and folder == "":
                        if folder:
                            sUrl = "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"])
                            statusUrl = "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"])
                        else:
                            sUrl = "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"])
                            statusUrl = "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"])


                        httpConn.request("POST", sUrl, params, headers)

                        # Get the response
                        servResponse = httpConn.getresponse()
                        readData = servResponse.read()
                        jsonOBJ = json.loads(readData)


                        # Submit the request to the server
                        httpConn.request("POST", statusUrl, params, headers)

                        # Get the response
                        servStatusResponse = httpConn.getresponse()
                        readData = servStatusResponse.read()
                        jsonOBJStatus = json.loads(readData)

                        # Build the line to write to the output file
                        ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["clusterName"]) + "," + "NA" + "," + str(jsonOBJ["properties"]["jobsDirectory"]) + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n"

                        # Write the results to the file
                        serviceResultFile.write(ln)

                        httpConn.close()

                    elif item["type"] == "ImageServer":

                        # Build the Service URL
                        if folder:
                            sUrl = "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"])
                        else:
                            sUrl = "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"])

                        # Submit the request to the server
                        httpConn.request("POST", sUrl, params, headers)

                        # Get the response
                        servResponse = httpConn.getresponse()
                        readData = servResponse.read()
                        jsonOBJ = json.loads(readData)

                        # Build the Service URL to test the running status
                        if folder:
                            statusUrl = "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"])
                        else:
                            statusUrl = "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"])

                        # Submit the request to the server
                        httpConn.request("POST", statusUrl, params, headers)

                        # Get the response
                        servStatusResponse = httpConn.getresponse()
                        readData = servStatusResponse.read()
                        jsonOBJStatus = json.loads(readData)

                        # Extract the WMS properties from the response
                        wmsProps = [imageWMS for imageWMS in jsonOBJ["extensions"] if imageWMS["typeName"] == 'WMSServer']#.items()[0][1] == 'WMSServer']

                        if len(wmsProps) > 0:
                            wmsStatus = str(wmsProps[0]["enabled"])
                        else:
                            wmsStatus = "NA"

                        # Build the line to write to the output file
                        ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + wmsStatus +"," + "NA" + "," + str(jsonOBJ["clusterName"]) + "," + str(jsonOBJ["properties"]["cacheDir"]) + "," + "NA," + str(jsonOBJ["properties"]["outputDir"]) +"\n"

                        # Write the results to the file
                        serviceResultFile.write(ln)

                        httpConn.close()

                    elif item["type"] == "GlobeServer":

                        # Build the Service URL
                        if folder:
                            sUrl = "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"])
                        else:
                            sUrl = "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"])

                        # Submit the request to the server
                        httpConn.request("POST", sUrl, params, headers)

                        # Get the response
                        servResponse = httpConn.getresponse()
                        readData = servResponse.read()
                        jsonOBJ = json.loads(readData)

                        #Build the Service URL to test the running status
                        if folder:
                            statusUrl = "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"])
                        else:
                            statusUrl = "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"])

                        # Submit the request to the server
                        httpConn.request("POST", statusUrl, params, headers)

                        # Get the response
                        servStatusResponse = httpConn.getresponse()
                        readData = servStatusResponse.read()
                        jsonOBJStatus = json.loads(readData)

                        # Build the line to write to the output file
                        ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["properties"]["maxRecordCount"]) + "," + str(jsonOBJ["clusterName"]) + "," + str(jsonOBJ["properties"]["cacheDir"]) + "," + "NA" + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n"

                        # Write the results to the file
                        serviceResultFile.write(ln)

                        httpConn.close()

                    elif item["type"] == "GPServer":

                        # Build the Service URL
                        if folder:
                            sUrl = "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"])
                        else:
                            sUrl = "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"])

                        # Submit the request to the server
                        httpConn.request("POST", sUrl, params, headers)

                        # Get the response
                        servResponse = httpConn.getresponse()
                        readData = servResponse.read()
                        jsonOBJ = json.loads(readData)

                        # Build the Service URL to test the running status
                        if folder:
                            statusUrl = "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"])
                        else:
                            statusUrl = "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"])

                        # Submit the request to the server
                        httpConn.request("POST", statusUrl, params, headers)

                        # Get the response
                        servStatusResponse = httpConn.getresponse()
                        readData = servStatusResponse.read()
                        jsonOBJStatus = json.loads(readData)

                        # Build the line to write to the output file
                        ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["clusterName"]) + "," + "NA" + "," + str(jsonOBJ["properties"]["jobsDirectory"]) + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n"

                        # Write the results to the file
                        serviceResultFile.write(ln)

                        httpConn.close()

                    elif item["type"] == "GeocodeServer":

                        # Build the Service URL
                        if folder:
                            sUrl = "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"])
                        else:
                            sUrl = "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"])

                        # Submit the request to the server
                        httpConn.request("POST", sUrl, params, headers)

                        # Get the response
                        servResponse = httpConn.getresponse()
                        readData = servResponse.read()
                        jsonOBJ = json.loads(readData)

                        if folder:
                            statusUrl = "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"])
                        else:
                            statusUrl = "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"])

                        # Submit the request to the server
                        httpConn.request("POST", statusUrl, params, headers)

                        # Get the response
                        servStatusResponse = httpConn.getresponse()
                        readData = servStatusResponse.read()
                        jsonOBJStatus = json.loads(readData)

                        # Build the line to write to the output file
                        ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["clusterName"]) + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n"

                        # Write the results to the file
                        serviceResultFile.write(ln)

                        httpConn.close()

                    elif item["type"] == "GeoDataServer":

                        # Build the Service URL
                        if folder:
                            sUrl = "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"])
                        else:
                            sUrl = "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"])

                        # Submit the request to the server
                        httpConn.request("POST", sUrl, params, headers)

                        # Get the response
                        servResponse = httpConn.getresponse()
                        readData = servResponse.read()
                        jsonOBJ = json.loads(readData)

                        if folder:
                            statusUrl = "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"])
                        else:
                            statusUrl = "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"])

                        # Submit the request to the server
                        httpConn.request("POST", statusUrl, params, headers)

                        # Get the response
                        servStatusResponse = httpConn.getresponse()
                        readData = servStatusResponse.read()
                        jsonOBJStatus = json.loads(readData)

                        # Build the line to write to the output file
                        ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["properties"]["maxRecordCount"]) + "," + str(jsonOBJ["clusterName"]) + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n"

                        # Write the results to the file
                        serviceResultFile.write(ln)

                        httpConn.close()

                    elif item["type"] == "MapServer":
                        serviceManifestUrl = ""

                        # Build the Service URL
                        if folder:
                            sUrl = "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"])
                            serviceManifestUrl = "/arcgis/admin/services/%s%s.%s/%s" %(folder,item["serviceName"], item["type"],"iteminfo")
                        else:
                            sUrl = "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"])
                            serviceManifestUrl = "/arcgis/admin/services/%s.%s/%s" %(item["serviceName"], item["type"],"iteminfo")

                        # Submit the request to the server
                        httpConn.request("POST", sUrl, params, headers)

                        # Get the response
                        servResponse = httpConn.getresponse()
                        readData = servResponse.read()
                        jsonOBJ = json.loads(readData)

                        # Build the Service URL to test the running status
                        if folder:
                            statusUrl = "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"])
                        else:
                            statusUrl = "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"])


                        # Submit the request to the server
                        httpConn.request("POST", statusUrl, params, headers)

                        # Get the response
                        servStatusResponse = httpConn.getresponse()
                        readData = servStatusResponse.read()
                        jsonOBJStatus = json.loads(readData)

                        # Check for Map Cache
                        isCached = jsonOBJ["properties"]["isCached"]
                        if isCached == "true":
                            cacheDir = str(jsonOBJ["properties"]["cacheDir"])
                        else:
                            cacheDir = jsonOBJ["properties"]["isCached"]

                        if len(jsonOBJ["extensions"]) == 0:
                            # Build the line to write to the output file
                            ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "FeatServHolder" + "," + "Disabled" + "," + "Disabled" +"," + str(jsonOBJ["properties"]["maxRecordCount"]) + "," + str(jsonOBJ["clusterName"]) + "," + cacheDir + "," + "NA" + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n"
                        else:
                            # Extract the KML properties from the response
                            kmlProps = [mapKML for mapKML in jsonOBJ["extensions"] if mapKML["typeName"] == 'KmlServer']#.items()[0][1] == 'KmlServer']

                            # Extract the WMS properties from the response
                            wmsProps = [mapWMS for mapWMS in jsonOBJ["extensions"] if mapWMS["typeName"] == 'WMSServer']#.items()[0][1] == 'WMSServer']

                            # Extract the FeatureService properties from the response
                            featServProps = [featServ for featServ in jsonOBJ["extensions"] if featServ["typeName"] == 'FeatureServer']#.items()[0][1] == 'FeatureServer']

                            if len(featServProps) > 0:
                                featureStatus = str(featServProps[0]["enabled"])
                            else:
                                featureStatus = "NA"

                            if len(kmlProps) > 0:
                                kmlStatus = str(kmlProps[0]["enabled"])
                            else:
                                kmlStatus = "NA"

                            if len(wmsProps) > 0:
                                wmsStatus = str(wmsProps[0]["enabled"])
                            else:
                                wmsStatus = "NA"


                            ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + featureStatus + "," + kmlStatus + "," + wmsStatus +"," + str(jsonOBJ["properties"]["maxRecordCount"]) + "," + str(jsonOBJ["clusterName"]) + "," + cacheDir + "," + "NA" + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n"

                        # Write the results to the file
                        serviceResultFile.write(ln)
                        # Get service manifest info
                        serviceManifestInfo(serviceManifestUrl,httpConn,params, headers)

                    else:
                        # Close the connection to the current service
                        httpConn.close()

        # Close the file
        serviceResultFile.close()



def serviceManifestInfo(in_url_fragment,httpConn,params, headers):

    # Submit the request to the server
    httpConn.request("POST", in_url_fragment + "/manifest/manifest.json", params, headers)
    # Get the response
    manifestServResponse = httpConn.getresponse()
    manifestJsonOBJ = json.loads(manifestServResponse.read())

    httpConn.close()

    dbObj = manifestJsonOBJ["databases"]

    for resource in dbObj:
        if len(dbObj) > 0:
           print '\nhttps://servername' + in_url_fragment
           #print resource["onServerConnectionString"]
           #print resource["onPremiseConnectionString"]
           print 'byReference ' + str(resource["byReference"])
           print 'onServerWorkspace ' + str(resource["onServerWorkspaceFactoryProgID"])

           if ("Sde" in resource["onServerWorkspaceFactoryProgID"] ):
               print 'onServerConnectionString ' + str(regexpSERVER.search(resource["onServerConnectionString"]).group(0))
               print 'onServerConnectionString ' + str(regexpVERSION.search(resource["onServerConnectionString"]).group(0))
               print 'onServerConnectionString ' + str(regexpDATABASE.search(resource["onServerConnectionString"]).group(0))

           tempDataSets = resource["datasets"]
           if len(tempDataSets) > 0:
               for dataset in tempDataSets:
                   print 'dataset ' + str(dataset['onServerName'])

def getToken(username, password, serverName, serverPort):
    # 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."
        return
    else:
        data = response.read()
        httpConn.close()

        # Check that data returned is not an error object
        if not assertJsonSuccess(data):
            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):
    obj = json.loads(data)
    if 'status' in obj and obj['status'] == "error":
        print "Error: JSON object returns an error. " + str(obj)
        return False
    else:
        return True

if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))

View solution in original post

0 Kudos
5 Replies
SethLewis1
Frequent Contributor

With admin privileges to the ArcGIS Server instance, the following script will print the service, onServerWorkspace, onServerConnectionStrings for the underlying database server, version and database as well as a list of tables/feature class objects that are referenced in the service. It'll also output a CSV listing all services and their properties but I think the print statements are what you're looking for.

# Reads the following properties from services and writes them to a comma-delimited file:
#  ServiceName, Folder, Type, Status, Min Instances, Max Instances, KML,
#  WMS, Max Records, Cluster, Cache Directory, Jobs Directory, Output Directory

# For HTTP calls
import httplib, urllib, json

# For system tools
import sys

# For reading passwords without echoing
import getpass

import uuid
import re

regexpSERVER = re.compile("SERVER=([^;]*);")
regexpVERSION = re.compile("VERSION=([^;]*);")
regexpDATABASE = re.compile("DATABASE=([^;]*);")

def main(argv=None):
    # Ask for admin/publisher user name and password
    username = "username"
    password = "password"

    # Ask for server name & port
    serverName = "servername"
    serverPort = 6080

    # Get the location and the name of the file to be created
    resultFile = str(uuid.uuid4()) + ".csv"

    # Get a token
    token = getToken(username, password, serverName, serverPort)

    # Get the root info
    serverURL = "/arcgis/admin/services/"

    # 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", serverURL, params, headers)

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

        # Check that data returned is not an error object
        if not assertJsonSuccess(data):
            print "Error when reading server information. " + str(data)
            return
        else:
            print "Processed server information successfully. Now processing folders..."

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

        #Store the Folders in a list to loop on
        folders = dataObj["folders"]

        #Remove the System and Utilities folders
        folders.remove("System")
        folders.remove("Utilities")

        #Add an entry for the root folder
        folders.append("")

        #Create the summary file of services
        serviceResultFile = open(resultFile,'w')
        serviceResultFile.write("ServiceName,Folder,Type,Status,Min Instances,Max Instances,FeatureService,kml,wms,Max Records,Cluster,Cache Directory,Jobs Directory,Output Directory" + "\n")

        #Loop on the found folders and discover the services and write the service information
        for folder in folders:

            # Determine if the loop is working on the root folder or not
            if folder != "":
                folder += "/"

            # Build the URL for the current folder
            folderURL = "/arcgis/admin/services/" + folder
            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."
                return
            else:
                data = response.read()

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

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

                # Loop through each service in the folder
                for item in dataObj['services']:

                    if item["type"] == "GeometryServer":# and folder == "":
                        # Build the Service URL
                        if folder:
                            sUrl = "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"])
                            statusUrl = "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"])
                        else:
                            sUrl = "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"])
                            statusUrl = "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"])



                        httpConn.request("POST", sUrl, params, headers)

                        # Get the response
                        servResponse = httpConn.getresponse()
                        readData = servResponse.read()
                        jsonOBJ = json.loads(readData)

                        # Build the Service URL to test the running status


                        # Submit the request to the server
                        httpConn.request("POST", statusUrl, params, headers)
                        servStatusResponse = httpConn.getresponse()

                        # Obtain the data from the response
                        readData = servStatusResponse.read()
                        jsonOBJStatus = json.loads(readData)

                        # Build the line to write to the output file
                        ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["clusterName"]) + "," + "NA" + "," + "NA" + "," + "NA" +"\n"

                        # Write the results to the file
                        serviceResultFile.write(ln)

                        httpConn.close()

                    elif item["type"] == "SearchServer":# and folder == "":
                        if folder:
                            sUrl = "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"])
                            statusUrl = "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"])
                        else:
                            sUrl = "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"])
                            statusUrl = "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"])


                        httpConn.request("POST", sUrl, params, headers)

                        # Get the response
                        servResponse = httpConn.getresponse()
                        readData = servResponse.read()
                        jsonOBJ = json.loads(readData)


                        # Submit the request to the server
                        httpConn.request("POST", statusUrl, params, headers)

                        # Get the response
                        servStatusResponse = httpConn.getresponse()
                        readData = servStatusResponse.read()
                        jsonOBJStatus = json.loads(readData)

                        # Build the line to write to the output file
                        ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["clusterName"]) + "," + "NA" + "," + str(jsonOBJ["properties"]["jobsDirectory"]) + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n"

                        # Write the results to the file
                        serviceResultFile.write(ln)

                        httpConn.close()

                    elif item["type"] == "ImageServer":

                        # Build the Service URL
                        if folder:
                            sUrl = "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"])
                        else:
                            sUrl = "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"])

                        # Submit the request to the server
                        httpConn.request("POST", sUrl, params, headers)

                        # Get the response
                        servResponse = httpConn.getresponse()
                        readData = servResponse.read()
                        jsonOBJ = json.loads(readData)

                        # Build the Service URL to test the running status
                        if folder:
                            statusUrl = "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"])
                        else:
                            statusUrl = "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"])

                        # Submit the request to the server
                        httpConn.request("POST", statusUrl, params, headers)

                        # Get the response
                        servStatusResponse = httpConn.getresponse()
                        readData = servStatusResponse.read()
                        jsonOBJStatus = json.loads(readData)

                        # Extract the WMS properties from the response
                        wmsProps = [imageWMS for imageWMS in jsonOBJ["extensions"] if imageWMS["typeName"] == 'WMSServer']#.items()[0][1] == 'WMSServer']

                        if len(wmsProps) > 0:
                            wmsStatus = str(wmsProps[0]["enabled"])
                        else:
                            wmsStatus = "NA"

                        # Build the line to write to the output file
                        ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + wmsStatus +"," + "NA" + "," + str(jsonOBJ["clusterName"]) + "," + str(jsonOBJ["properties"]["cacheDir"]) + "," + "NA," + str(jsonOBJ["properties"]["outputDir"]) +"\n"

                        # Write the results to the file
                        serviceResultFile.write(ln)

                        httpConn.close()

                    elif item["type"] == "GlobeServer":

                        # Build the Service URL
                        if folder:
                            sUrl = "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"])
                        else:
                            sUrl = "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"])

                        # Submit the request to the server
                        httpConn.request("POST", sUrl, params, headers)

                        # Get the response
                        servResponse = httpConn.getresponse()
                        readData = servResponse.read()
                        jsonOBJ = json.loads(readData)

                        #Build the Service URL to test the running status
                        if folder:
                            statusUrl = "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"])
                        else:
                            statusUrl = "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"])

                        # Submit the request to the server
                        httpConn.request("POST", statusUrl, params, headers)

                        # Get the response
                        servStatusResponse = httpConn.getresponse()
                        readData = servStatusResponse.read()
                        jsonOBJStatus = json.loads(readData)

                        # Build the line to write to the output file
                        ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["properties"]["maxRecordCount"]) + "," + str(jsonOBJ["clusterName"]) + "," + str(jsonOBJ["properties"]["cacheDir"]) + "," + "NA" + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n"

                        # Write the results to the file
                        serviceResultFile.write(ln)

                        httpConn.close()

                    elif item["type"] == "GPServer":

                        # Build the Service URL
                        if folder:
                            sUrl = "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"])
                        else:
                            sUrl = "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"])

                        # Submit the request to the server
                        httpConn.request("POST", sUrl, params, headers)

                        # Get the response
                        servResponse = httpConn.getresponse()
                        readData = servResponse.read()
                        jsonOBJ = json.loads(readData)

                        # Build the Service URL to test the running status
                        if folder:
                            statusUrl = "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"])
                        else:
                            statusUrl = "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"])

                        # Submit the request to the server
                        httpConn.request("POST", statusUrl, params, headers)

                        # Get the response
                        servStatusResponse = httpConn.getresponse()
                        readData = servStatusResponse.read()
                        jsonOBJStatus = json.loads(readData)

                        # Build the line to write to the output file
                        ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["clusterName"]) + "," + "NA" + "," + str(jsonOBJ["properties"]["jobsDirectory"]) + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n"

                        # Write the results to the file
                        serviceResultFile.write(ln)

                        httpConn.close()

                    elif item["type"] == "GeocodeServer":

                        # Build the Service URL
                        if folder:
                            sUrl = "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"])
                        else:
                            sUrl = "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"])

                        # Submit the request to the server
                        httpConn.request("POST", sUrl, params, headers)

                        # Get the response
                        servResponse = httpConn.getresponse()
                        readData = servResponse.read()
                        jsonOBJ = json.loads(readData)

                        if folder:
                            statusUrl = "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"])
                        else:
                            statusUrl = "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"])

                        # Submit the request to the server
                        httpConn.request("POST", statusUrl, params, headers)

                        # Get the response
                        servStatusResponse = httpConn.getresponse()
                        readData = servStatusResponse.read()
                        jsonOBJStatus = json.loads(readData)

                        # Build the line to write to the output file
                        ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["clusterName"]) + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n"

                        # Write the results to the file
                        serviceResultFile.write(ln)

                        httpConn.close()

                    elif item["type"] == "GeoDataServer":

                        # Build the Service URL
                        if folder:
                            sUrl = "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"])
                        else:
                            sUrl = "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"])

                        # Submit the request to the server
                        httpConn.request("POST", sUrl, params, headers)

                        # Get the response
                        servResponse = httpConn.getresponse()
                        readData = servResponse.read()
                        jsonOBJ = json.loads(readData)

                        if folder:
                            statusUrl = "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"])
                        else:
                            statusUrl = "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"])

                        # Submit the request to the server
                        httpConn.request("POST", statusUrl, params, headers)

                        # Get the response
                        servStatusResponse = httpConn.getresponse()
                        readData = servStatusResponse.read()
                        jsonOBJStatus = json.loads(readData)

                        # Build the line to write to the output file
                        ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["properties"]["maxRecordCount"]) + "," + str(jsonOBJ["clusterName"]) + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n"

                        # Write the results to the file
                        serviceResultFile.write(ln)

                        httpConn.close()

                    elif item["type"] == "MapServer":
                        serviceManifestUrl = ""

                        # Build the Service URL
                        if folder:
                            sUrl = "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"])
                            serviceManifestUrl = "/arcgis/admin/services/%s%s.%s/%s" %(folder,item["serviceName"], item["type"],"iteminfo")
                        else:
                            sUrl = "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"])
                            serviceManifestUrl = "/arcgis/admin/services/%s.%s/%s" %(item["serviceName"], item["type"],"iteminfo")

                        # Submit the request to the server
                        httpConn.request("POST", sUrl, params, headers)

                        # Get the response
                        servResponse = httpConn.getresponse()
                        readData = servResponse.read()
                        jsonOBJ = json.loads(readData)

                        # Build the Service URL to test the running status
                        if folder:
                            statusUrl = "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"])
                        else:
                            statusUrl = "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"])


                        # Submit the request to the server
                        httpConn.request("POST", statusUrl, params, headers)

                        # Get the response
                        servStatusResponse = httpConn.getresponse()
                        readData = servStatusResponse.read()
                        jsonOBJStatus = json.loads(readData)

                        # Check for Map Cache
                        isCached = jsonOBJ["properties"]["isCached"]
                        if isCached == "true":
                            cacheDir = str(jsonOBJ["properties"]["cacheDir"])
                        else:
                            cacheDir = jsonOBJ["properties"]["isCached"]

                        if len(jsonOBJ["extensions"]) == 0:
                            # Build the line to write to the output file
                            ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "FeatServHolder" + "," + "Disabled" + "," + "Disabled" +"," + str(jsonOBJ["properties"]["maxRecordCount"]) + "," + str(jsonOBJ["clusterName"]) + "," + cacheDir + "," + "NA" + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n"
                        else:
                            # Extract the KML properties from the response
                            kmlProps = [mapKML for mapKML in jsonOBJ["extensions"] if mapKML["typeName"] == 'KmlServer']#.items()[0][1] == 'KmlServer']

                            # Extract the WMS properties from the response
                            wmsProps = [mapWMS for mapWMS in jsonOBJ["extensions"] if mapWMS["typeName"] == 'WMSServer']#.items()[0][1] == 'WMSServer']

                            # Extract the FeatureService properties from the response
                            featServProps = [featServ for featServ in jsonOBJ["extensions"] if featServ["typeName"] == 'FeatureServer']#.items()[0][1] == 'FeatureServer']

                            if len(featServProps) > 0:
                                featureStatus = str(featServProps[0]["enabled"])
                            else:
                                featureStatus = "NA"

                            if len(kmlProps) > 0:
                                kmlStatus = str(kmlProps[0]["enabled"])
                            else:
                                kmlStatus = "NA"

                            if len(wmsProps) > 0:
                                wmsStatus = str(wmsProps[0]["enabled"])
                            else:
                                wmsStatus = "NA"


                            ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + featureStatus + "," + kmlStatus + "," + wmsStatus +"," + str(jsonOBJ["properties"]["maxRecordCount"]) + "," + str(jsonOBJ["clusterName"]) + "," + cacheDir + "," + "NA" + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n"

                        # Write the results to the file
                        serviceResultFile.write(ln)
                        # Get service manifest info
                        serviceManifestInfo(serviceManifestUrl,httpConn,params, headers)

                    else:
                        # Close the connection to the current service
                        httpConn.close()

        # Close the file
        serviceResultFile.close()



def serviceManifestInfo(in_url_fragment,httpConn,params, headers):

    # Submit the request to the server
    httpConn.request("POST", in_url_fragment + "/manifest/manifest.json", params, headers)
    # Get the response
    manifestServResponse = httpConn.getresponse()
    manifestJsonOBJ = json.loads(manifestServResponse.read())

    httpConn.close()

    dbObj = manifestJsonOBJ["databases"]

    for resource in dbObj:
        if len(dbObj) > 0:
           print '\nhttps://servername' + in_url_fragment
           #print resource["onServerConnectionString"]
           #print resource["onPremiseConnectionString"]
           print 'byReference ' + str(resource["byReference"])
           print 'onServerWorkspace ' + str(resource["onServerWorkspaceFactoryProgID"])

           if ("Sde" in resource["onServerWorkspaceFactoryProgID"] ):
               print 'onServerConnectionString ' + str(regexpSERVER.search(resource["onServerConnectionString"]).group(0))
               print 'onServerConnectionString ' + str(regexpVERSION.search(resource["onServerConnectionString"]).group(0))
               print 'onServerConnectionString ' + str(regexpDATABASE.search(resource["onServerConnectionString"]).group(0))

           tempDataSets = resource["datasets"]
           if len(tempDataSets) > 0:
               for dataset in tempDataSets:
                   print 'dataset ' + str(dataset['onServerName'])

def getToken(username, password, serverName, serverPort):
    # 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."
        return
    else:
        data = response.read()
        httpConn.close()

        # Check that data returned is not an error object
        if not assertJsonSuccess(data):
            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):
    obj = json.loads(data)
    if 'status' in obj and obj['status'] == "error":
        print "Error: JSON object returns an error. " + str(obj)
        return False
    else:
        return True

if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))
0 Kudos
AdamChavez
Deactivated User

I believe this is what I was looking for! Thank you for the script!

0 Kudos
SethLewis1
Frequent Contributor

Glad it helped Adam. Don't forget to mark this discussion as Answered if your question was solved.

0 Kudos
AdamChavez
Deactivated User

Is there any possible chance that you still have a ArcGIS Server 10.0 scripts available?

0 Kudos
SethLewis1
Frequent Contributor

I don't have any 10.0 specific scripts.

0 Kudos