-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnetlogserve
executable file
·61 lines (53 loc) · 1.54 KB
/
netlogserve
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env -S python3 -u
import datetime
import time
import sys
if len(sys.argv) < 2:
print("Syntax: netlogserve [-v] <filename>", file=sys.stderr)
exit(-1)
verbose = False
fn = sys.argv[1]
if fn == "-v":
verbose = True
fn = sys.argv[2]
f = open(fn)
lines = [line.strip() for line in f.readlines()]
f.close()
logs = {}
month = {"Jan": 1, "Feb": 2, "Mar": 3, "Apr": 4, "May": 5, "Jun": 6, "Jul": 7, "Aug": 8, "Sep": 9, "Oct": 10, "Nov": 11, "Dec": 12}
year = 2019
firststamp = None
laststamp = None
for line in lines:
try:
m, d, hms, *rest = line.split()
hms_h, hms_m, hms_s = hms.split(":")
dt = datetime.datetime(year, month[m], int(d), int(hms_h), int(hms_m), int(hms_s))
except:
print("Parse error - incorrect log format (lines need to begin with MMM DD hh:mm:ss)", file=sys.stderr)
exit(-1)
if verbose:
print("load", m, d, hms, "→", dt)
laststamp = int(dt.timestamp())
logs[laststamp] = logs.get(laststamp, []) + [line]
if not firststamp:
firststamp = laststamp
realstamp = int(time.time())
delta = realstamp - firststamp
if verbose:
print("delta", delta, "s")
quit = False
for i in range(firststamp, laststamp + 1):
while int(time.time()) - i < delta:
time.sleep(0.5)
if i in logs:
for line in logs[i]:
try:
print(line)
except:
quit = True
if verbose:
print("discarding remaining lines")
break
if quit:
break