And here an example of crawling through the service folders. Haven't used it for a while...:
import json
import urllib2
def main():
import codecs
import sys
import datetime
import os
log_folder = r'C:\GeoNet\ServiceHealth\txt'
dct_srvr = {"DEV 10.4.1": "https://myDEVservername",
"TEST 10.4.1": "https://myTESTservername",
"PROD 10.4.1": "https://myPRODservername"}
for a, s in dct_srvr.items():
baseUrl = "{0}/arcgis/rest/services".format(s)
svr = s.replace('https://', '')
file_name = 'REST_services_{0}_{1}.txt'.format(svr, datetime.date.today())
log_file = os.path.join(log_folder, file_name)
print log_file
with codecs.open(log_file, 'w', encoding='utf-8') as f:
f.write(u"DateTime\tEnvironment\tServiceName\tServiceType\tConnection\tLayername\tRESTcode\r\n")
getCatalog(baseUrl, f, a)
def getCatalog(baseUrl, f, ambiente):
try:
catalog = json.load(urllib2.urlopen(baseUrl + "/" + "?f=json"))
if "error" in catalog:
return
services = catalog['services']
for service in services:
response = json.load(urllib2.urlopen("{0}/{1}/{2}?f=json".format(baseUrl, service['name'], service['type'])))
URL = "{0}/{1}/{2}".format(baseUrl, service['name'], service['type'])
writeData(f, to_unicode_or_burst(URL), to_unicode_or_burst(ambiente), to_unicode_or_burst(service['name']), to_unicode_or_burst(service['type']), u'ERROR' if "error" in response else u'SUCCESS')
folders = catalog['folders']
for folderName in folders:
try:
catalog = json.load(urllib2.urlopen(baseUrl + "/" + folderName + "?f=json"))
if "error" in catalog:
return
services = catalog['services']
for service in services:
response = json.load(urllib2.urlopen("{0}/{1}/{2}?f=json".format(baseUrl, service['name'], service['type'])))
URL = "{0}/{1}/{2}".format(baseUrl, service['name'], service['type'])
writeData(f, to_unicode_or_burst(URL), to_unicode_or_burst(ambiente), to_unicode_or_burst(service['name']), to_unicode_or_burst(service['type']), u'ERROR' if "error" in response else u'SUCCESS')
except Exception as e:
f.write(u"{0}\t{1}\t{2}\t{3}\t{4}\n".format(getDateTime(), to_unicode_or_burst(ambiente), to_unicode_or_burst(e), to_unicode_or_burst(folderName), u"ERROR"))
except Exception as e:
f.write(u"{0}\t{1}\t{2}\t{3}\t{4}\n".format(getDateTime(), to_unicode_or_burst(ambiente), to_unicode_or_burst(e), u"ROOT", u"ERROR"))
def to_unicode_or_burst(obj, encoding='utf-8'):
if isinstance(obj, basestring):
if not isinstance(obj, unicode):
try:
obj = unicode(obj, encoding)
except Exception as e:
print e
return obj
def getDateTime():
from datetime import datetime
return datetime.now().strftime('%Y-%m-%d %H:%M:%S')
def getServiceProperties(URL, prop):
fURL = URL + "?f=json"
openURL = urllib2.urlopen(fURL, '').read()
outJson = json.loads(openURL)
if prop in outJson:
return outJson[prop]
else:
return ""
def writeData(f, URL, ambiente, service_name, service_type, status):
prop = "layers"
layers = getServiceProperties(URL, prop)
for lyr in layers:
try:
rest_code = lyr["id"]
except:
rest_code = "-1"
try:
layer_name = lyr["name"]
except:
layer_name = "utf8-error"
f.write(u"{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\r\n".format(getDateTime(), ambiente, service_name, service_type, status, layer_name, rest_code))
if __name__ == '__main__':
main()