log-search: Search for user-ids.

This commit is contained in:
Alex Vandiver 2022-04-28 17:54:33 -07:00 committed by Alex Vandiver
parent b355a0a63e
commit 056895cc33
1 changed files with 20 additions and 5 deletions

View File

@ -143,6 +143,7 @@ PYTHON_LOG_LINE_RE = re.compile(
class FilterType(Enum): class FilterType(Enum):
HOSTNAME = auto() HOSTNAME = auto()
CLIENT_IP = auto() CLIENT_IP = auto()
USER_ID = auto()
def main() -> None: def main() -> None:
@ -181,12 +182,15 @@ def main() -> None:
# invalid. However, we expect the input here to already be # invalid. However, we expect the input here to already be
# reasonably well-formed. # reasonably well-formed.
filter = args.filter filter = args.filter
if re.match(r"\d{1,3}(\.\d{1,3}){3}$", filter):
if re.match(r"\d+$", filter):
if args.nginx:
raise parser().error("Cannot parse user-ids with nginx logs; try without --nginx")
string_filter = lambda m: m["user_id"] == filter
filter_type = FilterType.USER_ID
elif re.match(r"\d{1,3}(\.\d{1,3}){3}$", filter):
string_filter = lambda m: m["ip"] == filter string_filter = lambda m: m["ip"] == filter
filter_type = FilterType.CLIENT_IP filter_type = FilterType.CLIENT_IP
elif re.match(r"\d{1,3}\.(\d{1,3}\.){0,2}$", filter):
string_filter = lambda m: m["ip"].startswith(filter)
filter_type = FilterType.CLIENT_IP
elif re.match(r"([a-f0-9:]+:+){1,7}[a-f0-9]+$", filter): elif re.match(r"([a-f0-9:]+:+){1,7}[a-f0-9]+$", filter):
string_filter = lambda m: m["ip"] == filter string_filter = lambda m: m["ip"] == filter
filter_type = FilterType.CLIENT_IP filter_type = FilterType.CLIENT_IP
@ -294,10 +298,21 @@ def print_line(
color = FAIL color = FAIL
url = f"{BOLD}{match['path']}" url = f"{BOLD}{match['path']}"
if filter_type != FilterType.HOSTNAME: if filter_type != FilterType.HOSTNAME:
url = "https://" + match["hostname"] + url hostname = match["hostname"]
if not args.nginx:
if hostname == "root":
hostname = "zulip.com"
else:
hostname += ".zulipchat.com"
url = "https://" + hostname + url
user_id = ""
if not args.nginx and match["user_id"] is not None:
user_id = match["user_id"] + "@"
parts = [ parts = [
ts, ts,
f"{user_id:7}" if not args.nginx and filter_type != FilterType.USER_ID else None,
f"{match['ip']:39}" if filter_type != FilterType.CLIENT_IP else None, f"{match['ip']:39}" if filter_type != FilterType.CLIENT_IP else None,
indicator + match["code"], indicator + match["code"],
f"{match['method']:6}", f"{match['method']:6}",