Created
October 21, 2020 09:45
-
-
Save matt-sm/d87f98160f643c1d3e5fee69b0659f2b to your computer and use it in GitHub Desktop.
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
# Parse syslog file for logs (per hour) that match message | |
# Prior art: https://github.com/dabeaz/generators | |
from dateutil import parser | |
from collections import Counter | |
import re | |
logpats = r"(\S+ \d+ \d+:\d+:\d+) (\S+) (.*]:|.*]\):|.*\):) (.*)" | |
colnames = ("datetime", "host", "process", "message") | |
message = "Killed" | |
loglines = open("/var/log/system.log", "r") | |
logpat = re.compile(logpats) | |
groups = (logpat.match(line) for line in loglines) | |
tuples = (g.groups() for g in groups if g) | |
log = (dict(zip(colnames, t)) for t in tuples) | |
parsed = (r for r in log if message in r["message"]) | |
hours = (parser.parse(p["datetime"]).hour for p in parsed) | |
if __name__ == "__main__": | |
msg_per_hour = Counter(hours) | |
print(msg_per_hour) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment