Page MenuHomePhabricator

parse_fsck.py

Authored By
dcaro
Oct 5 2021, 8:45 AM
Size
2 KB
Subscribers
None

parse_fsck.py

#!/usr/bin/env python3
from pprint import pprint
import click
from datetime import date, datetime
@click.group()
def main():
pass
@click.argument('infile', type=click.File('r'))
@main.command()
def last_occurrence(infile):
minute_to_hosts = {}
host = None
last_host = None
minute_bucket = None
for line in infile:
if line.startswith('('):
host = line.split(")", 1)[-1].strip()
if last_host is not None and minute_bucket is not None:
if minute_bucket in minute_to_hosts:
minute_to_hosts[minute_bucket].append(last_host)
else:
minute_to_hosts[minute_bucket] = [last_host]
minute_bucket = None
last_host = None
elif line.startswith('[') and host is not None:
datestamp_str = line.split(']', 1)[0][1:]
datestamp = datetime.strptime(datestamp_str, "%c")
minute_bucket = datetime(
year = datestamp.year,
month=datestamp.month,
day=datestamp.day,
hour=datestamp.hour,
minute=datestamp.minute,
)
last_host = host
for minute, hosts in sorted(minute_to_hosts.items()):
print("{minute} - count: {len_hosts}\n hosts:".format(minute=minute, len_hosts=len(hosts)))
print(" " + "\n ".join(hosts))
@click.argument('infile', type=click.File('r'))
@main.command()
def first_occurrence(infile):
minute_to_hosts = {}
seen_hosts = set()
host = None
for line in infile:
if line.startswith('('):
host = line.split(")", 1)[-1].strip()
continue
elif line.startswith('[') and host is not None:
if host in seen_hosts:
continue
seen_hosts.add(host)
datestamp_str = line.split(']', 1)[0][1:]
datestamp = datetime.strptime(datestamp_str, "%c")
minute_bucket = datetime(
year = datestamp.year,
month=datestamp.month,
day=datestamp.day,
hour=datestamp.hour,
minute=datestamp.minute,
)
if minute_bucket in minute_to_hosts:
minute_to_hosts[minute_bucket].append(host)
else:
minute_to_hosts[minute_bucket] = [host]
for minute, hosts in sorted(minute_to_hosts.items()):
print("{minute} - count: {len_hosts}\n hosts:".format(minute=minute, len_hosts=len(hosts)))
print(" " + "\n ".join(hosts))
if __name__ == "__main__":
main()

File Metadata

Mime Type
text/x-python
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
9197088
Default Alt Text
parse_fsck.py (2 KB)

Event Timeline