Skip to content

Commit ca20613

Browse files
committed
Merge pull request #459 from HubSpot/logfetch_history_search
Logfetch v12 (WIP)
2 parents d3bd662 + 019b4a2 commit ca20613

File tree

3 files changed

+46
-24
lines changed

3 files changed

+46
-24
lines changed

scripts/logfetch/entrypoint.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
import os
55
import pkg_resources
6+
from datetime import datetime
67
from termcolor import colored
78
from fake_section_head import FakeSectionHead
89
from live_logs import download_live_logs
@@ -56,6 +57,22 @@ def check_dest(args):
5657
if not os.path.exists(args.dest):
5758
os.makedirs(args.dest)
5859

60+
def check_args(args):
61+
if args.deployId and not args.requestId:
62+
exit("Must specify request-id (-r) when specifying deploy-id")
63+
elif not args.requestId and not args.deployId and not args.taskId:
64+
exit('Must specify one of\n -t task-id\n -r request-id and -d deploy-id\n -r request-id')
65+
66+
def convert_to_days(argument):
67+
try:
68+
val = int(argument)
69+
except:
70+
try:
71+
val = (datetime.now() - datetime.strptime(argument, "%m-%d-%Y")).days
72+
except:
73+
exit('Start/End days value must be either a number of days or a date in format "mm-dd-yyyy"')
74+
return val
75+
5976
def fetch():
6077
conf_parser = argparse.ArgumentParser(version=VERSION, description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, add_help=False)
6178
conf_parser.add_argument("-f", "--conf-folder", dest='conf_folder', help="specify a folder for config files to live")
@@ -70,7 +87,8 @@ def fetch():
7087
"chunk_size" : DEFAULT_CHUNK_SIZE,
7188
"dest" : DEFAULT_DEST,
7289
"task_count" : DEFAULT_TASK_COUNT,
73-
"start_days" : DEFAULT_DAYS
90+
"start_days" : DEFAULT_DAYS,
91+
"end_days" : 0 #today
7492
}
7593

7694
try:
@@ -91,17 +109,16 @@ def fetch():
91109
parser.add_argument("-n", "--num-parallel-fetches", dest="num_parallel_fetches", help="Number of fetches to make at once", type=int)
92110
parser.add_argument("-cs", "--chunk-size", dest="chunk_size", help="Chunk size for writing from response to filesystem", type=int)
93111
parser.add_argument("-u", "--singularity-uri-base", dest="singularity_uri_base", help="The base for singularity (eg. http://localhost:8080/singularity/v1)")
94-
parser.add_argument("-s", "--start-days", dest="start_days", help="Search for logs no older than this many days", type=int)
95-
parser.add_argument("-e", "--end-days", dest="end_days", help="Search for logs no new than this many days (defaults to None/today)", type=int)
112+
parser.add_argument("-s", "--start-days", dest="start_days", help="Search for logs no older than this, can be an integer number of days or date in format 'mm-dd-yyyy'")
113+
parser.add_argument("-e", "--end-days", dest="end_days", help="Search for logs no newer than this, can be an integer number of days or date in format 'mm-dd-yyyy' (defaults to None/today)")
96114
parser.add_argument("-l", "--log-type", dest="logtype", help="Logfile type to downlaod (ie 'access.log'), can be a glob (ie *.log)")
97115
parser.add_argument("-g", "--grep", dest="grep", help="Regex to grep for (normal grep syntax) or a full grep command")
98116

99117
args = parser.parse_args(remaining_argv)
100118

101-
if args.deployId and not args.requestId:
102-
exit("Must specify request-id (-r) when specifying deploy-id")
103-
elif not args.requestId and not args.deployId and not args.taskId:
104-
exit('Must specify one of\n -t task-id\n -r request-id and -d deploy-id\n -r request-id')
119+
check_args(args)
120+
args.start_days = convert_to_days(args.start_days)
121+
args.end_days = convert_to_days(args.end_days)
105122

106123
args.dest = os.path.expanduser(args.dest)
107124

@@ -121,7 +138,8 @@ def cat():
121138
"chunk_size" : DEFAULT_CHUNK_SIZE,
122139
"dest" : DEFAULT_DEST,
123140
"task_count" : DEFAULT_TASK_COUNT,
124-
"start_days" : DEFAULT_DAYS
141+
"start_days" : DEFAULT_DAYS,
142+
"end_days" : 0 #today
125143
}
126144

