-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_iftop.py
More file actions
76 lines (59 loc) · 1.92 KB
/
parse_iftop.py
File metadata and controls
76 lines (59 loc) · 1.92 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from itertools import chain
from pathlib import Path
from re import match
from typing import Any, Dict, List
import pandas as pd
from dateutil.parser import parse
def parse_single_iftop_log(filepath: Path):
"""
parse single file
"""
with filepath.open("r") as f:
lines: List[str] = f.readlines()[3:]
htop_cols = (
"#",
"host/name",
"direction",
"2s",
"10s",
"40s",
"cumulative",
"t",
)
res = []
def key_line(line: str) -> Dict[str, Any]:
if "=>" in line:
cols = htop_cols
else:
cols = htop_cols[1:]
values = line.split() + [filepath.stem]
return dict(zip(cols, values))
for line in lines:
if line.startswith("---"):
break
else:
res.append(key_line(line))
return res
def human_readable_to_bytes(size: str):
"""
Convert human readable file size to bytes.
"""
units = {"B": 1, "KB": 10**3, "MB": 10**6, "GB": 10**9, "TB": 10**12}
# Alternative unit definitions, notably used by Windows:# units = {"B": 1, "KB": 2**10, "MB": 2**20, "GB": 2**30, "TB": 2**40}def parse_size(size):
number, unit = match(r"([0-9.]+)(\w*)", size).groups()
return int(float(number) * units[unit])
def parse_directory(dir: str) -> pd.DataFrame:
"""
parse directory
"""
path = Path(dir)
parsed_logs = list(filter(bool, map(parse_single_iftop_log, path.glob("*.log"))))
parsed_logs = list(chain(*parsed_logs))
df = pd.DataFrame(parsed_logs)
df["cumulative_parsed"] = df["cumulative"].apply(human_readable_to_bytes)
df["t_parsed"] = pd.to_datetime(df["t"], format="%H-%M-%S.%f")
return df
if __name__ == "__main__":
logs_dir = "logs"
df = parse_directory(logs_dir)
df.to_csv("network_usage.csv")