Skip to content

Commit

Permalink
added download option
Browse files Browse the repository at this point in the history
  • Loading branch information
Imron Alston committed Dec 4, 2018
1 parent 030a8f4 commit 2ba621d
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions scalyr
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,84 @@ def commandListFiles(parser):
for i in range(len(paths)):
print(paths[i])

# Implement the "scalyr download-log" command
def commandDownloadLog(parser):

# Parse the command-line arguments.
parser.add_argument('--serverHost', default='',
help='the serverHost containing the log file that you wish to download - cannot be empty')
parser.add_argument('--logfile', default='agent.log',
help='the logfile on the serverHost that you want to download - cannot be empty. If the logfile does '
'not start with a `/` then the logfile is assumed to be relative to /var/log/scalyr-agent-2')
parser.add_argument('--start', default='',
help='beginning of the time range to query')
parser.add_argument('--end', default='',
help='end of the time range to query')

args = parser.parse_args()

server_host = args.serverHost
if server_host == '':
print_stderr('serverHost cannot be empty. Please specify a serverHost with the --serverHost argument')
sys.exit(1)

log_file = args.logfile
if log_file == '':
print_stderr('logfile cannot be empty. Please specify a logfile with the --logfile argument')
sys.exit(1)

if not log_file.startswith( '/' ):
log_file = '/var/log/scalyr-agent-2/' + log_file

# Get the API token.
apiToken = getApiToken(args, 'scalyr_readlog_token', 'Read Logs')

mode = 'head'
pageSize = 5000
start = ''
end = ''
priority = 'low'
output = 'multiline'

query = '$logfile = "%s" $serverHost = "%s"' % (log_file, server_host)

has_rows = True
continuation_token = None

while has_rows:
params = {
"token": apiToken,
"queryType": "log",
"filter": query,
"startTime": start,
"endTime": end,
"maxCount": pageSize,
"pageMode": mode,
"columns": 'message',
"priority": priority
}

if continuation_token is not None:
params['continuationToken'] = continuation_token

# Send the query to the server.
response, rawResponse = sendRequest(args, '/api/query', params )

# Print the log records.
matches = response['matches']

# Readable text format (singleline or multiline)
for i in range(len(matches)):
printReadableRow(output, matches[i])

continuation_token = None
if 'continuationToken' in response:
continuation_token = response['continuationToken']
else:
has_rows = False

if len( matches ) == 0:
has_rows = False

# Implement the "scalyr query" command.
def commandQuery(parser):
Expand Down Expand Up @@ -612,6 +690,7 @@ if __name__ == '__main__':
# All available commands
all_commands = {
'query': commandQuery,
'download-log': commandDownloadLog,
'tail': commandTail,
'numeric-query': commandNumericQuery,
'facet-query': commandFacetQuery,
Expand Down

0 comments on commit 2ba621d

Please sign in to comment.