127145
try:
@@ -142,16 +160,15 @@ def cat():
142160
parser.add_argument("-n", "--num-parallel-fetches", dest="num_parallel_fetches", help="Number of fetches to make at once", type=int)
143161
parser.add_argument("-cs", "--chunk-size", dest="chunk_size", help="Chunk size for writing from response to filesystem", type=int)
144162
parser.add_argument("-u", "--singularity-uri-base", dest="singularity_uri_base", help="The base for singularity (eg. http://localhost:8080/singularity/v1)")
145-
parser.add_argument("-s", "--start-days", dest="start_days", help="Search for logs no older than this many days", type=int)
146-
parser.add_argument("-e", "--end-days", dest="end_days", help="Search for logs no new than this many days (defaults to None/today)", type=int)
163+
parser.add_argument("-s", "--start-days", dest="start_days", help="Search for logs no older than this, can be an integer number of days or date in format 'mm-dd-yyyy'")
164+
parser.add_argument("-e", "--end-days", dest="end_days", help="Search for logs no newer than this, can be an integer number of days or date in format 'mm-dd-yyyy' (defaults to None/today)")
147165
parser.add_argument("-l", "--logtype", dest="logtype", help="Logfile type to downlaod (ie 'access.log'), can be a glob (ie *.log)")
148166

149167
args = parser.parse_args(remaining_argv)
150168

151-
if args.deployId and not args.requestId:
152-
exit("Must specify requestId (-r) when specifying deploy-id")
153-
elif not args.requestId and not args.deployId and not args.taskId:
154-
exit('Must specify one of\n -t task-id\n -r request-id and -d deploy-id\n -r request-id')
169+
check_args(args)
170+
args.start_days = convert_to_days(args.start_days)
171+
args.end_days = convert_to_days(args.end_days)
155172

156173
args.dest = os.path.expanduser(args.dest)
157174

@@ -188,12 +205,9 @@ def tail():
188205

189206
args = parser.parse_args(remaining_argv)
190207

191-
if args.deployId and not args.requestId:
192-
exit("Must specify request-id (-r) when specifying deploy-id")
193-
elif not args.requestId and not args.deployId and not args.taskId:
194-
exit('Must specify one of\n -t task-id\n -r request-id and -d deploy-id\n -r request-id')
195-
elif not args.logfile:
208+
if not args.logfile:
196209
exit("Must specify logfile to tail (-l)")
210+
check_args(args)
197211

198212
args.dest = os.path.expanduser(args.dest)
199213

scripts/logfetch/s3_logs.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
TASK_FORMAT = '/task/{0}'
1111
S3LOGS_URI_FORMAT = '{0}/logs{1}'
12+
REQUEST_FORMAT = '/request/{0}'
1213

1314
def download_s3_logs(args):
1415
sys.stderr.write(colored('Checking for S3 log files', 'cyan') + '\n')
@@ -37,19 +38,26 @@ def already_downloaded(dest, filename):
3738

3839
def logs_for_all_requests(args):
3940
if args.taskId:
40-
return get_json_response(singularity_s3logs_uri(args, args.taskId))
41+
return get_json_response(s3_task_logs_uri(args, args.taskId))
4142
else:
4243
tasks = logfetch_base.tasks_for_requests(args)
4344
logs = []
4445
for task in tasks:
45-
s3_logs = get_json_response(singularity_s3logs_uri(args, task))
46+
s3_logs = get_json_response(s3_task_logs_uri(args, task))
4647
logs = logs + s3_logs if s3_logs else logs
47-
return logs
48+
sys.stderr.write(colored('Also searching s3 history...\n', 'magenta'))
49+
for request in logfetch_base.all_requests(args):
50+
s3_logs = get_json_response(s3_request_logs_uri(args, request))
51+
logs = logs + s3_logs if s3_logs else logs
52+
return [dict(t) for t in set([tuple(l.items()) for l in logs])]
4853

4954
def time_from_filename(filename):
5055
time_string = re.search('(\d{13})', filename).group(1)
5156
return int(time_string[0:-3])
5257

53-
def singularity_s3logs_uri(args, idString):
58+
def s3_task_logs_uri(args, idString):
5459
return S3LOGS_URI_FORMAT.format(logfetch_base.base_uri(args), TASK_FORMAT.format(idString))
5560

61+
def s3_request_logs_uri(args, idString):
62+
return S3LOGS_URI_FORMAT.format(logfetch_base.base_uri(args), REQUEST_FORMAT.format(idString))
63+

scripts/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
setup(
1212
name='singularity-logfetch',
13-
version='0.11.0',
13+
version='0.12.0',
1414
description='Singularity log fetching and searching',
1515
author="HubSpot",
1616
author_email='singularity-users@googlegroups.com',

0 commit comments

Comments
 (0)