@@ -225,6 +225,84 @@ def commandListFiles(parser):
225225 for i in range (len (paths )):
226226 print (paths [i ])
227227
228+ # Implement the "scalyr download-log" command
229+ def commandDownloadLog (parser ):
230+
231+ # Parse the command-line arguments.
232+ parser .add_argument ('--serverHost' , default = '' ,
233+ help = 'the serverHost containing the log file that you wish to download - cannot be empty' )
234+ parser .add_argument ('--logfile' , default = 'agent.log' ,
235+ help = 'the logfile on the serverHost that you want to download - cannot be empty. If the logfile does '
236+ 'not start with a `/` then the logfile is assumed to be relative to /var/log/scalyr-agent-2' )
237+ parser .add_argument ('--start' , default = '' ,
238+ help = 'beginning of the time range to query' )
239+ parser .add_argument ('--end' , default = '' ,
240+ help = 'end of the time range to query' )
241+
242+ args = parser .parse_args ()
243+
244+ server_host = args .serverHost
245+ if server_host == '' :
246+ print_stderr ('serverHost cannot be empty. Please specify a serverHost with the --serverHost argument' )
247+ sys .exit (1 )
248+
249+ log_file = args .logfile
250+ if log_file == '' :
251+ print_stderr ('logfile cannot be empty. Please specify a logfile with the --logfile argument' )
252+ sys .exit (1 )
253+
254+ if not log_file .startswith ( '/' ):
255+ log_file = '/var/log/scalyr-agent-2/' + log_file
256+
257+ # Get the API token.
258+ apiToken = getApiToken (args , 'scalyr_readlog_token' , 'Read Logs' )
259+
260+ mode = 'head'
261+ pageSize = 5000
262+ start = ''
263+ end = ''
264+ priority = 'low'
265+ output = 'multiline'
266+
267+ query = '$logfile = "%s" $serverHost = "%s"' % (log_file , server_host )
268+
269+ has_rows = True
270+ continuation_token = None
271+
272+ while has_rows :
273+ params = {
274+ "token" : apiToken ,
275+ "queryType" : "log" ,
276+ "filter" : query ,
277+ "startTime" : start ,
278+ "endTime" : end ,
279+ "maxCount" : pageSize ,
280+ "pageMode" : mode ,
281+ "columns" : 'message' ,
282+ "priority" : priority
283+ }
284+
285+ if continuation_token is not None :
286+ params ['continuationToken' ] = continuation_token
287+
288+ # Send the query to the server.
289+ response , rawResponse = sendRequest (args , '/api/query' , params )
290+
291+ # Print the log records.
292+ matches = response ['matches' ]
293+
294+ # Readable text format (singleline or multiline)
295+ for i in range (len (matches )):
296+ printReadableRow (output , matches [i ])
297+
298+ continuation_token = None
299+ if 'continuationToken' in response :
300+ continuation_token = response ['continuationToken' ]
301+ else :
302+ has_rows = False
303+
304+ if len ( matches ) == 0 :
305+ has_rows = False
228306
229307# Implement the "scalyr query" command.
230308def commandQuery (parser ):
@@ -612,6 +690,7 @@ if __name__ == '__main__':
612690 # All available commands
613691 all_commands = {
614692 'query' : commandQuery ,
693+ 'download-log' : commandDownloadLog ,
615694 'tail' : commandTail ,
616695 'numeric-query' : commandNumericQuery ,
617696 'facet-query' : commandFacetQuery ,
0 commit comments