Created
March 28, 2021 11:55
-
-
Save ifooth/30189d70b6690af2e79d645d66e215e6 to your computer and use it in GitHub Desktop.
log-analyze.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import sys | |
import hashlib | |
def read_log(path): | |
with open(path, "rb") as fh: | |
data = fh.read() | |
return data.splitlines() | |
def refine_log(raw_log): | |
items = raw_log.split() | |
new_items = [] | |
for item in items: | |
item = item.decode("utf-8") | |
# 去掉时间戳 | |
if "ts" in item: | |
continue | |
# 去掉 num_alerts=xxx | |
if "num_alerts" in item: | |
continue | |
new_items.append(item) | |
return " ".join(new_items) | |
def main(path): | |
hash_map = {} | |
for raw_log in read_log(path): | |
log = refine_log(raw_log) | |
hex = hashlib.sha1(log.encode("utf-8")).hexdigest() | |
if hex not in hash_map: | |
print(raw_log) | |
hash_map[hex] = raw_log | |
if __name__ == "__main__": | |
main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment