Query server logs using python script not returning results

1920
4
Jump to solution
09-29-2016 08:24 AM
RachelAlbritton
Occasional Contributor III

I've adapted this python script to query a server across all of our servers for the past 24 hours. The script runs, but the error logs return empty when they shouldn't and I can't figure out why. Any thoughts?

Script (partial):

# Ask for map service name
 mapService = "service/mapservices/air_photo_hi_res-fall_2014.MapServer"
for serverName in serverList:
 
 # Get a token
 token = getToken(username, password, serverName, serverPort)
 if token == "":
 print "Could not generate a token with the username and password provided."
 return
 
 # Construct URL to query the logs
 logQueryURL = "/arcgis/admin/logs/query"
 logFilter = "{'services': ['" + mapService + "']}"
 
 # Supply the log level, filter, token, and return format
 now = datetime.datetime.now()
 params = urllib.urlencode({
 'startTime': now.strftime('%Y-%m-%dT%H:%M:%S,%f'), 
 'endTime': (now-datetime.timedelta(hours = 24)).strftime('%Y-%m-%dT%H:%M:%S,%f'), 
 'level': 'WARNING',
 'filter': logFilter,
 'token': token,
 'pageSize': 100,
 'f': 'JSON'})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
 
 # Connect to URL and post parameters
 httpConn = httplib.HTTPConnection(serverName, serverPort)
 request = httpConn.request("POST",logQueryURL, params, headers) 
 
 # Read response
 print serverName
 response = httpConn.getresponse()
 #print response.status
 if (response.status != 200):
 httpConn.close()
 print "Error while querying logs."
 return
 else:
 data = response.read()
 print data
# Check that data returned is not an error object
 if not assertJsonSuccess(data): 
 print "Error returned by operation. " + data
 else:
 print "Operation completed successfully!"
# Deserialize response into Python object
 dataObj = json.loads(data)
 httpConn.close()

Results (for each server listed):

{"hasMore":false,"startTime":0,"endTime":0,"logMessages":[]}

But if I query the logs directly, I get the errors that I expect to see:

{   "hasMore": true,   "startTime": 1475158749762,   "endTime": 1472583010032,   "logMessages": [     {       "type": "SEVERE",       "message": "Unable to process request. json",       "time": 1475158749762,       "source": "Rest",       "machine": "SERVER001",       "user": "",       "code": 9003,       "elapsed": "",       "process": "26244",       "thread": "39",       "methodName": ""     },
0 Kudos
1 Solution

Accepted Solutions
JonathanQuinn
Esri Notable Contributor

If I submit a request with just the 'services' parameter for the filter, I get no results:


If I include all of the default paramters, I do:

Since you want the results from specific servers, what if you try to include the default parameters for the filter:

{"codes":[],
"processIds":[],
"server": "*",
"services": ["SampleWorldCities.MapServer"],
"machines": "*"}

But pass in the server name for the "server" value through the for loop.  I'm not sure why errors unrelated to SampleWorldCities are returning, but that's a separate problem.

View solution in original post

4 Replies
JonathanQuinn
Esri Notable Contributor

If I submit a request with just the 'services' parameter for the filter, I get no results:


If I include all of the default paramters, I do:

Since you want the results from specific servers, what if you try to include the default parameters for the filter:

{"codes":[],
"processIds":[],
"server": "*",
"services": ["SampleWorldCities.MapServer"],
"machines": "*"}

But pass in the server name for the "server" value through the for loop.  I'm not sure why errors unrelated to SampleWorldCities are returning, but that's a separate problem.

RachelAlbritton
Occasional Contributor III

That worked! Thanks!

0 Kudos
DarrylKlassen1
Occasional Contributor II

I am having the same issue, so I modified my Params to this:

params = urllib.urlencode({
    'startTime':starttime,
    'endTime':endtime,
    'server':serverName,
    'level': 'WARNING',
    'filter': logFilter,
    'token': token,
    'pageSize': 100,
    'f': 'json'})

but I am still getting a output of : {u'logMessages': [], u'endTime': 0, u'hasMore': False, u'startTime': 0}  Not sure what I am doing wrong.

0 Kudos
Ranga_Tolapi
Occasional Contributor III

@JonathanQuinn any findings regarding the below. We are also getting unrelated results when passed "services" parameter with a service name. We are using ArcGIS Server 10.8.1.

 


@JonathanQuinn wrote:

I'm not sure why errors unrelated to SampleWorldCities are returning, but that's a separate problem.




0 Kudos